NAudio BufferedWaveProvider BufferDuration限制导致读数在一定时间后停止

时间:2017-07-24 08:22:47

标签: vb.net winforms text-to-speech naudio naturallyspeaking

我有一个文本到语音引擎,可以阅读考试题目。读者似乎工作正常,但是当有大量文本要继续阅读时,我已经找到了一个问题。

12分钟后(每次)读者停止阅读。我已将此描述为与Naudio BufferedWaveProvider的{​​{1}}有关。这是我的代码:

BufferDuration

我注意到默认Shared waveOut As WaveOut Shared waveFormat As WaveFormat Shared provider As BufferedWaveProvider Private Shared Function InitializeAudio(Freq As Integer) As Boolean Try bNoAudioDected = False waveOut = New WaveOut() waveFormat = New WaveFormat(Freq, 16, 1) provider = New BufferedWaveProvider(waveFormat) provider.BufferLength = 31457280 ''this was added for temporary duration fix Dim ts = New TimeSpan(0, 30, 0) provider.BufferDuration = ts ''''''''''''''''''''''''''''''''''''''''''' provider.DiscardOnBufferOverflow = True Try waveOut.Init(provider) Catch mmEx As NAudio.MmException ErrorMessage("Audio device not detected!" & vbNewLine & "Please connect an audio device to use the Text-To-Speech system!") bNoAudioDected = True bAudioInitialised = True Return False Catch ex As Exception WriteError(ex, "TextToSpeech -> InitializeAudio") bNoAudioDected = True bAudioInitialised = True Return False End Try Return True Catch ex As Exception End Try End Function Private Shared Sub ReleaseAudio() Try If provider IsNot Nothing Then provider.ClearBuffer() End If If waveOut IsNot Nothing Then waveOut.[Stop]() End If Catch ex As Exception End Try End Sub 设置为: 11:53分钟。我使用BufferDuration将代码更改为10秒。 10秒后,系统与12分钟问题完全相同。

我已根据上面的代码手动改变了持续时间,首先是6小时(只是过度补偿),这导致系统立即轰炸。然后到2小时似乎工作正常,但导致较小的文本选择问题。然后到30分钟,它现在是什么,这似乎是稳定的,但我担心可靠性,因为我的其他两次尝试似乎引起了更多的问题。

我的问题基本上是否这是对NAudio的dll的限制,它只能处理连续读取的数量?

我是否正在改变BufferDuration,或者默认值是否保持不变?

修改

这是我使用的Text-To-Speech包装器类。请注意,这是由我使用的文字转语音提供程序提供的。

TimeSpan

这是我点击“阅读文字”按钮时使用的代码:

Imports NAudio.Wave
Imports VENET
Imports System.Threading.Thread

Public Class TextToSpeech
#Region "Variables"
#Region "Shared"
    Shared sText As String
    Shared sDir As String()
    Public Shared iSpeechRate As Integer = 100

    Shared hTtsInstance As IntPtr
    Shared hTtsCl As IntPtr
    Public Shared bUploadTypePaper As Boolean = False

#End Region
#Region "Global"
    Dim lstText As List(Of String)
    Dim nErr As NUAN_ERROR
    Dim tts As New VETts
    Dim Lang As VE_LANGUAGE()
    Dim Voice As VE_VOICEINFO()
    Dim thread As Threading.Thread

#End Region
#End Region
#Region "Properties"
    Private _newSpeed As Integer
    Public Property NewSpeed() As Integer
        Get
            Return _newSpeed
        End Get
        Set(ByVal value As Integer)
            _newSpeed = value
        End Set
    End Property

