Note that there are some explanatory texts on larger screens.

plurals
  1. POPlaying a sound from a generated buffer in a Windows 8 app
    primarykey
    data
    text
    <p>I'm porting some C# Windows Phone 7 apps over to Windows 8.</p> <p>The phone apps used an XNA <a href="http://msdn.microsoft.com/en-us/library/dd282429.aspx" rel="nofollow">SoundEffect</a> to play arbitrary sounds from a buffer. In the simplest cases I'd just create a sine wave of the required duration and frequency. Both the duration and frequency can vary greatly, so I'd prefer not to rely on MediaElements (unless there is someway to shift the frequency of a base file, but that will only help me with the single frequency generation).</p> <p><strong>What is the equivalent of an XNA SoundEffectInstance in WinRT?</strong></p> <p>I assume I'll need to use DirectX for this, but I'm not sure how to go about this from an otherwise C#/XAML app. I've had a look at <a href="http://sharpdx.org/" rel="nofollow">SharpDX</a>, but it didn't seem to have the DirectSound, SecondaryBuffer, SecondaryBuffer classes that I assume I'd need to use.</p> <p>I've made a number of assumptions above. It may be I'm looking for the wrong classes or there is an entirely separate way to generate arbitrary sound from a Windows 8 app. </p> <hr> <p>I found <a href="http://sharpdx.org/forum/7-documentation/39-audio-samples#65" rel="nofollow">an example using XAudio2 from SharpDX to play a wav file via an AudioBuffer</a>. This seems promising, I'd just need to substitute my generated audio buffer for the native file stream.</p> <blockquote> <p>PM> Install-Package SharpDX</p> <p>PM> Install-Package SharpDX.XAudio2</p> </blockquote> <pre><code> 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); // There is also support for shifting the frequency. sourceVoice.SetFrequencyRatio(0.5f); sourceVoice.SubmitSourceBuffer(buffer, soundstream.DecodedPacketsInfo); sourceVoice.Start(); } </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload