Allegro.cc - Online Community
Post Reply

Allegro.cc Forums » Programming Questions » [Win32] MIDI Callback Crash...suggestions?

rss feed Print
[Win32] MIDI Callback Crash...suggestions?
ZoriaRPG
Member #16,714
July 2017
avatar

This bit of code seems to crash if the alleg process is running while a recording tool is capturing the audio and video, or possibly just in general on Win10, but not on Win7.

Is there anything here that stands out as being problematic?

#SelectExpand
1#ifdef _WIN32 2void switch_out_callback() 3{ 4 if(midi_patch_fix==0 || currmidi==0) 5 return; 6 7 bool was_paused=midi_paused; 8 long pos=midi_pos; 9 int digi_vol, midi_vol; 10 11 get_volume(&digi_vol, &midi_vol); 12 stop_midi(); 13 jukebox(currmidi); 14 set_volume(digi_vol, midi_vol); 15 midi_seek(pos); 16 17 if(was_paused) 18 { 19 midi_pause(); 20 midi_paused=true; 21 } 22} 23 24void switch_in_callback() 25{ 26 if(midi_patch_fix==0 || currmidi==0) 27 return; 28 29 bool was_paused=midi_paused; 30 long pos=midi_pos; 31 int digi_vol, midi_vol; 32 33 get_volume(&digi_vol, &midi_vol); 34 stop_midi(); 35 jukebox(currmidi); 36 set_volume(digi_vol, midi_vol); 37 midi_seek(pos); 38 39 if(was_paused) 40 { 41 midi_pause(); 42 midi_paused=true; 43 } 44}

Edgar Reynaldo
Major Reynaldo
May 2007
avatar

It all depends on what 'jukebox(currmidi);' does...

I don't see why you're changing the midi settings on switch in and out, could you explain what this code is supposed to be doing?

ZoriaRPG
Member #16,714
July 2017
avatar

ZC users who use soundfonts report that when they use alt+tab or otherwise change the focus, then return to the game, that the MIDI driver changes all of the instruments to piano instruments. This has been a longstanding bug, and this patch has existed for years.

On some newer systems, this patch, a callback used by set_display_switch_callback, crashes ZC. I'm not sure why the MIDI instrument changes on their systems, as I do npt use soundfont system modifications, and have never experience the bug; nor the crash, as I use Win7.

#SelectExpand
1void jukebox(int index,int loop) 2{ 3 music_stop(); 4 5 if(index<0) index=MAXMIDIS-1; 6 7 if(index>=MAXMIDIS) index=0; 8 9 music_stop(); 10 11 // Allegro's DIGMID driver (the one normally used on on Linux) gets 12 // stuck notes when a song stops. This fixes it. 13 if(strcmp(midi_driver->name, "DIGMID")==0) 14 set_volume(0, 0); 15 16 set_volume(-1, mixvol(tunes[index].volume,midi_volume>>1)); 17 play_midi((MIDI*)tunes[index].data,loop); 18 19 if(tunes[index].start>0) 20 midi_seek(tunes[index].start); 21 22 midi_loop_end = tunes[index].loop_end; 23 midi_loop_start = tunes[index].loop_start; 24 25 currmidi=index; 26 master_volume(digi_volume,midi_volume); 27 midi_paused=false; 28} 29 30void jukebox(int index) 31{ 32 if(index<0) index=MAXMIDIS-1; 33 34 if(index>=MAXMIDIS) index=0; 35 36 // do nothing if it's already playing 37 if(index==currmidi && midi_pos>=0) 38 { 39 midi_paused=false; 40 return; 41 } 42 43 jukebox(index,tunes[index].loop); 44}

That is the jukebox function.

Further, normally ZC uses the display switch move 'SWITCH_BACKGROUND' when windowed. I have tried to use 'SWITCH_PAUSE' instead, but a user reported that this corrected neither issue.

Edgar Reynaldo
Major Reynaldo
May 2007
avatar

I can't tell you the exact cause but I may be able to give you hints.

The audio threads run in the background. If allegro doesn't properly protect them with a mutex you might get corruption or crashes changing things during a callback.

One thing that bugs me is the multiple calls to music_stop in a row. Either that's a logical bug, or you're playing with globals, which makes the chances of a race condition infinitely more likely.

You're going to have to debug allegro and get a backtrace of all the running threads during the crash.

Sorry I can't help much with this you might have better luck asking on IRC.

EDIT
One other thing - you're not checking any return values.

Post Reply
Go to: