www.pudn.com > 1littlebox.rar > Midi.cpp
// Midi.cpp
#include "stdafx.h"
#include "Midi.h"
CMidi::CMidi()
{
m_nDevicesMid = midiOutGetNumDevs();
m_bOpenedMid = m_bPausedMid = m_bPlayingMid = FALSE;
m_wDeviceIDMid = 0;
}
CMidi::~CMidi()
{
CloseMid();
}
int CMidi::DeviceCountMid( void )
{
return( m_nDevicesMid );
}
BOOL CMidi::OpenMid( const char *lpszFilename )
{
if( !m_bOpenedMid ){
// Initialize member variables.
m_bPausedMid = m_bPlayingMid = FALSE;
m_wDeviceIDMid = 0;
// See if the file exists.
CFileStatus Status;
if( !CFile::GetStatus( lpszFilename, Status ) )
return( FALSE );
// Open the device.
MCI_OPEN_PARMS OpenParms;
OpenParms.lpstrDeviceType = (LPCSTR) MCI_DEVTYPE_SEQUENCER;
OpenParms.lpstrElementName = (LPCSTR) lpszFilename;
OpenParms.wDeviceID = 0;
if( mciSendCommand( NULL, MCI_OPEN, MCI_WAIT | MCI_OPEN_TYPE
| MCI_OPEN_TYPE_ID | MCI_OPEN_ELEMENT, (DWORD)(LPVOID) &OpenParms ) )
return( FALSE );
m_wDeviceIDMid = OpenParms.wDeviceID;
m_bOpenedMid = TRUE;
// Set the time format to milliseconds.
MCI_SET_PARMS SetParms;
SetParms.dwTimeFormat = MCI_FORMAT_MILLISECONDS;
if( mciSendCommand( m_wDeviceIDMid, MCI_SET, MCI_SET_TIME_FORMAT,
(DWORD)(LPVOID) &SetParms ) )
{
CloseMid();
return( FALSE );
}
mciSendCommand( m_wDeviceIDMid, MCI_SEEK, MCI_SEEK_TO_START, NULL );
return( TRUE );
}
return( FALSE );
}
BOOL CMidi::CloseMid( void )
{
if( m_bOpenedMid ){
// Stop the playing and close the device.
if( m_bPlayingMid || m_bPausedMid )
mciSendCommand( m_wDeviceIDMid, MCI_STOP, NULL, NULL );
mciSendCommand( m_wDeviceIDMid, MCI_CLOSE, NULL, NULL );
// Clear member variables.
m_bOpenedMid = m_bPausedMid = m_bPlayingMid = FALSE;
m_wDeviceIDMid = 0;
return( TRUE );
}
return( FALSE );
}
BOOL CMidi::PlayMid( void )
{
if( m_bOpenedMid ){
// Issue the 'play' command.
MCI_PLAY_PARMS PlayParms;
PlayParms.dwCallback = NULL;
PlayParms.dwFrom = ( ( GetMinutesMid() * 60 ) + GetSecondsMid () ) * 1000;
if( mciSendCommand( m_wDeviceIDMid, MCI_PLAY, MCI_FROM,
(DWORD)(LPVOID) &PlayParms ) )
return( FALSE );
// Set our class members so we know that
// we're currently playing.
m_bPlayingMid = TRUE;
m_bPausedMid = FALSE;
return( TRUE );
}
return( FALSE );
}
BOOL CMidi::StopMid( void )
{
if( m_bOpenedMid && m_bPlayingMid ){
// Issue the 'stop' command.
mciSendCommand( m_wDeviceIDMid, MCI_STOP, NULL, NULL );
// Issue the command that seeks back to the start.
mciSendCommand( m_wDeviceIDMid, MCI_SEEK, MCI_SEEK_TO_START, NULL );
m_bPausedMid = m_bPlayingMid = FALSE;
return( TRUE );
}
return( FALSE );
}
BOOL CMidi::PauseMid( void )
{
if( m_bOpenedMid && m_bPlayingMid ){
// Pause the CD.
MCI_PLAY_PARMS PlayParms;
if( mciSendCommand( m_wDeviceIDMid, MCI_PAUSE, 0,
(DWORD)(LPVOID) &PlayParms ) )
return( FALSE );
m_bPausedMid = TRUE;
return( TRUE );
}
return( FALSE );
}
BOOL CMidi::IsPlayingMid( void )
{
if( m_bOpenedMid ){
// Issue the 'status' command.
MCI_STATUS_PARMS StatusParms;
StatusParms.dwItem = MCI_STATUS_MODE;
if( mciSendCommand( m_wDeviceIDMid, MCI_STATUS,
MCI_WAIT | MCI_STATUS_ITEM, (DWORD)(LPVOID) &StatusParms ) )
return( FALSE );
if( StatusParms.dwReturn == MCI_MODE_PLAY ){
m_bPlayingMid = TRUE;
m_bPausedMid = FALSE;
return( TRUE );
}
else if( StatusParms.dwReturn == MCI_MODE_PAUSE ){
m_bPlayingMid = TRUE;
m_bPausedMid = FALSE;
return( TRUE );
}
else{
m_bPlayingMid = FALSE;
m_bPausedMid = FALSE;
return( FALSE );
}
}
return( FALSE );
}
BOOL CMidi::GetLengthMid( int *lpnMinutes, int *lpnSeconds )
{
// Set minutes and seconds to -1 in
// case there's an error.
*lpnMinutes = -1;
*lpnSeconds = -1;
if( m_bOpenedMid ){
// Issue the 'status/length' command.
MCI_STATUS_PARMS StatusParms;
StatusParms.dwItem = MCI_STATUS_LENGTH;
if( mciSendCommand( m_wDeviceIDMid, MCI_STATUS,
MCI_WAIT | MCI_STATUS_ITEM, (DWORD)(LPVOID) &StatusParms ) )
return( FALSE );
// Store the values in *lpnMinutes and *lpnSeconds.
*lpnMinutes = ( StatusParms.dwReturn / 1000 ) / 60;
*lpnSeconds = ( StatusParms.dwReturn / 1000 ) % 60;
return( TRUE );
}
return( FALSE );
}
int CMidi::GetMinutesMid( void )
{
if( m_bOpenedMid ){
// Get the current position.
MCI_STATUS_PARMS StatusParms;
StatusParms.dwItem = MCI_STATUS_POSITION;
if( mciSendCommand( m_wDeviceIDMid, MCI_STATUS, MCI_WAIT |
MCI_STATUS_ITEM, (DWORD)(LPVOID) &StatusParms ) )
return( -1 );
return( (int) ( ( StatusParms.dwReturn / 1000 ) / 60 ) );
}
return( -1 );
}
int CMidi::GetSecondsMid( void )
{
if( m_bOpenedMid ){
// Get the current position.
MCI_STATUS_PARMS StatusParms;
StatusParms.dwItem = MCI_STATUS_POSITION;
if( mciSendCommand( m_wDeviceIDMid, MCI_STATUS, MCI_WAIT |
MCI_STATUS_ITEM, (DWORD)(LPVOID) &StatusParms ) )
return( -1 );
return( (int) ( ( StatusParms.dwReturn / 1000 ) % 60 ) );
}
return( -1 );
}
BOOL CMidi::SeekToMid( int nMinute, int nSecond )
{
if( m_bOpenedMid ){
// Set dwTo to the correct value.
MCI_SEEK_PARMS SeekParms;
SeekParms.dwTo = ( nMinute * 60 + nSecond ) * 1000;
// Pause if we're not paused.
if( m_bPlayingMid && !m_bPausedMid )
mciSendCommand( m_wDeviceIDMid, MCI_PAUSE, 0, NULL );
// Issue the 'seek' command.
if( mciSendCommand( m_wDeviceIDMid, MCI_SEEK, MCI_TO |
MCI_WAIT, (DWORD)(LPVOID) &SeekParms ) )
return( FALSE );
// Restart the audio.
if( m_bPlayingMid && !m_bPausedMid )
return( PlayMid() );
}
return( FALSE );
}
BOOL CMidi::SkipForwardMid( int nSeconds )
{
if( m_bOpenedMid ){
// Get the current position.
MCI_STATUS_PARMS StatusParms;
DWORD dwPos;
StatusParms.dwItem = MCI_STATUS_POSITION;
mciSendCommand( m_wDeviceIDMid, MCI_STATUS, MCI_WAIT |
MCI_STATUS_ITEM, (DWORD)(LPVOID) &StatusParms );
dwPos = StatusParms.dwReturn;
// Skip forward n milliseconds
dwPos += (DWORD) nSeconds * 1000;
// Pause and seek to.
MCI_SEEK_PARMS SeekParms;
SeekParms.dwTo = dwPos;
if( m_bPlayingMid && !m_bPausedMid )
mciSendCommand( m_wDeviceIDMid, MCI_PAUSE, 0, NULL );
mciSendCommand (m_wDeviceIDMid, MCI_SEEK, MCI_TO | MCI_WAIT,
(DWORD)(LPVOID) &SeekParms);
// Restart the audio.
if( m_bPlayingMid && !m_bPausedMid )
return( PlayMid() );
}
return( FALSE );
}
BOOL CMidi::SkipBackMid( int nSeconds )
{
if( m_bOpenedMid ){
// Get the current position.
MCI_STATUS_PARMS StatusParms;
DWORD dwPos;
StatusParms.dwItem = MCI_STATUS_POSITION;
mciSendCommand( m_wDeviceIDMid, MCI_STATUS, MCI_WAIT |
MCI_STATUS_ITEM, (DWORD)(LPVOID) &StatusParms );
dwPos = StatusParms.dwReturn;
// Skip forward n milliseconds.
dwPos -= (DWORD) nSeconds * 1000;
// Pause and seek to.
MCI_SEEK_PARMS SeekParms;
SeekParms.dwTo = dwPos;
if( m_bPlayingMid && !m_bPausedMid )
mciSendCommand( m_wDeviceIDMid, MCI_PAUSE, 0, NULL );
mciSendCommand( m_wDeviceIDMid, MCI_SEEK, MCI_TO | MCI_WAIT,
(DWORD)(LPVOID) &SeekParms );
// Restart the audio.
if( m_bPlayingMid && !m_bPausedMid )
return( PlayMid() );
}
return( FALSE );
}
int CMidi::GetTempoMid( void )
{
if( m_bOpenedMid ){
// Issue the 'status/tempo' command.
MCI_STATUS_PARMS StatusParms;
StatusParms.dwItem = MCI_SEQ_STATUS_TEMPO;
if( mciSendCommand( m_wDeviceIDMid, MCI_STATUS, MCI_WAIT |
MCI_STATUS_ITEM, (DWORD)(LPVOID) &StatusParms ) )
return( -1 );
return( (int) StatusParms.dwReturn );
}
return( -1 );
}
BOOL CMidi::SetTempoMid( int nTempo )
{
if( m_bOpenedMid ){
// Set the tempo.
MCI_SEQ_SET_PARMS SeqSetParms;
SeqSetParms.dwTempo = nTempo;
mciSendCommand( m_wDeviceIDMid, MCI_SET, MCI_WAIT |
MCI_SEQ_SET_TEMPO, (DWORD)(LPVOID) &SeqSetParms );
return( TRUE );
}
return( FALSE );
}