private static IEnumerable <TranscriptionParagraph> MakeParagraphs(IEnumerable <XElement> elements, TimeSpan timeunit) { foreach (var e in elements) { var p = new TranscriptionParagraph() { Begin = new TimeSpan(timeunit.Ticks * XmlConvert.ToInt64(e.Attribute("bt").Value)), End = new TimeSpan(timeunit.Ticks * XmlConvert.ToInt64(e.Attribute("et").Value)), }; var ph = new TranscriptionPhrase() { Begin = p.Begin, End = p.End, Text = "", }; var idel = e.Element("Id"); if (idel != null) { p.InternalID = XmlConvert.ToInt32(idel.Attribute("id").Value); ph.Text = "score: " + idel.Attribute("score").Value; } p.Phrases.Add(ph); yield return(p); } }
private void CExportElement(object sender, ExecutedRoutedEventArgs e) { TranscriptionParagraph par = VirtualizingListBox.ActiveTransctiption as TranscriptionParagraph; var data = _WavReader.LoadaudioDataBuffer(par.Begin, par.End); Microsoft.Win32.SaveFileDialog dlg = new Microsoft.Win32.SaveFileDialog(); dlg.DefaultExt = ".wav"; dlg.Filter = "wav soubory (.wav)|*.wav"; if (dlg.ShowDialog() == true) { WavReader.SaveToWav(dlg.FileName, data); string ext = System.IO.Path.GetExtension(dlg.FileName); dlg.FileName = dlg.FileName.Substring(0, dlg.FileName.Length - ext.Length); string textf = dlg.FileName + ".txt"; File.WriteAllBytes(textf, win1250.GetBytes(par.Text)); if (!string.IsNullOrEmpty(par.Phonetics)) { textf = dlg.FileName + ".phn"; File.WriteAllBytes(textf, win1250.GetBytes(par.Phonetics)); } } }
private void CNewSection(object sender, ExecutedRoutedEventArgs e) { TranscriptionSection s = new TranscriptionSection(Properties.Strings.DefaultSectionText); TranscriptionParagraph p = new TranscriptionParagraph(); p.Add(new TranscriptionPhrase()); Transcription.Add(s); s.Add(p); VirtualizingListBox.ActiveTransctiption = s; }
private void CQuickExportElement(object sender, ExecutedRoutedEventArgs e) { if (!Settings.Default.FeatureEnabler.QuickExport) { return; } using (new WaitCursor()) { TranscriptionParagraph par = VirtualizingListBox.ActiveTransctiption as TranscriptionParagraph; var data = _WavReader.LoadaudioDataBuffer(par.Begin, par.End); if (FilePaths.QuickSaveDirectory == null || !Directory.Exists(FilePaths.QuickSaveDirectory)) { MessageBox.Show(Properties.Strings.QuickSavePathNotSpecifiedError, Properties.Strings.QuickSavePathNotSpecifiedError, MessageBoxButton.OK, MessageBoxImage.Error); return; } DirectoryInfo nfo = new DirectoryInfo(FilePaths.QuickSaveDirectory); int index = 0; var pars = nfo.GetFiles("*.wav").Where(p => p.Name.StartsWith("paragraph_")).ToArray(); if (pars.Count() > 0) { index = 1 + (int)pars.Max(p => { int res = 0; int.TryParse(System.IO.Path.GetFileNameWithoutExtension(p.Name.Substring(10)), out res); return(res); }); } var basename = System.IO.Path.Combine(nfo.FullName, "paragraph_" + index); WavReader.SaveToWav(basename + ".wav", data); string textf = basename + ".txt"; //File.WriteAllBytes(textf, win1250.GetBytes()); File.WriteAllText(textf, par.Text, win1250); if (!string.IsNullOrEmpty(par.Phonetics)) { textf = basename + ".phn"; File.WriteAllText(textf, par.Phonetics, win1250); //File.WriteAllBytes(textf, win1250.GetBytes(par.Phonetics)); } SystemSounds.Asterisk.Play(); } }
void l_NewRequest(object sender, EventArgs e) { Element el = (Element)sender; TranscriptionParagraph p = new TranscriptionParagraph(); p.Add(new TranscriptionPhrase()); if (el.ValueElement is TranscriptionParagraph) { TimeSpan pos; RequestTimePosition(out pos); el.ValueElement.End = pos; if ((el.ValueElement.End - el.ValueElement.Begin) < TimeSpan.FromMilliseconds(100)) { el.ValueElement.End = el.ValueElement.Begin + TimeSpan.FromMilliseconds(100); } if (el.ValueElement.Parent.End < el.ValueElement.End) el.ValueElement.Parent.End = el.ValueElement.End; p.Begin = el.ValueElement.Parent.End; p.Speaker = ((TranscriptionParagraph)el.ValueElement).Speaker; el.ValueElement.Parent.Insert(el.ValueElement.ParentIndex + 1, p); } else if (el.ValueElement is TranscriptionSection) { el.ValueElement.Parent.Insert(0, p); } else if (el.ValueElement is TranscriptionChapter) { el.ValueElement.Children[0].Insert(0, p); } FixPosSetFor(el.ValueElement); ActiveTransctiption = p; this.Dispatcher.Invoke(() => { FixPosTryRestore(); SetCaretIn(p, 0); }, DispatcherPriority.Background); }
private void CInsertNewSection(object sender, ExecutedRoutedEventArgs e) { if (VirtualizingListBox.ActiveTransctiption != null) { if (VirtualizingListBox.ActiveTransctiption.IsParagraph) { TranscriptionParagraph p = (TranscriptionParagraph)VirtualizingListBox.ActiveTransctiption; int idx = p.ParentIndex; TranscriptionSection sec = (TranscriptionSection)p.Parent; TranscriptionSection s = new TranscriptionSection(Properties.Strings.DefaultSectionText); for (int i = idx; i < sec.Children.Count; i++) { s.Add(sec[i]); } sec.Children.RemoveRange(idx, sec.Children.Count - idx); sec.Parent.Insert(sec.ParentIndex + 1, s); VirtualizingListBox.ActiveTransctiption = s; VirtualizingListBox.Reset(); } else if (VirtualizingListBox.ActiveTransctiption.IsSection) { var s = new TranscriptionSection(Properties.Strings.DefaultSectionText); s.Children.AddRange(VirtualizingListBox.ActiveTransctiption.Children); VirtualizingListBox.ActiveTransctiption.Children.Clear(); VirtualizingListBox.ActiveTransctiption.Parent.Insert(VirtualizingListBox.ActiveTransctiption.ParentIndex, s); VirtualizingListBox.ActiveTransctiption = s; VirtualizingListBox.Reset(); } else if (VirtualizingListBox.ActiveTransctiption.IsChapter) { var s = new TranscriptionSection(Properties.Strings.DefaultSectionText); VirtualizingListBox.ActiveTransctiption.Insert(0, s); VirtualizingListBox.ActiveTransctiption = s; VirtualizingListBox.Reset(); } } }
void l_SplitRequest(object sender, EventArgs e) { Element el = (Element)sender; if (el.ValueElement is TranscriptionParagraph) { TranscriptionParagraph par = (TranscriptionParagraph)el.ValueElement; par.Parent.BeginUpdate(); TimeSpan end = par.End; TranscriptionParagraph par2 = new TranscriptionParagraph(); TranscriptionParagraph par1 = new TranscriptionParagraph(); par1.AttributeString = par2.AttributeString = par.AttributeString; par1.Speaker = par2.Speaker = par.Speaker; par1.Begin = par.Begin; par2.End = end; int where = el.editor.CaretOffset; int sum = 0; for (int i = 0; i < par.Phrases.Count; i++) { TranscriptionPhrase p = par.Phrases[i]; if (sum + p.Text.Length <= where) //patri do prvniho { par1.Add(new TranscriptionPhrase(p)); } else if (sum >= where) { par2.Add(new TranscriptionPhrase(p)); } else if (sum <= where && sum + p.Text.Length > where) //uvnitr fraze { int offs = where - sum; double ratio = offs / (double)p.Text.Length; TimeSpan length = p.End - p.Begin; TimeSpan l1 = new TimeSpan((long)(ratio * length.Ticks)); TranscriptionPhrase p1 = new TranscriptionPhrase(); p1.Text = p.Text.Substring(0, offs); p1.Begin = p.Begin; p1.End = p1.Begin + l1; //if (p1.End <= par.Begin) // p1.End = par.Begin + TimeSpan.FromMilliseconds(100); //pojistka kvuli nezarovnanejm textum int idx = i; par1.Add(p1); TranscriptionPhrase p2 = new TranscriptionPhrase(); p2.Text = p.Text.Substring(offs); p2.Begin = p1.End; p2.End = p.End; par2.Add(p2); par2.Begin = p2.Begin; par1.End = p1.End; } sum += p.Text.Length; }//for if (!Keyboard.Modifiers.HasFlag(ModifierKeys.Control))//TODO: the keyboard check should be somewhere else? { if (RequestTimePosition != null) { TimeSpan pos; RequestTimePosition(out pos); par1.End = pos; par2.Begin = pos; } } var parent = par.Parent; int indx = par.ParentIndex; FixPosSetFor(par.Previous()); parent.Remove(par); parent.Insert(indx, par2); parent.Insert(indx, par1); parent.EndUpdate(); ActiveTransctiption = par2; this.Dispatcher.Invoke(() => { FixPosTryRestore(); SetCaretIn(par2, 0); }, DispatcherPriority.Background); } }
public void GetTranscription(bool useOrtoTP, bool removeNonPhonemes, Transcription storage) { Transcription data = storage; List <TranscriptionPhrase> phrazes = new List <TranscriptionPhrase>(); string[] orto = useOrtoTP ? ORTOTP : ORTOT; for (int i = 0; i < ORTOT.Length; i++) { TranscriptionPhrase ph = new TranscriptionPhrase(); ph.Text = orto[i].Trim() + " "; if (orto[i].Length == 0) //empty words { ph.Text = ""; } ph.Phonetics = PRON[i]; ph.Begin = START[i]; ph.End = STOP[i]; phrazes.Add(ph); } TranscriptionChapter c = new TranscriptionChapter(); TranscriptionSection sec = new TranscriptionSection(); var second = TimeSpan.FromSeconds(0.5); TranscriptionParagraph pah = new TranscriptionParagraph(); List <TranscriptionPhrase> silence = new List <TranscriptionPhrase>(); TimeSpan sec20 = TimeSpan.FromSeconds(20); #region sileny splitovaci algoritmus while (phrazes.Count > 0) { string tt = phrazes[0].Text.Trim(); if (string.IsNullOrWhiteSpace(tt) || (tt.First() == '[' && tt.Last() == ']'))//nerecova udalost { silence.Add(phrazes[0]); TimeSpan begin; TimeSpan end; if (pah.Phrases.Count > 0)//pokud uz jsou sestavene nejake fraze spocitam jestli presahnou spolu s tichy 20s { begin = pah.Phrases.First().Begin; end = silence.Last().End; if (end - begin >= sec20)//pokud ano reknu, ze pred tichy mel odstavec zkoncit { pah.Begin = pah.Phrases.First().Begin; pah.End = pah.Phrases.Last().End; sec.Paragraphs.Add(pah); pah = new TranscriptionParagraph(); } } } else if (silence.Count > 0) //mam nejaky nerecovy udalosti a prislo neco jinyho { if (silence.Last().End - silence.First().Begin >= TimeSpan.FromSeconds(2)) //mam vic nereci nez 2 sekundy udelat z ni samostatnej segment { if (pah.Phrases.Count > 0) { pah.Begin = pah.Phrases.First().Begin; pah.End = pah.Phrases.Last().End; sec.Paragraphs.Add(pah); pah = new TranscriptionParagraph(); } foreach (var ss in silence) { pah.Phrases.Add(ss); } silence.Clear(); pah.Begin = pah.Phrases.First().Begin; pah.End = pah.Phrases.Last().End; sec.Paragraphs.Add(pah); pah = new TranscriptionParagraph(); pah.Phrases.Add(phrazes[0]); } else//mam ji malo - prilepit nerecovy udalosti k odstavci { foreach (var ss in silence) { pah.Phrases.Add(ss); } silence.Clear(); pah.Phrases.Add(phrazes[0]); } } else//prisla recova fraze a nemam nic v tichu pridat do paragrafu { pah.Phrases.Add(phrazes[0]); } phrazes.RemoveAt(0); } //dosyp zbytek do prepisu while (silence.Count > 0) { pah.Phrases.Add(silence[0]); silence.RemoveAt(0); } #endregion pah.Begin = pah.Phrases.First().Begin; pah.End = pah.Phrases.Last().End; sec.Paragraphs.Add(pah); c.Sections.Add(sec); data.Chapters.Add(c); if (removeNonPhonemes) { { TranscriptionPhrase ph = (TranscriptionPhrase)sec[0][0]; while (ph != null) { var parent = ph.Parent; var ph2 = (TranscriptionPhrase)ph.NextSibling(); string t = ph.Text.Trim(); if (string.IsNullOrWhiteSpace(t) || (t.StartsWith("[") && t.EndsWith("]"))) { ph.Parent.Remove(ph); } ph = ph2; } } } data.MediaURI = FileName; }
private void CAssignSpeaker(object sender, ExecutedRoutedEventArgs e) { TranscriptionParagraph tpr = null; if (sender is TranscriptionParagraph) { tpr = (TranscriptionParagraph)sender; } else { tpr = VirtualizingListBox.ActiveTransctiption as TranscriptionParagraph; } var mgr = new SpeakersManager(tpr.Speaker, Transcription, Transcription.Speakers, SpeakersDatabase) { MessageLabel = Properties.Strings.mainWindowSpeakersManagerSelectedParagraphMessage, Message = VirtualizingListBox.ActiveTransctiption.Text }; if (smwidth != null && smheight != null && smleft != null && smtop != null) { mgr.Width = smwidth.Value; mgr.Height = smheight.Value; mgr.Left = smleft.Value; mgr.Top = smtop.Value; } if (mgr.ShowDialog() == true && mgr.SelectedSpeaker != null) { Transcription.BeginUpdate(); var origspk = tpr.Speaker; tpr.Speaker = mgr.SelectedSpeaker; if (!Transcription.EnumerateParagraphs().Any(p => p.Speaker == origspk)) { Transcription.Speakers.Remove(origspk); } var replaced = AdvancedSpeakerCollection.SynchronizedAdd(Transcription.Speakers, mgr.SelectedSpeaker); if (replaced != null) { foreach (var p in Transcription.EnumerateParagraphs().Where(p => p.Speaker == replaced)) { p.Speaker = mgr.SelectedSpeaker; } } Transcription.EndUpdate(); } smwidth = mgr.Width; smheight = mgr.Height; smleft = mgr.Left; smtop = mgr.Top; if (mgr.SpeakerChanged)//refresh all sepakers { var pinned = SpeakersDatabase.Where(s => s.PinnedToDocument); Transcription.Speakers.AddRange(pinned.Except(Transcription.Speakers)); } VirtualizingListBox.SpeakerChanged(VirtualizingListBox.ActiveElement); }
/// <summary> /// exportuje dokument do vybraneho formatu - pokud je cesta null, zavola savedialog /// </summary> /// <param name="aDokument"></param> /// <param name="vystup"></param> /// <param name="aFormat"></param> /// <returns></returns> public void ExportovatDokument(string vstup, string vystup, bool times, bool nonoises) { Transcription data = null; using (var file = File.OpenRead(vstup)) data = Transcription.Deserialize(file); if (times) { FileStream fs = new FileStream(vystup, FileMode.Create); StreamWriter sw = new StreamWriter(fs, Encoding.GetEncoding("windows-1250")); FileInfo fi = new FileInfo(vystup); string pNazev = fi.Name.ToUpper().Remove(fi.Name.Length - fi.Extension.Length); string pHlavicka = "<" + pNazev + ";"; for (int i = 0; i < data.Speakers.Count; i++) { if (i > 0) { pHlavicka += ","; } pHlavicka += " " + data.Speakers[i].FirstName; } pHlavicka += ">"; sw.WriteLine(pHlavicka); sw.WriteLine(); for (int i = 0; i < data.Chapters.Count; i++) { for (int j = 0; j < data.Chapters[i].Sections.Count; j++) { for (int k = 0; k < data.Chapters[i].Sections[j].Paragraphs.Count; k++) { TranscriptionParagraph pP = data.Chapters[i].Sections[j].Paragraphs[k]; //zapsani jednotlivych odstavcu string pRadek = "<" + (pP.InternalID - 1).ToString() + "> "; for (int l = 0; l < pP.Phrases.Count; l++) { TranscriptionPhrase pFraze = pP.Phrases[l]; pRadek += "[" + (pFraze.Begin).ToString() + "]" + pFraze.Text; } sw.WriteLine(pRadek); } } } sw.Close(); } else { StringBuilder sb = new StringBuilder(); foreach (var item in data.EnumerateParagraphs()) { if (nonoises) { var alltext = ignoredGroup.Replace(item.Text, ""); alltext = whitespaceGroup.Replace(alltext, " "); if (!string.IsNullOrWhiteSpace(alltext)) { sb.AppendLine(alltext); } } else { sb.AppendLine(item.Text); } } File.WriteAllText(vystup, sb.ToString()); } }