Audio Buffers = Nightmare

Talk about anything else you want

Moderator: BigEvilCorporation

Post Reply
TulioAdriano
Very interested
Posts: 81
Joined: Tue Jul 10, 2007 7:45 pm
Location: Brazil / USA
Contact:

Audio Buffers = Nightmare

Post by TulioAdriano » Thu Mar 20, 2008 4:58 pm

Not sure if Blabla is the best section for this topic but I didn't know where else to put it... please fell free to move it if necessary.

Ok I'll disclose some information on my secret project (not secret anymore as of now).

I am creating an app to play Sega Genesis music (nothing new so far) and also to test instruments with LFO, SSG-EG support, 6 channel poliphony (for rich chords) also DAC and PSG (now that's kinda new/not common). For that purpose first thing I made is a standard Windows API DLL (C/C++) with the YM2612 Emulator and SN76489 emulator. Then I created some interface sound libraries, currently C# and Visual Basic 6.

Now the thing that's killing me is that all works super fine as long as I retrieve static playable sound and play it at once... but when it comes to the looping buffer I am having a very hard time geting it to play clean.

I menaged to get clean sound from my work computer writing 10 ms on each time on a 200 ms buffer, but when I tried in my less powerful laptop I got sound gaps, means my buffer system is not effective.

Here is a sample code of my simple inneficient buffer system. The code is in C#:

Code: Select all

void soundTimer_Elapsed(object sender, System.Timers.ElapsedEventArgs e)
{
    lock (tlock)
    {
        if (sound.PlayPosition >= writePos)
        {
            soundEngine.UpdateMDBuffer(ref bufferByteArray);
            writePos += bufferByteArray.Length;
            if (writePos >= 35280) //sound.Caps.BufferBytes)
            {
                writePos = 0;
            }
            sound.Write(writePos, bufferByteArray, LockFlag.None);
        }
    }
}
This timer executes every 1 ms and retrieve 10 ms of sound that is added to the buffer as it is read.

I really tried to research on dynamic buffers but most things are based on sample playback from wave files, it gets too confuse since they're not oriented to a dynamic sound synthetizer like Shiru's TFM MM or emulators such as Gens...

Given the fact that these apps I mentioned play clean sound with immediate response (small buffer) and in all computers I tried so far... I'd like to get some tips for you guys!

And obviously of course, once I get everything working I'll release the DLL with the soundlibraries I've created. This might make easier for people to interface their apps with MD sound, such as I am doing. (or trying to)

Thank you very much.
Tulio
Image

Shiru
Very interested
Posts: 786
Joined: Sat Apr 07, 2007 3:11 am
Location: Russia, Moscow
Contact:

Post by Shiru » Wed Mar 26, 2008 4:03 pm

As I understand, you tried to use looped buffer and replace data in already played part of buffer? This method can work somewhat OK. I used it on few systems, but completely refused from it for Windows, because on some computers (possibly depends from sound card drivers) it just does not work, because current sample position function returns incorrect value, or something (forgot details, that was long ago).

You can use more traditional method with buffers pool instead (with many buffers). You can get working example of code (for Windows) from package of TFM MM v1.1 - /replayers/pc/waveout.h

TulioAdriano
Very interested
Posts: 81
Joined: Tue Jul 10, 2007 7:45 pm
Location: Brazil / USA
Contact:

Post by TulioAdriano » Wed Mar 26, 2008 4:12 pm

Thank you EXTREMELY very much. I'll study the code and try to figure out a C# implementation of it.

Tulio.
Image

Post Reply