Sign in to follow this  

More MCI trouble (Looping a midi)

This topic is 3874 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I'm trying to loop a midi file in my windows console application using MCI. I can successfully open, play, stop, and unload my midi. However, I don't see any way to make the track loop without creating a window (because the notify parameter in mciSendString is a handle. As such, it requires a window to interpret the handle). There is a repeat parameter for some MCI devices, but from what I gather, the sequencer device doesn't have that option. Here's my code so far:
#include "stdafx.h"
#include <windows.h>
#include <mmsystem.h>
#include <iostream>

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

int _tmain(int argc, _TCHAR* argv[])
{
  	LPCTSTR playSong = _T("open \"C:\\MySongForGame4.mid\" type sequencer alias mySong");
	mciSendString(playSong, NULL, 0, 0);

	playSong = _T("play mySong from 0 notify");
	if (mciSendString(playSong, NULL, 0, 0)) // Handle be passed in param4
	{
		std::cout << "Could not play intro music...";
	}
	else
	{
		std::cout << "Played music!";
	}


	int wait;
	std::cin >> wait;
	
	playSong = _T("stop mySong");
	mciSendString(playSong, NULL, 0, 0);

	playSong = _T("close mySong");
	mciSendString(playSong, NULL, 0, 0);
	return 0;
}





Is it possible to check for the end of the MIDI track? (Without using a window) Is there a built in repeat function for MIDIs that I am overlooking? Is there an easy way to create a timer? P.S. This is in cpp [Edited by - anothrguitarist on May 10, 2007 7:13:58 PM]

Share this post


Link to post
Share on other sites
Ok, so I can make a song loop by finding its position using the MCI_STATUS command. There is one problem though, the position returned is slightly inaccurate.

I get this warning, and I think it may be the source of the problem:

1>c:\documents and settings\john\my documents\visual studio 2005\projects\miditest2\miditest2\miditest2.cpp(98) : warning C4311: 'type cast' : pointer truncation from 'LPVOID' to 'DWORD'

Do you think this could be the root of my problem?

And the code:


// MidiTest2.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"
#include <windows.h>
#include <mmsystem.h>
#include <iostream>

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

typedef unsigned int UINT;

void stopMidi(UINT & wDeviceID);
void playMidi(UINT & wDeviceID, MCI_OPEN_PARMS & mciOpenParams ,MCI_STATUS_PARMS & mciStatus);

int _tmain(int argc, _TCHAR* argv[])
{
const unsigned short TENTH_OF_SECOND = 10;
const DWORD SONG_LENGTH = 608;
std::cout << SONG_LENGTH;

MCI_STATUS_PARMS mciStatus;
MCI_OPEN_PARMS mciOpenParams;
UINT wDeviceID;
DWORD dwReturn;


playMidi(wDeviceID, mciOpenParams, mciStatus);


bool stopPlayingMidi = false;
while (stopPlayingMidi == false)
{
while (mciStatus.dwReturn < SONG_LENGTH)
{
mciStatus.dwItem = MCI_STATUS_POSITION;
mciSendCommand(wDeviceID, MCI_STATUS, MCI_STATUS_ITEM,
(DWORD_PTR)&mciStatus);
std::cout << mciStatus.dwReturn;
}
stopMidi(wDeviceID);
playMidi(wDeviceID, mciOpenParams ,mciStatus);
stopPlayingMidi = true;
}

short wait;
std::cin >> wait;

return 0;
}

void playMidi(UINT & wDeviceID, MCI_OPEN_PARMS & mciOpenParams, MCI_STATUS_PARMS & mciStatus)
{
DWORD dwReturn;
MCI_PLAY_PARMS mciPlayParams;

mciOpenParams.lpstrDeviceType = _T("sequencer");
mciOpenParams.lpstrElementName = _T("C:\\MySongForGame4.mid");

if ( dwReturn = mciSendCommand(NULL, MCI_OPEN,
MCI_OPEN_TYPE | MCI_OPEN_ELEMENT,
(DWORD)(LPVOID) &mciOpenParams))
std::cout << "Error...";

wDeviceID = mciOpenParams.wDeviceID;

mciPlayParams.dwFrom = 0;
if ( dwReturn = mciSendCommand(wDeviceID, MCI_PLAY, 0,
(DWORD)(LPVOID) &mciPlayParams))
std::cout << "Error...";

mciStatus.dwItem = MCI_STATUS_POSITION;
mciSendCommand(wDeviceID, MCI_STATUS, MCI_STATUS_ITEM,
(DWORD)(LPVOID)&mciStatus);
}

void stopMidi(UINT & wDeviceID)
{
DWORD dwReturn;

MCI_GENERIC_PARMS mciStopParams;
mciSendCommand(wDeviceID, MCI_STOP, 0, (DWORD) (LPMCI_GENERIC_PARMS) &mciStopParams);

if (dwReturn = mciSendCommand(MCI_ALL_DEVICE_ID, MCI_CLOSE, MCI_WAIT,
NULL))
std::cout << "Error. Could not close media devices.";
}





[Edited by - anothrguitarist on May 10, 2007 7:13:48 PM]

Share this post


Link to post
Share on other sites

This topic is 3874 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this