Play sound from generated buffer in Windows 8 application

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);

        // There is also support for shifting the frequency.
        sourceVoice.SetFrequencyRatio(0.5f);

        sourceVoice.SubmitSourceBuffer(buffer, soundstream.DecodedPacketsInfo);

        sourceVoice.Start();
    }
+5
2

Win8RT XAudio2, SharpDX.XAudio2.

NativeFileStream DataStream, ( DataStream ). :

// 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();

:

    private void FillBuffer(short[] buffer, int sampleRate, double frequency)
    {
        double totalTime = 0;

        for (int i = 0; i < 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 & 0xFF);
            buffer[i + 1] = currentSample; //(short)(currentSample >> 8);

            totalTime += 2;
        }

    }
+8

WASAPI WinRT. (xaudio2 - ).

VB (# ): http://www.codeproject.com/Articles/460145/Recording-and-playing-PCM-audio-on-Windows-8-VB

, NAudio + NAudio Win8, .

+3

All Articles