Created
July 15, 2011 10:28
-
-
Save Cameron-D/1084449 to your computer and use it in GitHub Desktop.
SlimXD XAudio2 BasicSound example with comments
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
* Copyright (c) 2007-2009 SlimDX Group | |
* | |
* Permission is hereby granted, free of charge, to any person obtaining a copy | |
* of this software and associated documentation files (the "Software"), to deal | |
* in the Software without restriction, including without limitation the rights | |
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | |
* copies of the Software, and to permit persons to whom the Software is | |
* furnished to do so, subject to the following conditions: | |
* | |
* The above copyright notice and this permission notice shall be included in | |
* all copies or substantial portions of the Software. | |
* | |
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | |
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | |
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | |
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | |
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | |
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN | |
* THE SOFTWARE. | |
*/ | |
using System.Runtime.InteropServices; | |
using System.Threading; | |
using SlimDX; | |
using SlimDX.XAudio2; | |
using SlimDX.Multimedia; | |
namespace BasicSound | |
{ | |
class Program | |
{ | |
[DllImport("user32.dll", CharSet = CharSet.Auto)]; // | |
static extern short GetAsyncKeyState(int key); //spedific to this example. see below | |
const int VK_ESCAPE = 0x1B; // | |
static void Main() | |
{ | |
XAudio2 device = new XAudio2(); //audio output stream/device | |
MasteringVoice masteringVoice = new MasteringVoice(device); //no idea, presumably prepares the out stream | |
// play a PCM file | |
PlayPCM(device, "MusicMono.wav"); //this actually plays a wav file | |
// play a 5.1 PCM wave extensible file | |
PlayPCM(device, "MusicSurround.wav"); | |
masteringVoice.Dispose(); //cleanup | |
device.Dispose(); // | |
} | |
static void PlayPCM(XAudio2 device, string fileName) | |
{ | |
var s = System.IO.File.OpenRead(fileName); //open the wav file | |
WaveStream stream = new WaveStream(s); //pass the stream to the library | |
s.Close(); //close the file | |
AudioBuffer buffer = new AudioBuffer(); //init the buffer | |
buffer.AudioData = stream; //set the input stream for the audio | |
buffer.AudioBytes = (int)stream.Length; //set the size of the buffer to the size of the stream | |
buffer.Flags = BufferFlags.EndOfStream; //presumably set it to play until the end of the stream/file | |
SourceVoice sourceVoice = new SourceVoice(device, stream.Format); //this looks like it might initalise the actual output | |
sourceVoice.SubmitSourceBuffer(buffer); //pass the buffer to the output thingo | |
sourceVoice.Start(); //start the playback? | |
//above 2 sections are guessed, there is no documentation on the classes/proerties. | |
// loop until the sound is done playing | |
while (sourceVoice.State.BuffersQueued > 0) // This keeps looping while there is sound in the buffer | |
{ // (presumably). For this specific example it will stop | |
if (GetAsyncKeyState(VK_ESCAPE) != 0) // plying the sound if escape is pressed. That is what the | |
break; // DLLImport and stuff at the top is for | |
Thread.Sleep(10); // | |
} | |
// wait until the escape key is released | |
while (GetAsyncKeyState(VK_ESCAPE) != 0) //it jsut waits here until the person presses escape | |
Thread.Sleep(10); | |
// cleanup the voice | |
buffer.Dispose(); | |
sourceVoice.Dispose(); | |
stream.Dispose(); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment