CFugue
MidiRenderer.h
1 /*
2  This is part of CFugue, a C++ Runtime for MIDI Score Programming
3  Copyright (C) 2009 Gopalakrishna Palem
4 
5  For links to further information, or to contact the author,
6  see <http://cfugue.sourceforge.net/>.
7 
8  $LastChangedDate: 2014-05-11 07:42:46 +0530 (Sun, 11 May 2014) $
9  $Rev: 211 $
10  $LastChangedBy: krishnapg $
11 */
12 
13 #ifndef MIDIRENDERER_H__9266AE56_84CB_4662_8328_ED088111CFE0__
14 #define MIDIRENDERER_H__9266AE56_84CB_4662_8328_ED088111CFE0__
15 
16 #include "jdkmidi/manager.h"
17 #include "ParserListener.h"
18 #include "MidiEventManager.h"
19 #include "MidiTimer.h"
20 
21 #ifndef MIDI_MAPPER
22 #define MIDI_MAPPER ((unsigned int)-1)
23 #endif // MIDI_MAPPER
24 
25 namespace jdkmidi { class MIDIDriverWin32; }
26 namespace CFugue { class MIDIDriverAlsa; }
27 
28 namespace CFugue
29 {
30  ///<Summary>Takes care of Rendering MIDI Output either to a file or to a MIDI out Port</Summary>
32  {
33  #if defined _WIN32
34  jdkmidi::MIDIDriverWin32* m_pMIDIDriver;
35  #else
36  CFugue::MIDIDriverAlsa* m_pMIDIDriver;
37  #endif
38 
39  jdkmidi::MIDIManager m_MIDIManager;
40 
41  long m_nSequenceTime; // pseudo time tick to keep track of sequencer
42 
43  long m_lFirstNoteTime; // time of first parallel note. Used for parallel notes
44 
45  /// <Summary>Event handler for Channel Pressure event Raised by Parser</Summary>
46  virtual void OnChannelPressureEvent(const CParser* pParser, const ChannelPressure* pCP);
47 
48  /// <Summary>Event handler for Controller event Raised by Parser</Summary>
49  virtual void OnControllerEvent(const CParser* pParser, const ControllerEvent* pConEvent);
50 
51  /// <Summary>Event handler for Instrument event Raised by Parser</Summary>
52  virtual void OnInstrumentEvent(const CParser* pParser, const Instrument* pInstrument);
53 
54  /// <Summary>Event handler for KeySignature event Raised by Parser</Summary>
55  virtual void OnKeySignatureEvent(const CParser* pParser, const KeySignature* pKeySig);
56 
57  /// <Summary>Event handler for Layer event Raised by Parser</Summary>
58  virtual void OnLayerEvent(const CParser* pParser, const Layer* pLayer);
59 
60  /// <Summary>Event handler for PitchBend event Raised by Parser</Summary>
61  virtual void OnPitchBendEvent(const CParser* pParser, const PitchBend* pPB);
62 
63  /// <Summary>Event handler for Polyphonic Pressure event Raised by Parser</Summary>
64  virtual void OnPolyphonicPressureEvent(const CParser* pParser, const PolyphonicPressure* pPressure);
65 
66  /// <Summary>Event handler for Tempo event Raised by Parser </Summary>
67  virtual void OnTempoEvent(const CParser* pParser, const Tempo* pTempo);
68 
69  /// <Summary>Event handler for Time event Raised by Parser </Summary>
70  virtual void OnTimeEvent(const CParser* pParser, const Time* pTime);
71 
72  /// <Summary>Event handler for Voice event Raised by Parser</Summary>
73  virtual void OnVoiceEvent(const CParser* pParser, const Voice* pVoice);
74 
75  /// <Summary>Event handler for Note event Raised by Parser</Summary>
76  virtual void OnNoteEvent(const CParser* pParser, const Note* pNote);
77 
78  public:
79 
80  MIDIRenderer(void);
81 
82  ~MIDIRenderer(void);
83 
84  /// <Summary>
85  /// Clears the current state of the Renderer. Empties all the stored tracks and notes.
86  /// </Summary>
87  void Clear();
88 
89  /// <Summary>
90  /// Starts Rendering the MIDI output to MIDI port.
91  /// Each BeingPlayAsync call should have a matching EndPlayAsync call (no matter success or failure).
92  /// @param nMIDIOutPortID is the ID of the MIDI output port to open for play.
93  /// @param nTimerResolutionMS is the required minimum resolution for the MIDI timer (in MilliSeconds).
94  /// @return false if Unable to open MIDI port or unable to create timer with specified resolution.
95  /// </Summary>
96  bool BeginPlayAsync(int nMIDIOutPortID = MIDI_MAPPER, unsigned int nTimerResolutionMS = 20);
97 
98 
99  /// <Summary>
100  /// Stops Rendering the MIDI output to MIDI port.
101  /// Each BeingPlayAsync call should have a matching EndPlayAsync call.
102  /// </Summary>
103  void EndPlayAsync();
104 
105  /// <Summary>
106  /// After starting MIDI play with BeginPlayAsync(), use WaitTillDone()
107  /// to wait till the play is done. Caller gets blocked until completion.
108  /// Returns immediately if no play is in progress.
109  /// After WaitTillDone() returns, use EndPlayAsync() to release the resources.
110  /// In case you do want to know the status without geting blocked,
111  /// use IsPlaying() method.
112  /// </Summary>
113  void WaitTillDone();
114 
115  /// <Summary>
116  /// Indicates if the MIDI output is still being rendered (to MIDI Port) or finished.
117  /// </Summary>
118  inline bool IsPlaying() const { return m_MIDIManager.IsSeqPlay(); }
119 
120  /// <Summary>
121  /// Saves the current track/sequencer content to a MIDI Output file
122  /// </Summary>
123  bool SaveToFile(const char* szOutputFilePath); //TODO: Add the capatiblity to store custom MIDI Headers
124  };
125 
126 } // namespace CFugue
127 
128 #endif // MIDIRENDERER_H__9266AE56_84CB_4662_8328_ED088111CFE0__
Takes care of defining the MIDI Controller Events used in MusicStrings.
bool BeginPlayAsync(int nMIDIOutPortID=MIDI_MAPPER, unsigned int nTimerResolutionMS=20)
Takes care of Rendering MIDI Output either to a file or to a MIDI out Port
Definition: MidiRenderer.h:31
Class representing the Key Signature.
Definition: KeySignature.h:79
bool SaveToFile(const char *szOutputFilePath)
Class representing a Musical Note
Definition: Note.h:19
MIDI Driver for Linux Alsa based machines
Definition: AlsaDriver.h:30
Takes care of MIDI Events, Tracks and Sequencing
Base class that represents a Parser Object.
Definition: Parser.h:63
Base class that represents a Renderer Object
bool IsPlaying() const
Definition: MidiRenderer.h:118

CFugue, the C++ Music Programming Library © Copyright 2009 Cenacle Research India Private Limited Gopalakrishna Palem