我正在尝试在 Windows Phone 8.1 上以设定的频率和持续时间生成音调。跟进此处介绍的主题:在 Windows 8 应用程序中从生成的缓冲区播放声音,这是我尝试的 Windows Phone 8.1 解决方案,在 Visual Studio 2015 的模拟器中运行,在 VB.NET 中尝试实现 SharpDX.XAudio2。没有声音出来,但我认为它是正确的。有任何想法吗?
' 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)