Click to See Complete Forum and Search --> : Problem on playing MIDI using MCI


Helstorm
April 14th, 2009, 11:35 AM
hi,
I'm trying to use MCI to play a MIDI sequence, but the problem is that I can't hear anything after the device is successfully opened. Here's my code:

#include<windows.h>
#include<stdio.h>
#include<mmsystem.h>

#pragma comment(lib,"winmm.lib")

int main()
{
int i = 0;

MCI_OPEN_PARMS mciOpen;
mciOpen.lpstrDeviceType = L"mpegvideo";
mciOpen.lpstrElementName = L"F:\\Program Files\\RPG Maker XP\\RGSS\\Standard\\Audio\\BGM\\011-LastBoss03.mid";
mciSendCommand(0, MCI_OPEN, MCI_OPEN_TYPE | MCI_OPEN_ELEMENT, (DWORD)&mciOpen);

MCI_STATUS_PARMS mciStatusParms;
mciStatusParms.dwItem = MCI_STATUS_LENGTH;
mciSendCommand(mciOpen.wDeviceID, MCI_STATUS, MCI_STATUS_ITEM, (DWORD)&mciStatusParms);
int curLength = mciStatusParms.dwReturn;
printf("total length:%d\n", curLength);

MCI_PLAY_PARMS mciPlay;
mciSendCommand(mciOpen.wDeviceID, MCI_PLAY, MCI_NOTIFY, (DWORD)&mciPlay);
while(1)
{
printf("now playing\t%d\tseconds\r", i);
i++;
Sleep(1000);
}
return 0;
}

The program opened the device and obtained the media length (correctly), then the MCI_PLAY command is also sent and mciSendCommand returns. Then the program enters the while loop and starts to count in second, but I can't hear anything O.O...
I've tried to change the device type to "sequencer", it still doesn't work..
Then I tried to used mciSendString to play it, still..
I've also tried to play media that are in WAV and MP3 format, and they both worked, so I don't know why MIDI doesn't, since it works fine using Microsoft media player.
Any help is appreciated!

0xC0000005
April 14th, 2009, 05:11 PM
I would probably start by checking the return values of the MCI functions, but that's just me...

General MCI Errors (http://msdn.microsoft.com/en-us/library/ms708492(VS.85).aspx#)

Helstorm
April 15th, 2009, 03:37 PM
Actually it returned 0, which means that there is no error... but thanks anyway.

BobS0327
April 15th, 2009, 04:28 PM
Are you sure you do NOT have anything muted? You can check by right clicking on the speaker on task bar and checking the volume control. Be sure that mute is NOT checked for anything.

0xC0000005
April 15th, 2009, 05:09 PM
Actually it returned 0, which means that there is no error... but thanks anyway.
OK, I am not an expert at MCI but based on the MSDN documentation your code raise a few issues that I would be concerned about.

You call mciSendCommand() with the MCI_NOTIFY flag set but you pass it an uninitialized MCI_PLAY_PARMS structure - that doesn't look right at all to me. I would assume that you would need to specify the window (which possibly could be NULL for console app) plus the start and end positions.

Also you mention that you check 'it' (the return value) but 'it' implies one function to me - not ALL of the MCI commands. Are you checking the return values of ALL of the MCI commands?

And as for your MCI_OPEN_PARMS structure, you are not specifying all of the fields so you may be again passing uninitialized (random) data to the function. At the very least, you should be doing this with ALL of your structures:


MCI_OPEN_PARMS mciOpen;
::ZeroMemory(&mciOpen, sizeof(mciOpen));
... now set fields that you want to use

Helstorm
April 15th, 2009, 06:06 PM
Are you sure you do NOT have anything muted? You can check by right clicking on the speaker on task bar and checking the volume control. Be sure that mute is NOT checked for anything.

Wow, you just solved my problem >_<
In the volume control, my volume for SW Synth was all the way down, this is why I can't hear the music. I found that Windows Media Player automatically adjusts the volume to the midpoint of the scroll bar when playing MIDI, and sets it to 0 when the program exits whether I raise the volume from 0 or not. This is weird.
Thank you very much :)
And thanks to 0xC0000005 too, very nice advice.