在Windows Phone 8.1应用程序

时间:2016-03-15 09:00:06

标签: vb.net windows-phone-8 windows-phone-8.1 sharpdx xaudio2

我试图在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。没有声音,但我认为是对的。有什么想法吗?

' 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)

2 个答案:

答案 0 :(得分:0)

此问题已得到解决。不仅是音调,还有和弦。

答案 1 :(得分: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