- Online Community Forums » Programming Questions » DIGI fails after MIDI under DOS

This thread is locked; no one can reply to it. rss feed Print
DIGI fails after MIDI under DOS
Bill Morris
Member #3,734
July 2003

I have run various test programs, following the examples, to play a wav file, then a MIDI (with or without loading of patches), then play a wav file again. The first wav play is fine. MIDI is fine. The final wav always fails. I have used remove_sound, and remove_keyboard, remove_timer (after destroy_midi) in the MIDI function, followed by the install counterparts in the wav routine, tried MIDI independently as a child process, tried repeating Allegro initialization, even having a test program shut down with an atexit routine to call a batch to restart.

The only thing that works is letting the program exit and manually calling it again, whereupon it plays the first wav, then the MIDI, then fails with the second call to play a wav. I've tried even combining them in turn in main, leaving keyboard and timer installed. I've tried double installation and the appropriate _NONE calls.

I normally have MIDI as a child process and DIGI as a function in my program. It took a while to notice that the normal sounds weren't working after MIDI play, since MIDI is used by the alarm clock, and I or a user wouldn't normally touch the computer for a while after shutting off the alarm. Actually, the MP3 and voice alarm options get more usage than MIDI. No problem with them.

At first I thought it was a bug in my program. Then I made various tests, mentioned above. I thought remove_sound, if the MIDI routine were in the main program, or return 0 from main if a child process, would be enough to put MIDI to bed when finished. One thing I just thought of to try is exit(0) instead of return 0 in the child version.

I checked the old threads back to 1999 for this question. Didn't see it.

Member #88
April 2000

Do you have some code we could look at?

Graphic file formats used to fascinate me, but now I find them rather satanic.

Bill Morris
Member #3,734
July 2003

Here's a test. It plays the first wav then the MIDI but fails on the second wav. It displays all three messages to show it has bypassed the places where the usual error messages would be for an install_sound() or other such failure. It gets caught and needs a keypress while attemting play_sample() for the second wav. If I rem out the MIDI call in main(), it plays the two wavs and exits normally.

1#include <stdlib.h>
2#include "allegro.h"
4int PlayWav(char *filespec)
6 SAMPLE *the_sample;
7 int pan=128;
8 int pitch=1000;
9 int progress=0;
11 install_keyboard();
12 install_timer();
13 set_volume(128,128);
14 if(install_sound(DIGI_AUTODETECT, MIDI_NONE, NULL) != 0) return(1);
15 the_sample=load_sample(filespec);
16 if(!the_sample) return(1);
17 printf("Attempting to play %s\n", filespec);
18 play_sample(the_sample, 255, pan, pitch, 0);
19 do {
20 progress=voice_get_position(0);
21 if( progress < 0 ) break;
22 poll_keyboard();
23 rest(2);
24 } while ((!key[KEY_ESC]) && (!key[KEY_SPACE]));
26 destroy_sample(the_sample);
27 remove_sound();
28 remove_timer();
29 remove_keyboard();
30 return(0);
32int PlayMidi(char *filespec)
34 MIDI *the_music;
36 install_keyboard();
37 install_timer();
38 set_volume_per_voice(1);
39 if(install_sound(DIGI_NONE, MIDI_AUTODETECT, NULL) != 0) return(1);
40 set_volume(255,255);
41 the_music=load_midi(filespec);
42 if(!the_music) return(1);
43 printf("Attempting to play %s\n", filespec);
44 play_midi(the_music, 0);
45 readkey();
47 destroy_midi(the_music);
48 remove_sound();
49 remove_timer();
50 remove_keyboard();
51 return(0);
53int main(int argc, char *argv[])
55 allegro_init();
56 PlayWav("McCoy.wav");
57 PlayMidi("Mydir.mid");
58 PlayWav("standing.wav");
59 return 0;

Kitty Cat
Member #2,815
October 2002

That's wrong, as you're not always gauranteed to get the 1st (0th?) voice, and play_sample automatically deallocates the voice when it's done playing.
Instead, what you should do is:

int voice_num = play_sample(the_sample, 255, pan, pitch, 0);
while(voice_check(voice_num) != NULL && !keypressed())


"Do not meddle in the affairs of cats, for they are subtle and will pee on your computer." -- Bruce Graham

Bill Morris
Member #3,734
July 2003

Thank, Kitty Cat.

I added that to the test program and will check voice_num in the real program.

But I still need better cleanup at the end of that MIDI routine.

I found a dirty solution. In the test program above I followed PlayMidi() with a spawnl() to MpxPlay, after which the PlayWav() above was back in business. Otherwise it still hangs after MIDI, and I've got it displaying voice_num.

Member #794
November 2000

Why on earth are deinitialising everything at the end of one of those functions?
Initialise everything at the beginning of the program and deinitialise it at the end (or let allegro_exit() do that).

Bill Morris
Member #3,734
July 2003

I have been using the MIDI program as a child process. The above is just one test program I'm using to look for a solution. Somewhere in the MIDI portion above, something is wrong.

Go to: