Add MMCSS related code for raising thread priority on Windows
[ardour.git] / libs / backends / portaudio / winmmemidi_output_device.h
1 /*
2  * Copyright (C) 2015 Tim Mayberry <mojofunk@gmail.com>
3  *
4  * This program is free software; you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License as published by
6  * the Free Software Foundation; either version 2 of the License, or
7  * (at your option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program; if not, write to the Free Software
16  * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
17  */
18
19 #ifndef WINMME_MIDI_OUTPUT_DEVICE_H
20 #define WINMME_MIDI_OUTPUT_DEVICE_H
21
22 #include <windows.h>
23 #include <mmsystem.h>
24
25 #include <stdint.h>
26
27 #include <string>
28
29 #include <boost/scoped_ptr.hpp>
30
31 #include <pbd/ringbuffer.h>
32
33 namespace ARDOUR {
34
35 class WinMMEMidiOutputDevice {
36 public:
37         WinMMEMidiOutputDevice (int index);
38
39         ~WinMMEMidiOutputDevice ();
40
41         bool enqueue_midi_event (uint64_t rel_event_time_us,
42                                  const uint8_t* data,
43                                  const size_t size);
44
45         bool start ();
46         bool stop ();
47
48         void set_enabled (bool enable);
49         bool get_enabled ();
50
51         std::string name () const { return m_name; }
52
53 private: // Methods
54         bool open (UINT index, std::string& error_msg);
55         bool close (std::string& error_msg);
56
57         bool set_device_name (UINT index);
58
59         std::string get_error_string (MMRESULT error_code);
60
61         bool start_midi_output_thread ();
62         bool stop_midi_output_thread ();
63
64         bool signal (HANDLE semaphore);
65         bool wait (HANDLE semaphore);
66
67         static void* midi_output_thread (void*);
68         void midi_output_thread ();
69
70         bool wait_for_microseconds (int64_t us);
71
72         static void CALLBACK winmm_output_callback (HMIDIOUT handle,
73                                                     UINT msg,
74                                                     DWORD_PTR instance,
75                                                     DWORD_PTR midi_data,
76                                                     DWORD_PTR timestamp);
77
78         void midi_output_callback (UINT msg, DWORD_PTR data, DWORD_PTR timestamp);
79
80 private: // Data
81         HMIDIOUT m_handle;
82
83         HANDLE m_queue_semaphore;
84         HANDLE m_sysex_semaphore;
85
86         HANDLE m_timer;
87
88         bool m_started;
89         bool m_enabled;
90
91         std::string m_name;
92
93         pthread_t m_output_thread_handle;
94
95         bool m_thread_running;
96         bool m_thread_quit;
97
98         boost::scoped_ptr<RingBuffer<uint8_t> > m_midi_buffer;
99 };
100
101 } // namespace ARDOUR
102
103 #endif // WINMME_MIDI_OUTPUT_DEVICE_H