ResidualVM logo ResidualVM website - Forums - Contact us BuildBot - Doxygen - Wiki curved edge

midiparser.h

Go to the documentation of this file.
00001 /* ScummVM - Graphic Adventure Engine
00002  *
00003  * ScummVM is the legal property of its developers, whose names
00004  * are too numerous to list here. Please refer to the COPYRIGHT
00005  * file distributed with this source distribution.
00006  *
00007  * This program is free software; you can redistribute it and/or
00008  * modify it under the terms of the GNU General Public License
00009  * as published by the Free Software Foundation; either version 2
00010  * of the License, or (at your option) any later version.
00011  *
00012  * This program is distributed in the hope that it will be useful,
00013  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00014  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00015  * GNU General Public License for more details.
00016  *
00017  * You should have received a copy of the GNU General Public License
00018  * along with this program; if not, write to the Free Software
00019  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
00020  *
00021  */
00022 
00024 
00025 #ifndef AUDIO_MIDIPARSER_H
00026 #define AUDIO_MIDIPARSER_H
00027 
00028 #include "common/scummsys.h"
00029 #include "common/endian.h"
00030 
00031 class MidiDriver_BASE;
00032 
00033 
00034 
00036 //
00037 // Support entities
00038 //
00040 
00051 struct Tracker {
00052     byte * _playPos;        
00053     uint32 _playTime;       
00054     uint32 _playTick;       
00055     uint32 _lastEventTime; 
00056     uint32 _lastEventTick; 
00057     byte   _runningStatus;  
00058 
00059     Tracker() { clear(); }
00060 
00062     Tracker(const Tracker &copy) :
00063     _playPos(copy._playPos),
00064     _playTime(copy._playTime),
00065     _playTick(copy._playTick),
00066     _lastEventTime(copy._lastEventTime),
00067     _lastEventTick(copy._lastEventTick),
00068     _runningStatus(copy._runningStatus)
00069     { }
00070 
00072     void clear() {
00073         _playPos = 0;
00074         _playTime = 0;
00075         _playTick = 0;
00076         _lastEventTime = 0;
00077         _lastEventTick = 0;
00078         _runningStatus = 0;
00079     }
00080 };
00081 
00087 struct EventInfo {
00088     byte * start; 
00089 
00090     uint32 delta; 
00091     byte   event; 
00092 
00093     union {
00094         struct {
00095             byte param1; 
00096             byte param2; 
00097         } basic;
00098         struct {
00099             byte   type; 
00100             byte * data; 
00101         } ext;
00102     };
00103     uint32 length; 
00104 
00105 
00106 
00107 
00108     byte channel() const { return event & 0x0F; } 
00109     byte command() const { return event >> 4; }   
00110 };
00111 
00119 struct NoteTimer {
00120     byte channel;     
00121     byte note;        
00122     uint32 timeLeft; 
00123     NoteTimer() : channel(0), note(0), timeLeft(0) {}
00124 };
00125 
00126 
00127 
00128 
00130 //
00131 // MidiParser declaration
00132 //
00134 
00265 class MidiParser {
00266 protected:
00267     uint16    _activeNotes[128];   
00268     NoteTimer _hangingNotes[32];   
00269 
00270     byte      _hangingNotesCount; 
00271 
00272     MidiDriver_BASE *_driver;    
00273     uint32 _timerRate;     
00274     uint32 _ppqn;           
00275     uint32 _tempo;          
00276     uint32 _psecPerTick;  
00277     bool   _autoLoop;       
00278     bool   _smartJump;      
00279     bool   _centerPitchWheelOnUnload;  
00280     bool   _sendSustainOffOnNotesOff;   
00281     byte  *_tracks[120];    
00282     byte   _numTracks;     
00283     byte   _activeTrack;   
00284 
00285     Tracker _position;      
00286     EventInfo _nextEvent;  
00287 
00288 
00289     bool   _abortParse;    
00290     bool   _jumpingToTick; 
00291 
00292 protected:
00293     static uint32 readVLQ(byte * &data);
00294     virtual void resetTracking();
00295     virtual void allNotesOff();
00296     virtual void parseNextEvent(EventInfo &info) = 0;
00297     virtual bool processEvent(const EventInfo &info, bool fireEvents = true);
00298 
00299     void activeNote(byte channel, byte note, bool active);
00300     void hangingNote(byte channel, byte note, uint32 ticksLeft, bool recycle = true);
00301     void hangAllActiveNotes();
00302 
00303     virtual void sendToDriver(uint32 b);
00304     void sendToDriver(byte status, byte firstOp, byte secondOp) {
00305         sendToDriver(status | ((uint32)firstOp << 8) | ((uint32)secondOp << 16));
00306     }
00307 
00314     uint32 read4high(byte * &data) {
00315         uint32 val = READ_BE_UINT32(data);
00316         data += 4;
00317         return val;
00318     }
00319 
00326     uint16 read2low(byte * &data) {
00327         uint16 val = READ_LE_UINT16(data);
00328         data += 2;
00329         return val;
00330     }
00331 
00332 public:
00338     enum {
00344         mpMalformedPitchBends = 1,
00345 
00350         mpAutoLoop = 2,
00351 
00356         mpSmartJump = 3,
00357 
00362         mpCenterPitchWheelOnUnload = 4,
00363 
00368          mpSendSustainOffOnNotesOff = 5
00369     };
00370 
00371 public:
00372     typedef void (*XMidiCallbackProc)(byte eventData, void *refCon);
00373     typedef void (*XMidiNewTimbreListProc)(MidiDriver_BASE *driver, const byte *timbreListPtr, uint32 timbreListSize);
00374 
00375     MidiParser();
00376     virtual ~MidiParser() { allNotesOff(); }
00377 
00378     virtual bool loadMusic(byte *data, uint32 size) = 0;
00379     virtual void unloadMusic();
00380     virtual void property(int prop, int value);
00381 
00382     void setMidiDriver(MidiDriver_BASE *driver) { _driver = driver; }
00383     void setTimerRate(uint32 rate) { _timerRate = rate; }
00384     void setTempo(uint32 tempo);
00385     void onTimer();
00386 
00387     bool isPlaying() const { return (_position._playPos != 0); }
00388     void stopPlaying();
00389 
00390     bool setTrack(int track);
00391     bool jumpToTick(uint32 tick, bool fireEvents = false, bool stopNotes = true, bool dontSendNoteOn = false);
00392 
00393     uint32 getPPQN() { return _ppqn; }
00394     virtual uint32 getTick() { return _position._playTick; }
00395 
00396     static void defaultXMidiCallback(byte eventData, void *refCon);
00397 
00398     static MidiParser *createParser_SMF();
00399     static MidiParser *createParser_XMIDI(XMidiCallbackProc proc = defaultXMidiCallback, void *refCon = 0, XMidiNewTimbreListProc newTimbreListProc = NULL, MidiDriver_BASE *newTimbreListDriver = NULL);
00400     static MidiParser *createParser_QT();
00401     static void timerCallback(void *data) { ((MidiParser *) data)->onTimer(); }
00402 };
00403 
00404 #endif


Generated on Sat May 18 2019 05:01:09 for ResidualVM by doxygen 1.7.1
curved edge   curved edge