The Unofficial Newsletter of Delphi Users - by Robert
Vivrette
Digital Sound and Music in Delphi
by Stanislav Holenda - texmurphy@email.cz
Introduction
Digital sound on the PC has been a very tricky business in past years. There were no sound cards with a DAC (digital to analogue converter) and the PC speaker was capable of playing only 4-bits audio (compared to modern 16-bit cards), thus there were only a few programs capable of playing digital audio like MODPLAY (written in assembler). These however took a lot of processor time, because they had to do all the stuff.
Then the SoundBlaster was introduced. This card allowed you to play sound using DMA transfer. This means that the only thing the playing program has to do is prepare data - the playing of the sound itself is done by the hardware of the sound card.
This sound card allowed programs to play music in the background and do some other stuff. This brought a few sound-systems written in assembler, which might be used in Turbo Pascal as linked objects (e.g. GOLDPLAY). These programs were able to play usually 4 channel Amiga music modules. Some of these systems were good but they all had one common problem - they were design to work in real mode. In protected mode (BP7.0 or BPW and any Windows compiler) they crashed the application or even caused a system crash.
So disappointed I was with this fact, I started searching for a protected mode compatible player. Finally I’ve found it! - MIDAS.
MIDAS Digital Audio System 1.1.1
So let me tell you what MIDAS is and why it is so good:
In brief, MIDAS is a multichannel digital sound and music system, capable of playing an unlimited number of channels of digital sound on all supported platforms. It can play music modules, individual samples, and digital audio streams, in any combination.
MIDAS supports the following module formats:
In the Win32 and Linux platforms, MIDAS accesses the sound hardware through the system sound drivers, and thus works with any sound card that has proper drivers available. In Win95/NT, both the standard multimedia API's and DirectSound are supported. Under MS-DOS, MIDAS supports the following sound cards:
This release of MIDAS can be used for free for free programs, but commercial usage requires a separate license. Contact midas@housemarque.fi for details.
The Complete MIDAS package can be downloaded from http://www.s2.org/midas or http://kalahari.ton.tut.fi/s2/midas (about 1.5 megs), but all you need is MIDAS.DLL and MIDASDLL.PAS units plus licence and manuals and such stuff. See the link below for the Delphi source code that interfaces with this DLL.
MIDAS in Delphi
Usage of MIDAS in Delphi is quite easy - users call all the functions from the MIDAS.DLL using a supplied unit called MIDASDLL.PAS. The basic functions are documented in example application MINIPLAY.
First of all you need to initialize MIDAS (best in the form.create method or in application startup):
module := NIL;
playHandle := 0;
playing := false;
MIDASstartup;
if not MIDASinit then
MIDASerror;
if not MIDASstartBackgroundPlay(0) then
MIDASerror;
Second open one or more modules:
var
cfilename : array[0..256] of char;
info : MIDASmoduleInfo;
captxt : string[64];
playHandle : MIDASmodulePlayHandle;
playing : boolean;
begin
if playHandle <> 0 then begin
if not MIDASstopModule(playHandle) then
midasError;
playHandle := 0;
end;
if module <> NIL then begin
if not MIDASfreeModule(module) then
midasError;
module := NIL;
end;
module := MIDASloadModule(cfilename);
if module = NIL then begin
Application.MessageBox(MIDASgetErrorMessage(MIDASgetLastError),
'Module loading error', MB_OK or
MB_ICONSTOP);
exit;
end;
playHandle := MIDASplayModule(module, true);
if playHandle = 0 then
MIDASerror;
playing := true;
if not MIDASgetModuleInfo(module, @info) then
MIDASerror;
end;
Third play the module you want:
if (not playing) and (module <> NIL) then
begin
playHandle := MIDASplayModule(module, true);
if playHandle = 0 then
MIDASerror;
playing := true;
end;
Fourth stop the module:
if (playing) and (playHandle <> 0) then
begin
if not MIDASstopModule(playHandle) then
MIDASerror;
playHandle := 0;
playing := false;
end;
Fifth close and uninstall MIDAS on application exit:
procedure TForm1.FormClose(Sender: TObject; var Action: TCloseAction);
begin
if playing then BitBtn2Click(sender); {call the stop routine attached to
BitBtn2}
if not MIDASstopBackgroundPlay then
MIDASerror;
if not MIDASclose then
MIDASerror;
end;
Auxiliary routine(s) like MIDASError and routines for detection of song name, setting volume, echoes, etc. are in source code of my simple test player, MIDASDLL.PAS and MINIPLAY. I think they are fairly self-explanatory (if they don’t just e-mail me and we could discuss the problems). Next time we might take a look on the sound FX if someone is interested.
MIDAS was written by Petteri Kangaslampi (pekangas@sci.fi)and Jarno Paananen (jpaana@iki.fi)
Click here for the Source code and support files for this project.