I am porting some C # Windows Phone 7 applications to Windows 8.
Phone applications used XNA SoundEffect to play arbitrary sounds from the buffer. In the simplest cases, I would simply create a sine wave of the required duration and frequency. Both duration and frequency can vary greatly, so I would prefer not to rely on MediaElements (unless there is a frequency shift of the base file, but this will only help me with generating one frequency).
What is the equivalent of XNA SoundEffectInstance in WinRT?
I assume I need to use DirectX for this, but I'm not sure how to do this with another C # / XAML application. I looked at SharpDX , but it did not have the DirectSound, SecondaryBuffer, SecondaryBuffer classes, which, I believe, d should be used.
I made a few assumptions above. Maybe I'm looking for the wrong classes or there is a completely separate way to generate arbitrary sound from a Windows 8 application.
I found an example of using XAudio2 from SharpDX to play a wav file through AudioBuffer . This seems promising, I just need to replace the created sound buffer for my own file stream.
PM> Install SharpDX Package
PM> Install SharpDX.XAudio2 Package
public void PlaySound()
{
XAudio2 xaudio;
MasteringVoice masteringVoice;
xaudio = new XAudio2();
masteringVoice = new MasteringVoice(xaudio);
var nativefilestream = new NativeFileStream(
@"Assets\SpeechOn.wav",
NativeFileMode.Open,
NativeFileAccess.Read,
NativeFileShare.Read);
var soundstream = new SoundStream(nativefilestream);
var waveFormat = soundstream.Format;
var buffer = new AudioBuffer
{
Stream = soundstream.ToDataStream(),
AudioBytes = (int)soundstream.Length,
Flags = BufferFlags.EndOfStream
};
var sourceVoice = new SourceVoice(xaudio, waveFormat, true);
sourceVoice.SetFrequencyRatio(0.5f);
sourceVoice.SubmitSourceBuffer(buffer, soundstream.DecodedPacketsInfo);
sourceVoice.Start();
}