/* * protected override bool addAudioWavWithEndOfFileTolerance(urakawa.media.data.audio.codec.WavAudioMediaData mediaData, urakawa.data.FileDataProvider dataProv, Time clipB, Time clipE, TreeNode treeNode) * { * bool isClipEndError = true; * * uint dataLength = 0; * Stream wavStream = null; * AudioLibPCMFormat PCMFormat = null; * try * { * wavStream = dataProv.OpenInputStream(); * PCMFormat = AudioLibPCMFormat.RiffHeaderParse(wavStream, out dataLength); * } * catch (System.Exception ex) * { * Console.WriteLine(ex.ToString()); * isClipEndError = true; * } * finally * { * if (wavStream != null) wavStream.Close(); * } * * Time fileDuration = new Time(PCMFormat.ConvertBytesToTime((long)dataLength)); * if (clipB.IsLessThan(clipE)) * {//1 * double diff = clipE.AsMilliseconds - fileDuration.AsMilliseconds; * if (clipB.IsLessThan(fileDuration)) * {//2 * try * {//3 * mediaData.AppendPcmData(dataProv, clipB, fileDuration); * isClipEndError = false; * Console.WriteLine("Obi: clip end adjust according to file length : Clip end" + clipE.AsTimeSpan.ToString() + " File length:" + fileDuration.AsTimeSpan.ToString()); * }//-3 * catch (Exception ex) * {//3 * isClipEndError = true; * }//-3 * // to do: add obi specific code here * //if (diff > 100 && treeNode != null ) * if (diff > m_Settings.ImportToleranceForAudioInMs && treeNode != null) * { * EmptyNode eNode = (EmptyNode)treeNode; * eNode.TODO = true; * if (eNode.Role_ == EmptyNode.Role.Plain) * { * eNode.Role_ = EmptyNode.Role.Custom; * eNode.CustomRole = Localizer.Message("DAISY3_ObiImport_ErrorsList_truncated_audio"); * } * m_ErrorsList.Add(Localizer.Message("DAISY3_ObiImport_ErrorsList_truncated_audio_in_phrase") + eNode.Index.ToString() + Localizer.Message("DAISY3_ObiImport_ErrorsList_in_section") + eNode.ParentAs<SectionNode>().Label); * m_ErrorsList.Add(Localizer.Message("DAISY3_ObiImport_ErrorsList_expected_clip_end") + clipE.Format_H_MN_S_MS() + Localizer.Message("DAISY3_ObiImport_ErrorsList_imported_clip_end") + fileDuration.Format_H_MN_S_MS()); * } * }//-2 * * }//-1 * else * {//1 * Console.WriteLine("clip begin is larger than clip end"); * }//-1 * return isClipEndError; * } */ private void ReplaceExternalAudioMediaPhraseWithEmptyNode(TreeNode node) { if (node is PhraseNode) { PhraseNode phrase = (PhraseNode)node; SectionNode section = phrase.ParentAs <SectionNode>(); Console.WriteLine("replacing phrase node with empty node due to clip problem " + section.Label + " phrase index:" + phrase.Index.ToString()); int phraseIndex = phrase.Index; EmptyNode emptyNode = m_Presentation.TreeNodeFactory.Create <EmptyNode>(); emptyNode.CopyAttributes(phrase); phrase.Detach(); section.Insert(emptyNode, phraseIndex); emptyNode.TODO = true; m_ErrorsList.Add(Localizer.Message("DAISY3_ObiImport_ErrorsList_error_no_audio") + phraseIndex.ToString() + Localizer.Message("DAISY3_ObiImport_ErrorsList_in_section") + section.Label); } }
/// <summary> /// Create the phrase detection command for list of phrases. /// </summary> public static CompositeCommand GetPhraseDetectionCommand(ProjectView.ProjectView view, List <PhraseNode> phraseNodesList, long threshold, double gap, double before, bool mergeFirstTwoPhrases) { CompositeCommand command = view.Presentation.CreateCompositeCommand(Localizer.Message("phrase_detection")); List <List <PhraseNode> > phrasesToMerge = new List <List <PhraseNode> >(); int index = 0; if (phraseNodesList.Count > 0) { view.TriggerProgressChangedEvent(Localizer.Message("phrase_detection"), 0); } for (int j = 0; j < phraseNodesList.Count; j++) { PhraseNode phrase = phraseNodesList[j]; if (j == 0 || phraseNodesList[j].ParentAs <SectionNode>() != phraseNodesList[j - 1].ParentAs <SectionNode>()) { index = phrase.Index + 1; Console.WriteLine("PH Section name " + phrase.ParentAs <SectionNode>().Label); } else { int diff = phraseNodesList[j].Index - phraseNodesList[j - 1].Index - 1; // used -1 because the original phrase is deleted index = index + diff; } System.Collections.Generic.List <PhraseNode> phrases = view.Presentation.CreatePhraseNodesFromAudioAssetList( Obi.Audio.PhraseDetection.Apply(phrase.Audio.Copy(), threshold, gap, before)); for (int i = 0; i < phrases.Count; ++i) { // Copy page/heading role for the first phrase only if (i == 0 || (phrase.Role_ != EmptyNode.Role.Page && phrase.Role_ != EmptyNode.Role.Heading)) { phrases[i].CopyAttributes(phrase); } phrases[i].Used = phrase.Used; phrases[i].TODO = phrase.TODO; if (phrases[i].Role_ == EmptyNode.Role.Heading && i > 0) { phrases[i].Role_ = EmptyNode.Role.Plain; } Console.WriteLine("PH index: " + index); // in following add node constructor, update selection is made false, to improve performance (19 may, 2010) command.ChildCommands.Insert(command.ChildCommands.Count, new Commands.Node.AddNode(view, phrases[i], phrase.ParentAs <SectionNode>(), index, false)); // add first 2 phrases to the list if the merge flag is true if (phrases.Count >= 2 && mergeFirstTwoPhrases && phrases[0] is PhraseNode && phrases[1] is PhraseNode) { List <PhraseNode> mergeList = new List <PhraseNode>(); mergeList.Add(phrases[0]); mergeList.Add(phrases[1]); phrasesToMerge.Add(mergeList); } index++; } Commands.Node.Delete deleteCmd = new Commands.Node.Delete(view, phrase, false); command.ChildCommands.Insert(command.ChildCommands.Count, deleteCmd);//@singleSection: moved delete command last for improve undo selection if (Obi.Audio.PhraseDetection.CancelOperation) { break; } /* * // skip to next indexes if the two consequtive phrases in phrase list are not consequitive according to phrase index in the parent section * if (j < phraseNodesList.Count - 1 * && phrase.Index + 1 < phraseNodesList[j + 1].Index) * { * EmptyNode empty = null; * for (int i = phrase.Index + 1; i < phraseNodesList[j + 1].Index; ++i) * { * empty = phrase.ParentAs<SectionNode>().PhraseChild(i); * command.ChildCommands.Insert(command.ChildCommands.Count, new Commands.Node.Delete(view, empty, false)); * command.ChildCommands.Insert(command.ChildCommands.Count, new Commands.Node.AddNode(view, empty, parent, index, false)); * index++; * } * //index = index + (phraseNodesList[j + 1].Index - (phrase.Index + 1)); * } */ view.TriggerProgressChangedEvent(Localizer.Message("phrase_detection"), (100 * j) / phraseNodesList.Count); } if (phrasesToMerge.Count > 0) { for (int i = 0; i < phrasesToMerge.Count; i++) { List <PhraseNode> mergeList = phrasesToMerge[i]; Commands.Node.MergeAudio mergeCmd = new MergeAudio(view, mergeList[0], mergeList[1]); mergeCmd.UpdateSelection = false; command.ChildCommands.Insert(command.ChildCommands.Count, mergeCmd); } } return(command); }
/// <summary> /// Create the phrase detection command. /// </summary> public static CompositeCommand GetPhraseDetectionCommand(ProjectView.ProjectView view, ObiNode node, long threshold, double gap, double before, bool mergeFirstTwoPhrases, ObiPresentation presentation = null) { List <PhraseNode> phraseNodesList = new List <PhraseNode> (); if (node is PhraseNode) { phraseNodesList.Add((PhraseNode)node); } else if (node is SectionNode) { SectionNode section = (SectionNode)node; for (int i = 0; i < section.PhraseChildCount; i++) { if (section.PhraseChild(i) is PhraseNode && ((PhraseNode)section.PhraseChild(i)).Role_ != EmptyNode.Role.Silence) { phraseNodesList.Add((PhraseNode)section.PhraseChild(i)); } } } List <List <PhraseNode> > phrasesToMerge = new List <List <PhraseNode> >(); CompositeCommand command = null; if (view.Presentation == null && presentation != null) { command = presentation.CreateCompositeCommand(Localizer.Message("phrase_detection")); } else { command = view.Presentation.CreateCompositeCommand(Localizer.Message("phrase_detection")); } // if phrase is selected but phrase detection node is section,select section if (node is SectionNode && view.GetSelectedPhraseSection != null && view.GetSelectedPhraseSection == node) { command.ChildCommands.Insert(command.ChildCommands.Count, new UpdateSelection(view, new NodeSelection(node, view.Selection.Control))); } ObiNode parent = node is SectionNode ? node : node.ParentAs <ObiNode> (); int index = 0; if (phraseNodesList.Count > 0) { view.TriggerProgressChangedEvent(Localizer.Message("phrase_detection"), 0); } for (int j = 0; j < phraseNodesList.Count; j++) { PhraseNode phrase = phraseNodesList[j]; if (j == 0) { index = phrase.Index + 1; } System.Collections.Generic.List <PhraseNode> phrases = null; if (view.Presentation == null && presentation != null) { phrases = presentation.CreatePhraseNodesFromAudioAssetList( Obi.Audio.PhraseDetection.Apply(phrase.Audio.Copy(), threshold, gap, before)); } else { phrases = view.Presentation.CreatePhraseNodesFromAudioAssetList( Obi.Audio.PhraseDetection.Apply(phrase.Audio.Copy(), threshold, gap, before)); } for (int i = 0; i < phrases.Count; ++i) { // Copy page/heading role for the first phrase only if (i == 0 || (phrase.Role_ != EmptyNode.Role.Page && phrase.Role_ != EmptyNode.Role.Heading)) { phrases[i].CopyAttributes(phrase); } phrases[i].Used = phrase.Used; phrases[i].TODO = phrase.TODO; if (i == 0 && phrase.TODO && (phrase as EmptyNode).CommentText != null) { (phrases[i] as EmptyNode).CommentText = (phrase as EmptyNode).CommentText; } if (phrases[i].Role_ == EmptyNode.Role.Heading && i > 0) { phrases[i].Role_ = EmptyNode.Role.Plain; } // in following add node constructor, update selection is made false, to improve performance (19 may, 2010) command.ChildCommands.Insert(command.ChildCommands.Count, new Commands.Node.AddNode(view, phrases[i], parent, index, false)); index++; } // add first 2 phrases to the list if the merge flag is true if (phrases.Count >= 2 && mergeFirstTwoPhrases && phrases[0] is PhraseNode && phrases [1] is PhraseNode) { List <PhraseNode> mergeList = new List <PhraseNode>(); mergeList.Add(phrases[0]); mergeList.Add(phrases[1]); phrasesToMerge.Add(mergeList); } if (node is PhraseNode && phrases.Count > 0 && view.Selection != null) { //command.append ( new UpdateSelection ( view, new NodeSelection ( node, view.Selection.Control ) ) ); command.ChildCommands.Insert(command.ChildCommands.Count, new UpdateSelection(view, new NodeSelection(phrases[0], view.Selection.Control))); //uncommenting this because unexecute for update selection can handle null unexecute now } Commands.Node.Delete deleteCmd = new Commands.Node.Delete(view, phrase, false); command.ChildCommands.Insert(command.ChildCommands.Count, deleteCmd);//@singleSection: moved delete command last for improve undo selection if (Obi.Audio.PhraseDetection.CancelOperation) { break; } // skip to next indexes if the two consequtive phrases in phrase list are not consequitive according to phrase index in the parent section if (j < phraseNodesList.Count - 1 && phrase.Index + 1 < phraseNodesList[j + 1].Index) { EmptyNode empty = null; for (int i = phrase.Index + 1; i < phraseNodesList[j + 1].Index; ++i) { empty = phrase.ParentAs <SectionNode>().PhraseChild(i); command.ChildCommands.Insert(command.ChildCommands.Count, new Commands.Node.Delete(view, empty, false)); command.ChildCommands.Insert(command.ChildCommands.Count, new Commands.Node.AddNode(view, empty, parent, index, false)); index++; } //index = index + (phraseNodesList[j + 1].Index - (phrase.Index + 1)); } view.TriggerProgressChangedEvent(Localizer.Message("phrase_detection"), (100 * j) / phraseNodesList.Count); } if (phrasesToMerge.Count > 0) { for (int i = 0; i < phrasesToMerge.Count; i++) { List <PhraseNode> mergeList = phrasesToMerge[i]; Commands.Node.MergeAudio mergeCmd = new MergeAudio(view, mergeList[0], mergeList[1]); mergeCmd.UpdateSelection = false; command.ChildCommands.Insert(command.ChildCommands.Count, mergeCmd); } } return(command); }