• Announcements

    • khawk

      Download the Game Design and Indie Game Marketing Freebook   07/19/17

      GameDev.net and CRC Press have teamed up to bring a free ebook of content curated from top titles published by CRC Press. The freebook, Practices of Game Design & Indie Game Marketing, includes chapters from The Art of Game Design: A Book of Lenses, A Practical Guide to Indie Game Marketing, and An Architectural Approach to Level Design. The GameDev.net FreeBook is relevant to game designers, developers, and those interested in learning more about the challenges in game development. We know game development can be a tough discipline and business, so we picked several chapters from CRC Press titles that we thought would be of interest to you, the GameDev.net audience, in your journey to design, develop, and market your next game. The free ebook is available through CRC Press by clicking here. The Curated Books The Art of Game Design: A Book of Lenses, Second Edition, by Jesse Schell Presents 100+ sets of questions, or different lenses, for viewing a game’s design, encompassing diverse fields such as psychology, architecture, music, film, software engineering, theme park design, mathematics, anthropology, and more. Written by one of the world's top game designers, this book describes the deepest and most fundamental principles of game design, demonstrating how tactics used in board, card, and athletic games also work in video games. It provides practical instruction on creating world-class games that will be played again and again. View it here. A Practical Guide to Indie Game Marketing, by Joel Dreskin Marketing is an essential but too frequently overlooked or minimized component of the release plan for indie games. A Practical Guide to Indie Game Marketing provides you with the tools needed to build visibility and sell your indie games. With special focus on those developers with small budgets and limited staff and resources, this book is packed with tangible recommendations and techniques that you can put to use immediately. As a seasoned professional of the indie game arena, author Joel Dreskin gives you insight into practical, real-world experiences of marketing numerous successful games and also provides stories of the failures. View it here. An Architectural Approach to Level Design This is one of the first books to integrate architectural and spatial design theory with the field of level design. The book presents architectural techniques and theories for level designers to use in their own work. It connects architecture and level design in different ways that address the practical elements of how designers construct space and the experiential elements of how and why humans interact with this space. Throughout the text, readers learn skills for spatial layout, evoking emotion through gamespaces, and creating better levels through architectural theory. View it here. Learn more and download the ebook by clicking here. Did you know? GameDev.net and CRC Press also recently teamed up to bring GDNet+ Members up to a 20% discount on all CRC Press books. Learn more about this and other benefits here.
Sign in to follow this  
Followers 0
Meowza

XAudio2

9 posts in this topic

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

0

Share this post


Link to post
Share on other sites
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;
}
}


0

Share this post


Link to post
Share on other sites
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)
0

Share this post


Link to post
Share on other sites
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?
0

Share this post


Link to post
Share on other sites
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);
}
}
}




0

Share this post


Link to post
Share on other sites
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
0

Share this post


Link to post
Share on other sites
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.
0

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!


Register a new account

Sign in

Already have an account? Sign in here.


Sign In Now
Sign in to follow this  
Followers 0