#End Region

    Private SpeechHandler As New VENET.VE_OUTPUTDEVICE(AddressOf StreamSpeech)

    Private Function StreamSpeech(hTtsInst As IntPtr, Msg As VE_MSG, Param As VE_LPARAM, OutData As VE_OUTDATA) As NUAN_ERROR
        Try
            If OutData.pOutPcmBuf IsNot Nothing Then
                provider.AddSamples(OutData.pOutPcmBuf, 0, CInt(OutData.ulPcmBufLen))
            End If
        Catch ex As Exception
            MessageBox.Show("StreamSpeech FAILED: " & nErr)
            MessageBox.Show("StreamSpeech FAILED: " & ex.InnerException.ToString())
        End Try

        Return NUAN_ERROR.NUAN_OK


    End Function



    Private Sub ExecuteTts()
        Try

            NewSpeed = TtsSpeechSpeed

            nErr = tts.ve_ttsGetLanguageList(hTtsCl, Lang)
            If nErr <> NUAN_ERROR.NUAN_OK Then
                MessageBox.Show("ve_ttsGetLanguageList FAILED: " & nErr)
                Exit Sub
            End If

            If Lang.Length = 0 Then
                MessageBox.Show("There are no available languages!")
                Exit Sub
            End If

            nErr = tts.ve_ttsGetVoiceList(hTtsCl, Lang(iLang).szLanguage, 0, Voice)
            If nErr <> NUAN_ERROR.NUAN_OK Then
                MessageBox.Show("ve_ttsGetVoiceList FAILED: " & nErr)
                Exit Sub
            End If

            If Voice.Length = 0 Then
                MessageBox.Show("There are no available voices!")
                Exit Sub
            End If

            Dim paramList() As VE_PARAM

            paramList = New VE_PARAM() {New VE_PARAM(VE_PARAMID.VE_PARAM_LANGUAGE, Lang(iLang).szLanguage), New VE_PARAM(VE_PARAMID.VE_PARAM_SPEECHRATE), New VE_PARAM(VE_PARAMID.VE_PARAM_VOICE, Voice(iVoice).szVoiceName)}

            If NewSpeed <> 0 Then
                iSpeechRate = NewSpeed
            ElseIf NewSpeed = 0 Then
                paramList(1).usValue = iSpeechRate
            End If

            paramList(1).usValue = iSpeechRate

            Try
                nErr = tts.ve_ttsSetParamList(hTtsInstance, paramList)
            Catch ex As Exception
                MsgBox(ex.Message)
            End Try

            If nErr <> NUAN_ERROR.NUAN_OK Then
                MessageBox.Show("ve_ttsSetParamList FAILED: " & nErr)
                Exit Sub
            End If

            Dim FreqParam As VE_PARAM() = {New VE_PARAM(VE_PARAMID.VE_PARAM_FREQUENCY)}

            nErr = tts.ve_ttsGetParamList(hTtsInstance, FreqParam)
            If nErr <> NUAN_ERROR.NUAN_OK Then
                MessageBox.Show("ve_ttsGetParamList FAILED: " & nErr)
                Exit Sub
            End If

            tts.tts_setOutDevice(SpeechHandler)

            ' Start Streaming Audio
            Try
                waveOut.Play()
            Catch ex As Exception

            End Try


            nErr = tts.ve_ttsProcessText2Speech(hTtsInstance, VE_TEXTFORMAT.VE_NORM_TEXT, sText)

            If nErr <> NUAN_ERROR.NUAN_OK And nErr <> NUAN_ERROR.NUAN_E_TTS_USERSTOP Then
                MessageBox.Show("ve_ttsProcessText2Speech FAILED: " & nErr)
                Exit Sub
            End If
        Catch ex As Exception
            MessageBox.Show("ExecuteTts FAILED: " & nErr)
            MessageBox.Show("ExecuteTts FAILED: " & ex.Message.ToString())
            MessageBox.Show("ExecuteTts FAILED: " & ex.InnerException.ToString())
        End Try
    End Sub



    Public Function InitialiseTtsEngine() As Boolean
        Try


            sDir = {sTtsEnginePath}

            nErr = tts.ve_ttsInitialize(sDir, hTtsCl)
            If nErr <> NUAN_ERROR.NUAN_OK Then
                MessageBox.Show("ve_ttsInitialize FAILED: " & nErr)
                Exit Function
            End If

            nErr = tts.ve_ttsOpen(hTtsCl, hTtsInstance)
            If nErr <> NUAN_ERROR.NUAN_OK Then
                MessageBox.Show("ve_ttsOpen FAILED: " & nErr)
                Exit Function
            End If

            If Not InitializeAudio(22050) Then
                MessageBox.Show("Could not initialize audio with sampling frequency")
            End If
        Catch ex As Exception
        End Try
    End Function

    Public Sub CleanUp()
        If hTtsInstance <> 0 Then
            nErr = tts.ve_ttsStop(hTtsInstance)
            Application.DoEvents()
            If nErr <> NUAN_ERROR.NUAN_OK Then
                MessageBox.Show("ve_ttsStop FAILED: " & nErr)
                Exit Sub
            End If

            Sleep(1500)

            nErr = tts.ve_ttsClose(hTtsInstance)
            Application.DoEvents()
            If nErr <> NUAN_ERROR.NUAN_OK Then
                MessageBox.Show("ve_ttsClose FAILED: " & nErr)
                Exit Sub
            End If
        End If

    End Sub

    Public Function UnInitialiseTtsEngineForTest() As Boolean

        Try
            If hTtsInstance <> 0 Then

                nErr = tts.ve_ttsStop(hTtsInstance)
                Application.DoEvents()
                If nErr <> NUAN_ERROR.NUAN_OK Then
                    MessageBox.Show("ve_ttsStop FAILED: " & nErr)

                End If

                Sleep(1500)

                nErr = tts.ve_ttsClose(hTtsInstance)
                Application.DoEvents()
                If nErr <> NUAN_ERROR.NUAN_OK Then
                    MessageBox.Show("ve_ttsClose FAILED: " & nErr)
                    Exit Function
                End If

                nErr = tts.ve_ttsUnInitialize(hTtsCl)
                Application.DoEvents()
                If nErr <> NUAN_ERROR.NUAN_OK Then
                    MessageBox.Show("ve_ttsUnInitialize FAILED: " & nErr)
                    Exit Function
                End If

                ReleaseAudio()

                GC.Collect()
            End If
        Catch ex As Exception
        End Try

    End Function

    Public Function UnInitialiseTtsEngineForQTCTest() As Boolean

        Try
            If hTtsInstance <> 0 Then

                nErr = tts.ve_ttsStop(hTtsInstance)
                Application.DoEvents()
                If nErr <> NUAN_ERROR.NUAN_OK Then
                    MessageBox.Show("ve_ttsStop FAILED: " & nErr)

                End If

                nErr = tts.ve_ttsClose(hTtsInstance)
                Application.DoEvents()
                If nErr <> NUAN_ERROR.NUAN_OK Then
                    MessageBox.Show("ve_ttsClose FAILED: " & nErr)
                    Exit Function
                End If

                nErr = tts.ve_ttsUnInitialize(hTtsCl)
                Application.DoEvents()
                If nErr <> NUAN_ERROR.NUAN_OK Then
                    MessageBox.Show("ve_ttsUnInitialize FAILED: " & nErr)
                    Exit Function
                End If

                ReleaseAudio()

                GC.Collect()
            End If
        Catch ex As Exception
        End Try

    End Function

    Public Function UnInitialiseTtsEngine() As Boolean

        Try
            If hTtsInstance <> 0 Then

                nErr = tts.ve_ttsClose(hTtsInstance)
                Application.DoEvents()
                If nErr <> NUAN_ERROR.NUAN_OK Then
                    MessageBox.Show("ve_ttsClose FAILED: " & nErr)
                    Exit Function
                End If

                nErr = tts.ve_ttsUnInitialize(hTtsCl)
                Application.DoEvents()
                If nErr <> NUAN_ERROR.NUAN_OK Then
                    MessageBox.Show("ve_ttsUnInitialize FAILED: " & nErr)
                    Exit Function
                End If

                ReleaseAudio()

                GC.Collect()
            End If
        Catch ex As Exception
        End Try

    End Function

    Public Sub PlayTts(ByVal text As String)
        Try

            sText = text

            If thread IsNot Nothing Then
                tts.ve_ttsStop(hTtsInstance)
                ReleaseAudio()
                thread.Join()
                thread = Nothing
            End If
            thread = New Threading.Thread(AddressOf ExecuteTts)
            thread.Start()

        Catch ex As Exception


        End Try
    End Sub

    Public Sub PauseResumeTts()
        Try
            If waveOut.PlaybackState = PlaybackState.Playing Then
                waveOut.Pause()
            ElseIf waveOut.PlaybackState = PlaybackState.Paused Then
                waveOut.Resume()
            End If
        Catch ex As Exception
            WriteError(ex, "TextToSpeech -> PlayPauseTts")
        End Try
    End Sub

    Shared waveOut As WaveOut
    Shared waveFormat As WaveFormat
    Shared provider As BufferedWaveProvider

    Private Shared Function InitializeAudio(Freq As Integer) As Boolean

        If bUploadTypePaper = True Then


            Try
                bNoAudioDected = False
                waveOut = New WaveOut()
                waveFormat = New WaveFormat(Freq, 16, 1)


                provider = New BufferedWaveProvider(waveFormat)
                provider.BufferLength = 31457280
                Dim ts = New TimeSpan(0, 30, 0)
                provider.BufferDuration = ts
                provider.DiscardOnBufferOverflow = True
                Try
                    waveOut.Init(provider)
                Catch mmEx As NAudio.MmException
                    ErrorMessage("Audio device not detected!" & vbNewLine & "Please connect an audio device to use the Text-To-Speech system!")
                    bNoAudioDected = True
                    bAudioInitialised = True
                    Return False
                Catch ex As Exception
                    WriteError(ex, "TextToSpeech -> InitializeAudio")
                    bNoAudioDected = True
                    bAudioInitialised = True
                    Return False
                End Try

                Return True
            Catch ex As Exception

            End Try

        End If


        ''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
        ''This was provided
        ''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
        Try
            bNoAudioDected = False
            waveOut = New WaveOut()
            waveFormat = New WaveFormat(Freq, 16, 1)


            provider = New BufferedWaveProvider(waveFormat)
            provider.BufferLength = 31457280
            provider.DiscardOnBufferOverflow = True
            Try
                waveOut.Init(provider)
            Catch mmEx As NAudio.MmException
                ErrorMessage("Audio device not detected!" & vbNewLine & "Please connect an audio device to use the Text-To-Speech system!")
                bNoAudioDected = True
                bAudioInitialised = True
                Return False
            Catch ex As Exception
                WriteError(ex, "TextToSpeech -> InitializeAudio")
                bNoAudioDected = True
                bAudioInitialised = True
                Return False
            End Try

            Return True
        Catch ex As Exception

        End Try
    End Function

    Private Shared Sub ReleaseAudio()
        Try
            If provider IsNot Nothing Then
                provider.ClearBuffer()
            End If

            If waveOut IsNot Nothing Then
                waveOut.[Stop]()
            End If
        Catch ex As Exception

        End Try
    End Sub
End Class

最后是If CStr(currentSelection.type) <> "None" Then Dim range As IHTMLTxtRange = TryCast(currentSelection.createRange(), IHTMLTxtRange) If IsNothing(range.text) Then MessageBox.Show("There is no available text to be read!", "No Text Available", MessageBoxButtons.OK, MessageBoxIcon.Hand) Exit Sub End If If range IsNot Nothing Then CallTts(range.text) End If ElseIf currentSelection Is Nothing And String.IsNullOrEmpty(browserContents) Then MessageBox.Show("There is no available text to be read!", "No Text Available", MessageBoxButtons.OK, MessageBoxIcon.Hand) Exit Sub Else CallTts(browserContents) End If 函数代码,然后转到提供的TextToSpeech类:

CallTts

1 个答案:

答案 0 :(得分:1)

BufferedWaveProvider背后的整个想法是,一个线程正在填充它,而另一个线程正在从中读取它。填充和读取的速度需要大致相同,否则填充线程将溢出缓冲区或读取线程将有丢失。如果您尝试将缓冲区的大小设置为几个小时,则听起来您的用例根本不适合BufferedWaveProvider。如何将音频添加到BufferedWaveProvider