CFugue
Player.cpp
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 
9 #include "stdafx.h"
10 #include "Player.h"
11 
12 #if defined WIN32 || defined _WIN32
13 #define SLEEP(x) Sleep(x)
14 #elif defined __GNUC__
15 #include <thread>
16 #include <chrono>
17 #define SLEEP(x) std::this_thread::sleep_for(std::chrono::milliseconds(x))
18 #endif
19 
20 namespace CFugue
21 {
22  Player::Player(unsigned int nMIDIOutPortID /*= MIDI_MAPPER*/, unsigned int nMIDITimerResMS /*= 20*/)
23  : m_nOutPort(nMIDIOutPortID), m_nTimerRes(nMIDITimerResMS)
24  {
25  m_Parser.AddListener(&m_Renderer);
26  }
27 
28  bool Player::Play(const MString& strMusicNotes)
29  {
30  bool bRetVal = PlayAsync(strMusicNotes);
31 
32  m_Renderer.WaitTillDone();
33 
34  StopPlay();
35 
36  return bRetVal;
37  }
38 
39 
40  bool Player::PlayAsync(const MString& strMusicNotes)
41  {
42  m_Renderer.Clear(); // Clear any previous Notes
43 
44  if(false == m_Parser.Parse(strMusicNotes)) // Parse and Load the Notes into MIDI MultiTrack
45  return false;
46 
47  return m_Renderer.BeginPlayAsync(m_nOutPort, m_nTimerRes); // Start Playing on the given MIDIport with supplied resolution
48  }
49 
51  {
52  m_Renderer.EndPlayAsync();
53  }
54 
56  {
57  m_Renderer.WaitTillDone();
58  }
59 
60  bool Player::SaveAsMidiFile(const MString& strMusicNotes, const char* szOutputFilePath)
61  {
62  m_Renderer.Clear(); // Clear any previous Notes
63 
64  m_Parser.Parse(strMusicNotes); // Parse the Notes and Load the Midi Events into MIDI MultiTrack
65 
66  return m_Renderer.SaveToFile(szOutputFilePath);
67  }
68 
69  bool Player::SaveToMidiFile(const char* szOutputFilePath)
70  {
71  return m_Renderer.SaveToFile(szOutputFilePath);
72  }
73 
74 } // namespace CFugue
void AddListener(CParserListener *pListener)
Subscribes a Listener object for all events
Definition: Parser.cpp:16
void StopPlay()
Definition: Player.cpp:50
bool PlayAsync(const MString &strMusicNotes)
Definition: Player.cpp:40
bool BeginPlayAsync(int nMIDIOutPortID=MIDI_MAPPER, unsigned int nTimerResolutionMS=20)
bool Parse(const TCHAR *szTokens)
bool Play(const MString &strMusicNotes)
Definition: Player.cpp:28
bool SaveToFile(const char *szOutputFilePath)
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
bool SaveAsMidiFile(const MString &strMusicNotes, const char *szOutputFilePath)
Definition: Player.cpp:60
bool SaveToMidiFile(const char *szOutputFilePath)
Definition: Player.cpp:69

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