Directsound and streaming buffers problem

Started by
0 comments, last by bakery2k1 20 years, 1 month ago
I am currently writing a console emulator, and am about to start working on sound emulation. The machine to be emulated has a simple square-wave generator (among other things), and I think the best way to emulate this would be to use a DirectSound streaming buffer. I have read about these, and would be comfortable with playing a WAV file or similar using one. However, this project has the added complication that the sound to be played is not known in advance. For example, consider that the machine has asked for a square wave of frequency A is to be played. So, I fill the buffer with the data for this wave. Now, an instruction changes the frequency to be played to frequency B. How can I change the output to the new frequency (almost) instantly? The same problem occurs as the machine has a digitized-audio-channel. Whetever value is in a given 8-bit register should be output. Howeverm, this value may change at any time, and is likely to change very rapidly.
Advertisement
There are probably many ways to solve this problem, but what I would do in your case (since you want instant change) is to pick a fixed DirectSound buffer size and generate the wave function you want to produce on the fly (easy and cheap for square waves...I do it for alarms). You would have a thread that continously (well, every so often but at rapid rates) load ''x'' bytes of generated sound data into the buffer. When you hit some button or whatever that changes the frequency your generator responds and now the bytes being loaded are for the new wave.

The tricky part is timing the loop. Notifications are the easy way but in DirectSound that causes an inability to use hardware accelleration in some cards (so I hear). In DirectSoundCapture there is no such problem.

So if you can use notifications then you have:

Setup DirectSound Object, then DirectSoundBuffer Object and then Notification Object on that buffer. You also have to set the Windows WaitForSingleObject() junk. The ''response'' time will depend upon how frequently you notify. However, there is a cost for increasingly rapid notifications! In any case, if you want to write 100 ms of data each notification, then you would place the notifications every ''x'' bytes in the buffer depending upon sample rate, bit depth, blah, blah.

So:

In thread:

while(1)
{
WaitForSingleObject // thread sleeps until x bytes are played
Get_X_Bytes_of_SquareWaveData() // Here you generate square wave
Write_X_Bytes_to_Buffer() // Heres where all that Lock crud is
}

So now the main work is the square wave generator. And this is where you will have to keep a running "phase" indicator since your ''x'' bytes will NOT in general be an integer multiple of the frequency. But that is quite simple. When you want to change the frequency via a user imput or whatever, your "phase" indicator will have to change.

consider:

for(i = 0; i < xbytes; i++) // Do loop to make x bytes
{
if(phase_indicator < first_half_of_period) // first half of wave
{
amp = some_value;
}
else // second half of wave
{
amp = -some_value;
}
phase_indicator++; // update phase
if(phase_indicator == period) // do next period
{
phase_indicator = 0;
}
}

Hope you can read that. In any case, the ''phase_indicator'', ''first_half_of_period'', ''second_half_of_period'', and ''period'' would all have to be globals which you would change.

I am sure more efficient approaches can be thought of but this is basically how I do it (but I use sine waves). My alarms use a fixed buffer that are not streaming but that''s because I don''t change their tones, but for my synthesized sounds, I do!

Brian
Brian Reinhold

This topic is closed to new replies.

Advertisement