CFugue
AlsaDriver.h
Go to the documentation of this file.
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: 2013-12-18 10:59:57 +0530 (Wed, 18 Dec 2013) $
9  $Rev: 197 $
10  $LastChangedBy: krishnapg $
11 */
12 
13 #ifndef __ALSADRIVER_H__9857EC39_234E_411E_9558_EFDA218796AA__
14 #define __ALSADRIVER_H__9857EC39_234E_411E_9558_EFDA218796AA__
15 
16 /** @file AlsaDriver.h
17  * \brief Declares MIDIDriverAlsa class for CFugue
18  */
19 #include "rtmidi/RtMidi.h"
20 #include "jdkmidi/msg.h"
21 #include "jdkmidi/driver.h"
22 #include "jdkmidi/sequencer.h"
23 
24 #include <thread> // std::thread
25 #include <future> // std::future
26 
27 namespace CFugue
28 {
29  ///<Summary>MIDI Driver for Linux Alsa based machines</Summary>
30  class MIDIDriverAlsa : public jdkmidi::MIDIDriver
31  {
32  RtMidiIn* m_pMidiIn;
33  RtMidiOut* m_pMidiOut;
34  std::future<bool> m_bgTaskResult;
35  public:
36  MIDIDriverAlsa ( int queue_size );
37  virtual ~MIDIDriverAlsa();
38 
39  void ResetMIDIOut();
40 
41  /// <Summary>
42  /// Creates a background thread to pump MIDI events
43  /// at the supplied timer resolution.
44  /// Use WaitTillDone() to wait till the background processing completes.
45  /// Use StopTimer() after the background processing is completed, to release resources.
46  /// @param resolution_ms MIDI Timer resolution in milliseconds
47  /// @return false if background thread cannot be started
48  /// </Summary>
49  bool StartTimer ( int resolution_ms );
50 
51  /// Waits (blocks) till the background thread created with StartTimer()
52  /// completes its processing.
53  /// After StartTimer() succeeds, use WaitTillDone() followed by StopTimer().
54  /// Returns immediately if no background thread is running.
55  void WaitTillDone();
56 
57  /// Call StopTimer() to release the resources used by the background
58  /// procedure created with StartTimer(). StopTimer() Should be called
59  /// <i>after</i> the background procedure is done (indicated by BGThreadStatus::COMPLETED).
60  /// If background procedure is still running while StopTimer() is called, caller gets
61  /// blocked till the background procedure completes.
62  /// If no background procedure exists, returns immediately.
63  void StopTimer();
64 
65  /// Opens the MIDI input port with the given ID
66  /// @return false if the given input port cannot be opened
67  bool OpenMIDIInPort ( int id );
68 
69  /// Opens the MIDI output port with the given ID
70  /// @return false if the given output port cannot be opened
71  bool OpenMIDIOutPort ( int id );
72 
73  /// Closed any previously opened MIDI Input port
74  void CloseMIDIInPort();
75 
76  /// Closed any previously opened MIDI Output port
77  void CloseMIDIOutPort();
78 
79  enum BGThreadStatus { RUNNING, ///< Async procedure is running - use WaitTillDone() to wait for completion
80  COMPLETED, ///< Async procedure completed running - use StopTimer() to finish
81  INVALID ///< No background procedure running - use StartTimer() to start one
82  };
83  protected:
84  bool HardwareMsgOut ( const jdkmidi::MIDITimedBigMessage &msg );
85 
86  std::thread* m_pThread;
87 
88  int timer_id;
89  int timer_res;
90 
91  //bool in_open;
92  //bool out_open;
93  //bool timer_open;
94  };
95 
96 } // namespace CFugue
97 
98 #endif // __ALSADRIVER_H__9857EC39_234E_411E_9558_EFDA218796AA__
Async procedure completed running - use StopTimer() to finish.
Definition: AlsaDriver.h:80
void CloseMIDIOutPort()
Closed any previously opened MIDI Output port.
Definition: AlsaDriver.cpp:192
Async procedure is running - use WaitTillDone() to wait for completion.
Definition: AlsaDriver.h:79
bool OpenMIDIOutPort(int id)
Definition: AlsaDriver.cpp:70
bool StartTimer(int resolution_ms)
Definition: AlsaDriver.cpp:151
bool OpenMIDIInPort(int id)
Definition: AlsaDriver.cpp:35
MIDI Driver for Linux Alsa based machines
Definition: AlsaDriver.h:30
No background procedure running - use StartTimer() to start one.
Definition: AlsaDriver.h:81
void CloseMIDIInPort()
Closed any previously opened MIDI Input port.
Definition: AlsaDriver.cpp:182

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