A midi controller in a desktop program makes the sound files stop sounding

I don’t know if this issue is directly related to Fmod, but as I have not gotten any useful answer on a MSDN forum for almost the same question, I try to post it here. If someone at least could give me a hint or an idea what to look for, it could be very helpful.

I have made a Windows desktop program in C with Fmod and also Dirac. As a part of the program I have included a midi controller, which I use by connecting a midi keyboard. I use my own sound files that are played by Fmod commands.

When I turn on the midi function, it works fine for a few minutes, but then stops sounding, and without any error messages.

The code of the midi controller is in a callback function that is called from the main thread by a button that is supposed to turn the midi functionality on and off, and which has the following code:

if (LOWORD(wParam) == 10) { 
	
		  cursorused = LoadCursor(NULL, IDC_WAIT);
		  SetCursor(cursorused);
		  FMOD_System_Update(systemx);
		  
		  if(tampuraflag == 0){  
			
			 SetWindowText(Control[10], TEXT("Off"));
			 tampurafrequency = kfreq/44100;			    	 
			 timestretch(1, tampuratime, tampurafrequency, infileName2);
			 tampuraflag = 1;
			 FMOD_Channel_SetVolume(channel[1], tampuravolume); 	 
			 nMidiDeviceNum = midiInGetNumDevs();
			 
			 rv = midiInOpen(&hMidiDevice, nMidiPort, (DWORD)(void*)MidiInProc, 0, CALLBACK_FUNCTION);
			 midiInStart(hMidiDevice);
			
		  } else if(tampuraflag == 1){ 
			tampuraflag = 0;
			SetWindowText(Control[10], TEXT("On"));  
			FMOD_Channel_SetPaused(channel[1], 1);
			FMOD_Channel_SetPaused(channel[0], 1);
			FMOD_System_Update(systemx);
		
			rv = midiInStop(hMidiDevice);
			rv = midiInClose(hMidiDevice);				
			hMidiDevice = NULL;			
		  }		  			  
		  if (Focus) { Focus = 0; SetFocus(hwnd); }
		  return 0;
	 }

When the button is pressed a sound file of about 15 seconds in length starts and goes in a loop. This is then running in the background when using the midi keyboard. I have also found that I can’t turn off the midi functionality by clicking the button once more. The “rv” variable for midiInStop(hMidiDevice) and midiInClose(hMidiDevice) gives MMSYSERR_INVALHANDLE. After playing for a few minutes, the sound of the background file stops first, and then a few seconds later,the sound of the midi keyboard.

It would be grateful if somebody could give me an idea what could be wrong.

Thanks in advance.

Sincerely

I have now found out that the program only stops sounding when having been compiled in Debug mode. When it is compiled in Release mode, the sound doesn’t stop, instead the GUI controls freezes. It means that they work in a way, but nothing shows on the screen when clicking or dragging on them, which is off course not satisfactory. I have, however, found that I can turn off the midi by clicking the on/off button a second time after having set the hMidiDevice variable to static.

If your GUI controls are freezing it sounds like something is blocking the UI thread from running smoothly, can you break the debugger to see what the callstack is?

I am not familiar with the callstack and uncertain where to place the break. But if I for instance place the break right after the midiInStart command I get this.

>	DhruvaNada-midi.exe!WndProc(HWND__ * hwnd, unsigned int msg, unsigned int wParam, long lParam) Line 4123	C
[External Code]	
user32.dll![Frames below may be incorrect and/or missing, no symbols loaded for user32.dll]	Unknown
DhruvaNada-midi.exe!WinMain(HINSTANCE__ * hInstance, HINSTANCE__ * hPrevInstance, char * lpCmdLine, int nCmdShow) Line 324	C
[External Code]

I believe I have resolved the issue. I had some graphics commands in the backup function, among others some swprintf comands for testing. When I removed everything that had to do with graphics, it seems that the problem has dissapeared. I have no idea why this is so, but as long as it works I am happy.Thanks a lot for trying to help me out.

Sincerely