예제 #1
0
        public AudioSettings(AudioPaneViewModel viewModel)
        {
            ViewModel   = viewModel;
            DataContext = ViewModel;
            ViewModel.SelectVoiceTTS(Settings.Default.Audio_TTS_Voice);

            InitializeComponent();
        }
        public override void DoWork()
        {
            var pcmFormat = m_viewModel.m_UrakawaSession.DocumentProject.Presentations.Get(0).MediaDataManager.DefaultPCMFormat;

            if (pcmFormat.Data.BitDepth != 16)
            {
#if DEBUG
                Debugger.Break();
#endif
                return;
            }

            Tuple <TreeNode, TreeNode> treeNodeSelection = m_viewModel.m_UrakawaSession.GetTreeNodeSelection();
            TreeNode treeNode = treeNodeSelection.Item1;

            bool needsRefresh = false;
            bool skipDrawing  = Settings.Default.AudioWaveForm_DisableDraw;
            Settings.Default.AudioWaveForm_DisableDraw = true;

            //m_viewModel.m_UrakawaSession.PerformanceFlag = true;

#if !ENABLE_TTS_SPEAK_PROGRESS
            int percent = 0;
#endif

            bool initial = true;
            try
            {
                TreeNode adjustedNode = null;
next:

                if (m_viewModel.IsSimpleMode)
                {
                    adjustedNode = treeNodeSelection.Item1;
                }
                else
                {
                    TreeNode root = treeNodeSelection.Item1;
                    if (initial)
                    {
                        var lname = root.GetXmlElementLocalName();
                        if (lname != null && (lname.Equals("math", StringComparison.OrdinalIgnoreCase) ||
                                              lname.Equals("svg", StringComparison.OrdinalIgnoreCase)))
                        {
                            adjustedNode = root;
                        }
                        else
                        {
                            adjustedNode = TreeNode.NavigateInsideSignificantText(root);
                        }

                        //TreeNode candidate = m_viewModel.m_UrakawaSession.AdjustTextSyncGranularity(adjustedNode, adjustedNode);
                        //if (candidate != null)
                        //{
                        //    treeNode = candidate;
                        //}
                    }
                    else
                    {
                        adjustedNode = treeNode;
                    }

                    if (adjustedNode == null ||
                        !adjustedNode.IsDescendantOf(root) && adjustedNode != root)
                    {
                        return;
                    }

                    TreeNode math_ = adjustedNode.GetFirstAncestorWithXmlElement("math");
                    if (math_ != null)
                    {
                        adjustedNode = math_;
                    }
                    else
                    {
                        TreeNode svg_ = adjustedNode.GetFirstAncestorWithXmlElement("svg");
                        if (svg_ != null)
                        {
                            adjustedNode = svg_;
                        }
                        //else
                        //{
                        //    TreeNode candidate = m_viewModel.m_UrakawaSession.AdjustTextSyncGranularity(adjustedNode, adjustedNode);
                        //    if (candidate != null)
                        //    {
                        //        adjustedNode = candidate;
                        //    }
                        //}
                    }

                    if (adjustedNode == null ||
                        !adjustedNode.IsDescendantOf(root) && adjustedNode != root)
                    {
                        return;
                    }
                }

                if (adjustedNode.GetManagedAudioMedia() != null ||
                    adjustedNode.GetFirstDescendantWithManagedAudio() != null)
                {
#if DEBUG
                    Debugger.Break();
#endif
                    return;
                }

                initial = false;

                var text = adjustedNode.GetTextFlattened();
                if (string.IsNullOrEmpty(text))
                {
#if DEBUG
                    Debugger.Break();
#endif
                    return;
                }

#if DEBUG
                DebugFix.Assert(TreeNode.GetLengthStringChunks(adjustedNode.GetTextFlattened_()) != 0);
#endif

                m_viewModel.SelectVoiceTTS(Settings.Default.Audio_TTS_Voice);
                //m_viewModel.m_SpeechSynthesizer.SelectVoice(Settings.Default.Audio_TTS_Voice);

                var adjustedNodeName = adjustedNode.GetXmlElementLocalName();
                if (adjustedNodeName != null)
                {
                    foreach (string elementName in m_ttsVoiceMap.Keys)
                    {
                        if (elementName.Equals(adjustedNodeName, StringComparison.OrdinalIgnoreCase))
                        {
                            string voiceName = m_ttsVoiceMap[elementName];

                            m_viewModel.SelectVoiceTTS(voiceName);
                            //m_viewModel.m_SpeechSynthesizer.SelectVoice(voiceName);

                            break;
                        }
                    }
                }

#if !ENABLE_TTS_SPEAK_PROGRESS
                percent += 10;
                if (percent > 100)
                {
                    percent = 10;
                }

                string msg = Tobi_Plugin_AudioPane_Lang.GeneratingTTSAudio + " [" + (text.Length > 20 ? text.Substring(0, 19) + "..." : text) + "]";
                reportProgress_Throttle(percent, msg);
#endif

                var converter = new AudioTTSGenerator(text, pcmFormat.Data, m_viewModel.m_Recorder.RecordingDirectory, m_viewModel.m_SpeechSynthesizer);

                AddSubCancellable(converter);
                converter.DoWork();
                RemoveSubCancellable(converter);

                if (RequestCancellation)
                {
                    if (!string.IsNullOrEmpty(converter.GeneratedAudioFilePath) &&
                        File.Exists(converter.GeneratedAudioFilePath))
                    {
                        File.Delete(converter.GeneratedAudioFilePath);
                    }
                    return;
                }

                if (!File.Exists(converter.GeneratedAudioFilePath))
                {
#if DEBUG
                    Debugger.Break();
#endif
                    RequestCancellation = true;
                    return;
                }

                //var manualResetEvent = new ManualResetEvent(false);
                m_viewModel.TheDispatcher.Invoke(DispatcherPriority.Normal, (Action)(() =>
                {
                    TreeNode sub = (adjustedNode == treeNodeSelection.Item1 ? null : adjustedNode);
                    Tuple <TreeNode, TreeNode> newSelection = m_viewModel.m_UrakawaSession.PerformTreeNodeSelection(treeNodeSelection.Item1, false, sub);
                    if (newSelection.Item1 != treeNodeSelection.Item1 || newSelection.Item2 != sub)
                    {
#if DEBUG
                        Debugger.Break();
#endif
                        RequestCancellation = true;
                        return;
                    }

                    TreeNode selectedNode = newSelection.Item2 ?? newSelection.Item1;
                    if (selectedNode.GetManagedAudioMedia() != null ||
                        selectedNode.GetFirstDescendantWithManagedAudio() != null)
                    {
#if DEBUG
                        Debugger.Break();
#endif
                        RequestCancellation = true;
                        return;
                    }

#if DEBUG
                    Tuple <TreeNode, TreeNode> treeNodeSelectionCheck = m_viewModel.m_UrakawaSession.GetTreeNodeSelection();
                    if (newSelection.Item1 != treeNodeSelectionCheck.Item1)
                    {
                        Debugger.Break();
                    }
                    if (newSelection.Item2 != treeNodeSelectionCheck.Item2)
                    {
                        Debugger.Break();
                    }
#endif


                    m_viewModel.openFile(converter.GeneratedAudioFilePath, true, true, pcmFormat);

                    needsRefresh = true;

                    //m_viewModel.CommandRefresh.Execute();
                    //if (m_viewModel.View != null)
                    //{
                    //    m_viewModel.View.CancelWaveFormLoad(true);
                    //}
                }));

#if DEBUG
                if (m_viewModel.TheDispatcher.CheckAccess())
                {
                    Debugger.Break();
                }
#endif

                //Thread.Sleep(100);

                ////Action EmptyDelegate = delegate() { };
                //m_viewModel.TheDispatcher.Invoke(DispatcherPriority.Background, (Action)(() =>
                //{
                //    //nop
                //}));
                ////m_viewModel.m_ShellView.PumpDispatcherFrames(DispatcherPriority.Background);


                if (RequestCancellation)
                {
                    if (!string.IsNullOrEmpty(converter.GeneratedAudioFilePath) &&
                        File.Exists(converter.GeneratedAudioFilePath))
                    {
                        File.Delete(converter.GeneratedAudioFilePath);
                    }
                    return;
                }

                //TreeNode nested;
                //treeNode = TreeNode.GetNextTreeNodeWithNoSignificantTextOnlySiblings(false, adjustedNode, out nested);
                treeNode = TreeNode.NavigatePreviousNextSignificantText(false, adjustedNode);
                if (treeNode == null)
                {
                    return;
                }

                TreeNode math = treeNode.GetFirstAncestorWithXmlElement("math");
                if (math != null)
                {
                    treeNode = math;
                }
                else
                {
                    TreeNode svg = treeNode.GetFirstAncestorWithXmlElement("svg");
                    if (svg != null)
                    {
                        treeNode = svg;
                    }
                    //else
                    //{
                    //    TreeNode candidate = m_viewModel.m_UrakawaSession.AdjustTextSyncGranularity(treeNode, adjustedNode);
                    //    if (candidate != null)
                    //    {
                    //        treeNode = candidate;
                    //    }
                    //}
                }

                goto next;
            }
            catch (Exception ex)
            {
#if DEBUG
                Debugger.Break();
#endif
                RequestCancellation = true;
                return;
            }
            finally
            {
                //m_viewModel.m_UrakawaSession.PerformanceFlag = false;

                m_viewModel.TheDispatcher.Invoke(DispatcherPriority.Normal, (Action)(() =>
                {
                    Settings.Default.AudioWaveForm_DisableDraw = skipDrawing;

                    if (needsRefresh)
                    {
                        m_viewModel.CommandRefresh.Execute();
                    }
                }));
            }
        }