CFugue
Player.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 __PLAYER_H__83B70D04_18A9_48ff_B08B_81BF613138DB__
14 #define __PLAYER_H__83B70D04_18A9_48ff_B08B_81BF613138DB__
15 
16 #include "MusicStringParser.h"
17 #include "MidiRenderer.h"
18 
19 namespace CFugue
20 {
21  /// <Summary> MIDI Player for Music Strings </Summary>
22  class Player
23  {
24  MIDIRenderer m_Renderer;
25  MusicStringParser m_Parser;
26  unsigned int m_nOutPort; // The MIDI Output port that should be used for Play
27  unsigned int m_nTimerRes; // The Timer Resolution in MilliSeconds
28  public:
29 
30  /// Construct the Player Object using supplied Midi Output port and Timer Resolution
31  /// @param nMIDIOutPortID the output MIDI port to be used for the play
32  /// @param nMIDITimerResMS timer resolution in Milliseconds
33  Player(unsigned int nMIDIOutPortID = MIDI_MAPPER, unsigned int nMIDITimerResMS = 20);
34 
35  inline ~Player(void)
36  {
37  }
38 
39  /// Returns the associated Parser object
40  inline MusicStringParser& Parser() { return m_Parser; }
41 
42  /// Get/Set the Midi Output Port that should be used with this Player
43  inline unsigned int MidiOutPort() { return m_nOutPort; }
44 
45  /// Get/Set the Timer Resolution (in MilliSeconds) that should be used with this Player
46  inline unsigned int TimerResolution() { return m_nTimerRes; }
47 
48  /// <Summary>
49  /// Plays a string of music notes. Will not return till the play is complete.
50  /// To Play the Notes asynchronously, use the PlayAsync() method instead.
51  ///
52  /// Returns false if unable to start the play. Failures can happen if unable to open
53  /// the MIDI output port or if unable to create a MIDI timer with supplied resolution or
54  /// if there are any critical parsing errors in the supplied Music String.
55  ///
56  /// @param strMusicNotes the input Music string to be played on MIDI output port
57  /// @return True if play was successful, false otherwise
58  /// </Summary>
59  /// Example Usage:
60  /** <pre>
61  CFugue::Player player; // Create the Player Object with the default MIDI output port
62 
63  player.Play(_T("ci di f fi")); // Play the Music Notes
64  </pre> */
65  bool Play(const MString& strMusicNotes);
66 
67  /// <Summary>
68  /// Starts playing the notes asynchronously. Returns false if unable to start the Play.
69  /// Play failures can happen if unable to open the MIDI output port or if unable to
70  /// create a MIDI timer with supplied resolution or if there any critical errors in
71  /// the Music String parsing.
72  ///
73  /// After Play starts, use IsPlaying() method to determine if play is still in progress.
74  /// Use the StopPlay() method to stop the play.
75  ///
76  /// You can also use WaitTillDone() to wait till the play completes. Refer \ref WaitTillDone.
77  ///
78  /// Note that each PlayAsync() should have a matching StopPlay() method call to release MIDI resources.
79  ///
80  /// @param strMusicNotes the input Music string to be played on MIDI output port
81  /// @return True if play started successfully, false otherwise
82  /// </Summary>
83  /// Example Usage:
84  /** <pre>
85  CFugue::Player player(nMIDIOutPortID, nMIDITimerResMS); // Create the Player object
86 
87  if(player.PlayAsync(strMusicNotes) // Start Playing Asynchronously
88  while(player.IsPlaying()) // Wait while the play is still in progress
89  Sleep(1000);
90 
91  player.StopPlay(); // Stop the Play and release MIDI resources
92  </pre> */
93  bool PlayAsync(const MString& strMusicNotes);
94 
95  /// <Summary>
96  /// After play starts asynchronously with PlayAsync(), use WaitTillDone() to wait
97  /// till the play completes. Caller gets blocked. Once WaitTillDone() returns call
98  /// StopPlay() to release MIDI resources.
99  /// </Summary>
100  /// Example Usage:
101  /** <pre>
102  CFugue::Player player(nMIDIOutPortID, nMIDITimerResMS); // Create the Player object
103 
104  if(player.PlayAsync(strMusicNotes) // Start Playing Asynchronously
105  player.WaitTillDone(); // wait while the play is in progress
106 
107  player.StopPlay(); // Stop the Play and release MIDI resources
108  </pre> */
109  void WaitTillDone();
110 
111  /// <Summary>
112  /// Stops the Play started with PlayAsync().
113  /// Each PlayAsync() should have a matching StopPlay() method call.
114  /// </Summary>
115  void StopPlay();
116 
117  /// <Summary>Returns true if an asynchronous Play is currently in progress</Summary>
118  inline bool IsPlaying() const { return m_Renderer.IsPlaying(); }
119 
120  /// <Summary>
121  /// Parses the Music String and saves the generated MIDI events to a Midi output file.
122  ///
123  /// Returns true upon success. If there are any parsing errors for the Music string, result is undefined.
124  ///
125  /// To get notified about the parsing events, retrieve the associated parser
126  /// object using the Player::Parser() method and subscribe to the <code>evTrace</code>
127  /// and <code>evError</code> events.
128  ///
129  /// @param strMusicNotes the input Music string to be converted to MIDI output
130  /// @param szOutputFilePath the output MIDI file path
131  /// @return True if content is saved successfully, False otherwise
132  /// </Summary>
133  /// Example Usage:
134  /** <pre>
135  void OnParseTrace(const CFugue::CParser*, CFugue::CParser::TraceEventHandlerArgs* pEvArgs)
136  {
137  OutputDebugString(_T("\n"));
138  OutputDebugString(pEvArgs->szTraceMsg);
139  }
140 
141  void OnParseError(const CFugue::CParser*, CFugue::CParser::ErrorEventHandlerArgs* pEvArgs)
142  {
143  OutputDebugString(_T("\nError --> "));
144  OutputDebugString(pEvArgs->szErrMsg);
145  if(pEvArgs->szToken)
146  {
147  OutputDebugString(_T("\t Token: "));
148  OutputDebugString(pEvArgs->szToken);
149  }
150  }
151 
152  CFugue::Player player; // Create the Player Object
153 
154  player.Parser().evTrace.Subscribe(&OnParseTrace); // Subscribe to the Trace Events
155  player.Parser().evError.Subscribe(&OnParseError); // Subscribe to the Error Events
156 
157  player.SaveAsMidiFile(_T("Cq Dw Ex"), "MidiOutput.midi"); // Save the Music Notes to Midi file directly, without playing
158 
159  </pre> */
160  bool SaveAsMidiFile(const MString& strMusicNotes, const char* szOutputFilePath);
161 
162  /// <Summary>
163  /// Saves any previously played Music Notes into a Midi output file.
164  /// If Play() or PlayAsync() is not called on this object previously,
165  /// the output MIDI file will be empty.
166  ///
167  /// To save the notes directly to MIDI file without playing, use Player::SaveAsMidiFile
168  /// method instead.
169  ///
170  /// @param szOutputFilePath the output MIDI file path
171  /// @return True if content is saved successfully, False otherwise
172  /// </Summary>
173  /// Example Usage:
174  /** <pre>
175  CFugue::Player player; // Create the Player Object
176 
177  player.Play(_T("ci di f fi")); // Play the Music Notes on MIDI output port
178 
179  player.SaveToMidiFile("Output.mid"); // Save the played content to MIDI Output file
180  </pre> */
181  bool SaveToMidiFile(const char* szOutputFilePath);
182  };
183 
184 } // namespace CFugue
185 
186 #endif // __PLAYER_H__83B70D04_18A9_48ff_B08B_81BF613138DB__
void StopPlay()
Definition: Player.cpp:50
bool PlayAsync(const MString &strMusicNotes)
Definition: Player.cpp:40
Implements a MusicString Parsing functionality
bool IsPlaying() const
Returns true if an asynchronous Play is currently in progress
Definition: Player.h:118
bool Play(const MString &strMusicNotes)
Definition: Player.cpp:28
Takes care of Rendering MIDI Output either to a file or to a MIDI out Port
Definition: MidiRenderer.h:31
MIDI Player for Music Strings
Definition: Player.h:22
Player(unsigned int nMIDIOutPortID=MIDI_MAPPER, unsigned int nMIDITimerResMS=20)
Definition: Player.cpp:22
void WaitTillDone()
Definition: Player.cpp:55
Helper class for simple string manipulations
Definition: MString.h:51
unsigned int TimerResolution()
Get/Set the Timer Resolution (in MilliSeconds) that should be used with this Player.
Definition: Player.h:46
bool SaveAsMidiFile(const MString &strMusicNotes, const char *szOutputFilePath)
Definition: Player.cpp:60
MusicStringParser & Parser()
Returns the associated Parser object.
Definition: Player.h:40
bool IsPlaying() const
Definition: MidiRenderer.h:118
bool SaveToMidiFile(const char *szOutputFilePath)
Definition: Player.cpp:69
unsigned int MidiOutPort()
Get/Set the Midi Output Port that should be used with this Player.
Definition: Player.h:43

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