Generate tone, or sound, in Windows Phone 8.1 app

爷,独闯天下 提交于 2019-12-25 04:18:06

问题


I am attempting to generate a tone, for a set frequency and duration, on Windows Phone 8.1. Following up on the topic presented here: Playing a sound from a generated buffer in a Windows 8 app, here's my attempted solution for Windows Phone 8.1, running in a simulator in Visual Studio 2015, in VB.NET attempting to implement SharpDX.XAudio2. No sound comes out, but I think it's right. Any ideas?

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

回答1:


This issue was resolved. Not only the tones, but playing chords, as well.




回答2:


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


来源:https://stackoverflow.com/questions/36006601/generate-tone-or-sound-in-windows-phone-8-1-app

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!