重点是 以LPVOID 指针做参数的 Create() 方法
/////////////////////////////////////////////////////////////////////////////
// Copyright (C) 1998 by J鰎g K鰊ig
// All rights reserved
//
// This file is part of the completely free tetris clone "CGTetris".
//
// This is free software.
// You may redistribute it by any means providing it is not sold for profit
// without the authors written consent.
//
// No warrantee of any kind, expressed or implied, is included with this
// software; use at your own risk, responsibility for damages (if any) to
// anyone resulting from the use of this software rests entirely with the
// user.
//
// Send bug reports, bug fixes, enhancements, requests, flames, etc., and
// I'll try to keep a version up to date. I can be reached as follows:
// J.Koenig@adg.de (company site)
// Joerg.Koenig@rhein-neckar.de (private site)
/////////////////////////////////////////////////////////////////////////////
// Midi.h : main header file for the MIDI application
//
// This class is based on the DirectX sample "mstream".
#include <mmsystem.h>
#pragma message("linking with multimedia library")
#pragma comment(lib, "winmm.lib")
#include <vector>
using namespace std;
// This message is sent to the controlling window, if the volume changes in
// another way than explicitly set by the owner of the CMIDI object.
// WPARAM the pointer to the MIDI object
// LPARAM lo-word: the number of the channel that changed volume
// hi-word: the new volume in percent
#define WM_MIDI_VOLUMECHANGED WM_USER+23
#define MIDI_SYSEX ((BYTE)0xF0) // SysEx begin
#define MIDI_SYSEXEND ((BYTE)0xF7) // SysEx end
#define MIDI_META ((BYTE)0xFF) // Meta event begin
#define MIDI_META_TEMPO ((BYTE)0x51) // Tempo change
#define MIDI_META_EOT ((BYTE)0x2F) // End-of-track
// flags for the ConvertToBuffer() method
#define CONVERTF_RESET 0x00000001
#define CONVERTF_STATUS_DONE 0x00000001
#define CONVERTF_STATUS_STUCK 0x00000002
#define CONVERTF_STATUS_GOTEVENT 0x00000004
// Return values from the ConvertToBuffer() method
#define CONVERTERR_NOERROR 0 // No error occured
#define CONVERTERR_CORRUPT -101 // The input file is corrupt
// The converter has already encountered a corrupt file and cannot convert any
// more of this file -- must reset the converter
#define CONVERTERR_STUCK -102
#define CONVERTERR_DONE -103 // Converter is done
#define CONVERTERR_BUFFERFULL -104 // The buffer is full
#define CONVERTERR_METASKIP -105 // Skipping unknown meta event
#define STATUS_KILLCALLBACK 100 // Signals that the callback should die
#define STATUS_CALLBACKDEAD 200 // Signals callback is done processing
#define STATUS_WAITINGFOREND 300 // Callback's waiting for buffers to play
// Description of a track
//
struct TRACK
{
DWORD fdwTrack; // Track's flags
DWORD dwTrackLength; // Total bytes in track
LPBYTE pTrackStart; // -> start of track data buffer
LPBYTE pTrackCurrent; // -> next byte to read in buffer
DWORD tkNextEventDue; // Absolute time of next event in track
BYTE byRunningStatus;// Running status from last channel msg
// This structure is used to pass information to the ConvertToBuffer()
// system and then internally by that function to send information about the
// target stream buffer and current state of the conversion process to internal
// lower level conversion routines.
struct CONVERTINFO
{
MIDIHDR mhBuffer; // Standard Windows stream buffer header
DWORD dwStartOffset; // Start offset from mhStreamBuffer.lpStart
DWORD dwMaxLength; // Max length to convert on this pass
DWORD dwBytesRecorded;
DWORD tkStart;
BOOL bTimesUp;
// Temporary event structure which stores event data until we're ready to
// dump it into a stream buffer
struct TEMPEVENT
{
DWORD tkEvent; // Absolute time of event
BYTE byShortData[4]; // Event type and parameters if channel msg
DWORD dwEventLength; // Length of data which follows if meta or sysex
LPBYTE pLongData; // -> Event data if applicable
};
// Set playback position back to the start
BOOL Rewind();
// Get the number of volume channels
DWORD GetChannelCount() const;
// Set the volume of a channel in percent. Channels are from 0 to (GetChannelCount()-1)
void SetChannelVolume(DWORD channel, DWORD percent);
// Get the volume of a channel in percent
DWORD GetChannelVolume(DWORD channel) const;
// Set the volume for all channels in percent
void SetVolume(DWORD percent);
// Get the average volume for all channels
DWORD GetVolume() const;
// Set the tempo of the playback. Default: 100%
void SetTempo(DWORD percent);
// Get the current tempo in percent (usually 100)
DWORD GetTempo() const;
// You can (un)set an infinite loop during playback.
// Note that "Play()" resets this setting!
void SetInfinitePlay(BOOL bSet = TRUE);
void FreeBuffers();
protected: // implementation
// This function converts MIDI data from the track buffers.
int ConvertToBuffer(DWORD dwFlags, CONVERTINFO * lpciInfo);
// Fills in the event struct with the next event from the track
BOOL GetTrackEvent(TRACK * ptsTrack, TEMPEVENT * pteTemp);
// Retrieve the next byte from the track buffer, refilling the buffer from
// disk if necessary.
BOOL GetTrackByte(TRACK * ptsTrack, LPBYTE lpbyByte) {
if( DWORD(ptsTrack->pTrackCurrent - ptsTrack->pTrackStart) == ptsTrack->dwTrackLength )
return FALSE;
*lpbyByte = *ptsTrack->pTrackCurrent++;
return TRUE;
}
// Attempts to parse a variable length DWORD from the given track.
BOOL GetTrackVDWord(TRACK * ptsTrack, LPDWORD lpdw);
// Put the given event into the given stream buffer at the given location.
int AddEventToStreamBuffer( TEMPEVENT * pteTemp, CONVERTINFO * lpciInfo );
// Opens a MIDI stream. Then it goes about converting the data into a midiStream buffer for playback.
BOOL StreamBufferSetup();
protected: // error handling
// The default implementation writes the error message in the
// debuggers output window. Override if you want a different
// behavior.
virtual void MidiError(MMRESULT Result);
// Failure in converting track into stream.
// The default implementation displays the offset and the total
// number of bytes of the failed track and the error message in
// the debuggers output window.
virtual void TrackError(TRACK *, LPSTR ErrMsg);
protected: // overridables
// NOTE THAT, IF YOU OVERRIDE ONE OF THESE METHODS, YOU MUST CALL
// THE BASE CLASS IMPLEMENTATION TOO!
// called when a MIDI output device is opened
virtual void OnMidiOutOpen();
// called when the MIDI output device is closed
virtual void OnMidiOutClose();
// called when the specified system-exclusive or stream buffer
// has been played and is being returned to the application
virtual void OnMidiOutDone(MIDIHDR &);
// called when a MEVT_F_CALLBACK event is reached in the MIDI output stream
virtual void OnMidiOutPositionCB(MIDIHDR &, MIDIEVENT &);
private: // callback procedure
// This procedure calls the overridables above.
static void CALLBACK MidiProc(HMIDIOUT, UINT, DWORD, DWORD, DWORD);
// data members especially for ConvertToBuffer()
TRACK * m_ptsTrack;
TRACK * m_ptsFound;
DWORD m_dwStatus;
DWORD m_tkNext;
DWORD m_dwMallocBlocks;
TEMPEVENT m_teTemp;
};
//{{AFX_INSERT_LOCATION}}
// Microsoft Developer Studio will insert additional declarations immediately before the previous line.
#endif // MIDI_h
/////////////////////////////////////////////////////////////////////////////
// Copyright (C) 1998 by J鰎g K鰊ig
// All rights reserved
//
// This file is part of the completely free tetris clone "CGTetris".
//
// This is free software.
// You may redistribute it by any means providing it is not sold for profit
// without the authors written consent.
//
// No warrantee of any kind, expressed or implied, is included with this
// software; use at your own risk, responsibility for damages (if any) to
// anyone resulting from the use of this software rests entirely with the
// user.
//
// Send bug reports, bug fixes, enhancements, requests, flames, etc., and
// I'll try to keep a version up to date. I can be reached as follows:
// J.Koenig@adg.de (company site)
// Joerg.Koenig@rhein-neckar.de (private site)
/////////////////////////////////////////////////////////////////////////////
// Midi.cpp
//
// The CMIDI class is based on a sample in the DirectX SDK (mstream)
#define MThd 0x6468544D // Start of file
#define MTrk 0x6B72544D // Start of track
#define BUFFER_TIME_LENGTH 60 // Amount to fill in milliseconds
// These structures are stored in MIDI files; they need to be byte aligned.
//
#pragma pack(1)
// Contents of MThd chunk.
struct MIDIFILEHDR
{
WORD wFormat; // Format (hi-lo)
WORD wTrackCount; // # tracks (hi-lo)
WORD wTimeDivision; // Time division (hi-lo)
};
#pragma pack() // End of need for byte-aligned structures
// Macros for swapping hi/lo-endian data
//
#define WORDSWAP(w) (((w) >> 8) | (((w) << 8) & 0xFF00))
// We always preread the time from each track so the mixer code can
// determine which track has the next event with a minimum of work
if( !GetTrackVDWord( &m_Tracks[i], &m_Tracks[i].tkNextEventDue )) {
TRACE0("Error in MIDI data\n");
ASSERT(FALSE);
return FALSE;
}
}
// allocate volume channels and initialise them
m_Volumes.resize(NUM_CHANNELS, VOLUME_INIT);
/*deleted by anp move it to the play() function
if( ! StreamBufferSetup() ) {
ASSERT(FALSE);
return FALSE;
}
*/
return TRUE;
}
if( (mmrRetVal = midiStreamStop(m_hStream) ) != MMSYSERR_NOERROR ) {
MidiError(mmrRetVal);
return FALSE;
}
if( (mmrRetVal = midiOutReset((HMIDIOUT)m_hStream)) != MMSYSERR_NOERROR ) {
MidiError(mmrRetVal);
return FALSE;
}
// Wait for the callback thread to release this thread, which it will do by
// calling SetEvent() once all buffers are returned to it
if( WaitForSingleObject( m_hBufferReturnEvent, DEBUG_CALLBACK_TIMEOUT ) == WAIT_TIMEOUT ) {
// Note, this is a risky move because the callback may be genuinely busy, but
// when we're debugging, it's safer and faster than freezing the application,
// which leaves the MIDI device locked up and forces a system reset...
TRACE0("Timed out waiting for MIDI callback\n");
m_uCallbackStatus = STATUS_CALLBACKDEAD;
}
}
// We always preread the time from each track so the mixer code can
// determine which track has the next event with a minimum of work
if( !GetTrackVDWord( &m_Tracks[i], &m_Tracks[i].tkNextEventDue )) {
TRACE0("Error in MIDI data\n");
ASSERT(FALSE);
return FALSE;
}
}
// This function converts MIDI data from the track buffers setup by a
// previous call to ConverterInit(). It will convert data until an error is
// encountered or the output buffer has been filled with as much event data
// as possible, not to exceed dwMaxLength. This function can take a couple
// bit flags, passed through dwFlags. Information about the success/failure
// of this operation and the number of output bytes actually converted will
// be returned in the CONVERTINFO structure pointed at by lpciInfo.
int CMIDI :: ConvertToBuffer(DWORD dwFlags, CONVERTINFO * lpciInfo) {
int nChkErr;
// If we were already done, then return with a warning...
if( m_dwStatus & CONVERTF_STATUS_DONE ) {
if( m_bLooped ) {
Rewind();
m_dwProgressBytes = 0;
m_dwStatus = 0;
} else
return CONVERTERR_DONE;
} else if( m_dwStatus & CONVERTF_STATUS_STUCK ) {
// The caller is asking us to continue, but we're already hosed because we
// previously identified something as corrupt, so complain louder this time.
return( CONVERTERR_STUCK );
} else if( m_dwStatus & CONVERTF_STATUS_GOTEVENT ) {
// Turn off this bit flag
m_dwStatus ^= CONVERTF_STATUS_GOTEVENT;
// The following code for this case is duplicated from below, and is
// designed to handle a "straggler" event, should we have one left over
// from previous processing the last time this function was called.
// Don't add end of track event 'til we're done
if( m_teTemp.byShortData[0] == MIDI_META && m_teTemp.byShortData[1] == MIDI_META_EOT ) {
if( m_dwMallocBlocks ) {
delete [] m_teTemp.pLongData;
--m_dwMallocBlocks;
}
} else if(( nChkErr = AddEventToStreamBuffer( &m_teTemp, lpciInfo )) != CONVERTERR_NOERROR ) {
if( nChkErr == CONVERTERR_BUFFERFULL ) {
// Do some processing and tell caller that this buffer's full
m_dwStatus |= CONVERTF_STATUS_GOTEVENT;
return CONVERTERR_NOERROR;
} else if( nChkErr == CONVERTERR_METASKIP ) {
// We skip by all meta events that aren't tempo changes...
} else {
TRACE0("Unable to add event to stream buffer.\n");
if( m_dwMallocBlocks ) {
delete [] m_teTemp.pLongData;
m_dwMallocBlocks--;
}
return( TRUE );
}
}
}
// None found? We must be done, so return to the caller with a smile.
if( !m_ptsFound ) {
m_dwStatus |= CONVERTF_STATUS_DONE;
// Need to set return buffer members properly
return CONVERTERR_NOERROR;
}
// Ok, get the event header from that track
if( !GetTrackEvent( m_ptsFound, &m_teTemp )) {
// Warn future calls that this converter is stuck at a corrupt spot
// and can't continue
m_dwStatus |= CONVERTF_STATUS_STUCK;
return CONVERTERR_CORRUPT;
}
if(( nChkErr = AddEventToStreamBuffer( &m_teTemp, lpciInfo )) != CONVERTERR_NOERROR ) {
if( nChkErr == CONVERTERR_BUFFERFULL ) {
// Do some processing and tell somebody this buffer is full...
m_dwStatus |= CONVERTF_STATUS_GOTEVENT;
return CONVERTERR_NOERROR;
} else if( nChkErr == CONVERTERR_METASKIP ) {
// We skip by all meta events that aren't tempo changes...
} else {
TRACE0("Unable to add event to stream buffer.\n");
if( m_dwMallocBlocks ) {
delete [] m_teTemp.pLongData;
m_dwMallocBlocks--;
}
return TRUE;
}
}
}
return CONVERTERR_NOERROR;
}
// GetTrackEvent
//
// Fills in the event struct with the next event from the track
//
// pteTemp->tkEvent will contain the absolute tick time of the event
// pteTemp->byShortData[0] will contain
// MIDI_META if the event is a meta event;
// in this case pteTemp->byShortData[1] will contain the meta class
// MIDI_SYSEX or MIDI_SYSEXEND if the event is a SysEx event
// Otherwise, the event is a channel message and pteTemp->byShortData[1]
// and pteTemp->byShortData[2] will contain the rest of the event.
//
// pteTemp->dwEventLength will contain
// The total length of the channel message in pteTemp->byShortData if
// the event is a channel message
// The total length of the paramter data pointed to by
// pteTemp->pLongData otherwise
//
// pteTemp->pLongData will point at any additional paramters if the
// event is a SysEx or meta event with non-zero length; else
// it will contain NULL
//
// Returns TRUE on success or FALSE on any kind of parse error
// Prints its own error message ONLY in the debug version
//
// Maintains the state of the input track (i.e.
// ptsTrack->pTrackPointers, and ptsTrack->byRunningStatus).
//
BOOL CMIDI :: GetTrackEvent(TRACK * ptsTrack, TEMPEVENT * pteTemp) {
DWORD idx;
UINT dwEventLength;
// Clear out the temporary event structure to get rid of old data...
memset( pteTemp, 0, sizeof(TEMPEVENT));
// Already at end of track? There's nothing to read.
if( ptsTrack->fdwTrack & ITS_F_ENDOFTRK )
return FALSE;
// Get the first byte, which determines the type of event.
BYTE byByte;
if( !GetTrackByte(ptsTrack, &byByte) )
return FALSE;
// If the high bit is not set, then this is a channel message
// which uses the status byte from the last channel message
// we saw. NOTE: We do not clear running status across SysEx or
// meta events even though the spec says to because there are
// actually files out there which contain that sequence of data.
if( !(byByte & 0x80) ) {
// No previous status byte? We're hosed.
if( !ptsTrack->byRunningStatus ) {
TrackError(ptsTrack, gteBadRunStat);
return FALSE;
}
// Only program change and channel pressure events are 2 bytes long;
// the rest are 3 and need another byte
if(( byByte != MIDI_PRGMCHANGE ) && ( byByte != MIDI_CHANPRESS )) {
if( !GetTrackByte( ptsTrack, &pteTemp->byShortData[2] ))
return FALSE;
++pteTemp->dwEventLength;
}
} else if(( byByte & 0xF0 ) != MIDI_SYSEX ) {
// Not running status, not in SysEx range - must be
// normal channel message (0x80-0xEF)
pteTemp->byShortData[0] = byByte;
ptsTrack->byRunningStatus = byByte;
// Strip off channel and just keep message type
byByte &= 0xF0;
if( !GetTrackByte( ptsTrack, &pteTemp->byShortData[1] ))
return FALSE;
if( dwEventLength == 2 )
if( !GetTrackByte( ptsTrack, &pteTemp->byShortData[2] ))
return FALSE;
} else if(( byByte == MIDI_SYSEX ) || ( byByte == MIDI_SYSEXEND )) {
// One of the SysEx types. (They are the same as far as we're concerned;
// there is only a semantic difference in how the data would actually
// get sent when the file is played. We must take care to put the proper
// event type back on the output track, however.)
//
// Parse the general format of:
// BYTE bEvent (MIDI_SYSEX or MIDI_SYSEXEND)
// VDWORD cbParms
// BYTE abParms[cbParms]
pteTemp->byShortData[0] = byByte;
if( !GetTrackVDWord( ptsTrack, &pteTemp->dwEventLength )) {
TrackError( ptsTrack, gteSysExLenTrunc );
return FALSE;
}
// Malloc a temporary memory block to hold the parameter data
pteTemp->pLongData = new BYTE [pteTemp->dwEventLength];
if( pteTemp->pLongData == 0 ) {
TrackError( ptsTrack, gteNoMem );
return FALSE;
}
// Increment our counter, which tells the program to look around for
// a malloc block to free, should it need to exit or reset before the
// block would normally be freed
++m_dwMallocBlocks;
// Copy from the input buffer to the parameter data buffer
for( idx = 0; idx < pteTemp->dwEventLength; idx++ )
if( !GetTrackByte( ptsTrack, pteTemp->pLongData + idx )) {
TrackError( ptsTrack, gteSysExTrunc );
return FALSE;
}
} else if( byByte == MIDI_META ) {
// It's a meta event. Parse the general form:
// BYTE bEvent (MIDI_META)
// BYTE bClass
// VDWORD cbParms
// BYTE abParms[cbParms]
pteTemp->byShortData[0] = byByte;
// NOTE: It's perfectly valid to have a meta with no data
// In this case, dwEventLength == 0 and pLongData == NULL
if( pteTemp->dwEventLength ) {
// Malloc a temporary memory block to hold the parameter data
pteTemp->pLongData = new BYTE [pteTemp->dwEventLength];
if( pteTemp->pLongData == 0 ) {
TrackError( ptsTrack, gteNoMem );
return FALSE;
}
// Increment our counter, which tells the program to look around for
// a malloc block to free, should it need to exit or reset before the
// block would normally be freed
++m_dwMallocBlocks;
// Copy from the input buffer to the parameter data buffer
for( idx = 0; idx < pteTemp->dwEventLength; idx++ )
if( !GetTrackByte( ptsTrack, pteTemp->pLongData + idx )) {
TrackError( ptsTrack, gteMetaTrunc );
return FALSE;
}
}
if( pteTemp->byShortData[1] == MIDI_META_EOT )
ptsTrack->fdwTrack |= ITS_F_ENDOFTRK;
} else {
// Messages in this range are system messages and aren't supposed to
// be in a normal MIDI file. If they are, we've either misparsed or the
// authoring software is stupid.
return FALSE;
}
// Event time was already stored as the current track time
pteTemp->tkEvent = ptsTrack->tkNextEventDue;
// Now update to the next event time. The code above MUST properly
// maintain the end of track flag in case the end of track meta is
// missing. NOTE: This code is a continuation of the track event
// time pre-read which is done at the end of track initialization.
if( !( ptsTrack->fdwTrack & ITS_F_ENDOFTRK )) {
DWORD tkDelta;
// GetTrackVDWord
//
// Attempts to parse a variable length DWORD from the given track. A VDWord
// in a MIDI file
// (a) is in lo-hi format
// (b) has the high bit set on every byte except the last
//
// Returns the DWORD in *lpdw and TRUE on success; else
// FALSE if we hit end of track first.
BOOL CMIDI :: GetTrackVDWord(TRACK * ptsTrack, LPDWORD lpdw) {
ASSERT(ptsTrack != 0);
ASSERT(lpdw != 0);
// AddEventToStreamBuffer
//
// Put the given event into the given stream buffer at the given location
// pteTemp must point to an event filled out in accordance with the
// description given in GetTrackEvent
//
// Handles its own error notification by displaying to the appropriate
// output device (either our debugging window, or the screen).
int CMIDI :: AddEventToStreamBuffer( TEMPEVENT * pteTemp, CONVERTINFO *lpciInfo ) {
MIDIEVENT * pmeEvent = (MIDIEVENT *)( lpciInfo->mhBuffer.lpData
+ lpciInfo->dwStartOffset
+ lpciInfo->dwBytesRecorded );
// When we see a new, empty buffer, set the start time on it...
if( !lpciInfo->dwBytesRecorded )
lpciInfo->tkStart = m_tkCurrentTime;
// Use the above set start time to figure out how much longer we should fill
// this buffer before officially declaring it as "full"
if( m_tkCurrentTime - lpciInfo->tkStart > m_dwBufferTickLength )
if( lpciInfo->bTimesUp ) {
lpciInfo->bTimesUp = FALSE;
return CONVERTERR_BUFFERFULL;
} else
lpciInfo->bTimesUp = TRUE;
DWORD tkNow = m_tkCurrentTime;
// Delta time is absolute event time minus absolute time
// already gone by on this track
DWORD tkDelta = pteTemp->tkEvent - m_tkCurrentTime;
// Event time is now current time on this track
m_tkCurrentTime = pteTemp->tkEvent;
if( pteTemp->byShortData[0] < MIDI_SYSEX ) {
// Channel message. We know how long it is, just copy it.
// Need 3 DWORD's: delta-t, stream-ID, event
if( lpciInfo->dwMaxLength-lpciInfo->dwBytesRecorded < 3*sizeof(DWORD)) {
// Cleanup from our write operation
return CONVERTERR_BUFFERFULL;
}
if((( pteTemp->byShortData[0] & 0xF0) == MIDI_CTRLCHANGE ) && ( pteTemp->byShortData[1] == MIDICTRL_VOLUME )) {
// If this is a volume change, generate a callback so we can grab
// the new volume for our cache
pmeEvent->dwEvent |= MEVT_F_CALLBACK;
}
lpciInfo->dwBytesRecorded += 3 *sizeof(DWORD);
} else if(( pteTemp->byShortData[0] == MIDI_SYSEX ) || ( pteTemp->byShortData[0] == MIDI_SYSEXEND )) {
TRACE0("AddEventToStreamBuffer: Ignoring SysEx event.\n");
if( m_dwMallocBlocks ) {
delete [] pteTemp->pLongData;
--m_dwMallocBlocks;
}
} else {
// Better be a meta event.
// BYTE byEvent
// BYTE byEventType
// VDWORD dwEventLength
// BYTE pLongEventData[dwEventLength]
ASSERT( pteTemp->byShortData[0] == MIDI_META );
// The only meta-event we care about is change tempo
if( pteTemp->byShortData[1] != MIDI_META_TEMPO ) {
if( m_dwMallocBlocks ) {
delete [] pteTemp->pLongData;
--m_dwMallocBlocks;
}
return CONVERTERR_METASKIP;
}
// We should have three bytes of parameter data...
ASSERT(pteTemp->dwEventLength == 3);
// Need 3 DWORD's: delta-t, stream-ID, event data
if( lpciInfo->dwMaxLength - lpciInfo->dwBytesRecorded < 3 *sizeof(DWORD)) {
// Cleanup the temporary event if necessary and return
if( m_dwMallocBlocks ) {
delete [] pteTemp->pLongData;
--m_dwMallocBlocks;
}
return CONVERTERR_BUFFERFULL;
}
pmeEvent->dwDeltaTime = tkDelta;
pmeEvent->dwStreamID = 0;
// Note: this is backwards from above because we're converting a single
// data value from hi-lo to lo-hi format...
pmeEvent->dwEvent = ( pteTemp->pLongData[2] )
| (((DWORD)pteTemp->pLongData[1] ) << 8 )
| (((DWORD)pteTemp->pLongData[0] ) << 16 );
// This next step has absolutely nothing to do with the conversion of a
// MIDI file to a stream, it's simply put here to add the functionality
// of the tempo slider. If you don't need this, be sure to remove the
// next two lines.
m_dwCurrentTempo = pmeEvent->dwEvent;
pmeEvent->dwEvent = (pmeEvent->dwEvent * 100 ) / m_dwTempoMultiplier;
// StreamBufferSetup()
//
// Opens a MIDI stream. Then it goes about converting the data into a midiStream buffer for playback.
BOOL CMIDI :: StreamBufferSetup()
{
int nChkErr;
BOOL bFoundEnd = FALSE;
// Tell the converter to convert up to one entire buffer's length of output
// data. Also, set a flag so it knows to reset any saved state variables it
// may keep from call to call.
m_StreamBuffers[m_nCurrentBuffer].dwStartOffset = 0;
m_StreamBuffers[m_nCurrentBuffer].dwMaxLength = OUT_BUFFER_SIZE;
m_StreamBuffers[m_nCurrentBuffer].tkStart = 0;
m_StreamBuffers[m_nCurrentBuffer].bTimesUp = FALSE;
// This function unprepares and frees all our buffers -- something we must
// do to work around a bug in MMYSYSTEM that prevents a device from playing
// back properly unless it is closed and reopened after each stop.
void CMIDI :: FreeBuffers() {
DWORD idx;
MMRESULT mmrRetVal;
// This flag is set whenever the callback is waiting for all buffers to
// come back.
if( m_uCallbackStatus == STATUS_KILLCALLBACK ) {
// Count NUM_STREAM_BUFFERS-1 being returned for the last time
if( m_nEmptyBuffers < NUM_STREAM_BUFFERS )
return;
else {
// Change the status to callback dead
m_uCallbackStatus = STATUS_CALLBACKDEAD;
SetEvent(m_hBufferReturnEvent);
return;
}
}
void CMIDI :: OnMidiOutPositionCB(MIDIHDR & rHdr, MIDIEVENT & rEvent) {
if( MIDIEVENT_TYPE(rEvent.dwEvent) == MIDI_CTRLCHANGE )
{
if( MIDIEVENT_DATA1(rEvent.dwEvent) == MIDICTRL_VOLUME ) {
// Mask off the channel number and cache the volume data byte
m_Volumes[MIDIEVENT_CHANNEL(rEvent.dwEvent)] = DWORD(MIDIEVENT_VOLUME(rEvent.dwEvent)*100/VOLUME_MAX);
if( m_pWndParent && ::IsWindow(m_pWndParent->GetSafeHwnd()) )
// Do not use SendMessage(), because a change of the midi stream has no effect
// during callback handling, so if the owner wants to adjust the volume, as a
// result of the windows message, (s)he will not hear that change.
m_pWndParent->PostMessage(
WM_MIDI_VOLUMECHANGED,
WPARAM(this),
LPARAM(
MAKELONG(
WORD(MIDIEVENT_CHANNEL(rEvent.dwEvent)),
WORD(MIDIEVENT_VOLUME(rEvent.dwEvent)*100/VOLUME_MAX)
)
)
);
}
}
}
void CMIDI :: OnMidiOutClose() {
}
//////////////////////////////////////////////////////////////////////
// CMIDI -- static members
//////////////////////////////////////////////////////////////////////
// CMIDI* tMidi=new CMIDI();
CMIDI *tMidi;
LPBYTE tSoundData =(LPBYTE) m_pSoundData;
LPBYTE pResult;
DWORD i,j,start,count;
//Get the start,end&count info of the select track
j=0;
for (i=0;i < m_dwSoundSize;i++)
{
if ((*(DWORD*)(tSoundData)) == 0x6b72544d)
{
j++;
if (j == idx)
start=i;
if (j == idx+1)
break;
}
tSoundData++;
}
count = i - start + 1;
//Copy the midi header to the result
pResult = new BYTE(count+14);
memcpy(pResult,m_pSoundData,14);
pResult[10]=0x00;
pResult[11]=0x01;
//Copy the Track Data to the result
tSoundData=(LPBYTE) m_pSoundData;
tSoundData+=start;
memcpy(pResult+14,tSoundData,count);
//Create a new MIDI pointor contain the selected track
tMidi = new CMIDI();
tMidi->Create(pResult,count+14);
return tMidi;
}