Jump to content

  • Log In with Google      Sign In   
  • Create Account

XAudio2


Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

  • You cannot reply to this topic
9 replies to this topic

#1 Meowza   Members   -  Reputation: 100

Like
0Likes
Like

Posted 11 February 2010 - 08:49 AM

Hello, I look at SlimDX and DirectX samples for XAudio2 (they look exactly alike). I couldn't find any tutorials besides these for XAudio2 so I got some questions. 1. I can play simple sounds in XAudio2 with the example. I was just wondering if this way is feasible for playing lots of sounds at once. 2. Using this way, wouldn't I be using a ton of memory? Or should I have a list of Streams with preloaded wav / ogg files? 3. Do I need an AudioBuffer for each sound I want to play? Or can I play multiple sounds at once with one AudioBuffer? 4. I couldn't figure out how to load an ogg file with XAudio2, does anyone know how? thank you
[source = "csharp"]
using SlimDX;
using SlimDX.Multimedia;
using SlimDX.XAudio2;

public class Sound
{
    public static XAudio2 device;
    public static MasteringVoice mv;

    public static void Initialize()
    {
        device = new XAudio2();
        mv = new MasteringVoice(device);
    }

    public static void PlayBGM(string name)
    {
        WaveStream stream = new WaveStream(name);

        AudioBuffer buffer = new AudioBuffer();
        buffer.AudioData = stream;
        buffer.AudioBytes = (int)stream.Length;
        buffer.Flags = BufferFlags.EndOfStream;

        SourceVoice sourceVoice = new SourceVoice(device, stream.Format);
        sourceVoice.SubmitSourceBuffer(buffer);
        sourceVoice.Start();
    }



Sponsor:

#2 Fire Lancer   Members   -  Reputation: 189

Like
0Likes
Like

Posted 12 February 2010 - 01:55 AM

Quote:
Original post by Meowza
Hello, I look at SlimDX and DirectX samples for XAudio2 (they look exactly alike). I couldn't find any tutorials besides these for XAudio2 so I got some questions.

Information on XAudio2 is a little thin :(

Each IXAudio2SourceVoice can play one sound, and you can use the same buffer with multiple IXAudio2SourceVoice 's. This is mostly useful for sound effects.

What I do is load the entire wav file into a buffer (sound effects generally are not that big), and then just pass the buffer to each new IXAudio2SourceVoice to play the sound. This way very little memory is used for playing each sound.



I don't know how you would do ogg/vorbis from .net. I used the reference libogg and libvorbisfile to decode the track into PCM data I could send to XAudio2. Here I kept a one source voice to one set of buffers ratio.

I had 2 fairly large buffers (around 5 seconds of stereo audio each). When the source voice finished playing one, I loaded the next block of data into it and resubmitted it.


Here is some bits of my code (C++) for streaming ogg/vorbis.

...run every few seconds
if(voice)
{
XAUDIO2_VOICE_STATE state;
voice->GetState(&state);
if(!almostDone)//more data to play
if(state.BuffersQueued < BUFFER_COUNT)
fillBuffer();
else if(state.BuffersQueued == 0)//all buffers played and no more to come
stop();//finished
}

void fillBuffer()
{
size_t readBytes;
//fill a buffer with audio data, returns false if there is no more data to come after this
if(!data->read(buffers[currentDiskReadBuffer], BUFFER_COUNT, &readBytes))
almostDone = true;
assert(readBytes > 0);
//submit buffer
XAUDIO2_BUFFER buffer = {0};
buffer.pAudioData = buffers[currentDiskReadBuffer];
buffer.AudioBytes = readBytes;
if(almostDone)
buffer.Flags |= XAUDIO2_END_OF_STREAM;
if(FAILED(voice->SubmitSourceBuffer(&buffer)))
throwex(XAudio2Error);
//cycle buffers
(++currentDiskReadBuffer) %= BUFFER_COUNT;
}

//handles the loading of vorbis audio data, in this case streaming
//I could also stream .wav files if I wanted using a WavData class, however
//since I only use those for sound effects, i normally read the entire thing at once into a buffer and then close the WavData object.
//ie WavData::read(data, WavData::getLengthBytes(), &read)
class VorbisData : public AudioData
...
bool VorbisData::read(unsigned char *data, unsigned targetBytes, unsigned *readBytes)
{
unsigned bytes = 0;
int sec = 0;
int ret = 1;
memset(data, 0, targetBytes);
//Read in the bits
while(ret > 0 && bytes<targetBytes)
{
//read and decode the next bit of data
//returns bytes read, or <0 for errors
ret = ov_read(&vorbisFile, (char*)data+bytes,
targetBytes-bytes, 0, 2, 1, &sec);
bytes += ret;
}
*readBytes = bytes;
switch(ret)
{
//eof
case 0:return false;
//errors
case OV_HOLE:
logWrite(LOG_ERROR,
L"Vorbis: Interuption in audio data.\n"+sourceName);
return false;
case OV_EINVAL:
logWrite(LOG_ERROR,
L"Vorbis: Failed to read headers.\n"+sourceName);
return false;
case OV_EBADLINK:
logWrite(LOG_ERROR,
L"Vorbis: Invalid stream section.\n"+sourceName);
return false;
//more data
default:return true;
}
}




#3 feal87   Members   -  Reputation: 238

Like
0Likes
Like

Posted 12 February 2010 - 02:22 AM

Quote:
Original post by Meowza
Hello, I look at SlimDX and DirectX samples for XAudio2 (they look exactly alike). I couldn't find any tutorials besides these for XAudio2 so I got some questions.

1. I can play simple sounds in XAudio2 with the example. I was just wondering if this way is feasible for playing lots of sounds at once.

2. Using this way, wouldn't I be using a ton of memory? Or should I have a list of Streams with preloaded wav / ogg files?

3. Do I need an AudioBuffer for each sound I want to play? Or can I play multiple sounds at once with one AudioBuffer?

4. I couldn't figure out how to load an ogg file with XAudio2, does anyone know how? thank you

*** Source Snippet Removed ***


I'll give some hints gathered from using XAudio2 in my engine :

1) Dispose the voice on a secondary thread cause the dispose works only when the XAudio2 thread is COMPLETELY IDLE, so it may take 1-2 ms. It is safe to do so (the MSDN agrees)
2) As you are using SlimDX, take extremely care to the GC usage as the last version is affected by a "bug" (or as they say "feature") that create tons of MB of trash for 100-200 sounds played. (Try searching the issue on their tracker, its related to the event OnProcessingPassStart and affect even if you don't use it)
3) As far as I know you need a separate audiobuffer for each voice, you may reuse the same audiobuffer, but only after the buffer has been completely read. (There is an event if i'm correct)
4) You have to parse by yourself (or using a library), create a waveformat and pass the data to XAudio2. Its not a one-click process. (The same for Mp3 or XWM or even WAV (but the WAV format parse is done by the SlimDX guys))
I don't have experience with OGG, but as for XWM i pass the compressed data to the XAudio2 thread (correctly parsed and divided)

