在 Windows Phone 8.1 应用程序中生成音调或声音 [英] Generate tone, or sound, in Windows Phone 8.1 app
本文介绍了在 Windows Phone 8.1 应用程序中生成音调或声音的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在尝试在 Windows Phone 8.1 上以设定的频率和持续时间生成音调.跟进此处介绍的主题:从 Windows 8 应用程序中生成的缓冲区播放声音,这是我尝试的 Windows Phone 8.1 解决方案,在 Visual Studio 2015 的模拟器中运行,在 VB.NET 中尝试实现 SharpDX.XAudio2.没有声音出来,但我认为这是正确的.有什么想法吗?
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)
推荐答案
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 应用程序中生成音调或声音的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文