next up previous contents
Next: 3 MIDAS concepts Up: 2 Getting started Previous: 2.2 Linking with MIDAS

2.3 An example program

This section describes a very simple example program that uses MIDAS for playing music. First, the complete program source is given in both C and Delphi format, and after that the operation of the program is described line by line. To keep the program as short as possible, all error checking is omitted, and therefore it should not be used as a template for building real applications -- the other example programs included in the MIDAS distribution are more suitable for that.

Both versions of the program should be compiled as console applications in the Win32 environment. Under MS-DOS and Linux the default compiler settings are fine.

2.3.1 C program

 1  #include <stdio.h>
 2  #include <conio.h>
 3  #include "midasdll.h"
 4
 5  int main(void)
 6  {
 7      MIDASmodule module;
 8
 9      MIDASstartup();
10      MIDASinit();
11      MIDASstartBackgroundPlay(0);
12
13      module = MIDASloadModule("..\\data\\templsun.xm");
14      MIDASplayModule(module, 0);
15
16      puts("Playing - press any key");
17      getch();
18
19      MIDASstopModule(module);
20      MIDASfreeModule(module);
21
22      MIDASstopBackgroundPlay();
23      MIDASclose();
24
25      return 0;
26  }

2.3.2 Delphi program

1   uses midasdll;
2
3   var module : MIDASmodule;
4
5   BEGIN
6       MIDASstartup;
7       MIDASinit;
8       MIDASstartBackgroundPlay(0)
9
10      module := MIDASloadModule('..\data\templsun.xm');
11      MIDASplayModule(module, 0);
12
13      WriteLn('Playing - Press Enter');
14      ReadLn;
15
16      MIDASstopModule(module);
17      MIDASfreeModule(module);
18
19      MIDASstopBackgroundPlay;
20      MIDASclose;
21  END.

2.3.3 Program description

Apart from minor syntax differences, the C and Delphi versions of the program work nearly identically. This section describes the operation of the programs line by line. The line numbers below are given in pairs: first for C, second for Delphi.

1-3, 1
Includes necessary system and MIDAS definition files
7, 3
Defines a variable for the module that will be played
9, 6
Resets the MIDAS internal state -- This needs to be done before MIDAS is configured and initialized.
10, 7
Initializes MIDAS
11, 8
Starts playing sound in the background
13, 10
Loads the module file
14, 11
Starts playing the module we just loaded, leaving no channels available for sound effects.
16-17, 13-14
Simply waits for a keypress
19, 16
Stops playing the module
20, 17
Deallocates the module we loaded
22, 19
Stops playing sound in the background
23, 20
Uninitializes the MIDAS Sound System



Petteri Kangaslampi
Mon Jan 27 00:15:29 EET 1997