#4 Meowza   Members   -  Reputation: 100

Like
0Likes
Like

Posted 12 February 2010 - 06:35 AM

Thank you Fire Lancer and feal87. This info is very helpful!

#5 Meowza   Members   -  Reputation: 100

Like
0Likes
Like

Posted 12 February 2010 - 10:24 AM

I use your method Fire Lancer. It crashes when I do lots of sounds in quick succession though. I guess this is because last source voice is still trying to read from buffer when I change it to a new sound.

I try FlushSourceBuffers() but that does not help. Is there way to do this?

#6 Meowza   Members   -  Reputation: 100

Like
0Likes
Like

Posted 12 February 2010 - 02:11 PM

Here is my code, I call PlaySound every 50ms or so.

I get this error when I try to submit the source buffer. It plays the sound once but when I try and play it again is when I get the error.

Specified argument was out of the range of valid values.
Parameter name: readLength

[source = "csharp"]
using SlimDX;
using SlimDX.Multimedia;
using System;
using System.IO;
using System.Collections.Generic;
using SlimDX.XAudio2;

public class Sound
{
public static XAudio2 device;
public static MasteringVoice mv;
public static AudioBuffer ab;
public static AudioBuffer se;

public static void Initialize()
{
device = new XAudio2();
mv = new MasteringVoice(device);
ab = new AudioBuffer();
se = new AudioBuffer();
}

public static void PlaySound(string name)
{
se.AudioData = Logic.s[name];
se.AudioBytes = (int)Logic.s[name].Length;
se.Flags = BufferFlags.None;

SourceVoice sv = new SourceVoice(device, Logic.s[name].Format);
sv.SubmitSourceBuffer(se); // Errors here
sv.Start();
}

public static void LoadSounds()
{ // Load all our sounds into dictionary
Logic.s = new Dictionary<string, WaveStream>();

DirectoryInfo di = new DirectoryInfo(@"C:\Work\Sounds\");
FileInfo[] fi = di.GetFiles("*.wav");
foreach (FileInfo f in fi)
{
WaveStream s = new WaveStream(f.FullName);
Logic.s.Add(Path.GetFileNameWithoutExtension(f.Name), s);
}
}
}






#7 feal87   Members   -  Reputation: 238

Like
0Likes
Like

Posted 12 February 2010 - 08:04 PM

Set the position of the stream of the audiobuffer to 0. That's the problem.
If you leave it as is, it try to start from the end of the buffer...:P

#8 Meowza   Members   -  Reputation: 100

Like
0Likes
Like

Posted 12 February 2010 - 08:52 PM

ty for reply feal87,

it works now :)

#9 Fire Lancer   Members   -  Reputation: 189

Like
0Likes
Like

Posted 12 February 2010 - 09:55 PM

Quote:
Original post by Meowza
I use your method Fire Lancer. It crashes when I do lots of sounds in quick succession though. I guess this is because last source voice is still trying to read from buffer when I change it to a new sound.

I try FlushSourceBuffers() but that does not help. Is there way to do this?


The important thing is you must 100% guarantee you wont change the buffer while its being played, that's why for streaming I only ever have the one source voice per set of buffers, cause there always changing.


To make the guarantee you have a couple of options:

1) Load all the sounds you want into buffers (at the start of a level or whatever), then unload them at the end of the level after terminating any playing sounds.

2) Create a way to tell if the buffer is still being used. My method was to add some reference counting to my buffer objects, increment it when I play it, then decrement it when the source voice is finished. This way I know if its safe to change the buffer in any way.


In .NET I assume the buffer objects get garbage collected? In which case option 2 is more or less done for you. Load your sounds into brand new buffers, and when your done playing new instances of it clear your references to it. When XAudio2 is also done with it, the GC should be able to reclaim the memory.

EDIT: It probably worth noting that by buffer I mean the actual data buffer, not the AudioBuffer object that takes a reference to it, those are cheap to create, use very little memory, (assuming .NET didn't do something with them, in C++ the equivalent object is just a small data structure) and should generally be created once for each time you play the "real buffer" IMO. You can see I did this in the "//submit buffer" portion of the code I posted.


EDIT2:
Also be sure to set XAUDIO2_END_OF_STREAM flag as needed. If you don't XAudio2 will complain that the source is being starved when it runs out of data to play.

#10 Meowza   Members   -  Reputation: 100

Like
0Likes
Like

Posted 13 February 2010 - 08:40 AM

i see, thx again Fire Lancer




Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.



PARTNERS