The Unofficial Newsletter of Delphi Users - by Robert Vivrette




Digital Sound in Delphi - Part II

by Stanislav Holenda - texmurphy@email.cz

As I promised in my previous article, I’m back with more Delphi sound stuff. Today we shall look at possibilities of producing multi-channel sound effects. First of all we should theoretically discuss how to play two samples at one time on one sound device - this bring us to:

Mixing the Samples

Windows has ability to play sounds (I call them samples) by "itself", but when you want to, for example, play digital music together with sound FX (like clicking sounds on buttons, ...) you have a hard choice - to play music or sounds?

Well if you want to play them both, you’ll have to mix them. Mixing of samples with same sample rate (frequency) is fairly easy - you just have to calculate average value of their current values:

If you have stored your sample in array like this (let’s assume they are 8-bit mono, <64kB):

You will mix them like this:

In the ppos variable is stored the playing position, and the return value is the current value to be send to the DAC. A bit harder is when you must play sample on different rates (when playing music, you’ll have to play the instrument sample on different rate to create accord, for example) - you’ll have to choose the mixing rate which will be the frequency of sending data to DAC.

For example: Let’s assume we have mixing rate 11025 Hz (=11025 times per second, we will write to DAC) and want to play one sample on 11025 Hz and second on 22050 Hz. We will take each byte from first sample (play rate/mixing rate = step) and each second byte from second sample (22050/11025=2) and mix them together and the output will be one sound playing two times faster than another. This is relatively easy to understand but harder to make a mixing routine that is fast enough to keep up. To make a good mixing routine, you’ll have to think out an optimized algorithm and write it in assembly language, if you're a tough programmer, or you can use the easy and simple interface of the MIDAS Digital Sound System.

MIDAS Sound FX engine

I expect you are now either bored or confused from my previous paragraph full of theory and now you'd like to do something easy that works well... Well here goes a step-by-step sound FX engine guide:

First of all you need to initialize MIDAS (best in the form.create method or in application startup), but slightly different as in previous article:

Second open some channels for sound FX:

Third load samples:

Fourth play them when you want:

'sample' is a variable of type MIDASsample which must point to some data loaded with MIDASloadWave/RawSample function. MIDAS_CHANNEL_AUTO  commands MIDAS to select some free channel for playing the sample automatically. 0  this number is priority - higher means higher priority
11025  play rate of sample (=the rate you want to sample at). 63 is the volume, and MIDAS_PAN_MIDDLE is the panning constants - you can use constants or numbers in range -63..63 for panning from left to right.

Also this usage of PlaySample command will only play it, if you want also control sample properties during playback (rate, volume, panning) you’ll have to use it like this:

And then use commands described in MIDAS help under "Controlling sample playback".

Fifth you must deallocate all samples, close sound device, and other stuff when you exit your program:

So this would be in brief how to use MIDAS sound mixing capabilities. Next time we might talk about echo-effects and post processor in MIDAS and also samples & modules type and formats. If anybody got any questions about this article, or other sound-related problems let me know.

You can get a copy of the source code for my demo application by clicking here, and you can also download the files for a minimal Midas configuration, by clicking here.