0

我正在尝试在 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)
4

2 回答 2

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
于 2016-04-13T07:52:32.617 回答
0

此问题已解决。不仅是音调,还有和弦的演奏。

于 2016-03-16T19:24:46.267 回答