PDA

View Full Version : C++ midi impementation problem



uco
11-19-2006, 12:08 PM
I'm writting a program for the GT-8 and I'm a bit lost...

This code runs fine except on midiOutClose() garbage is send to midi device!? When I run it without midiOutClose() the sysex message is send as it should be, but the mididevice is not closed correctly.... and garbage is sent after the message seems to be random midi...

I'm out in the dark....


Follow code depends on: winmm.lib

Source:

#include <iostream>
#include <stdlib.h>
#include <windows.h>
using namespace std;

void PrintMidiOutErrorMsg&#40;unsigned long err&#41;
&#123;
#define BUFFERSIZE 128
WCHAR errMsg&#91;BUFFERSIZE&#93;;

if &#40;!&#40;err = midiOutGetErrorText&#40;err, &errMsg&#91;0&#93;, BUFFERSIZE&#41;&#41;&#41;
&#123;
printf&#40;"%s\r\n", &errMsg&#91;0&#93;&#41;;
&#125;
else if &#40;err == MMSYSERR_BADERRNUM&#41;
&#123;
printf&#40;"Strange error number returned!\r\n"&#41;;
&#125;
else
&#123;
printf&#40;"Specified pointer is invalid!\r\n"&#41;;
&#125;
&#125;

int main&#40;&#41;
&#123;
HMIDIOUT handle;
MIDIHDR midiHdr;
UINT err;
const BYTE sysEx&#91;&#93; = &#123;0xF0, 0x41, 0x00, 0x00, 0x00, 0x06, 0x12, 0x0D, 0x00, 0x12, 0x00,
0x53, 0x79, 0x73, 0x65, 0x78, 0x20, 0x54, 0x45, 0x53, 0x54, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20,
0x47, 0xF7&#125;;
int i;

if &#40;!&#40;err = midiOutOpen&#40;&handle, &#40;UINT&#41;5, 0, 0, CALLBACK_NULL&#41;&#41;&#41;
&#123;
err = 0;


midiHdr.lpData = &#40;LPSTR&#41; sysEx;
midiHdr.dwBufferLength = &#40;DWORD&#41; sizeof&#40;sysEx&#41;;
midiHdr.dwFlags = 0;

err = midiOutPrepareHeader&#40;handle, &midiHdr, sizeof&#40;MIDIHDR&#41;&#41;;
if &#40;!err&#41;
&#123;
cout << "Sending system exclusive MIDI message &#40;Sysex&#41;&#58;" << endl;
cout << "sysEx value&#58; " << endl;
for&#40;i=0;i<sizeof&#40;sysEx&#41;;i++&#41;
&#123;
printf &#40;"%X ",sysEx&#91;i&#93;&#41;;
&#125;
cout << "sysEx size&#58; " << sizeof&#40;sysEx&#41; << " Bytes" << endl;


err = midiOutLongMsg&#40;handle, &midiHdr, sizeof&#40;MIDIHDR&#41;&#41;;
if &#40;err&#41;
&#123;
PrintMidiOutErrorMsg&#40;err&#41;;
&#125;

while &#40;MIDIERR_STILLPLAYING == midiOutUnprepareHeader&#40;handle, &midiHdr, sizeof&#40;MIDIHDR&#41;&#41;&#41;
&#123;
Sleep&#40;1000&#41;;
&#125;
&#125;
else
&#123;
PrintMidiOutErrorMsg&#40;err&#41;;
&#125;

midiOutClose&#40;handle&#41;;
&#125;
else
&#123;
printf&#40;"Error opening default MIDI Out device!\r\n"&#41;;
PrintMidiOutErrorMsg&#40;err&#41;;
&#125;

printf&#40;"\r\n"&#41;;
system&#40;"PAUSE"&#41;;
return 0;
&#125;

Message sent:

0000B84D 2 4 F0 Buffer&#58; 17 Bytes System Exclusive
SYSX&#58; F0 41 00 00 00 06 11 0D 00 00 00 00 00 1F 00 55 F7

What follows is send on the call of midiOutClose():

00012B2F 2 4 B0 40 00 1 --- CC&#58; Pedal &#40;Sustain&#41;
00012B2F 2 4 B1 40 00 2 --- CC&#58; Pedal &#40;Sustain&#41;
00012B30 2 4 B2 40 00 3 --- CC&#58; Pedal &#40;Sustain&#41;
00012B30 2 4 B3 40 00 4 --- CC&#58; Pedal &#40;Sustain&#41;
00012B30 2 4 B4 40 00 5 --- CC&#58; Pedal &#40;Sustain&#41;
00012B30 2 4 B5 40 00 6 --- CC&#58; Pedal &#40;Sustain&#41;
00012B30 2 4 B6 40 00 7 --- CC&#58; Pedal &#40;Sustain&#41;
00012B31 2 4 B7 40 00 8 --- CC&#58; Pedal &#40;Sustain&#41;
00012B32 2 4 B8 40 00 9 --- CC&#58; Pedal &#40;Sustain&#41;
00012B32 2 4 B9 40 00 10 --- CC&#58; Pedal &#40;Sustain&#41;
00012B32 2 4 BA 40 00 11 --- CC&#58; Pedal &#40;Sustain&#41;
00012B33 2 4 BB 40 00 12 --- CC&#58; Pedal &#40;Sustain&#41;
00012B33 2 4 BC 40 00 13 --- CC&#58; Pedal &#40;Sustain&#41;
00012B33 2 4 BD 40 00 14 --- CC&#58; Pedal &#40;Sustain&#41;
00012B33 2 4 BE 40 00 15 --- CC&#58; Pedal &#40;Sustain&#41;
00012B34 2 4 BF 40 00 16 --- CC&#58; Pedal &#40;Sustain&#41;

FlyMojo
11-19-2006, 01:20 PM
This code runs fine except on midiOutClose() garbage is send to midi device!?

I've never tried to write MIDI code but I'd be curious to see the effect of freeing the buffer (http://msdn2.microsoft.com/en-gb/library/ms711641.aspx) before closing the stream. This activity seems to be recommended here (http://www.borg.com/~jglatt/tech/lowmidi.htm) - After you're done with the buffer, you must "unprepare" it by calling midiOutUnprepareHeader().

Bill

uco
11-19-2006, 03:05 PM
Dude thanks will try that... Might be the sleep that waits for the message to finish not doing his job, didn't think about that, deleting the buffer does the job so must be it... breaking my head on it for months and posting on several programming forums...

BIG THANKS!!!!

mmm
11-19-2006, 07:16 PM
ohhh ... can't wait uco.