Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>The only way to generate dynamic sound in Win8RT is to use XAudio2, so you should be able to do this with SharpDX.XAudio2. </p> <p>Instead of using NativeFileStream, just instantiate a DataStream directly giving your managed buffer (or you can use an unmanaged buffer or let DataStream instantiate one for you). The code would be like this:</p> <pre><code>// Initialization phase, keep this buffer during the life of your application // Allocate 10s at 44.1Khz of stereo 16bit signals var myBufferOfSamples = new short[44100 * 10 * 2]; // Create a DataStream with pinned managed buffer var dataStream = DataStream.Create(myBufferOfSamples, true, true); var buffer = new AudioBuffer { Stream = dataStream, AudioBytes = (int)dataStream.Length, Flags = BufferFlags.EndOfStream }; //... // Fill myBufferOfSamples //... // PCM 44.1Khz stereo 16 bit format var waveFormat = new WaveFormat(); XAudio2 xaudio = new XAudio2(); MasteringVoice masteringVoice = new MasteringVoice(xaudio); var sourceVoice = new SourceVoice(xaudio, waveFormat, true); // Submit the buffer sourceVoice.SubmitSourceBuffer(buffer, null); // Start playing sourceVoice.Start(); </code></pre> <p>Sample method to fill the buffer with a Sine wave:</p> <pre><code> private void FillBuffer(short[] buffer, int sampleRate, double frequency) { double totalTime = 0; for (int i = 0; i &lt; buffer.Length - 1; i += 2) { double time = (double)totalTime / (double)sampleRate; short currentSample = (short)(Math.Sin(2 * Math.PI * frequency * time) * (double)short.MaxValue); buffer[i] = currentSample; //(short)(currentSample &amp; 0xFF); buffer[i + 1] = currentSample; //(short)(currentSample &gt;&gt; 8); totalTime += 2; } } </code></pre>
 

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