2016-03-15 118 views
0

我正在嘗試在Windows Phone 8.1上爲設定的頻率和持續時間生成一個音調。繼續介紹這裏的主題:Playing a sound from a generated buffer in a Windows 8 app,這裏是我嘗試的Windows Phone 8.1解決方案,在Visual Studio 2015中的模擬器中運行,在VB.NET中嘗試實現SharpDX.XAudio2。沒有聲音出現,但我認爲是正確的。有任何想法嗎?在Windows Phone 8.1應用程序中生成音調或聲音

' Initialization phase, keep this buffer during the life of your application 
' Allocate 10s at 44.1Khz of stereo 16bit signals 
Dim myBufferOfSamples = New Short(44100 * 10 * 2 - 1) {} 

' Create a DataStream with pinned managed buffer 
Dim ds = SharpDX.DataStream.Create(myBufferOfSamples, True, True) 

Dim bu As New SharpDX.XAudio2.AudioBuffer 
bu.Stream = ds 
bu.AudioBytes = ds.Length 
bu.Flags = SharpDX.XAudio2.BufferFlags.EndOfStream 

'Fill myBufferOfSamples 
Dim sampleBuffer() As Short = myBufferOfSamples 
Dim sampleRate As Integer = 44100 
Dim frequency As Double = 440 
' 
Dim totalTime As Double = 0 
For i As Integer = 0 To sampleBuffer.Length - 2 Step 2 
Dim sampleTime As Double = totalTime/sampleRate 
Dim currentSample As Short 
currentSample = Math.Sin(2 * Math.PI * frequency * sampleTime) * Short.MaxValue 
sampleBuffer(i) = currentSample 
sampleBuffer(i + 1) = currentSample 
totalTime += 1 
Next 

' PCM 44.1Khz stereo 16 bit format 
Dim waveFormat = New SharpDX.Multimedia.WaveFormat() 

Dim xaudio As New SharpDX.XAudio2.XAudio2() 
Dim masteringVoice As New SharpDX.XAudio2.MasteringVoice(xaudio) 
Dim sourceVoice = New SharpDX.XAudio2.SourceVoice(xaudio, waveFormat, True) 

' Submit the buffer 
sourceVoice.SubmitSourceBuffer(bu, Nothing) 

回答

0
Sub Beeper(ByVal Amp As Integer, ByVal Duration As Double, ByVal Sync As Boolean, ByVal Frequencies() As Integer) 
    'Frequencies = {440, 523, 659} 
    Duration = (Duration/1000) 

    ' Initialization phase, keep this buffer during the life of your application 
    ' Allocate 10s at 44.1Khz of stereo 16bit signals 
    Dim sampleBuffer = New Short(44100 * Duration * 2 - 1) {} 

    ' Create a DataStream with pinned managed buffer 
    Dim ds = SharpDX.DataStream.Create(sampleBuffer, True, True, 0, True) 

    Dim bu As New SharpDX.XAudio2.AudioBuffer 
    bu.LoopCount = 0 'SharpDX.XAudio2.AudioBuffer.LoopInfinite 
    bu.Stream = ds 
    bu.AudioBytes = ds.Length 
    bu.Flags = SharpDX.XAudio2.BufferFlags.EndOfStream 

    'Fill myBufferOfSamples 
    Dim sampleRate As Integer = 44100 
    Dim Amplitude As Double = (1/Frequencies.Length) 
    '      ' 
    Dim totalTime As Double = 0 
    For i As Integer = 0 To sampleBuffer.Length - 2 Step 2 
     Dim sampleTime As Double = totalTime/sampleRate 
     Dim currentSample As Short 
     currentSample = 0 'must manually reset 
     For y As Integer = 0 To Frequencies.Length - 1 
      currentSample += Amplitude * Math.Sin(2 * Math.PI * Frequencies(y) * sampleTime) * Short.MaxValue 
     Next 
     sampleBuffer(i) = currentSample 
     sampleBuffer(i + 1) = currentSample 
     totalTime += 1 
    Next 

    ' PCM 44.1Khz stereo 16 bit format 
    Dim waveFormat = New SharpDX.Multimedia.WaveFormat() 
    Dim xaudio As New SharpDX.XAudio2.XAudio2() 
    Dim masteringVoice As New SharpDX.XAudio2.MasteringVoice(xaudio) 
    Dim sourceVoice = New SharpDX.XAudio2.SourceVoice(xaudio, waveFormat, True) 
    sourceVoice.Stop() 
    sourceVoice.FlushSourceBuffers() 
    sourceVoice.SetVolume(1) 

    ' Submit the buffer 
    sourceVoice.SubmitSourceBuffer(bu, Nothing) 
    sourceVoice.Start() 

End Sub 
0

此問題已解決。不僅是音調,還要演奏和絃。

+0

你介意分享改變後的內容嗎 ? – John

+0

@John:請參閱下面的其他答案。 – swabygw

+0

好的,明白。你能接受這個答案嗎? – John

相關問題