在 Windows Phone 8.1 应用程序中生成音调或声音
Generate tone, or sound, in Windows Phone 8.1 app
我正在尝试在 Windows Phone 8.1 上以设定的频率和持续时间生成音调。跟进此处提出的主题:Playing a sound from a generated buffer in a Windows 8 app,这是我在 Visual Studio 2015 年 Visual Studio 模拟器中针对 Windows Phone 8.1、运行 的尝试解决方案,在 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)
此问题已解决。不仅是音调,还有和弦。
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
我正在尝试在 Windows Phone 8.1 上以设定的频率和持续时间生成音调。跟进此处提出的主题:Playing a sound from a generated buffer in a Windows 8 app,这是我在 Visual Studio 2015 年 Visual Studio 模拟器中针对 Windows Phone 8.1、运行 的尝试解决方案,在 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)
此问题已解决。不仅是音调,还有和弦。
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