Chris@559
|
1 /**********************************************************************/
|
Chris@559
|
2 /*! \class RtMidi
|
Chris@559
|
3 \brief An abstract base class for realtime MIDI input/output.
|
Chris@559
|
4
|
Chris@559
|
5 This class implements some common functionality for the realtime
|
Chris@559
|
6 MIDI input/output subclasses RtMidiIn and RtMidiOut.
|
Chris@559
|
7
|
Chris@559
|
8 RtMidi WWW site: http://music.mcgill.ca/~gary/rtmidi/
|
Chris@559
|
9
|
Chris@559
|
10 RtMidi: realtime MIDI i/o C++ classes
|
Chris@1397
|
11 Copyright (c) 2003-2016 Gary P. Scavone
|
Chris@559
|
12
|
Chris@559
|
13 Permission is hereby granted, free of charge, to any person
|
Chris@559
|
14 obtaining a copy of this software and associated documentation files
|
Chris@559
|
15 (the "Software"), to deal in the Software without restriction,
|
Chris@559
|
16 including without limitation the rights to use, copy, modify, merge,
|
Chris@559
|
17 publish, distribute, sublicense, and/or sell copies of the Software,
|
Chris@559
|
18 and to permit persons to whom the Software is furnished to do so,
|
Chris@559
|
19 subject to the following conditions:
|
Chris@559
|
20
|
Chris@559
|
21 The above copyright notice and this permission notice shall be
|
Chris@559
|
22 included in all copies or substantial portions of the Software.
|
Chris@559
|
23
|
Chris@559
|
24 Any person wishing to distribute modifications to the Software is
|
Chris@1397
|
25 asked to send the modifications to the original developer so that
|
Chris@1397
|
26 they can be incorporated into the canonical version. This is,
|
Chris@1397
|
27 however, not a binding provision of this license.
|
Chris@559
|
28
|
Chris@559
|
29 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
Chris@559
|
30 EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
Chris@559
|
31 MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
|
Chris@559
|
32 IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR
|
Chris@559
|
33 ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF
|
Chris@559
|
34 CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
Chris@559
|
35 WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
Chris@559
|
36 */
|
Chris@559
|
37 /**********************************************************************/
|
Chris@559
|
38
|
Chris@559
|
39 #include "RtMidi.h"
|
Chris@559
|
40 #include <sstream>
|
Chris@559
|
41
|
Chris@1397
|
42 #if defined(__MACOSX_CORE__)
|
Chris@1397
|
43 #if TARGET_OS_IPHONE
|
Chris@1397
|
44 #define AudioGetCurrentHostTime CAHostTimeBase::GetCurrentTime
|
Chris@1397
|
45 #define AudioConvertHostTimeToNanos CAHostTimeBase::ConvertToNanos
|
Chris@1397
|
46 #endif
|
Chris@1397
|
47 #endif
|
Chris@843
|
48
|
Chris@559
|
49 //*********************************************************************//
|
Chris@1397
|
50 // RtMidi Definitions
|
Chris@559
|
51 //*********************************************************************//
|
Chris@559
|
52
|
Chris@559
|
53 RtMidi :: RtMidi()
|
Chris@1397
|
54 : rtapi_(0)
|
Chris@559
|
55 {
|
Chris@559
|
56 }
|
Chris@559
|
57
|
Chris@1397
|
58 RtMidi :: ~RtMidi()
|
Chris@559
|
59 {
|
Chris@1397
|
60 if ( rtapi_ )
|
Chris@1397
|
61 delete rtapi_;
|
Chris@1397
|
62 rtapi_ = 0;
|
Chris@1397
|
63 }
|
Chris@1397
|
64
|
Chris@1397
|
65 std::string RtMidi :: getVersion( void ) throw()
|
Chris@1397
|
66 {
|
Chris@1397
|
67 return std::string( RTMIDI_VERSION );
|
Chris@1397
|
68 }
|
Chris@1397
|
69
|
Chris@1397
|
70 void RtMidi :: getCompiledApi( std::vector<RtMidi::Api> &apis ) throw()
|
Chris@1397
|
71 {
|
Chris@1397
|
72 apis.clear();
|
Chris@1397
|
73
|
Chris@1397
|
74 // The order here will control the order of RtMidi's API search in
|
Chris@1397
|
75 // the constructor.
|
Chris@1397
|
76 #if defined(__MACOSX_CORE__)
|
Chris@1397
|
77 apis.push_back( MACOSX_CORE );
|
Chris@1397
|
78 #endif
|
Chris@1397
|
79 #if defined(__LINUX_ALSA__)
|
Chris@1397
|
80 apis.push_back( LINUX_ALSA );
|
Chris@1397
|
81 #endif
|
Chris@1397
|
82 #if defined(__UNIX_JACK__)
|
Chris@1397
|
83 apis.push_back( UNIX_JACK );
|
Chris@1397
|
84 #endif
|
Chris@1397
|
85 #if defined(__WINDOWS_MM__)
|
Chris@1397
|
86 apis.push_back( WINDOWS_MM );
|
Chris@1397
|
87 #endif
|
Chris@1397
|
88 #if defined(__RTMIDI_DUMMY__)
|
Chris@1397
|
89 apis.push_back( RTMIDI_DUMMY );
|
Chris@1397
|
90 #endif
|
Chris@1397
|
91 }
|
Chris@1397
|
92
|
Chris@1397
|
93 //*********************************************************************//
|
Chris@1397
|
94 // RtMidiIn Definitions
|
Chris@1397
|
95 //*********************************************************************//
|
Chris@1397
|
96
|
Chris@1397
|
97 void RtMidiIn :: openMidiApi( RtMidi::Api api, const std::string clientName, unsigned int queueSizeLimit )
|
Chris@1397
|
98 {
|
Chris@1397
|
99 if ( rtapi_ )
|
Chris@1397
|
100 delete rtapi_;
|
Chris@1397
|
101 rtapi_ = 0;
|
Chris@1397
|
102
|
Chris@1397
|
103 #if defined(__UNIX_JACK__)
|
Chris@1397
|
104 if ( api == UNIX_JACK )
|
Chris@1397
|
105 rtapi_ = new MidiInJack( clientName, queueSizeLimit );
|
Chris@1397
|
106 #endif
|
Chris@1397
|
107 #if defined(__LINUX_ALSA__)
|
Chris@1397
|
108 if ( api == LINUX_ALSA )
|
Chris@1397
|
109 rtapi_ = new MidiInAlsa( clientName, queueSizeLimit );
|
Chris@1397
|
110 #endif
|
Chris@1397
|
111 #if defined(__WINDOWS_MM__)
|
Chris@1397
|
112 if ( api == WINDOWS_MM )
|
Chris@1397
|
113 rtapi_ = new MidiInWinMM( clientName, queueSizeLimit );
|
Chris@1397
|
114 #endif
|
Chris@1397
|
115 #if defined(__MACOSX_CORE__)
|
Chris@1397
|
116 if ( api == MACOSX_CORE )
|
Chris@1397
|
117 rtapi_ = new MidiInCore( clientName, queueSizeLimit );
|
Chris@1397
|
118 #endif
|
Chris@1397
|
119 #if defined(__RTMIDI_DUMMY__)
|
Chris@1397
|
120 if ( api == RTMIDI_DUMMY )
|
Chris@1397
|
121 rtapi_ = new MidiInDummy( clientName, queueSizeLimit );
|
Chris@1397
|
122 #endif
|
Chris@1397
|
123 }
|
Chris@1397
|
124
|
Chris@1397
|
125 RtMidiIn :: RtMidiIn( RtMidi::Api api, const std::string clientName, unsigned int queueSizeLimit )
|
Chris@1397
|
126 : RtMidi()
|
Chris@1397
|
127 {
|
Chris@1397
|
128 if ( api != UNSPECIFIED ) {
|
Chris@1397
|
129 // Attempt to open the specified API.
|
Chris@1397
|
130 openMidiApi( api, clientName, queueSizeLimit );
|
Chris@1397
|
131 if ( rtapi_ ) return;
|
Chris@1397
|
132
|
Chris@1397
|
133 // No compiled support for specified API value. Issue a warning
|
Chris@1397
|
134 // and continue as if no API was specified.
|
Chris@1397
|
135 std::cerr << "\nRtMidiIn: no compiled support for specified API argument!\n\n" << std::endl;
|
Chris@559
|
136 }
|
Chris@1397
|
137
|
Chris@1397
|
138 // Iterate through the compiled APIs and return as soon as we find
|
Chris@1397
|
139 // one with at least one port or we reach the end of the list.
|
Chris@1397
|
140 std::vector< RtMidi::Api > apis;
|
Chris@1397
|
141 getCompiledApi( apis );
|
Chris@1397
|
142 for ( unsigned int i=0; i<apis.size(); i++ ) {
|
Chris@1397
|
143 openMidiApi( apis[i], clientName, queueSizeLimit );
|
Chris@1397
|
144 if ( rtapi_->getPortCount() ) break;
|
Chris@1397
|
145 }
|
Chris@1397
|
146
|
Chris@1397
|
147 if ( rtapi_ ) return;
|
Chris@1397
|
148
|
Chris@1397
|
149 // It should not be possible to get here because the preprocessor
|
Chris@1397
|
150 // definition __RTMIDI_DUMMY__ is automatically defined if no
|
Chris@1397
|
151 // API-specific definitions are passed to the compiler. But just in
|
Chris@1397
|
152 // case something weird happens, we'll throw an error.
|
Chris@1397
|
153 std::string errorText = "RtMidiIn: no compiled API support found ... critical error!!";
|
Chris@1397
|
154 throw( RtMidiError( errorText, RtMidiError::UNSPECIFIED ) );
|
Chris@1397
|
155 }
|
Chris@1397
|
156
|
Chris@1397
|
157 RtMidiIn :: ~RtMidiIn() throw()
|
Chris@1397
|
158 {
|
Chris@1397
|
159 }
|
Chris@1397
|
160
|
Chris@1397
|
161
|
Chris@1397
|
162 //*********************************************************************//
|
Chris@1397
|
163 // RtMidiOut Definitions
|
Chris@1397
|
164 //*********************************************************************//
|
Chris@1397
|
165
|
Chris@1397
|
166 void RtMidiOut :: openMidiApi( RtMidi::Api api, const std::string clientName )
|
Chris@1397
|
167 {
|
Chris@1397
|
168 if ( rtapi_ )
|
Chris@1397
|
169 delete rtapi_;
|
Chris@1397
|
170 rtapi_ = 0;
|
Chris@1397
|
171
|
Chris@1397
|
172 #if defined(__UNIX_JACK__)
|
Chris@1397
|
173 if ( api == UNIX_JACK )
|
Chris@1397
|
174 rtapi_ = new MidiOutJack( clientName );
|
Chris@1397
|
175 #endif
|
Chris@1397
|
176 #if defined(__LINUX_ALSA__)
|
Chris@1397
|
177 if ( api == LINUX_ALSA )
|
Chris@1397
|
178 rtapi_ = new MidiOutAlsa( clientName );
|
Chris@1397
|
179 #endif
|
Chris@1397
|
180 #if defined(__WINDOWS_MM__)
|
Chris@1397
|
181 if ( api == WINDOWS_MM )
|
Chris@1397
|
182 rtapi_ = new MidiOutWinMM( clientName );
|
Chris@1397
|
183 #endif
|
Chris@1397
|
184 #if defined(__MACOSX_CORE__)
|
Chris@1397
|
185 if ( api == MACOSX_CORE )
|
Chris@1397
|
186 rtapi_ = new MidiOutCore( clientName );
|
Chris@1397
|
187 #endif
|
Chris@1397
|
188 #if defined(__RTMIDI_DUMMY__)
|
Chris@1397
|
189 if ( api == RTMIDI_DUMMY )
|
Chris@1397
|
190 rtapi_ = new MidiOutDummy( clientName );
|
Chris@1397
|
191 #endif
|
Chris@1397
|
192 }
|
Chris@1397
|
193
|
Chris@1397
|
194 RtMidiOut :: RtMidiOut( RtMidi::Api api, const std::string clientName )
|
Chris@1397
|
195 {
|
Chris@1397
|
196 if ( api != UNSPECIFIED ) {
|
Chris@1397
|
197 // Attempt to open the specified API.
|
Chris@1397
|
198 openMidiApi( api, clientName );
|
Chris@1397
|
199 if ( rtapi_ ) return;
|
Chris@1397
|
200
|
Chris@1397
|
201 // No compiled support for specified API value. Issue a warning
|
Chris@1397
|
202 // and continue as if no API was specified.
|
Chris@1397
|
203 std::cerr << "\nRtMidiOut: no compiled support for specified API argument!\n\n" << std::endl;
|
Chris@1397
|
204 }
|
Chris@1397
|
205
|
Chris@1397
|
206 // Iterate through the compiled APIs and return as soon as we find
|
Chris@1397
|
207 // one with at least one port or we reach the end of the list.
|
Chris@1397
|
208 std::vector< RtMidi::Api > apis;
|
Chris@1397
|
209 getCompiledApi( apis );
|
Chris@1397
|
210 for ( unsigned int i=0; i<apis.size(); i++ ) {
|
Chris@1397
|
211 openMidiApi( apis[i], clientName );
|
Chris@1397
|
212 if ( rtapi_->getPortCount() ) break;
|
Chris@1397
|
213 }
|
Chris@1397
|
214
|
Chris@1397
|
215 if ( rtapi_ ) return;
|
Chris@1397
|
216
|
Chris@1397
|
217 // It should not be possible to get here because the preprocessor
|
Chris@1397
|
218 // definition __RTMIDI_DUMMY__ is automatically defined if no
|
Chris@1397
|
219 // API-specific definitions are passed to the compiler. But just in
|
Chris@1397
|
220 // case something weird happens, we'll thrown an error.
|
Chris@1397
|
221 std::string errorText = "RtMidiOut: no compiled API support found ... critical error!!";
|
Chris@1397
|
222 throw( RtMidiError( errorText, RtMidiError::UNSPECIFIED ) );
|
Chris@1397
|
223 }
|
Chris@1397
|
224
|
Chris@1397
|
225 RtMidiOut :: ~RtMidiOut() throw()
|
Chris@1397
|
226 {
|
Chris@1397
|
227 }
|
Chris@1397
|
228
|
Chris@1397
|
229 //*********************************************************************//
|
Chris@1397
|
230 // Common MidiApi Definitions
|
Chris@1397
|
231 //*********************************************************************//
|
Chris@1397
|
232
|
Chris@1397
|
233 MidiApi :: MidiApi( void )
|
Chris@1397
|
234 : apiData_( 0 ), connected_( false ), errorCallback_(0), errorCallbackUserData_(0)
|
Chris@1397
|
235 {
|
Chris@1397
|
236 }
|
Chris@1397
|
237
|
Chris@1397
|
238 MidiApi :: ~MidiApi( void )
|
Chris@1397
|
239 {
|
Chris@1397
|
240 }
|
Chris@1397
|
241
|
Chris@1397
|
242 void MidiApi :: setErrorCallback( RtMidiErrorCallback errorCallback, void *userData = 0 )
|
Chris@1397
|
243 {
|
Chris@1397
|
244 errorCallback_ = errorCallback;
|
Chris@1397
|
245 errorCallbackUserData_ = userData;
|
Chris@1397
|
246 }
|
Chris@1397
|
247
|
Chris@1397
|
248 void MidiApi :: error( RtMidiError::Type type, std::string errorString )
|
Chris@1397
|
249 {
|
Chris@1397
|
250 if ( errorCallback_ ) {
|
Chris@1397
|
251
|
Chris@1397
|
252 if ( firstErrorOccurred_ )
|
Chris@1397
|
253 return;
|
Chris@1397
|
254
|
Chris@1397
|
255 firstErrorOccurred_ = true;
|
Chris@1397
|
256 const std::string errorMessage = errorString;
|
Chris@1397
|
257
|
Chris@1397
|
258 errorCallback_( type, errorMessage, errorCallbackUserData_);
|
Chris@1397
|
259 firstErrorOccurred_ = false;
|
Chris@1397
|
260 return;
|
Chris@1397
|
261 }
|
Chris@1397
|
262
|
Chris@1397
|
263 if ( type == RtMidiError::WARNING ) {
|
Chris@1397
|
264 std::cerr << '\n' << errorString << "\n\n";
|
Chris@1397
|
265 }
|
Chris@1397
|
266 else if ( type == RtMidiError::DEBUG_WARNING ) {
|
Chris@559
|
267 #if defined(__RTMIDI_DEBUG__)
|
Chris@1397
|
268 std::cerr << '\n' << errorString << "\n\n";
|
Chris@559
|
269 #endif
|
Chris@559
|
270 }
|
Chris@559
|
271 else {
|
Chris@1397
|
272 std::cerr << '\n' << errorString << "\n\n";
|
Chris@1397
|
273 throw RtMidiError( errorString, type );
|
Chris@559
|
274 }
|
Chris@559
|
275 }
|
Chris@559
|
276
|
Chris@559
|
277 //*********************************************************************//
|
Chris@1397
|
278 // Common MidiInApi Definitions
|
Chris@559
|
279 //*********************************************************************//
|
Chris@559
|
280
|
Chris@1397
|
281 MidiInApi :: MidiInApi( unsigned int queueSizeLimit )
|
Chris@1397
|
282 : MidiApi()
|
Chris@559
|
283 {
|
Chris@1397
|
284 // Allocate the MIDI queue.
|
Chris@1397
|
285 inputData_.queue.ringSize = queueSizeLimit;
|
Chris@1397
|
286 if ( inputData_.queue.ringSize > 0 )
|
Chris@1397
|
287 inputData_.queue.ring = new MidiMessage[ inputData_.queue.ringSize ];
|
Chris@559
|
288 }
|
Chris@559
|
289
|
Chris@1397
|
290 MidiInApi :: ~MidiInApi( void )
|
Chris@1397
|
291 {
|
Chris@1397
|
292 // Delete the MIDI queue.
|
Chris@1397
|
293 if ( inputData_.queue.ringSize > 0 ) delete [] inputData_.queue.ring;
|
Chris@1397
|
294 }
|
Chris@1397
|
295
|
Chris@1397
|
296 void MidiInApi :: setCallback( RtMidiIn::RtMidiCallback callback, void *userData )
|
Chris@559
|
297 {
|
Chris@559
|
298 if ( inputData_.usingCallback ) {
|
Chris@1397
|
299 errorString_ = "MidiInApi::setCallback: a callback function is already set!";
|
Chris@1397
|
300 error( RtMidiError::WARNING, errorString_ );
|
Chris@559
|
301 return;
|
Chris@559
|
302 }
|
Chris@559
|
303
|
Chris@559
|
304 if ( !callback ) {
|
Chris@559
|
305 errorString_ = "RtMidiIn::setCallback: callback function value is invalid!";
|
Chris@1397
|
306 error( RtMidiError::WARNING, errorString_ );
|
Chris@559
|
307 return;
|
Chris@559
|
308 }
|
Chris@559
|
309
|
Chris@1397
|
310 inputData_.userCallback = callback;
|
Chris@559
|
311 inputData_.userData = userData;
|
Chris@559
|
312 inputData_.usingCallback = true;
|
Chris@559
|
313 }
|
Chris@559
|
314
|
Chris@1397
|
315 void MidiInApi :: cancelCallback()
|
Chris@559
|
316 {
|
Chris@559
|
317 if ( !inputData_.usingCallback ) {
|
Chris@559
|
318 errorString_ = "RtMidiIn::cancelCallback: no callback function was set!";
|
Chris@1397
|
319 error( RtMidiError::WARNING, errorString_ );
|
Chris@559
|
320 return;
|
Chris@559
|
321 }
|
Chris@559
|
322
|
Chris@559
|
323 inputData_.userCallback = 0;
|
Chris@559
|
324 inputData_.userData = 0;
|
Chris@559
|
325 inputData_.usingCallback = false;
|
Chris@559
|
326 }
|
Chris@559
|
327
|
Chris@1397
|
328 void MidiInApi :: ignoreTypes( bool midiSysex, bool midiTime, bool midiSense )
|
Chris@559
|
329 {
|
Chris@559
|
330 inputData_.ignoreFlags = 0;
|
Chris@559
|
331 if ( midiSysex ) inputData_.ignoreFlags = 0x01;
|
Chris@559
|
332 if ( midiTime ) inputData_.ignoreFlags |= 0x02;
|
Chris@559
|
333 if ( midiSense ) inputData_.ignoreFlags |= 0x04;
|
Chris@559
|
334 }
|
Chris@559
|
335
|
Chris@1397
|
336 double MidiInApi :: getMessage( std::vector<unsigned char> *message )
|
Chris@559
|
337 {
|
Chris@559
|
338 message->clear();
|
Chris@559
|
339
|
Chris@559
|
340 if ( inputData_.usingCallback ) {
|
Chris@559
|
341 errorString_ = "RtMidiIn::getNextMessage: a user callback is currently set for this port.";
|
Chris@1397
|
342 error( RtMidiError::WARNING, errorString_ );
|
Chris@559
|
343 return 0.0;
|
Chris@559
|
344 }
|
Chris@559
|
345
|
Chris@1397
|
346 if ( inputData_.queue.size == 0 ) return 0.0;
|
Chris@559
|
347
|
Chris@559
|
348 // Copy queued message to the vector pointer argument and then "pop" it.
|
Chris@1397
|
349 std::vector<unsigned char> *bytes = &(inputData_.queue.ring[inputData_.queue.front].bytes);
|
Chris@559
|
350 message->assign( bytes->begin(), bytes->end() );
|
Chris@1397
|
351 double deltaTime = inputData_.queue.ring[inputData_.queue.front].timeStamp;
|
Chris@1397
|
352 inputData_.queue.size--;
|
Chris@1397
|
353 inputData_.queue.front++;
|
Chris@1397
|
354 if ( inputData_.queue.front == inputData_.queue.ringSize )
|
Chris@1397
|
355 inputData_.queue.front = 0;
|
Chris@559
|
356
|
Chris@559
|
357 return deltaTime;
|
Chris@559
|
358 }
|
Chris@559
|
359
|
Chris@559
|
360 //*********************************************************************//
|
Chris@1397
|
361 // Common MidiOutApi Definitions
|
Chris@559
|
362 //*********************************************************************//
|
Chris@559
|
363
|
Chris@1397
|
364 MidiOutApi :: MidiOutApi( void )
|
Chris@1397
|
365 : MidiApi()
|
Chris@559
|
366 {
|
Chris@559
|
367 }
|
Chris@559
|
368
|
Chris@1397
|
369 MidiOutApi :: ~MidiOutApi( void )
|
Chris@1397
|
370 {
|
Chris@1397
|
371 }
|
Chris@1397
|
372
|
Chris@1397
|
373 // *************************************************** //
|
Chris@1397
|
374 //
|
Chris@1397
|
375 // OS/API-specific methods.
|
Chris@1397
|
376 //
|
Chris@1397
|
377 // *************************************************** //
|
Chris@559
|
378
|
Chris@559
|
379 #if defined(__MACOSX_CORE__)
|
Chris@559
|
380
|
Chris@559
|
381 // The CoreMIDI API is based on the use of a callback function for
|
Chris@559
|
382 // MIDI input. We convert the system specific time stamps to delta
|
Chris@559
|
383 // time values.
|
Chris@559
|
384
|
Chris@559
|
385 // OS-X CoreMIDI header files.
|
Chris@559
|
386 #include <CoreMIDI/CoreMIDI.h>
|
Chris@559
|
387 #include <CoreAudio/HostTime.h>
|
Chris@1397
|
388 #include <CoreServices/CoreServices.h>
|
Chris@559
|
389
|
Chris@559
|
390 // A structure to hold variables related to the CoreMIDI API
|
Chris@559
|
391 // implementation.
|
Chris@559
|
392 struct CoreMidiData {
|
Chris@559
|
393 MIDIClientRef client;
|
Chris@559
|
394 MIDIPortRef port;
|
Chris@559
|
395 MIDIEndpointRef endpoint;
|
Chris@559
|
396 MIDIEndpointRef destinationId;
|
Chris@559
|
397 unsigned long long lastTime;
|
Chris@1397
|
398 MIDISysexSendRequest sysexreq;
|
Chris@559
|
399 };
|
Chris@559
|
400
|
Chris@559
|
401 //*********************************************************************//
|
Chris@559
|
402 // API: OS-X
|
Chris@1397
|
403 // Class Definitions: MidiInCore
|
Chris@559
|
404 //*********************************************************************//
|
Chris@559
|
405
|
Chris@1397
|
406 static void midiInputCallback( const MIDIPacketList *list, void *procRef, void */*srcRef*/ )
|
Chris@559
|
407 {
|
Chris@1397
|
408 MidiInApi::RtMidiInData *data = static_cast<MidiInApi::RtMidiInData *> (procRef);
|
Chris@559
|
409 CoreMidiData *apiData = static_cast<CoreMidiData *> (data->apiData);
|
Chris@559
|
410
|
Chris@559
|
411 unsigned char status;
|
Chris@559
|
412 unsigned short nBytes, iByte, size;
|
Chris@559
|
413 unsigned long long time;
|
Chris@565
|
414
|
Chris@565
|
415 bool& continueSysex = data->continueSysex;
|
Chris@1397
|
416 MidiInApi::MidiMessage& message = data->message;
|
Chris@565
|
417
|
Chris@559
|
418 const MIDIPacket *packet = &list->packet[0];
|
Chris@559
|
419 for ( unsigned int i=0; i<list->numPackets; ++i ) {
|
Chris@559
|
420
|
Chris@559
|
421 // My interpretation of the CoreMIDI documentation: all message
|
Chris@559
|
422 // types, except sysex, are complete within a packet and there may
|
Chris@559
|
423 // be several of them in a single packet. Sysex messages can be
|
Chris@565
|
424 // broken across multiple packets and PacketLists but are bundled
|
Chris@565
|
425 // alone within each packet (these packets do not contain other
|
Chris@565
|
426 // message types). If sysex messages are split across multiple
|
Chris@565
|
427 // MIDIPacketLists, they must be handled by multiple calls to this
|
Chris@565
|
428 // function.
|
Chris@559
|
429
|
Chris@559
|
430 nBytes = packet->length;
|
Chris@559
|
431 if ( nBytes == 0 ) continue;
|
Chris@559
|
432
|
Chris@559
|
433 // Calculate time stamp.
|
Chris@1397
|
434
|
Chris@1397
|
435 if ( data->firstMessage ) {
|
Chris@1397
|
436 message.timeStamp = 0.0;
|
Chris@559
|
437 data->firstMessage = false;
|
Chris@1397
|
438 }
|
Chris@559
|
439 else {
|
Chris@559
|
440 time = packet->timeStamp;
|
Chris@1397
|
441 if ( time == 0 ) { // this happens when receiving asynchronous sysex messages
|
Chris@1397
|
442 time = AudioGetCurrentHostTime();
|
Chris@1397
|
443 }
|
Chris@559
|
444 time -= apiData->lastTime;
|
Chris@559
|
445 time = AudioConvertHostTimeToNanos( time );
|
Chris@1397
|
446 if ( !continueSysex )
|
Chris@1397
|
447 message.timeStamp = time * 0.000000001;
|
Chris@559
|
448 }
|
Chris@559
|
449 apiData->lastTime = packet->timeStamp;
|
Chris@1397
|
450 if ( apiData->lastTime == 0 ) { // this happens when receiving asynchronous sysex messages
|
Chris@1397
|
451 apiData->lastTime = AudioGetCurrentHostTime();
|
Chris@1397
|
452 }
|
Chris@1397
|
453 //std::cout << "TimeStamp = " << packet->timeStamp << std::endl;
|
Chris@559
|
454
|
Chris@559
|
455 iByte = 0;
|
Chris@559
|
456 if ( continueSysex ) {
|
Chris@559
|
457 // We have a continuing, segmented sysex message.
|
Chris@565
|
458 if ( !( data->ignoreFlags & 0x01 ) ) {
|
Chris@559
|
459 // If we're not ignoring sysex messages, copy the entire packet.
|
Chris@1397
|
460 for ( unsigned int j=0; j<nBytes; ++j )
|
Chris@559
|
461 message.bytes.push_back( packet->data[j] );
|
Chris@559
|
462 }
|
Chris@565
|
463 continueSysex = packet->data[nBytes-1] != 0xF7;
|
Chris@565
|
464
|
Chris@1397
|
465 if ( !( data->ignoreFlags & 0x01 ) && !continueSysex ) {
|
Chris@559
|
466 // If not a continuing sysex message, invoke the user callback function or queue the message.
|
Chris@1397
|
467 if ( data->usingCallback ) {
|
Chris@559
|
468 RtMidiIn::RtMidiCallback callback = (RtMidiIn::RtMidiCallback) data->userCallback;
|
Chris@559
|
469 callback( message.timeStamp, &message.bytes, data->userData );
|
Chris@559
|
470 }
|
Chris@559
|
471 else {
|
Chris@559
|
472 // As long as we haven't reached our queue size limit, push the message.
|
Chris@1397
|
473 if ( data->queue.size < data->queue.ringSize ) {
|
Chris@1397
|
474 data->queue.ring[data->queue.back++] = message;
|
Chris@1397
|
475 if ( data->queue.back == data->queue.ringSize )
|
Chris@1397
|
476 data->queue.back = 0;
|
Chris@1397
|
477 data->queue.size++;
|
Chris@1397
|
478 }
|
Chris@559
|
479 else
|
Chris@1397
|
480 std::cerr << "\nMidiInCore: message queue limit reached!!\n\n";
|
Chris@559
|
481 }
|
Chris@559
|
482 message.bytes.clear();
|
Chris@559
|
483 }
|
Chris@559
|
484 }
|
Chris@559
|
485 else {
|
Chris@559
|
486 while ( iByte < nBytes ) {
|
Chris@559
|
487 size = 0;
|
Chris@559
|
488 // We are expecting that the next byte in the packet is a status byte.
|
Chris@559
|
489 status = packet->data[iByte];
|
Chris@559
|
490 if ( !(status & 0x80) ) break;
|
Chris@559
|
491 // Determine the number of bytes in the MIDI message.
|
Chris@559
|
492 if ( status < 0xC0 ) size = 3;
|
Chris@559
|
493 else if ( status < 0xE0 ) size = 2;
|
Chris@559
|
494 else if ( status < 0xF0 ) size = 3;
|
Chris@559
|
495 else if ( status == 0xF0 ) {
|
Chris@559
|
496 // A MIDI sysex
|
Chris@559
|
497 if ( data->ignoreFlags & 0x01 ) {
|
Chris@559
|
498 size = 0;
|
Chris@559
|
499 iByte = nBytes;
|
Chris@559
|
500 }
|
Chris@559
|
501 else size = nBytes - iByte;
|
Chris@1397
|
502 continueSysex = packet->data[nBytes-1] != 0xF7;
|
Chris@559
|
503 }
|
Chris@1397
|
504 else if ( status == 0xF1 ) {
|
Chris@1397
|
505 // A MIDI time code message
|
Chris@1397
|
506 if ( data->ignoreFlags & 0x02 ) {
|
Chris@559
|
507 size = 0;
|
Chris@1397
|
508 iByte += 2;
|
Chris@1397
|
509 }
|
Chris@1397
|
510 else size = 2;
|
Chris@559
|
511 }
|
Chris@1397
|
512 else if ( status == 0xF2 ) size = 3;
|
Chris@559
|
513 else if ( status == 0xF3 ) size = 2;
|
Chris@1397
|
514 else if ( status == 0xF8 && ( data->ignoreFlags & 0x02 ) ) {
|
Chris@1397
|
515 // A MIDI timing tick message and we're ignoring it.
|
Chris@1397
|
516 size = 0;
|
Chris@1397
|
517 iByte += 1;
|
Chris@559
|
518 }
|
Chris@1397
|
519 else if ( status == 0xFE && ( data->ignoreFlags & 0x04 ) ) {
|
Chris@559
|
520 // A MIDI active sensing message and we're ignoring it.
|
Chris@559
|
521 size = 0;
|
Chris@559
|
522 iByte += 1;
|
Chris@559
|
523 }
|
Chris@559
|
524 else size = 1;
|
Chris@559
|
525
|
Chris@559
|
526 // Copy the MIDI data to our vector.
|
Chris@559
|
527 if ( size ) {
|
Chris@559
|
528 message.bytes.assign( &packet->data[iByte], &packet->data[iByte+size] );
|
Chris@559
|
529 if ( !continueSysex ) {
|
Chris@559
|
530 // If not a continuing sysex message, invoke the user callback function or queue the message.
|
Chris@559
|
531 if ( data->usingCallback ) {
|
Chris@559
|
532 RtMidiIn::RtMidiCallback callback = (RtMidiIn::RtMidiCallback) data->userCallback;
|
Chris@559
|
533 callback( message.timeStamp, &message.bytes, data->userData );
|
Chris@559
|
534 }
|
Chris@559
|
535 else {
|
Chris@559
|
536 // As long as we haven't reached our queue size limit, push the message.
|
Chris@1397
|
537 if ( data->queue.size < data->queue.ringSize ) {
|
Chris@1397
|
538 data->queue.ring[data->queue.back++] = message;
|
Chris@1397
|
539 if ( data->queue.back == data->queue.ringSize )
|
Chris@1397
|
540 data->queue.back = 0;
|
Chris@1397
|
541 data->queue.size++;
|
Chris@1397
|
542 }
|
Chris@559
|
543 else
|
Chris@1397
|
544 std::cerr << "\nMidiInCore: message queue limit reached!!\n\n";
|
Chris@559
|
545 }
|
Chris@559
|
546 message.bytes.clear();
|
Chris@559
|
547 }
|
Chris@559
|
548 iByte += size;
|
Chris@559
|
549 }
|
Chris@559
|
550 }
|
Chris@559
|
551 }
|
Chris@559
|
552 packet = MIDIPacketNext(packet);
|
Chris@559
|
553 }
|
Chris@559
|
554 }
|
Chris@559
|
555
|
Chris@1397
|
556 MidiInCore :: MidiInCore( const std::string clientName, unsigned int queueSizeLimit ) : MidiInApi( queueSizeLimit )
|
Chris@1397
|
557 {
|
Chris@1397
|
558 initialize( clientName );
|
Chris@1397
|
559 }
|
Chris@1397
|
560
|
Chris@1397
|
561 MidiInCore :: ~MidiInCore( void )
|
Chris@1397
|
562 {
|
Chris@1397
|
563 // Close a connection if it exists.
|
Chris@1397
|
564 closePort();
|
Chris@1397
|
565
|
Chris@1397
|
566 // Cleanup.
|
Chris@1397
|
567 CoreMidiData *data = static_cast<CoreMidiData *> (apiData_);
|
Chris@1397
|
568 MIDIClientDispose( data->client );
|
Chris@1397
|
569 if ( data->endpoint ) MIDIEndpointDispose( data->endpoint );
|
Chris@1397
|
570 delete data;
|
Chris@1397
|
571 }
|
Chris@1397
|
572
|
Chris@1397
|
573 void MidiInCore :: initialize( const std::string& clientName )
|
Chris@559
|
574 {
|
Chris@559
|
575 // Set up our client.
|
Chris@559
|
576 MIDIClientRef client;
|
Chris@1397
|
577 CFStringRef name = CFStringCreateWithCString( NULL, clientName.c_str(), kCFStringEncodingASCII );
|
Chris@1397
|
578 OSStatus result = MIDIClientCreate(name, NULL, NULL, &client );
|
Chris@559
|
579 if ( result != noErr ) {
|
Chris@1397
|
580 errorString_ = "MidiInCore::initialize: error creating OS-X MIDI client object.";
|
Chris@1397
|
581 error( RtMidiError::DRIVER_ERROR, errorString_ );
|
Chris@1397
|
582 return;
|
Chris@559
|
583 }
|
Chris@559
|
584
|
Chris@559
|
585 // Save our api-specific connection information.
|
Chris@559
|
586 CoreMidiData *data = (CoreMidiData *) new CoreMidiData;
|
Chris@559
|
587 data->client = client;
|
Chris@559
|
588 data->endpoint = 0;
|
Chris@559
|
589 apiData_ = (void *) data;
|
Chris@559
|
590 inputData_.apiData = (void *) data;
|
Chris@1397
|
591 CFRelease(name);
|
Chris@559
|
592 }
|
Chris@559
|
593
|
Chris@1397
|
594 void MidiInCore :: openPort( unsigned int portNumber, const std::string portName )
|
Chris@559
|
595 {
|
Chris@559
|
596 if ( connected_ ) {
|
Chris@1397
|
597 errorString_ = "MidiInCore::openPort: a valid connection already exists!";
|
Chris@1397
|
598 error( RtMidiError::WARNING, errorString_ );
|
Chris@559
|
599 return;
|
Chris@559
|
600 }
|
Chris@559
|
601
|
Chris@1397
|
602 CFRunLoopRunInMode( kCFRunLoopDefaultMode, 0, false );
|
Chris@559
|
603 unsigned int nSrc = MIDIGetNumberOfSources();
|
Chris@559
|
604 if (nSrc < 1) {
|
Chris@1397
|
605 errorString_ = "MidiInCore::openPort: no MIDI input sources found!";
|
Chris@1397
|
606 error( RtMidiError::NO_DEVICES_FOUND, errorString_ );
|
Chris@1397
|
607 return;
|
Chris@559
|
608 }
|
Chris@559
|
609
|
Chris@559
|
610 if ( portNumber >= nSrc ) {
|
Chris@1397
|
611 std::ostringstream ost;
|
Chris@1397
|
612 ost << "MidiInCore::openPort: the 'portNumber' argument (" << portNumber << ") is invalid.";
|
Chris@559
|
613 errorString_ = ost.str();
|
Chris@1397
|
614 error( RtMidiError::INVALID_PARAMETER, errorString_ );
|
Chris@1397
|
615 return;
|
Chris@559
|
616 }
|
Chris@559
|
617
|
Chris@559
|
618 MIDIPortRef port;
|
Chris@559
|
619 CoreMidiData *data = static_cast<CoreMidiData *> (apiData_);
|
Chris@565
|
620 OSStatus result = MIDIInputPortCreate( data->client,
|
Chris@565
|
621 CFStringCreateWithCString( NULL, portName.c_str(), kCFStringEncodingASCII ),
|
Chris@565
|
622 midiInputCallback, (void *)&inputData_, &port );
|
Chris@559
|
623 if ( result != noErr ) {
|
Chris@559
|
624 MIDIClientDispose( data->client );
|
Chris@1397
|
625 errorString_ = "MidiInCore::openPort: error creating OS-X MIDI input port.";
|
Chris@1397
|
626 error( RtMidiError::DRIVER_ERROR, errorString_ );
|
Chris@1397
|
627 return;
|
Chris@559
|
628 }
|
Chris@559
|
629
|
Chris@559
|
630 // Get the desired input source identifier.
|
Chris@559
|
631 MIDIEndpointRef endpoint = MIDIGetSource( portNumber );
|
Chris@1397
|
632 if ( endpoint == 0 ) {
|
Chris@559
|
633 MIDIPortDispose( port );
|
Chris@559
|
634 MIDIClientDispose( data->client );
|
Chris@1397
|
635 errorString_ = "MidiInCore::openPort: error getting MIDI input source reference.";
|
Chris@1397
|
636 error( RtMidiError::DRIVER_ERROR, errorString_ );
|
Chris@1397
|
637 return;
|
Chris@559
|
638 }
|
Chris@559
|
639
|
Chris@559
|
640 // Make the connection.
|
Chris@559
|
641 result = MIDIPortConnectSource( port, endpoint, NULL );
|
Chris@559
|
642 if ( result != noErr ) {
|
Chris@559
|
643 MIDIPortDispose( port );
|
Chris@559
|
644 MIDIClientDispose( data->client );
|
Chris@1397
|
645 errorString_ = "MidiInCore::openPort: error connecting OS-X MIDI input port.";
|
Chris@1397
|
646 error( RtMidiError::DRIVER_ERROR, errorString_ );
|
Chris@1397
|
647 return;
|
Chris@559
|
648 }
|
Chris@559
|
649
|
Chris@559
|
650 // Save our api-specific port information.
|
Chris@559
|
651 data->port = port;
|
Chris@559
|
652
|
Chris@559
|
653 connected_ = true;
|
Chris@559
|
654 }
|
Chris@559
|
655
|
Chris@1397
|
656 void MidiInCore :: openVirtualPort( const std::string portName )
|
Chris@559
|
657 {
|
Chris@559
|
658 CoreMidiData *data = static_cast<CoreMidiData *> (apiData_);
|
Chris@559
|
659
|
Chris@559
|
660 // Create a virtual MIDI input destination.
|
Chris@559
|
661 MIDIEndpointRef endpoint;
|
Chris@559
|
662 OSStatus result = MIDIDestinationCreate( data->client,
|
Chris@559
|
663 CFStringCreateWithCString( NULL, portName.c_str(), kCFStringEncodingASCII ),
|
Chris@559
|
664 midiInputCallback, (void *)&inputData_, &endpoint );
|
Chris@559
|
665 if ( result != noErr ) {
|
Chris@1397
|
666 errorString_ = "MidiInCore::openVirtualPort: error creating virtual OS-X MIDI destination.";
|
Chris@1397
|
667 error( RtMidiError::DRIVER_ERROR, errorString_ );
|
Chris@1397
|
668 return;
|
Chris@559
|
669 }
|
Chris@559
|
670
|
Chris@559
|
671 // Save our api-specific connection information.
|
Chris@559
|
672 data->endpoint = endpoint;
|
Chris@559
|
673 }
|
Chris@559
|
674
|
Chris@1397
|
675 void MidiInCore :: closePort( void )
|
Chris@559
|
676 {
|
Chris@1397
|
677 CoreMidiData *data = static_cast<CoreMidiData *> (apiData_);
|
Chris@1397
|
678
|
Chris@1397
|
679 if ( data->endpoint ) {
|
Chris@1397
|
680 MIDIEndpointDispose( data->endpoint );
|
Chris@1397
|
681 }
|
Chris@1397
|
682
|
Chris@1397
|
683 if ( data->port ) {
|
Chris@559
|
684 MIDIPortDispose( data->port );
|
Chris@559
|
685 }
|
Chris@1397
|
686
|
Chris@1397
|
687 connected_ = false;
|
Chris@559
|
688 }
|
Chris@559
|
689
|
Chris@1397
|
690 unsigned int MidiInCore :: getPortCount()
|
Chris@1397
|
691 {
|
Chris@1397
|
692 CFRunLoopRunInMode( kCFRunLoopDefaultMode, 0, false );
|
Chris@1397
|
693 return MIDIGetNumberOfSources();
|
Chris@1397
|
694 }
|
Chris@1397
|
695
|
Chris@1397
|
696 // This function was submitted by Douglas Casey Tucker and apparently
|
Chris@1397
|
697 // derived largely from PortMidi.
|
Chris@1397
|
698 CFStringRef EndpointName( MIDIEndpointRef endpoint, bool isExternal )
|
Chris@1397
|
699 {
|
Chris@1397
|
700 CFMutableStringRef result = CFStringCreateMutable( NULL, 0 );
|
Chris@1397
|
701 CFStringRef str;
|
Chris@1397
|
702
|
Chris@1397
|
703 // Begin with the endpoint's name.
|
Chris@1397
|
704 str = NULL;
|
Chris@1397
|
705 MIDIObjectGetStringProperty( endpoint, kMIDIPropertyName, &str );
|
Chris@1397
|
706 if ( str != NULL ) {
|
Chris@1397
|
707 CFStringAppend( result, str );
|
Chris@1397
|
708 CFRelease( str );
|
Chris@1397
|
709 }
|
Chris@1397
|
710
|
Chris@1397
|
711 MIDIEntityRef entity = 0;
|
Chris@1397
|
712 MIDIEndpointGetEntity( endpoint, &entity );
|
Chris@1397
|
713 if ( entity == 0 )
|
Chris@1397
|
714 // probably virtual
|
Chris@1397
|
715 return result;
|
Chris@1397
|
716
|
Chris@1397
|
717 if ( CFStringGetLength( result ) == 0 ) {
|
Chris@1397
|
718 // endpoint name has zero length -- try the entity
|
Chris@1397
|
719 str = NULL;
|
Chris@1397
|
720 MIDIObjectGetStringProperty( entity, kMIDIPropertyName, &str );
|
Chris@1397
|
721 if ( str != NULL ) {
|
Chris@1397
|
722 CFStringAppend( result, str );
|
Chris@1397
|
723 CFRelease( str );
|
Chris@1397
|
724 }
|
Chris@1397
|
725 }
|
Chris@1397
|
726 // now consider the device's name
|
Chris@1397
|
727 MIDIDeviceRef device = 0;
|
Chris@1397
|
728 MIDIEntityGetDevice( entity, &device );
|
Chris@1397
|
729 if ( device == 0 )
|
Chris@1397
|
730 return result;
|
Chris@1397
|
731
|
Chris@1397
|
732 str = NULL;
|
Chris@1397
|
733 MIDIObjectGetStringProperty( device, kMIDIPropertyName, &str );
|
Chris@1397
|
734 if ( CFStringGetLength( result ) == 0 ) {
|
Chris@1397
|
735 CFRelease( result );
|
Chris@1397
|
736 return str;
|
Chris@1397
|
737 }
|
Chris@1397
|
738 if ( str != NULL ) {
|
Chris@1397
|
739 // if an external device has only one entity, throw away
|
Chris@1397
|
740 // the endpoint name and just use the device name
|
Chris@1397
|
741 if ( isExternal && MIDIDeviceGetNumberOfEntities( device ) < 2 ) {
|
Chris@1397
|
742 CFRelease( result );
|
Chris@1397
|
743 return str;
|
Chris@1397
|
744 } else {
|
Chris@1397
|
745 if ( CFStringGetLength( str ) == 0 ) {
|
Chris@1397
|
746 CFRelease( str );
|
Chris@1397
|
747 return result;
|
Chris@1397
|
748 }
|
Chris@1397
|
749 // does the entity name already start with the device name?
|
Chris@1397
|
750 // (some drivers do this though they shouldn't)
|
Chris@1397
|
751 // if so, do not prepend
|
Chris@1397
|
752 if ( CFStringCompareWithOptions( result, /* endpoint name */
|
Chris@1397
|
753 str /* device name */,
|
Chris@1397
|
754 CFRangeMake(0, CFStringGetLength( str ) ), 0 ) != kCFCompareEqualTo ) {
|
Chris@1397
|
755 // prepend the device name to the entity name
|
Chris@1397
|
756 if ( CFStringGetLength( result ) > 0 )
|
Chris@1397
|
757 CFStringInsert( result, 0, CFSTR(" ") );
|
Chris@1397
|
758 CFStringInsert( result, 0, str );
|
Chris@1397
|
759 }
|
Chris@1397
|
760 CFRelease( str );
|
Chris@1397
|
761 }
|
Chris@1397
|
762 }
|
Chris@1397
|
763 return result;
|
Chris@1397
|
764 }
|
Chris@1397
|
765
|
Chris@1397
|
766 // This function was submitted by Douglas Casey Tucker and apparently
|
Chris@1397
|
767 // derived largely from PortMidi.
|
Chris@1397
|
768 static CFStringRef ConnectedEndpointName( MIDIEndpointRef endpoint )
|
Chris@1397
|
769 {
|
Chris@1397
|
770 CFMutableStringRef result = CFStringCreateMutable( NULL, 0 );
|
Chris@1397
|
771 CFStringRef str;
|
Chris@1397
|
772 OSStatus err;
|
Chris@1397
|
773 int i;
|
Chris@1397
|
774
|
Chris@1397
|
775 // Does the endpoint have connections?
|
Chris@1397
|
776 CFDataRef connections = NULL;
|
Chris@1397
|
777 int nConnected = 0;
|
Chris@1397
|
778 bool anyStrings = false;
|
Chris@1397
|
779 err = MIDIObjectGetDataProperty( endpoint, kMIDIPropertyConnectionUniqueID, &connections );
|
Chris@1397
|
780 if ( connections != NULL ) {
|
Chris@1397
|
781 // It has connections, follow them
|
Chris@1397
|
782 // Concatenate the names of all connected devices
|
Chris@1397
|
783 nConnected = CFDataGetLength( connections ) / sizeof(MIDIUniqueID);
|
Chris@1397
|
784 if ( nConnected ) {
|
Chris@1397
|
785 const SInt32 *pid = (const SInt32 *)(CFDataGetBytePtr(connections));
|
Chris@1397
|
786 for ( i=0; i<nConnected; ++i, ++pid ) {
|
Chris@1397
|
787 MIDIUniqueID id = EndianS32_BtoN( *pid );
|
Chris@1397
|
788 MIDIObjectRef connObject;
|
Chris@1397
|
789 MIDIObjectType connObjectType;
|
Chris@1397
|
790 err = MIDIObjectFindByUniqueID( id, &connObject, &connObjectType );
|
Chris@1397
|
791 if ( err == noErr ) {
|
Chris@1397
|
792 if ( connObjectType == kMIDIObjectType_ExternalSource ||
|
Chris@1397
|
793 connObjectType == kMIDIObjectType_ExternalDestination ) {
|
Chris@1397
|
794 // Connected to an external device's endpoint (10.3 and later).
|
Chris@1397
|
795 str = EndpointName( (MIDIEndpointRef)(connObject), true );
|
Chris@1397
|
796 } else {
|
Chris@1397
|
797 // Connected to an external device (10.2) (or something else, catch-
|
Chris@1397
|
798 str = NULL;
|
Chris@1397
|
799 MIDIObjectGetStringProperty( connObject, kMIDIPropertyName, &str );
|
Chris@1397
|
800 }
|
Chris@1397
|
801 if ( str != NULL ) {
|
Chris@1397
|
802 if ( anyStrings )
|
Chris@1397
|
803 CFStringAppend( result, CFSTR(", ") );
|
Chris@1397
|
804 else anyStrings = true;
|
Chris@1397
|
805 CFStringAppend( result, str );
|
Chris@1397
|
806 CFRelease( str );
|
Chris@1397
|
807 }
|
Chris@1397
|
808 }
|
Chris@1397
|
809 }
|
Chris@1397
|
810 }
|
Chris@1397
|
811 CFRelease( connections );
|
Chris@1397
|
812 }
|
Chris@1397
|
813 if ( anyStrings )
|
Chris@1397
|
814 return result;
|
Chris@1397
|
815
|
Chris@1397
|
816 CFRelease( result );
|
Chris@1397
|
817
|
Chris@1397
|
818 // Here, either the endpoint had no connections, or we failed to obtain names
|
Chris@1397
|
819 return EndpointName( endpoint, false );
|
Chris@1397
|
820 }
|
Chris@1397
|
821
|
Chris@1397
|
822 std::string MidiInCore :: getPortName( unsigned int portNumber )
|
Chris@1397
|
823 {
|
Chris@1397
|
824 CFStringRef nameRef;
|
Chris@1397
|
825 MIDIEndpointRef portRef;
|
Chris@1397
|
826 char name[128];
|
Chris@1397
|
827
|
Chris@1397
|
828 std::string stringName;
|
Chris@1397
|
829 CFRunLoopRunInMode( kCFRunLoopDefaultMode, 0, false );
|
Chris@1397
|
830 if ( portNumber >= MIDIGetNumberOfSources() ) {
|
Chris@1397
|
831 std::ostringstream ost;
|
Chris@1397
|
832 ost << "MidiInCore::getPortName: the 'portNumber' argument (" << portNumber << ") is invalid.";
|
Chris@1397
|
833 errorString_ = ost.str();
|
Chris@1397
|
834 error( RtMidiError::WARNING, errorString_ );
|
Chris@1397
|
835 return stringName;
|
Chris@1397
|
836 }
|
Chris@1397
|
837
|
Chris@1397
|
838 portRef = MIDIGetSource( portNumber );
|
Chris@1397
|
839 nameRef = ConnectedEndpointName(portRef);
|
Chris@1397
|
840 CFStringGetCString( nameRef, name, sizeof(name), CFStringGetSystemEncoding());
|
Chris@1397
|
841 CFRelease( nameRef );
|
Chris@1397
|
842
|
Chris@1397
|
843 return stringName = name;
|
Chris@1397
|
844 }
|
Chris@1397
|
845
|
Chris@1397
|
846 //*********************************************************************//
|
Chris@1397
|
847 // API: OS-X
|
Chris@1397
|
848 // Class Definitions: MidiOutCore
|
Chris@1397
|
849 //*********************************************************************//
|
Chris@1397
|
850
|
Chris@1397
|
851 MidiOutCore :: MidiOutCore( const std::string clientName ) : MidiOutApi()
|
Chris@1397
|
852 {
|
Chris@1397
|
853 initialize( clientName );
|
Chris@1397
|
854 }
|
Chris@1397
|
855
|
Chris@1397
|
856 MidiOutCore :: ~MidiOutCore( void )
|
Chris@559
|
857 {
|
Chris@559
|
858 // Close a connection if it exists.
|
Chris@559
|
859 closePort();
|
Chris@559
|
860
|
Chris@559
|
861 // Cleanup.
|
Chris@559
|
862 CoreMidiData *data = static_cast<CoreMidiData *> (apiData_);
|
Chris@559
|
863 MIDIClientDispose( data->client );
|
Chris@559
|
864 if ( data->endpoint ) MIDIEndpointDispose( data->endpoint );
|
Chris@559
|
865 delete data;
|
Chris@559
|
866 }
|
Chris@559
|
867
|
Chris@1397
|
868 void MidiOutCore :: initialize( const std::string& clientName )
|
Chris@559
|
869 {
|
Chris@559
|
870 // Set up our client.
|
Chris@559
|
871 MIDIClientRef client;
|
Chris@1397
|
872 CFStringRef name = CFStringCreateWithCString( NULL, clientName.c_str(), kCFStringEncodingASCII );
|
Chris@1397
|
873 OSStatus result = MIDIClientCreate(name, NULL, NULL, &client );
|
Chris@559
|
874 if ( result != noErr ) {
|
Chris@1397
|
875 errorString_ = "MidiOutCore::initialize: error creating OS-X MIDI client object.";
|
Chris@1397
|
876 error( RtMidiError::DRIVER_ERROR, errorString_ );
|
Chris@1397
|
877 return;
|
Chris@559
|
878 }
|
Chris@559
|
879
|
Chris@559
|
880 // Save our api-specific connection information.
|
Chris@559
|
881 CoreMidiData *data = (CoreMidiData *) new CoreMidiData;
|
Chris@559
|
882 data->client = client;
|
Chris@559
|
883 data->endpoint = 0;
|
Chris@559
|
884 apiData_ = (void *) data;
|
Chris@1397
|
885 CFRelease( name );
|
Chris@559
|
886 }
|
Chris@559
|
887
|
Chris@1397
|
888 unsigned int MidiOutCore :: getPortCount()
|
Chris@1397
|
889 {
|
Chris@1397
|
890 CFRunLoopRunInMode( kCFRunLoopDefaultMode, 0, false );
|
Chris@1397
|
891 return MIDIGetNumberOfDestinations();
|
Chris@1397
|
892 }
|
Chris@1397
|
893
|
Chris@1397
|
894 std::string MidiOutCore :: getPortName( unsigned int portNumber )
|
Chris@1397
|
895 {
|
Chris@1397
|
896 CFStringRef nameRef;
|
Chris@1397
|
897 MIDIEndpointRef portRef;
|
Chris@1397
|
898 char name[128];
|
Chris@1397
|
899
|
Chris@1397
|
900 std::string stringName;
|
Chris@1397
|
901 CFRunLoopRunInMode( kCFRunLoopDefaultMode, 0, false );
|
Chris@1397
|
902 if ( portNumber >= MIDIGetNumberOfDestinations() ) {
|
Chris@1397
|
903 std::ostringstream ost;
|
Chris@1397
|
904 ost << "MidiOutCore::getPortName: the 'portNumber' argument (" << portNumber << ") is invalid.";
|
Chris@1397
|
905 errorString_ = ost.str();
|
Chris@1397
|
906 error( RtMidiError::WARNING, errorString_ );
|
Chris@1397
|
907 return stringName;
|
Chris@1397
|
908 }
|
Chris@1397
|
909
|
Chris@1397
|
910 portRef = MIDIGetDestination( portNumber );
|
Chris@1397
|
911 nameRef = ConnectedEndpointName(portRef);
|
Chris@1397
|
912 CFStringGetCString( nameRef, name, sizeof(name), CFStringGetSystemEncoding());
|
Chris@1397
|
913 CFRelease( nameRef );
|
Chris@1397
|
914
|
Chris@1397
|
915 return stringName = name;
|
Chris@1397
|
916 }
|
Chris@1397
|
917
|
Chris@1397
|
918 void MidiOutCore :: openPort( unsigned int portNumber, const std::string portName )
|
Chris@559
|
919 {
|
Chris@559
|
920 if ( connected_ ) {
|
Chris@1397
|
921 errorString_ = "MidiOutCore::openPort: a valid connection already exists!";
|
Chris@1397
|
922 error( RtMidiError::WARNING, errorString_ );
|
Chris@559
|
923 return;
|
Chris@559
|
924 }
|
Chris@559
|
925
|
Chris@1397
|
926 CFRunLoopRunInMode( kCFRunLoopDefaultMode, 0, false );
|
Chris@559
|
927 unsigned int nDest = MIDIGetNumberOfDestinations();
|
Chris@559
|
928 if (nDest < 1) {
|
Chris@1397
|
929 errorString_ = "MidiOutCore::openPort: no MIDI output destinations found!";
|
Chris@1397
|
930 error( RtMidiError::NO_DEVICES_FOUND, errorString_ );
|
Chris@1397
|
931 return;
|
Chris@559
|
932 }
|
Chris@559
|
933
|
Chris@559
|
934 if ( portNumber >= nDest ) {
|
Chris@1397
|
935 std::ostringstream ost;
|
Chris@1397
|
936 ost << "MidiOutCore::openPort: the 'portNumber' argument (" << portNumber << ") is invalid.";
|
Chris@559
|
937 errorString_ = ost.str();
|
Chris@1397
|
938 error( RtMidiError::INVALID_PARAMETER, errorString_ );
|
Chris@1397
|
939 return;
|
Chris@559
|
940 }
|
Chris@559
|
941
|
Chris@559
|
942 MIDIPortRef port;
|
Chris@559
|
943 CoreMidiData *data = static_cast<CoreMidiData *> (apiData_);
|
Chris@565
|
944 OSStatus result = MIDIOutputPortCreate( data->client,
|
Chris@565
|
945 CFStringCreateWithCString( NULL, portName.c_str(), kCFStringEncodingASCII ),
|
Chris@565
|
946 &port );
|
Chris@559
|
947 if ( result != noErr ) {
|
Chris@559
|
948 MIDIClientDispose( data->client );
|
Chris@1397
|
949 errorString_ = "MidiOutCore::openPort: error creating OS-X MIDI output port.";
|
Chris@1397
|
950 error( RtMidiError::DRIVER_ERROR, errorString_ );
|
Chris@1397
|
951 return;
|
Chris@559
|
952 }
|
Chris@559
|
953
|
Chris@559
|
954 // Get the desired output port identifier.
|
Chris@559
|
955 MIDIEndpointRef destination = MIDIGetDestination( portNumber );
|
Chris@1397
|
956 if ( destination == 0 ) {
|
Chris@559
|
957 MIDIPortDispose( port );
|
Chris@559
|
958 MIDIClientDispose( data->client );
|
Chris@1397
|
959 errorString_ = "MidiOutCore::openPort: error getting MIDI output destination reference.";
|
Chris@1397
|
960 error( RtMidiError::DRIVER_ERROR, errorString_ );
|
Chris@1397
|
961 return;
|
Chris@559
|
962 }
|
Chris@559
|
963
|
Chris@559
|
964 // Save our api-specific connection information.
|
Chris@559
|
965 data->port = port;
|
Chris@559
|
966 data->destinationId = destination;
|
Chris@559
|
967 connected_ = true;
|
Chris@559
|
968 }
|
Chris@559
|
969
|
Chris@1397
|
970 void MidiOutCore :: closePort( void )
|
Chris@559
|
971 {
|
Chris@559
|
972 CoreMidiData *data = static_cast<CoreMidiData *> (apiData_);
|
Chris@559
|
973
|
Chris@559
|
974 if ( data->endpoint ) {
|
Chris@1397
|
975 MIDIEndpointDispose( data->endpoint );
|
Chris@1397
|
976 }
|
Chris@1397
|
977
|
Chris@1397
|
978 if ( data->port ) {
|
Chris@1397
|
979 MIDIPortDispose( data->port );
|
Chris@1397
|
980 }
|
Chris@1397
|
981
|
Chris@1397
|
982 connected_ = false;
|
Chris@1397
|
983 }
|
Chris@1397
|
984
|
Chris@1397
|
985 void MidiOutCore :: openVirtualPort( std::string portName )
|
Chris@1397
|
986 {
|
Chris@1397
|
987 CoreMidiData *data = static_cast<CoreMidiData *> (apiData_);
|
Chris@1397
|
988
|
Chris@1397
|
989 if ( data->endpoint ) {
|
Chris@1397
|
990 errorString_ = "MidiOutCore::openVirtualPort: a virtual output port already exists!";
|
Chris@1397
|
991 error( RtMidiError::WARNING, errorString_ );
|
Chris@559
|
992 return;
|
Chris@559
|
993 }
|
Chris@559
|
994
|
Chris@559
|
995 // Create a virtual MIDI output source.
|
Chris@559
|
996 MIDIEndpointRef endpoint;
|
Chris@559
|
997 OSStatus result = MIDISourceCreate( data->client,
|
Chris@559
|
998 CFStringCreateWithCString( NULL, portName.c_str(), kCFStringEncodingASCII ),
|
Chris@559
|
999 &endpoint );
|
Chris@559
|
1000 if ( result != noErr ) {
|
Chris@1397
|
1001 errorString_ = "MidiOutCore::initialize: error creating OS-X virtual MIDI source.";
|
Chris@1397
|
1002 error( RtMidiError::DRIVER_ERROR, errorString_ );
|
Chris@1397
|
1003 return;
|
Chris@559
|
1004 }
|
Chris@559
|
1005
|
Chris@559
|
1006 // Save our api-specific connection information.
|
Chris@559
|
1007 data->endpoint = endpoint;
|
Chris@559
|
1008 }
|
Chris@559
|
1009
|
Chris@1397
|
1010 void MidiOutCore :: sendMessage( std::vector<unsigned char> *message )
|
Chris@559
|
1011 {
|
Chris@1397
|
1012 // We use the MIDISendSysex() function to asynchronously send sysex
|
Chris@1397
|
1013 // messages. Otherwise, we use a single CoreMidi MIDIPacket.
|
Chris@559
|
1014 unsigned int nBytes = message->size();
|
Chris@565
|
1015 if ( nBytes == 0 ) {
|
Chris@1397
|
1016 errorString_ = "MidiOutCore::sendMessage: no data in message argument!";
|
Chris@1397
|
1017 error( RtMidiError::WARNING, errorString_ );
|
Chris@565
|
1018 return;
|
Chris@565
|
1019 }
|
Chris@559
|
1020
|
Chris@1397
|
1021 MIDITimeStamp timeStamp = AudioGetCurrentHostTime();
|
Chris@1397
|
1022 CoreMidiData *data = static_cast<CoreMidiData *> (apiData_);
|
Chris@1397
|
1023 OSStatus result;
|
Chris@1397
|
1024
|
Chris@1397
|
1025 if ( message->at(0) != 0xF0 && nBytes > 3 ) {
|
Chris@1397
|
1026 errorString_ = "MidiOutCore::sendMessage: message format problem ... not sysex but > 3 bytes?";
|
Chris@1397
|
1027 error( RtMidiError::WARNING, errorString_ );
|
Chris@565
|
1028 return;
|
Chris@565
|
1029 }
|
Chris@565
|
1030
|
Chris@1397
|
1031 Byte buffer[nBytes+(sizeof(MIDIPacketList))];
|
Chris@1397
|
1032 ByteCount listSize = sizeof(buffer);
|
Chris@1397
|
1033 MIDIPacketList *packetList = (MIDIPacketList*)buffer;
|
Chris@1397
|
1034 MIDIPacket *packet = MIDIPacketListInit( packetList );
|
Chris@1397
|
1035
|
Chris@1397
|
1036 ByteCount remainingBytes = nBytes;
|
Chris@1397
|
1037 while (remainingBytes && packet) {
|
Chris@1397
|
1038 ByteCount bytesForPacket = remainingBytes > 65535 ? 65535 : remainingBytes; // 65535 = maximum size of a MIDIPacket
|
Chris@1397
|
1039 const Byte* dataStartPtr = (const Byte *) &message->at( nBytes - remainingBytes );
|
Chris@1397
|
1040 packet = MIDIPacketListAdd( packetList, listSize, packet, timeStamp, bytesForPacket, dataStartPtr);
|
Chris@1397
|
1041 remainingBytes -= bytesForPacket;
|
Chris@1397
|
1042 }
|
Chris@1397
|
1043
|
Chris@1397
|
1044 if ( !packet ) {
|
Chris@1397
|
1045 errorString_ = "MidiOutCore::sendMessage: could not allocate packet list";
|
Chris@1397
|
1046 error( RtMidiError::DRIVER_ERROR, errorString_ );
|
Chris@1397
|
1047 return;
|
Chris@1397
|
1048 }
|
Chris@1397
|
1049
|
Chris@1397
|
1050 // Send to any destinations that may have connected to us.
|
Chris@1397
|
1051 if ( data->endpoint ) {
|
Chris@1397
|
1052 result = MIDIReceived( data->endpoint, packetList );
|
Chris@1397
|
1053 if ( result != noErr ) {
|
Chris@1397
|
1054 errorString_ = "MidiOutCore::sendMessage: error sending MIDI to virtual destinations.";
|
Chris@1397
|
1055 error( RtMidiError::WARNING, errorString_ );
|
Chris@559
|
1056 }
|
Chris@1397
|
1057 }
|
Chris@1397
|
1058
|
Chris@1397
|
1059 // And send to an explicit destination port if we're connected.
|
Chris@1397
|
1060 if ( connected_ ) {
|
Chris@1397
|
1061 result = MIDISend( data->port, data->destinationId, packetList );
|
Chris@1397
|
1062 if ( result != noErr ) {
|
Chris@1397
|
1063 errorString_ = "MidiOutCore::sendMessage: error sending MIDI message to port.";
|
Chris@1397
|
1064 error( RtMidiError::WARNING, errorString_ );
|
Chris@559
|
1065 }
|
Chris@559
|
1066 }
|
Chris@559
|
1067 }
|
Chris@559
|
1068
|
Chris@559
|
1069 #endif // __MACOSX_CORE__
|
Chris@559
|
1070
|
Chris@559
|
1071
|
Chris@559
|
1072 //*********************************************************************//
|
Chris@559
|
1073 // API: LINUX ALSA SEQUENCER
|
Chris@559
|
1074 //*********************************************************************//
|
Chris@559
|
1075
|
Chris@559
|
1076 // API information found at:
|
Chris@559
|
1077 // - http://www.alsa-project.org/documentation.php#Library
|
Chris@559
|
1078
|
Chris@1397
|
1079 #if defined(__LINUX_ALSA__)
|
Chris@559
|
1080
|
Chris@559
|
1081 // The ALSA Sequencer API is based on the use of a callback function for
|
Chris@559
|
1082 // MIDI input.
|
Chris@559
|
1083 //
|
Chris@559
|
1084 // Thanks to Pedro Lopez-Cabanillas for help with the ALSA sequencer
|
Chris@559
|
1085 // time stamps and other assorted fixes!!!
|
Chris@559
|
1086
|
Chris@1397
|
1087 // If you don't need timestamping for incoming MIDI events, define the
|
Chris@1397
|
1088 // preprocessor definition AVOID_TIMESTAMPING to save resources
|
Chris@1397
|
1089 // associated with the ALSA sequencer queues.
|
Chris@1397
|
1090
|
Chris@559
|
1091 #include <pthread.h>
|
Chris@559
|
1092 #include <sys/time.h>
|
Chris@559
|
1093
|
Chris@559
|
1094 // ALSA header file.
|
Chris@559
|
1095 #include <alsa/asoundlib.h>
|
Chris@559
|
1096
|
Chris@559
|
1097 // A structure to hold variables related to the ALSA API
|
Chris@559
|
1098 // implementation.
|
Chris@559
|
1099 struct AlsaMidiData {
|
Chris@559
|
1100 snd_seq_t *seq;
|
Chris@1397
|
1101 unsigned int portNum;
|
Chris@559
|
1102 int vport;
|
Chris@559
|
1103 snd_seq_port_subscribe_t *subscription;
|
Chris@559
|
1104 snd_midi_event_t *coder;
|
Chris@559
|
1105 unsigned int bufferSize;
|
Chris@559
|
1106 unsigned char *buffer;
|
Chris@559
|
1107 pthread_t thread;
|
Chris@1397
|
1108 pthread_t dummy_thread_id;
|
Chris@559
|
1109 unsigned long long lastTime;
|
Chris@559
|
1110 int queue_id; // an input queue is needed to get timestamped events
|
Chris@1397
|
1111 int trigger_fds[2];
|
Chris@559
|
1112 };
|
Chris@559
|
1113
|
Chris@559
|
1114 #define PORT_TYPE( pinfo, bits ) ((snd_seq_port_info_get_capability(pinfo) & (bits)) == (bits))
|
Chris@559
|
1115
|
Chris@559
|
1116 //*********************************************************************//
|
Chris@559
|
1117 // API: LINUX ALSA
|
Chris@1397
|
1118 // Class Definitions: MidiInAlsa
|
Chris@559
|
1119 //*********************************************************************//
|
Chris@559
|
1120
|
Chris@1397
|
1121 static void *alsaMidiHandler( void *ptr )
|
Chris@559
|
1122 {
|
Chris@1397
|
1123 MidiInApi::RtMidiInData *data = static_cast<MidiInApi::RtMidiInData *> (ptr);
|
Chris@559
|
1124 AlsaMidiData *apiData = static_cast<AlsaMidiData *> (data->apiData);
|
Chris@559
|
1125
|
Chris@559
|
1126 long nBytes;
|
Chris@559
|
1127 unsigned long long time, lastTime;
|
Chris@559
|
1128 bool continueSysex = false;
|
Chris@1397
|
1129 bool doDecode = false;
|
Chris@1397
|
1130 MidiInApi::MidiMessage message;
|
Chris@1397
|
1131 int poll_fd_count;
|
Chris@1397
|
1132 struct pollfd *poll_fds;
|
Chris@559
|
1133
|
Chris@559
|
1134 snd_seq_event_t *ev;
|
Chris@559
|
1135 int result;
|
Chris@559
|
1136 apiData->bufferSize = 32;
|
Chris@559
|
1137 result = snd_midi_event_new( 0, &apiData->coder );
|
Chris@559
|
1138 if ( result < 0 ) {
|
Chris@559
|
1139 data->doInput = false;
|
Chris@1397
|
1140 std::cerr << "\nMidiInAlsa::alsaMidiHandler: error initializing MIDI event parser!\n\n";
|
Chris@559
|
1141 return 0;
|
Chris@559
|
1142 }
|
Chris@559
|
1143 unsigned char *buffer = (unsigned char *) malloc( apiData->bufferSize );
|
Chris@559
|
1144 if ( buffer == NULL ) {
|
Chris@559
|
1145 data->doInput = false;
|
Chris@1397
|
1146 snd_midi_event_free( apiData->coder );
|
Chris@1397
|
1147 apiData->coder = 0;
|
Chris@1397
|
1148 std::cerr << "\nMidiInAlsa::alsaMidiHandler: error initializing buffer memory!\n\n";
|
Chris@559
|
1149 return 0;
|
Chris@559
|
1150 }
|
Chris@559
|
1151 snd_midi_event_init( apiData->coder );
|
Chris@559
|
1152 snd_midi_event_no_status( apiData->coder, 1 ); // suppress running status messages
|
Chris@559
|
1153
|
Chris@1397
|
1154 poll_fd_count = snd_seq_poll_descriptors_count( apiData->seq, POLLIN ) + 1;
|
Chris@1397
|
1155 poll_fds = (struct pollfd*)alloca( poll_fd_count * sizeof( struct pollfd ));
|
Chris@1397
|
1156 snd_seq_poll_descriptors( apiData->seq, poll_fds + 1, poll_fd_count - 1, POLLIN );
|
Chris@1397
|
1157 poll_fds[0].fd = apiData->trigger_fds[0];
|
Chris@1397
|
1158 poll_fds[0].events = POLLIN;
|
Chris@1397
|
1159
|
Chris@559
|
1160 while ( data->doInput ) {
|
Chris@559
|
1161
|
Chris@559
|
1162 if ( snd_seq_event_input_pending( apiData->seq, 1 ) == 0 ) {
|
Chris@1397
|
1163 // No data pending
|
Chris@1397
|
1164 if ( poll( poll_fds, poll_fd_count, -1) >= 0 ) {
|
Chris@1397
|
1165 if ( poll_fds[0].revents & POLLIN ) {
|
Chris@1397
|
1166 bool dummy;
|
Chris@1397
|
1167 int res = read( poll_fds[0].fd, &dummy, sizeof(dummy) );
|
Chris@1397
|
1168 (void) res;
|
Chris@1397
|
1169 }
|
Chris@1397
|
1170 }
|
Chris@559
|
1171 continue;
|
Chris@559
|
1172 }
|
Chris@559
|
1173
|
Chris@559
|
1174 // If here, there should be data.
|
Chris@559
|
1175 result = snd_seq_event_input( apiData->seq, &ev );
|
Chris@559
|
1176 if ( result == -ENOSPC ) {
|
Chris@1397
|
1177 std::cerr << "\nMidiInAlsa::alsaMidiHandler: MIDI input buffer overrun!\n\n";
|
Chris@559
|
1178 continue;
|
Chris@559
|
1179 }
|
Chris@559
|
1180 else if ( result <= 0 ) {
|
Chris@1397
|
1181 std::cerr << "\nMidiInAlsa::alsaMidiHandler: unknown MIDI input error!\n";
|
Chris@1397
|
1182 perror("System reports");
|
Chris@559
|
1183 continue;
|
Chris@559
|
1184 }
|
Chris@559
|
1185
|
Chris@559
|
1186 // This is a bit weird, but we now have to decode an ALSA MIDI
|
Chris@559
|
1187 // event (back) into MIDI bytes. We'll ignore non-MIDI types.
|
Chris@1397
|
1188 if ( !continueSysex ) message.bytes.clear();
|
Chris@1397
|
1189
|
Chris@1397
|
1190 doDecode = false;
|
Chris@559
|
1191 switch ( ev->type ) {
|
Chris@559
|
1192
|
Chris@1397
|
1193 case SND_SEQ_EVENT_PORT_SUBSCRIBED:
|
Chris@559
|
1194 #if defined(__RTMIDI_DEBUG__)
|
Chris@1397
|
1195 std::cout << "MidiInAlsa::alsaMidiHandler: port connection made!\n";
|
Chris@559
|
1196 #endif
|
Chris@559
|
1197 break;
|
Chris@559
|
1198
|
Chris@1397
|
1199 case SND_SEQ_EVENT_PORT_UNSUBSCRIBED:
|
Chris@565
|
1200 #if defined(__RTMIDI_DEBUG__)
|
Chris@1397
|
1201 std::cerr << "MidiInAlsa::alsaMidiHandler: port connection has closed!\n";
|
Chris@1397
|
1202 std::cout << "sender = " << (int) ev->data.connect.sender.client << ":"
|
Chris@1397
|
1203 << (int) ev->data.connect.sender.port
|
Chris@1397
|
1204 << ", dest = " << (int) ev->data.connect.dest.client << ":"
|
Chris@1397
|
1205 << (int) ev->data.connect.dest.port
|
Chris@1397
|
1206 << std::endl;
|
Chris@565
|
1207 #endif
|
Chris@559
|
1208 break;
|
Chris@559
|
1209
|
Chris@559
|
1210 case SND_SEQ_EVENT_QFRAME: // MIDI time code
|
Chris@1397
|
1211 if ( !( data->ignoreFlags & 0x02 ) ) doDecode = true;
|
Chris@1397
|
1212 break;
|
Chris@1397
|
1213
|
Chris@1397
|
1214 case SND_SEQ_EVENT_TICK: // 0xF9 ... MIDI timing tick
|
Chris@1397
|
1215 if ( !( data->ignoreFlags & 0x02 ) ) doDecode = true;
|
Chris@1397
|
1216 break;
|
Chris@1397
|
1217
|
Chris@1397
|
1218 case SND_SEQ_EVENT_CLOCK: // 0xF8 ... MIDI timing (clock) tick
|
Chris@1397
|
1219 if ( !( data->ignoreFlags & 0x02 ) ) doDecode = true;
|
Chris@1397
|
1220 break;
|
Chris@559
|
1221
|
Chris@559
|
1222 case SND_SEQ_EVENT_SENSING: // Active sensing
|
Chris@1397
|
1223 if ( !( data->ignoreFlags & 0x04 ) ) doDecode = true;
|
Chris@1397
|
1224 break;
|
Chris@559
|
1225
|
Chris@559
|
1226 case SND_SEQ_EVENT_SYSEX:
|
Chris@559
|
1227 if ( (data->ignoreFlags & 0x01) ) break;
|
Chris@559
|
1228 if ( ev->data.ext.len > apiData->bufferSize ) {
|
Chris@559
|
1229 apiData->bufferSize = ev->data.ext.len;
|
Chris@559
|
1230 free( buffer );
|
Chris@559
|
1231 buffer = (unsigned char *) malloc( apiData->bufferSize );
|
Chris@559
|
1232 if ( buffer == NULL ) {
|
Chris@559
|
1233 data->doInput = false;
|
Chris@1397
|
1234 std::cerr << "\nMidiInAlsa::alsaMidiHandler: error resizing buffer memory!\n\n";
|
Chris@559
|
1235 break;
|
Chris@559
|
1236 }
|
Chris@559
|
1237 }
|
Chris@559
|
1238
|
Chris@559
|
1239 default:
|
Chris@1397
|
1240 doDecode = true;
|
Chris@1397
|
1241 }
|
Chris@1397
|
1242
|
Chris@1397
|
1243 if ( doDecode ) {
|
Chris@1397
|
1244
|
Chris@559
|
1245 nBytes = snd_midi_event_decode( apiData->coder, buffer, apiData->bufferSize, ev );
|
Chris@1397
|
1246 if ( nBytes > 0 ) {
|
Chris@1397
|
1247 // The ALSA sequencer has a maximum buffer size for MIDI sysex
|
Chris@1397
|
1248 // events of 256 bytes. If a device sends sysex messages larger
|
Chris@1397
|
1249 // than this, they are segmented into 256 byte chunks. So,
|
Chris@1397
|
1250 // we'll watch for this and concatenate sysex chunks into a
|
Chris@1397
|
1251 // single sysex message if necessary.
|
Chris@1397
|
1252 if ( !continueSysex )
|
Chris@1397
|
1253 message.bytes.assign( buffer, &buffer[nBytes] );
|
Chris@1397
|
1254 else
|
Chris@1397
|
1255 message.bytes.insert( message.bytes.end(), buffer, &buffer[nBytes] );
|
Chris@1397
|
1256
|
Chris@1397
|
1257 continueSysex = ( ( ev->type == SND_SEQ_EVENT_SYSEX ) && ( message.bytes.back() != 0xF7 ) );
|
Chris@1397
|
1258 if ( !continueSysex ) {
|
Chris@1397
|
1259
|
Chris@1397
|
1260 // Calculate the time stamp:
|
Chris@1397
|
1261 message.timeStamp = 0.0;
|
Chris@1397
|
1262
|
Chris@1397
|
1263 // Method 1: Use the system time.
|
Chris@1397
|
1264 //(void)gettimeofday(&tv, (struct timezone *)NULL);
|
Chris@1397
|
1265 //time = (tv.tv_sec * 1000000) + tv.tv_usec;
|
Chris@1397
|
1266
|
Chris@1397
|
1267 // Method 2: Use the ALSA sequencer event time data.
|
Chris@1397
|
1268 // (thanks to Pedro Lopez-Cabanillas!).
|
Chris@1397
|
1269 time = ( ev->time.time.tv_sec * 1000000 ) + ( ev->time.time.tv_nsec/1000 );
|
Chris@1397
|
1270 lastTime = time;
|
Chris@1397
|
1271 time -= apiData->lastTime;
|
Chris@1397
|
1272 apiData->lastTime = lastTime;
|
Chris@1397
|
1273 if ( data->firstMessage == true )
|
Chris@1397
|
1274 data->firstMessage = false;
|
Chris@1397
|
1275 else
|
Chris@1397
|
1276 message.timeStamp = time * 0.000001;
|
Chris@1397
|
1277 }
|
Chris@1397
|
1278 else {
|
Chris@559
|
1279 #if defined(__RTMIDI_DEBUG__)
|
Chris@1397
|
1280 std::cerr << "\nMidiInAlsa::alsaMidiHandler: event parsing error or not a MIDI event!\n\n";
|
Chris@559
|
1281 #endif
|
Chris@1397
|
1282 }
|
Chris@559
|
1283 }
|
Chris@559
|
1284 }
|
Chris@559
|
1285
|
Chris@1397
|
1286 snd_seq_free_event( ev );
|
Chris@1397
|
1287 if ( message.bytes.size() == 0 || continueSysex ) continue;
|
Chris@1397
|
1288
|
Chris@1397
|
1289 if ( data->usingCallback ) {
|
Chris@559
|
1290 RtMidiIn::RtMidiCallback callback = (RtMidiIn::RtMidiCallback) data->userCallback;
|
Chris@559
|
1291 callback( message.timeStamp, &message.bytes, data->userData );
|
Chris@559
|
1292 }
|
Chris@559
|
1293 else {
|
Chris@559
|
1294 // As long as we haven't reached our queue size limit, push the message.
|
Chris@1397
|
1295 if ( data->queue.size < data->queue.ringSize ) {
|
Chris@1397
|
1296 data->queue.ring[data->queue.back++] = message;
|
Chris@1397
|
1297 if ( data->queue.back == data->queue.ringSize )
|
Chris@1397
|
1298 data->queue.back = 0;
|
Chris@1397
|
1299 data->queue.size++;
|
Chris@1397
|
1300 }
|
Chris@559
|
1301 else
|
Chris@1397
|
1302 std::cerr << "\nMidiInAlsa: message queue limit reached!!\n\n";
|
Chris@559
|
1303 }
|
Chris@559
|
1304 }
|
Chris@559
|
1305
|
Chris@559
|
1306 if ( buffer ) free( buffer );
|
Chris@559
|
1307 snd_midi_event_free( apiData->coder );
|
Chris@559
|
1308 apiData->coder = 0;
|
Chris@1397
|
1309 apiData->thread = apiData->dummy_thread_id;
|
Chris@559
|
1310 return 0;
|
Chris@559
|
1311 }
|
Chris@559
|
1312
|
Chris@1397
|
1313 MidiInAlsa :: MidiInAlsa( const std::string clientName, unsigned int queueSizeLimit ) : MidiInApi( queueSizeLimit )
|
Chris@1397
|
1314 {
|
Chris@1397
|
1315 initialize( clientName );
|
Chris@1397
|
1316 }
|
Chris@1397
|
1317
|
Chris@1397
|
1318 MidiInAlsa :: ~MidiInAlsa()
|
Chris@1397
|
1319 {
|
Chris@1397
|
1320 // Close a connection if it exists.
|
Chris@1397
|
1321 closePort();
|
Chris@1397
|
1322
|
Chris@1397
|
1323 // Shutdown the input thread.
|
Chris@1397
|
1324 AlsaMidiData *data = static_cast<AlsaMidiData *> (apiData_);
|
Chris@1397
|
1325 if ( inputData_.doInput ) {
|
Chris@1397
|
1326 inputData_.doInput = false;
|
Chris@1397
|
1327 int res = write( data->trigger_fds[1], &inputData_.doInput, sizeof(inputData_.doInput) );
|
Chris@1397
|
1328 (void) res;
|
Chris@1397
|
1329 if ( !pthread_equal(data->thread, data->dummy_thread_id) )
|
Chris@1397
|
1330 pthread_join( data->thread, NULL );
|
Chris@1397
|
1331 }
|
Chris@1397
|
1332
|
Chris@1397
|
1333 // Cleanup.
|
Chris@1397
|
1334 close ( data->trigger_fds[0] );
|
Chris@1397
|
1335 close ( data->trigger_fds[1] );
|
Chris@1397
|
1336 if ( data->vport >= 0 ) snd_seq_delete_port( data->seq, data->vport );
|
Chris@1397
|
1337 #ifndef AVOID_TIMESTAMPING
|
Chris@1397
|
1338 snd_seq_free_queue( data->seq, data->queue_id );
|
Chris@1397
|
1339 #endif
|
Chris@1397
|
1340 snd_seq_close( data->seq );
|
Chris@1397
|
1341 delete data;
|
Chris@1397
|
1342 }
|
Chris@1397
|
1343
|
Chris@1397
|
1344 void MidiInAlsa :: initialize( const std::string& clientName )
|
Chris@559
|
1345 {
|
Chris@559
|
1346 // Set up the ALSA sequencer client.
|
Chris@565
|
1347 snd_seq_t *seq;
|
Chris@559
|
1348 int result = snd_seq_open(&seq, "default", SND_SEQ_OPEN_DUPLEX, SND_SEQ_NONBLOCK);
|
Chris@559
|
1349 if ( result < 0 ) {
|
Chris@1397
|
1350 errorString_ = "MidiInAlsa::initialize: error creating ALSA sequencer client object.";
|
Chris@1397
|
1351 error( RtMidiError::DRIVER_ERROR, errorString_ );
|
Chris@1397
|
1352 return;
|
Chris@1397
|
1353 }
|
Chris@559
|
1354
|
Chris@559
|
1355 // Set client name.
|
Chris@565
|
1356 snd_seq_set_client_name( seq, clientName.c_str() );
|
Chris@559
|
1357
|
Chris@559
|
1358 // Save our api-specific connection information.
|
Chris@559
|
1359 AlsaMidiData *data = (AlsaMidiData *) new AlsaMidiData;
|
Chris@559
|
1360 data->seq = seq;
|
Chris@1397
|
1361 data->portNum = -1;
|
Chris@559
|
1362 data->vport = -1;
|
Chris@1397
|
1363 data->subscription = 0;
|
Chris@1397
|
1364 data->dummy_thread_id = pthread_self();
|
Chris@1397
|
1365 data->thread = data->dummy_thread_id;
|
Chris@1397
|
1366 data->trigger_fds[0] = -1;
|
Chris@1397
|
1367 data->trigger_fds[1] = -1;
|
Chris@559
|
1368 apiData_ = (void *) data;
|
Chris@559
|
1369 inputData_.apiData = (void *) data;
|
Chris@559
|
1370
|
Chris@1397
|
1371 if ( pipe(data->trigger_fds) == -1 ) {
|
Chris@1397
|
1372 errorString_ = "MidiInAlsa::initialize: error creating pipe objects.";
|
Chris@1397
|
1373 error( RtMidiError::DRIVER_ERROR, errorString_ );
|
Chris@1397
|
1374 return;
|
Chris@1397
|
1375 }
|
Chris@1397
|
1376
|
Chris@559
|
1377 // Create the input queue
|
Chris@1397
|
1378 #ifndef AVOID_TIMESTAMPING
|
Chris@559
|
1379 data->queue_id = snd_seq_alloc_named_queue(seq, "RtMidi Queue");
|
Chris@559
|
1380 // Set arbitrary tempo (mm=100) and resolution (240)
|
Chris@559
|
1381 snd_seq_queue_tempo_t *qtempo;
|
Chris@559
|
1382 snd_seq_queue_tempo_alloca(&qtempo);
|
Chris@559
|
1383 snd_seq_queue_tempo_set_tempo(qtempo, 600000);
|
Chris@559
|
1384 snd_seq_queue_tempo_set_ppq(qtempo, 240);
|
Chris@559
|
1385 snd_seq_set_queue_tempo(data->seq, data->queue_id, qtempo);
|
Chris@559
|
1386 snd_seq_drain_output(data->seq);
|
Chris@1397
|
1387 #endif
|
Chris@559
|
1388 }
|
Chris@559
|
1389
|
Chris@559
|
1390 // This function is used to count or get the pinfo structure for a given port number.
|
Chris@559
|
1391 unsigned int portInfo( snd_seq_t *seq, snd_seq_port_info_t *pinfo, unsigned int type, int portNumber )
|
Chris@559
|
1392 {
|
Chris@1397
|
1393 snd_seq_client_info_t *cinfo;
|
Chris@559
|
1394 int client;
|
Chris@559
|
1395 int count = 0;
|
Chris@1397
|
1396 snd_seq_client_info_alloca( &cinfo );
|
Chris@1397
|
1397
|
Chris@1397
|
1398 snd_seq_client_info_set_client( cinfo, -1 );
|
Chris@1397
|
1399 while ( snd_seq_query_next_client( seq, cinfo ) >= 0 ) {
|
Chris@559
|
1400 client = snd_seq_client_info_get_client( cinfo );
|
Chris@559
|
1401 if ( client == 0 ) continue;
|
Chris@1397
|
1402 // Reset query info
|
Chris@1397
|
1403 snd_seq_port_info_set_client( pinfo, client );
|
Chris@1397
|
1404 snd_seq_port_info_set_port( pinfo, -1 );
|
Chris@1397
|
1405 while ( snd_seq_query_next_port( seq, pinfo ) >= 0 ) {
|
Chris@565
|
1406 unsigned int atyp = snd_seq_port_info_get_type( pinfo );
|
Chris@1397
|
1407 if ( ( ( atyp & SND_SEQ_PORT_TYPE_MIDI_GENERIC ) == 0 ) &&
|
Chris@1397
|
1408 ( ( atyp & SND_SEQ_PORT_TYPE_SYNTH ) == 0 ) ) continue;
|
Chris@565
|
1409 unsigned int caps = snd_seq_port_info_get_capability( pinfo );
|
Chris@565
|
1410 if ( ( caps & type ) != type ) continue;
|
Chris@559
|
1411 if ( count == portNumber ) return 1;
|
Chris@1397
|
1412 ++count;
|
Chris@1397
|
1413 }
|
Chris@1397
|
1414 }
|
Chris@559
|
1415
|
Chris@559
|
1416 // If a negative portNumber was used, return the port count.
|
Chris@559
|
1417 if ( portNumber < 0 ) return count;
|
Chris@559
|
1418 return 0;
|
Chris@559
|
1419 }
|
Chris@559
|
1420
|
Chris@1397
|
1421 unsigned int MidiInAlsa :: getPortCount()
|
Chris@1397
|
1422 {
|
Chris@1397
|
1423 snd_seq_port_info_t *pinfo;
|
Chris@1397
|
1424 snd_seq_port_info_alloca( &pinfo );
|
Chris@1397
|
1425
|
Chris@1397
|
1426 AlsaMidiData *data = static_cast<AlsaMidiData *> (apiData_);
|
Chris@1397
|
1427 return portInfo( data->seq, pinfo, SND_SEQ_PORT_CAP_READ|SND_SEQ_PORT_CAP_SUBS_READ, -1 );
|
Chris@1397
|
1428 }
|
Chris@1397
|
1429
|
Chris@1397
|
1430 std::string MidiInAlsa :: getPortName( unsigned int portNumber )
|
Chris@1397
|
1431 {
|
Chris@1397
|
1432 snd_seq_client_info_t *cinfo;
|
Chris@1397
|
1433 snd_seq_port_info_t *pinfo;
|
Chris@1397
|
1434 snd_seq_client_info_alloca( &cinfo );
|
Chris@1397
|
1435 snd_seq_port_info_alloca( &pinfo );
|
Chris@1397
|
1436
|
Chris@1397
|
1437 std::string stringName;
|
Chris@1397
|
1438 AlsaMidiData *data = static_cast<AlsaMidiData *> (apiData_);
|
Chris@1397
|
1439 if ( portInfo( data->seq, pinfo, SND_SEQ_PORT_CAP_READ|SND_SEQ_PORT_CAP_SUBS_READ, (int) portNumber ) ) {
|
Chris@1397
|
1440 int cnum = snd_seq_port_info_get_client( pinfo );
|
Chris@1397
|
1441 snd_seq_get_any_client_info( data->seq, cnum, cinfo );
|
Chris@1397
|
1442 std::ostringstream os;
|
Chris@1397
|
1443 os << snd_seq_client_info_get_name( cinfo );
|
Chris@1397
|
1444 os << " "; // These lines added to make sure devices are listed
|
Chris@1397
|
1445 os << snd_seq_port_info_get_client( pinfo ); // with full portnames added to ensure individual device names
|
Chris@1397
|
1446 os << ":";
|
Chris@1397
|
1447 os << snd_seq_port_info_get_port( pinfo );
|
Chris@1397
|
1448 stringName = os.str();
|
Chris@1397
|
1449 return stringName;
|
Chris@1397
|
1450 }
|
Chris@1397
|
1451
|
Chris@1397
|
1452 // If we get here, we didn't find a match.
|
Chris@1397
|
1453 errorString_ = "MidiInAlsa::getPortName: error looking for port name!";
|
Chris@1397
|
1454 error( RtMidiError::WARNING, errorString_ );
|
Chris@1397
|
1455 return stringName;
|
Chris@1397
|
1456 }
|
Chris@1397
|
1457
|
Chris@1397
|
1458 void MidiInAlsa :: openPort( unsigned int portNumber, const std::string portName )
|
Chris@559
|
1459 {
|
Chris@559
|
1460 if ( connected_ ) {
|
Chris@1397
|
1461 errorString_ = "MidiInAlsa::openPort: a valid connection already exists!";
|
Chris@1397
|
1462 error( RtMidiError::WARNING, errorString_ );
|
Chris@559
|
1463 return;
|
Chris@559
|
1464 }
|
Chris@559
|
1465
|
Chris@559
|
1466 unsigned int nSrc = this->getPortCount();
|
Chris@1397
|
1467 if ( nSrc < 1 ) {
|
Chris@1397
|
1468 errorString_ = "MidiInAlsa::openPort: no MIDI input sources found!";
|
Chris@1397
|
1469 error( RtMidiError::NO_DEVICES_FOUND, errorString_ );
|
Chris@1397
|
1470 return;
|
Chris@559
|
1471 }
|
Chris@559
|
1472
|
Chris@1397
|
1473 snd_seq_port_info_t *src_pinfo;
|
Chris@1397
|
1474 snd_seq_port_info_alloca( &src_pinfo );
|
Chris@559
|
1475 AlsaMidiData *data = static_cast<AlsaMidiData *> (apiData_);
|
Chris@1397
|
1476 if ( portInfo( data->seq, src_pinfo, SND_SEQ_PORT_CAP_READ|SND_SEQ_PORT_CAP_SUBS_READ, (int) portNumber ) == 0 ) {
|
Chris@1397
|
1477 std::ostringstream ost;
|
Chris@1397
|
1478 ost << "MidiInAlsa::openPort: the 'portNumber' argument (" << portNumber << ") is invalid.";
|
Chris@559
|
1479 errorString_ = ost.str();
|
Chris@1397
|
1480 error( RtMidiError::INVALID_PARAMETER, errorString_ );
|
Chris@1397
|
1481 return;
|
Chris@559
|
1482 }
|
Chris@559
|
1483
|
Chris@559
|
1484 snd_seq_addr_t sender, receiver;
|
Chris@1397
|
1485 sender.client = snd_seq_port_info_get_client( src_pinfo );
|
Chris@1397
|
1486 sender.port = snd_seq_port_info_get_port( src_pinfo );
|
Chris@1397
|
1487 receiver.client = snd_seq_client_id( data->seq );
|
Chris@1397
|
1488
|
Chris@1397
|
1489 snd_seq_port_info_t *pinfo;
|
Chris@1397
|
1490 snd_seq_port_info_alloca( &pinfo );
|
Chris@559
|
1491 if ( data->vport < 0 ) {
|
Chris@559
|
1492 snd_seq_port_info_set_client( pinfo, 0 );
|
Chris@559
|
1493 snd_seq_port_info_set_port( pinfo, 0 );
|
Chris@559
|
1494 snd_seq_port_info_set_capability( pinfo,
|
Chris@559
|
1495 SND_SEQ_PORT_CAP_WRITE |
|
Chris@559
|
1496 SND_SEQ_PORT_CAP_SUBS_WRITE );
|
Chris@559
|
1497 snd_seq_port_info_set_type( pinfo,
|
Chris@559
|
1498 SND_SEQ_PORT_TYPE_MIDI_GENERIC |
|
Chris@559
|
1499 SND_SEQ_PORT_TYPE_APPLICATION );
|
Chris@559
|
1500 snd_seq_port_info_set_midi_channels(pinfo, 16);
|
Chris@1397
|
1501 #ifndef AVOID_TIMESTAMPING
|
Chris@559
|
1502 snd_seq_port_info_set_timestamping(pinfo, 1);
|
Chris@559
|
1503 snd_seq_port_info_set_timestamp_real(pinfo, 1);
|
Chris@559
|
1504 snd_seq_port_info_set_timestamp_queue(pinfo, data->queue_id);
|
Chris@1397
|
1505 #endif
|
Chris@565
|
1506 snd_seq_port_info_set_name(pinfo, portName.c_str() );
|
Chris@559
|
1507 data->vport = snd_seq_create_port(data->seq, pinfo);
|
Chris@559
|
1508
|
Chris@559
|
1509 if ( data->vport < 0 ) {
|
Chris@1397
|
1510 errorString_ = "MidiInAlsa::openPort: ALSA error creating input port.";
|
Chris@1397
|
1511 error( RtMidiError::DRIVER_ERROR, errorString_ );
|
Chris@1397
|
1512 return;
|
Chris@1397
|
1513 }
|
Chris@1397
|
1514 data->vport = snd_seq_port_info_get_port(pinfo);
|
Chris@1397
|
1515 }
|
Chris@1397
|
1516
|
Chris@1397
|
1517 receiver.port = data->vport;
|
Chris@1397
|
1518
|
Chris@1397
|
1519 if ( !data->subscription ) {
|
Chris@1397
|
1520 // Make subscription
|
Chris@1397
|
1521 if (snd_seq_port_subscribe_malloc( &data->subscription ) < 0) {
|
Chris@1397
|
1522 errorString_ = "MidiInAlsa::openPort: ALSA error allocation port subscription.";
|
Chris@1397
|
1523 error( RtMidiError::DRIVER_ERROR, errorString_ );
|
Chris@1397
|
1524 return;
|
Chris@1397
|
1525 }
|
Chris@1397
|
1526 snd_seq_port_subscribe_set_sender(data->subscription, &sender);
|
Chris@1397
|
1527 snd_seq_port_subscribe_set_dest(data->subscription, &receiver);
|
Chris@1397
|
1528 if ( snd_seq_subscribe_port(data->seq, data->subscription) ) {
|
Chris@1397
|
1529 snd_seq_port_subscribe_free( data->subscription );
|
Chris@1397
|
1530 data->subscription = 0;
|
Chris@1397
|
1531 errorString_ = "MidiInAlsa::openPort: ALSA error making port connection.";
|
Chris@1397
|
1532 error( RtMidiError::DRIVER_ERROR, errorString_ );
|
Chris@1397
|
1533 return;
|
Chris@559
|
1534 }
|
Chris@559
|
1535 }
|
Chris@559
|
1536
|
Chris@559
|
1537 if ( inputData_.doInput == false ) {
|
Chris@559
|
1538 // Start the input queue
|
Chris@1397
|
1539 #ifndef AVOID_TIMESTAMPING
|
Chris@559
|
1540 snd_seq_start_queue( data->seq, data->queue_id, NULL );
|
Chris@559
|
1541 snd_seq_drain_output( data->seq );
|
Chris@1397
|
1542 #endif
|
Chris@559
|
1543 // Start our MIDI input thread.
|
Chris@559
|
1544 pthread_attr_t attr;
|
Chris@559
|
1545 pthread_attr_init(&attr);
|
Chris@559
|
1546 pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_JOINABLE);
|
Chris@559
|
1547 pthread_attr_setschedpolicy(&attr, SCHED_OTHER);
|
Chris@559
|
1548
|
Chris@559
|
1549 inputData_.doInput = true;
|
Chris@559
|
1550 int err = pthread_create(&data->thread, &attr, alsaMidiHandler, &inputData_);
|
Chris@559
|
1551 pthread_attr_destroy(&attr);
|
Chris@1397
|
1552 if ( err ) {
|
Chris@559
|
1553 snd_seq_unsubscribe_port( data->seq, data->subscription );
|
Chris@559
|
1554 snd_seq_port_subscribe_free( data->subscription );
|
Chris@1397
|
1555 data->subscription = 0;
|
Chris@559
|
1556 inputData_.doInput = false;
|
Chris@1397
|
1557 errorString_ = "MidiInAlsa::openPort: error starting MIDI input thread!";
|
Chris@1397
|
1558 error( RtMidiError::THREAD_ERROR, errorString_ );
|
Chris@1397
|
1559 return;
|
Chris@559
|
1560 }
|
Chris@559
|
1561 }
|
Chris@559
|
1562
|
Chris@559
|
1563 connected_ = true;
|
Chris@559
|
1564 }
|
Chris@559
|
1565
|
Chris@1397
|
1566 void MidiInAlsa :: openVirtualPort( std::string portName )
|
Chris@559
|
1567 {
|
Chris@559
|
1568 AlsaMidiData *data = static_cast<AlsaMidiData *> (apiData_);
|
Chris@559
|
1569 if ( data->vport < 0 ) {
|
Chris@559
|
1570 snd_seq_port_info_t *pinfo;
|
Chris@559
|
1571 snd_seq_port_info_alloca( &pinfo );
|
Chris@559
|
1572 snd_seq_port_info_set_capability( pinfo,
|
Chris@559
|
1573 SND_SEQ_PORT_CAP_WRITE |
|
Chris@559
|
1574 SND_SEQ_PORT_CAP_SUBS_WRITE );
|
Chris@559
|
1575 snd_seq_port_info_set_type( pinfo,
|
Chris@559
|
1576 SND_SEQ_PORT_TYPE_MIDI_GENERIC |
|
Chris@559
|
1577 SND_SEQ_PORT_TYPE_APPLICATION );
|
Chris@559
|
1578 snd_seq_port_info_set_midi_channels(pinfo, 16);
|
Chris@1397
|
1579 #ifndef AVOID_TIMESTAMPING
|
Chris@559
|
1580 snd_seq_port_info_set_timestamping(pinfo, 1);
|
Chris@559
|
1581 snd_seq_port_info_set_timestamp_real(pinfo, 1);
|
Chris@559
|
1582 snd_seq_port_info_set_timestamp_queue(pinfo, data->queue_id);
|
Chris@1397
|
1583 #endif
|
Chris@559
|
1584 snd_seq_port_info_set_name(pinfo, portName.c_str());
|
Chris@559
|
1585 data->vport = snd_seq_create_port(data->seq, pinfo);
|
Chris@559
|
1586
|
Chris@559
|
1587 if ( data->vport < 0 ) {
|
Chris@1397
|
1588 errorString_ = "MidiInAlsa::openVirtualPort: ALSA error creating virtual port.";
|
Chris@1397
|
1589 error( RtMidiError::DRIVER_ERROR, errorString_ );
|
Chris@1397
|
1590 return;
|
Chris@559
|
1591 }
|
Chris@1397
|
1592 data->vport = snd_seq_port_info_get_port(pinfo);
|
Chris@559
|
1593 }
|
Chris@559
|
1594
|
Chris@559
|
1595 if ( inputData_.doInput == false ) {
|
Chris@1397
|
1596 // Wait for old thread to stop, if still running
|
Chris@1397
|
1597 if ( !pthread_equal(data->thread, data->dummy_thread_id) )
|
Chris@1397
|
1598 pthread_join( data->thread, NULL );
|
Chris@1397
|
1599
|
Chris@559
|
1600 // Start the input queue
|
Chris@1397
|
1601 #ifndef AVOID_TIMESTAMPING
|
Chris@559
|
1602 snd_seq_start_queue( data->seq, data->queue_id, NULL );
|
Chris@559
|
1603 snd_seq_drain_output( data->seq );
|
Chris@1397
|
1604 #endif
|
Chris@559
|
1605 // Start our MIDI input thread.
|
Chris@559
|
1606 pthread_attr_t attr;
|
Chris@559
|
1607 pthread_attr_init(&attr);
|
Chris@559
|
1608 pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_JOINABLE);
|
Chris@559
|
1609 pthread_attr_setschedpolicy(&attr, SCHED_OTHER);
|
Chris@559
|
1610
|
Chris@559
|
1611 inputData_.doInput = true;
|
Chris@559
|
1612 int err = pthread_create(&data->thread, &attr, alsaMidiHandler, &inputData_);
|
Chris@559
|
1613 pthread_attr_destroy(&attr);
|
Chris@1397
|
1614 if ( err ) {
|
Chris@1397
|
1615 if ( data->subscription ) {
|
Chris@1397
|
1616 snd_seq_unsubscribe_port( data->seq, data->subscription );
|
Chris@1397
|
1617 snd_seq_port_subscribe_free( data->subscription );
|
Chris@1397
|
1618 data->subscription = 0;
|
Chris@1397
|
1619 }
|
Chris@559
|
1620 inputData_.doInput = false;
|
Chris@1397
|
1621 errorString_ = "MidiInAlsa::openPort: error starting MIDI input thread!";
|
Chris@1397
|
1622 error( RtMidiError::THREAD_ERROR, errorString_ );
|
Chris@1397
|
1623 return;
|
Chris@559
|
1624 }
|
Chris@559
|
1625 }
|
Chris@559
|
1626 }
|
Chris@559
|
1627
|
Chris@1397
|
1628 void MidiInAlsa :: closePort( void )
|
Chris@559
|
1629 {
|
Chris@1397
|
1630 AlsaMidiData *data = static_cast<AlsaMidiData *> (apiData_);
|
Chris@1397
|
1631
|
Chris@559
|
1632 if ( connected_ ) {
|
Chris@1397
|
1633 if ( data->subscription ) {
|
Chris@1397
|
1634 snd_seq_unsubscribe_port( data->seq, data->subscription );
|
Chris@1397
|
1635 snd_seq_port_subscribe_free( data->subscription );
|
Chris@1397
|
1636 data->subscription = 0;
|
Chris@1397
|
1637 }
|
Chris@559
|
1638 // Stop the input queue
|
Chris@1397
|
1639 #ifndef AVOID_TIMESTAMPING
|
Chris@559
|
1640 snd_seq_stop_queue( data->seq, data->queue_id, NULL );
|
Chris@559
|
1641 snd_seq_drain_output( data->seq );
|
Chris@1397
|
1642 #endif
|
Chris@559
|
1643 connected_ = false;
|
Chris@559
|
1644 }
|
Chris@1397
|
1645
|
Chris@1397
|
1646 // Stop thread to avoid triggering the callback, while the port is intended to be closed
|
Chris@1397
|
1647 if ( inputData_.doInput ) {
|
Chris@1397
|
1648 inputData_.doInput = false;
|
Chris@1397
|
1649 int res = write( data->trigger_fds[1], &inputData_.doInput, sizeof(inputData_.doInput) );
|
Chris@1397
|
1650 (void) res;
|
Chris@1397
|
1651 if ( !pthread_equal(data->thread, data->dummy_thread_id) )
|
Chris@1397
|
1652 pthread_join( data->thread, NULL );
|
Chris@1397
|
1653 }
|
Chris@559
|
1654 }
|
Chris@559
|
1655
|
Chris@1397
|
1656 //*********************************************************************//
|
Chris@1397
|
1657 // API: LINUX ALSA
|
Chris@1397
|
1658 // Class Definitions: MidiOutAlsa
|
Chris@1397
|
1659 //*********************************************************************//
|
Chris@1397
|
1660
|
Chris@1397
|
1661 MidiOutAlsa :: MidiOutAlsa( const std::string clientName ) : MidiOutApi()
|
Chris@1397
|
1662 {
|
Chris@1397
|
1663 initialize( clientName );
|
Chris@1397
|
1664 }
|
Chris@1397
|
1665
|
Chris@1397
|
1666 MidiOutAlsa :: ~MidiOutAlsa()
|
Chris@559
|
1667 {
|
Chris@559
|
1668 // Close a connection if it exists.
|
Chris@559
|
1669 closePort();
|
Chris@559
|
1670
|
Chris@1397
|
1671 // Cleanup.
|
Chris@559
|
1672 AlsaMidiData *data = static_cast<AlsaMidiData *> (apiData_);
|
Chris@559
|
1673 if ( data->vport >= 0 ) snd_seq_delete_port( data->seq, data->vport );
|
Chris@1397
|
1674 if ( data->coder ) snd_midi_event_free( data->coder );
|
Chris@1397
|
1675 if ( data->buffer ) free( data->buffer );
|
Chris@559
|
1676 snd_seq_close( data->seq );
|
Chris@559
|
1677 delete data;
|
Chris@559
|
1678 }
|
Chris@559
|
1679
|
Chris@1397
|
1680 void MidiOutAlsa :: initialize( const std::string& clientName )
|
Chris@1397
|
1681 {
|
Chris@1397
|
1682 // Set up the ALSA sequencer client.
|
Chris@1397
|
1683 snd_seq_t *seq;
|
Chris@1397
|
1684 int result1 = snd_seq_open( &seq, "default", SND_SEQ_OPEN_OUTPUT, SND_SEQ_NONBLOCK );
|
Chris@1397
|
1685 if ( result1 < 0 ) {
|
Chris@1397
|
1686 errorString_ = "MidiOutAlsa::initialize: error creating ALSA sequencer client object.";
|
Chris@1397
|
1687 error( RtMidiError::DRIVER_ERROR, errorString_ );
|
Chris@1397
|
1688 return;
|
Chris@1397
|
1689 }
|
Chris@1397
|
1690
|
Chris@1397
|
1691 // Set client name.
|
Chris@1397
|
1692 snd_seq_set_client_name( seq, clientName.c_str() );
|
Chris@1397
|
1693
|
Chris@1397
|
1694 // Save our api-specific connection information.
|
Chris@1397
|
1695 AlsaMidiData *data = (AlsaMidiData *) new AlsaMidiData;
|
Chris@1397
|
1696 data->seq = seq;
|
Chris@1397
|
1697 data->portNum = -1;
|
Chris@1397
|
1698 data->vport = -1;
|
Chris@1397
|
1699 data->bufferSize = 32;
|
Chris@1397
|
1700 data->coder = 0;
|
Chris@1397
|
1701 data->buffer = 0;
|
Chris@1397
|
1702 int result = snd_midi_event_new( data->bufferSize, &data->coder );
|
Chris@1397
|
1703 if ( result < 0 ) {
|
Chris@1397
|
1704 delete data;
|
Chris@1397
|
1705 errorString_ = "MidiOutAlsa::initialize: error initializing MIDI event parser!\n\n";
|
Chris@1397
|
1706 error( RtMidiError::DRIVER_ERROR, errorString_ );
|
Chris@1397
|
1707 return;
|
Chris@1397
|
1708 }
|
Chris@1397
|
1709 data->buffer = (unsigned char *) malloc( data->bufferSize );
|
Chris@1397
|
1710 if ( data->buffer == NULL ) {
|
Chris@1397
|
1711 delete data;
|
Chris@1397
|
1712 errorString_ = "MidiOutAlsa::initialize: error allocating buffer memory!\n\n";
|
Chris@1397
|
1713 error( RtMidiError::MEMORY_ERROR, errorString_ );
|
Chris@1397
|
1714 return;
|
Chris@1397
|
1715 }
|
Chris@1397
|
1716 snd_midi_event_init( data->coder );
|
Chris@1397
|
1717 apiData_ = (void *) data;
|
Chris@1397
|
1718 }
|
Chris@1397
|
1719
|
Chris@1397
|
1720 unsigned int MidiOutAlsa :: getPortCount()
|
Chris@559
|
1721 {
|
Chris@559
|
1722 snd_seq_port_info_t *pinfo;
|
Chris@559
|
1723 snd_seq_port_info_alloca( &pinfo );
|
Chris@559
|
1724
|
Chris@559
|
1725 AlsaMidiData *data = static_cast<AlsaMidiData *> (apiData_);
|
Chris@1397
|
1726 return portInfo( data->seq, pinfo, SND_SEQ_PORT_CAP_WRITE|SND_SEQ_PORT_CAP_SUBS_WRITE, -1 );
|
Chris@559
|
1727 }
|
Chris@559
|
1728
|
Chris@1397
|
1729 std::string MidiOutAlsa :: getPortName( unsigned int portNumber )
|
Chris@559
|
1730 {
|
Chris@565
|
1731 snd_seq_client_info_t *cinfo;
|
Chris@565
|
1732 snd_seq_port_info_t *pinfo;
|
Chris@565
|
1733 snd_seq_client_info_alloca( &cinfo );
|
Chris@565
|
1734 snd_seq_port_info_alloca( &pinfo );
|
Chris@559
|
1735
|
Chris@1397
|
1736 std::string stringName;
|
Chris@559
|
1737 AlsaMidiData *data = static_cast<AlsaMidiData *> (apiData_);
|
Chris@559
|
1738 if ( portInfo( data->seq, pinfo, SND_SEQ_PORT_CAP_WRITE|SND_SEQ_PORT_CAP_SUBS_WRITE, (int) portNumber ) ) {
|
Chris@565
|
1739 int cnum = snd_seq_port_info_get_client(pinfo);
|
Chris@565
|
1740 snd_seq_get_any_client_info( data->seq, cnum, cinfo );
|
Chris@565
|
1741 std::ostringstream os;
|
Chris@565
|
1742 os << snd_seq_client_info_get_name(cinfo);
|
Chris@1397
|
1743 os << " "; // These lines added to make sure devices are listed
|
Chris@1397
|
1744 os << snd_seq_port_info_get_client( pinfo ); // with full portnames added to ensure individual device names
|
Chris@565
|
1745 os << ":";
|
Chris@565
|
1746 os << snd_seq_port_info_get_port(pinfo);
|
Chris@1397
|
1747 stringName = os.str();
|
Chris@559
|
1748 return stringName;
|
Chris@559
|
1749 }
|
Chris@559
|
1750
|
Chris@559
|
1751 // If we get here, we didn't find a match.
|
Chris@1397
|
1752 errorString_ = "MidiOutAlsa::getPortName: error looking for port name!";
|
Chris@1397
|
1753 error( RtMidiError::WARNING, errorString_ );
|
Chris@1397
|
1754 return stringName;
|
Chris@559
|
1755 }
|
Chris@559
|
1756
|
Chris@1397
|
1757 void MidiOutAlsa :: openPort( unsigned int portNumber, const std::string portName )
|
Chris@559
|
1758 {
|
Chris@559
|
1759 if ( connected_ ) {
|
Chris@1397
|
1760 errorString_ = "MidiOutAlsa::openPort: a valid connection already exists!";
|
Chris@1397
|
1761 error( RtMidiError::WARNING, errorString_ );
|
Chris@559
|
1762 return;
|
Chris@559
|
1763 }
|
Chris@559
|
1764
|
Chris@559
|
1765 unsigned int nSrc = this->getPortCount();
|
Chris@559
|
1766 if (nSrc < 1) {
|
Chris@1397
|
1767 errorString_ = "MidiOutAlsa::openPort: no MIDI output sources found!";
|
Chris@1397
|
1768 error( RtMidiError::NO_DEVICES_FOUND, errorString_ );
|
Chris@1397
|
1769 return;
|
Chris@559
|
1770 }
|
Chris@559
|
1771
|
Chris@565
|
1772 snd_seq_port_info_t *pinfo;
|
Chris@565
|
1773 snd_seq_port_info_alloca( &pinfo );
|
Chris@559
|
1774 AlsaMidiData *data = static_cast<AlsaMidiData *> (apiData_);
|
Chris@559
|
1775 if ( portInfo( data->seq, pinfo, SND_SEQ_PORT_CAP_WRITE|SND_SEQ_PORT_CAP_SUBS_WRITE, (int) portNumber ) == 0 ) {
|
Chris@1397
|
1776 std::ostringstream ost;
|
Chris@1397
|
1777 ost << "MidiOutAlsa::openPort: the 'portNumber' argument (" << portNumber << ") is invalid.";
|
Chris@559
|
1778 errorString_ = ost.str();
|
Chris@1397
|
1779 error( RtMidiError::INVALID_PARAMETER, errorString_ );
|
Chris@1397
|
1780 return;
|
Chris@559
|
1781 }
|
Chris@559
|
1782
|
Chris@559
|
1783 snd_seq_addr_t sender, receiver;
|
Chris@1397
|
1784 receiver.client = snd_seq_port_info_get_client( pinfo );
|
Chris@1397
|
1785 receiver.port = snd_seq_port_info_get_port( pinfo );
|
Chris@1397
|
1786 sender.client = snd_seq_client_id( data->seq );
|
Chris@559
|
1787
|
Chris@559
|
1788 if ( data->vport < 0 ) {
|
Chris@565
|
1789 data->vport = snd_seq_create_simple_port( data->seq, portName.c_str(),
|
Chris@559
|
1790 SND_SEQ_PORT_CAP_READ|SND_SEQ_PORT_CAP_SUBS_READ,
|
Chris@1397
|
1791 SND_SEQ_PORT_TYPE_MIDI_GENERIC|SND_SEQ_PORT_TYPE_APPLICATION );
|
Chris@559
|
1792 if ( data->vport < 0 ) {
|
Chris@1397
|
1793 errorString_ = "MidiOutAlsa::openPort: ALSA error creating output port.";
|
Chris@1397
|
1794 error( RtMidiError::DRIVER_ERROR, errorString_ );
|
Chris@1397
|
1795 return;
|
Chris@559
|
1796 }
|
Chris@559
|
1797 }
|
Chris@559
|
1798
|
Chris@1397
|
1799 sender.port = data->vport;
|
Chris@559
|
1800
|
Chris@559
|
1801 // Make subscription
|
Chris@1397
|
1802 if (snd_seq_port_subscribe_malloc( &data->subscription ) < 0) {
|
Chris@1397
|
1803 snd_seq_port_subscribe_free( data->subscription );
|
Chris@1397
|
1804 errorString_ = "MidiOutAlsa::openPort: error allocating port subscription.";
|
Chris@1397
|
1805 error( RtMidiError::DRIVER_ERROR, errorString_ );
|
Chris@1397
|
1806 return;
|
Chris@1397
|
1807 }
|
Chris@559
|
1808 snd_seq_port_subscribe_set_sender(data->subscription, &sender);
|
Chris@559
|
1809 snd_seq_port_subscribe_set_dest(data->subscription, &receiver);
|
Chris@559
|
1810 snd_seq_port_subscribe_set_time_update(data->subscription, 1);
|
Chris@559
|
1811 snd_seq_port_subscribe_set_time_real(data->subscription, 1);
|
Chris@559
|
1812 if ( snd_seq_subscribe_port(data->seq, data->subscription) ) {
|
Chris@1397
|
1813 snd_seq_port_subscribe_free( data->subscription );
|
Chris@1397
|
1814 errorString_ = "MidiOutAlsa::openPort: ALSA error making port connection.";
|
Chris@1397
|
1815 error( RtMidiError::DRIVER_ERROR, errorString_ );
|
Chris@1397
|
1816 return;
|
Chris@559
|
1817 }
|
Chris@559
|
1818
|
Chris@559
|
1819 connected_ = true;
|
Chris@559
|
1820 }
|
Chris@559
|
1821
|
Chris@1397
|
1822 void MidiOutAlsa :: closePort( void )
|
Chris@559
|
1823 {
|
Chris@559
|
1824 if ( connected_ ) {
|
Chris@559
|
1825 AlsaMidiData *data = static_cast<AlsaMidiData *> (apiData_);
|
Chris@559
|
1826 snd_seq_unsubscribe_port( data->seq, data->subscription );
|
Chris@559
|
1827 snd_seq_port_subscribe_free( data->subscription );
|
Chris@559
|
1828 connected_ = false;
|
Chris@559
|
1829 }
|
Chris@559
|
1830 }
|
Chris@559
|
1831
|
Chris@1397
|
1832 void MidiOutAlsa :: openVirtualPort( std::string portName )
|
Chris@559
|
1833 {
|
Chris@559
|
1834 AlsaMidiData *data = static_cast<AlsaMidiData *> (apiData_);
|
Chris@559
|
1835 if ( data->vport < 0 ) {
|
Chris@559
|
1836 data->vport = snd_seq_create_simple_port( data->seq, portName.c_str(),
|
Chris@559
|
1837 SND_SEQ_PORT_CAP_READ|SND_SEQ_PORT_CAP_SUBS_READ,
|
Chris@1397
|
1838 SND_SEQ_PORT_TYPE_MIDI_GENERIC|SND_SEQ_PORT_TYPE_APPLICATION );
|
Chris@559
|
1839
|
Chris@559
|
1840 if ( data->vport < 0 ) {
|
Chris@1397
|
1841 errorString_ = "MidiOutAlsa::openVirtualPort: ALSA error creating virtual port.";
|
Chris@1397
|
1842 error( RtMidiError::DRIVER_ERROR, errorString_ );
|
Chris@559
|
1843 }
|
Chris@559
|
1844 }
|
Chris@559
|
1845 }
|
Chris@559
|
1846
|
Chris@1397
|
1847 void MidiOutAlsa :: sendMessage( std::vector<unsigned char> *message )
|
Chris@559
|
1848 {
|
Chris@559
|
1849 int result;
|
Chris@559
|
1850 AlsaMidiData *data = static_cast<AlsaMidiData *> (apiData_);
|
Chris@1397
|
1851 unsigned int nBytes = message->size();
|
Chris@559
|
1852 if ( nBytes > data->bufferSize ) {
|
Chris@559
|
1853 data->bufferSize = nBytes;
|
Chris@559
|
1854 result = snd_midi_event_resize_buffer ( data->coder, nBytes);
|
Chris@559
|
1855 if ( result != 0 ) {
|
Chris@1397
|
1856 errorString_ = "MidiOutAlsa::sendMessage: ALSA error resizing MIDI event buffer.";
|
Chris@1397
|
1857 error( RtMidiError::DRIVER_ERROR, errorString_ );
|
Chris@1397
|
1858 return;
|
Chris@559
|
1859 }
|
Chris@559
|
1860 free (data->buffer);
|
Chris@559
|
1861 data->buffer = (unsigned char *) malloc( data->bufferSize );
|
Chris@559
|
1862 if ( data->buffer == NULL ) {
|
Chris@1397
|
1863 errorString_ = "MidiOutAlsa::initialize: error allocating buffer memory!\n\n";
|
Chris@1397
|
1864 error( RtMidiError::MEMORY_ERROR, errorString_ );
|
Chris@1397
|
1865 return;
|
Chris@559
|
1866 }
|
Chris@559
|
1867 }
|
Chris@559
|
1868
|
Chris@559
|
1869 snd_seq_event_t ev;
|
Chris@559
|
1870 snd_seq_ev_clear(&ev);
|
Chris@1397
|
1871 snd_seq_ev_set_source(&ev, data->vport);
|
Chris@559
|
1872 snd_seq_ev_set_subs(&ev);
|
Chris@559
|
1873 snd_seq_ev_set_direct(&ev);
|
Chris@1397
|
1874 for ( unsigned int i=0; i<nBytes; ++i ) data->buffer[i] = message->at(i);
|
Chris@1397
|
1875 result = snd_midi_event_encode( data->coder, data->buffer, (long)nBytes, &ev );
|
Chris@559
|
1876 if ( result < (int)nBytes ) {
|
Chris@1397
|
1877 errorString_ = "MidiOutAlsa::sendMessage: event parsing error!";
|
Chris@1397
|
1878 error( RtMidiError::WARNING, errorString_ );
|
Chris@559
|
1879 return;
|
Chris@559
|
1880 }
|
Chris@559
|
1881
|
Chris@559
|
1882 // Send the event.
|
Chris@559
|
1883 result = snd_seq_event_output(data->seq, &ev);
|
Chris@559
|
1884 if ( result < 0 ) {
|
Chris@1397
|
1885 errorString_ = "MidiOutAlsa::sendMessage: error sending MIDI message to port.";
|
Chris@1397
|
1886 error( RtMidiError::WARNING, errorString_ );
|
Chris@1397
|
1887 return;
|
Chris@559
|
1888 }
|
Chris@559
|
1889 snd_seq_drain_output(data->seq);
|
Chris@559
|
1890 }
|
Chris@559
|
1891
|
Chris@559
|
1892 #endif // __LINUX_ALSA__
|
Chris@559
|
1893
|
Chris@559
|
1894
|
Chris@559
|
1895 //*********************************************************************//
|
Chris@559
|
1896 // API: Windows Multimedia Library (MM)
|
Chris@559
|
1897 //*********************************************************************//
|
Chris@559
|
1898
|
Chris@559
|
1899 // API information deciphered from:
|
Chris@559
|
1900 // - http://msdn.microsoft.com/library/default.asp?url=/library/en-us/multimed/htm/_win32_midi_reference.asp
|
Chris@559
|
1901
|
Chris@559
|
1902 // Thanks to Jean-Baptiste Berruchon for the sysex code.
|
Chris@559
|
1903
|
Chris@559
|
1904 #if defined(__WINDOWS_MM__)
|
Chris@559
|
1905
|
Chris@559
|
1906 // The Windows MM API is based on the use of a callback function for
|
Chris@559
|
1907 // MIDI input. We convert the system specific time stamps to delta
|
Chris@559
|
1908 // time values.
|
Chris@559
|
1909
|
Chris@559
|
1910 // Windows MM MIDI header files.
|
Chris@559
|
1911 #include <windows.h>
|
Chris@559
|
1912 #include <mmsystem.h>
|
Chris@559
|
1913
|
Chris@1397
|
1914 #define RT_SYSEX_BUFFER_SIZE 1024
|
Chris@1397
|
1915 #define RT_SYSEX_BUFFER_COUNT 4
|
Chris@1397
|
1916
|
Chris@559
|
1917 // A structure to hold variables related to the CoreMIDI API
|
Chris@559
|
1918 // implementation.
|
Chris@559
|
1919 struct WinMidiData {
|
Chris@559
|
1920 HMIDIIN inHandle; // Handle to Midi Input Device
|
Chris@559
|
1921 HMIDIOUT outHandle; // Handle to Midi Output Device
|
Chris@559
|
1922 DWORD lastTime;
|
Chris@1397
|
1923 MidiInApi::MidiMessage message;
|
Chris@1397
|
1924 LPMIDIHDR sysexBuffer[RT_SYSEX_BUFFER_COUNT];
|
Chris@1397
|
1925 CRITICAL_SECTION _mutex; // [Patrice] see https://groups.google.com/forum/#!topic/mididev/6OUjHutMpEo
|
Chris@559
|
1926 };
|
Chris@559
|
1927
|
Chris@559
|
1928 //*********************************************************************//
|
Chris@559
|
1929 // API: Windows MM
|
Chris@1397
|
1930 // Class Definitions: MidiInWinMM
|
Chris@559
|
1931 //*********************************************************************//
|
Chris@559
|
1932
|
Chris@1397
|
1933 static void CALLBACK midiInputCallback( HMIDIIN /*hmin*/,
|
Chris@559
|
1934 UINT inputStatus,
|
Chris@1397
|
1935 DWORD_PTR instancePtr,
|
Chris@1397
|
1936 DWORD_PTR midiMessage,
|
Chris@559
|
1937 DWORD timestamp )
|
Chris@559
|
1938 {
|
Chris@1397
|
1939 if ( inputStatus != MIM_DATA && inputStatus != MIM_LONGDATA && inputStatus != MIM_LONGERROR ) return;
|
Chris@1397
|
1940
|
Chris@1397
|
1941 //MidiInApi::RtMidiInData *data = static_cast<MidiInApi::RtMidiInData *> (instancePtr);
|
Chris@1397
|
1942 MidiInApi::RtMidiInData *data = (MidiInApi::RtMidiInData *)instancePtr;
|
Chris@559
|
1943 WinMidiData *apiData = static_cast<WinMidiData *> (data->apiData);
|
Chris@559
|
1944
|
Chris@559
|
1945 // Calculate time stamp.
|
Chris@1397
|
1946 if ( data->firstMessage == true ) {
|
Chris@1397
|
1947 apiData->message.timeStamp = 0.0;
|
Chris@1397
|
1948 data->firstMessage = false;
|
Chris@1397
|
1949 }
|
Chris@559
|
1950 else apiData->message.timeStamp = (double) ( timestamp - apiData->lastTime ) * 0.001;
|
Chris@559
|
1951 apiData->lastTime = timestamp;
|
Chris@559
|
1952
|
Chris@559
|
1953 if ( inputStatus == MIM_DATA ) { // Channel or system message
|
Chris@559
|
1954
|
Chris@559
|
1955 // Make sure the first byte is a status byte.
|
Chris@559
|
1956 unsigned char status = (unsigned char) (midiMessage & 0x000000FF);
|
Chris@559
|
1957 if ( !(status & 0x80) ) return;
|
Chris@559
|
1958
|
Chris@559
|
1959 // Determine the number of bytes in the MIDI message.
|
Chris@559
|
1960 unsigned short nBytes = 1;
|
Chris@559
|
1961 if ( status < 0xC0 ) nBytes = 3;
|
Chris@559
|
1962 else if ( status < 0xE0 ) nBytes = 2;
|
Chris@559
|
1963 else if ( status < 0xF0 ) nBytes = 3;
|
Chris@1397
|
1964 else if ( status == 0xF1 ) {
|
Chris@1397
|
1965 if ( data->ignoreFlags & 0x02 ) return;
|
Chris@1397
|
1966 else nBytes = 2;
|
Chris@559
|
1967 }
|
Chris@1397
|
1968 else if ( status == 0xF2 ) nBytes = 3;
|
Chris@559
|
1969 else if ( status == 0xF3 ) nBytes = 2;
|
Chris@559
|
1970 else if ( status == 0xF8 && (data->ignoreFlags & 0x02) ) {
|
Chris@559
|
1971 // A MIDI timing tick message and we're ignoring it.
|
Chris@559
|
1972 return;
|
Chris@559
|
1973 }
|
Chris@559
|
1974 else if ( status == 0xFE && (data->ignoreFlags & 0x04) ) {
|
Chris@559
|
1975 // A MIDI active sensing message and we're ignoring it.
|
Chris@559
|
1976 return;
|
Chris@559
|
1977 }
|
Chris@559
|
1978
|
Chris@559
|
1979 // Copy bytes to our MIDI message.
|
Chris@559
|
1980 unsigned char *ptr = (unsigned char *) &midiMessage;
|
Chris@1397
|
1981 for ( int i=0; i<nBytes; ++i ) apiData->message.bytes.push_back( *ptr++ );
|
Chris@559
|
1982 }
|
Chris@1397
|
1983 else { // Sysex message ( MIM_LONGDATA or MIM_LONGERROR )
|
Chris@565
|
1984 MIDIHDR *sysex = ( MIDIHDR *) midiMessage;
|
Chris@1397
|
1985 if ( !( data->ignoreFlags & 0x01 ) && inputStatus != MIM_LONGERROR ) {
|
Chris@565
|
1986 // Sysex message and we're not ignoring it
|
Chris@1397
|
1987 for ( int i=0; i<(int)sysex->dwBytesRecorded; ++i )
|
Chris@565
|
1988 apiData->message.bytes.push_back( sysex->lpData[i] );
|
Chris@565
|
1989 }
|
Chris@559
|
1990
|
Chris@565
|
1991 // The WinMM API requires that the sysex buffer be requeued after
|
Chris@565
|
1992 // input of each sysex message. Even if we are ignoring sysex
|
Chris@565
|
1993 // messages, we still need to requeue the buffer in case the user
|
Chris@565
|
1994 // decides to not ignore sysex messages in the future. However,
|
Chris@565
|
1995 // it seems that WinMM calls this function with an empty sysex
|
Chris@565
|
1996 // buffer when an application closes and in this case, we should
|
Chris@565
|
1997 // avoid requeueing it, else the computer suddenly reboots after
|
Chris@565
|
1998 // one or two minutes.
|
Chris@1397
|
1999 if ( apiData->sysexBuffer[sysex->dwUser]->dwBytesRecorded > 0 ) {
|
Chris@565
|
2000 //if ( sysex->dwBytesRecorded > 0 ) {
|
Chris@1397
|
2001 EnterCriticalSection( &(apiData->_mutex) );
|
Chris@1397
|
2002 MMRESULT result = midiInAddBuffer( apiData->inHandle, apiData->sysexBuffer[sysex->dwUser], sizeof(MIDIHDR) );
|
Chris@1397
|
2003 LeaveCriticalSection( &(apiData->_mutex) );
|
Chris@559
|
2004 if ( result != MMSYSERR_NOERROR )
|
Chris@1397
|
2005 std::cerr << "\nRtMidiIn::midiInputCallback: error sending sysex to Midi device!!\n\n";
|
Chris@565
|
2006
|
Chris@565
|
2007 if ( data->ignoreFlags & 0x01 ) return;
|
Chris@559
|
2008 }
|
Chris@565
|
2009 else return;
|
Chris@559
|
2010 }
|
Chris@559
|
2011
|
Chris@559
|
2012 if ( data->usingCallback ) {
|
Chris@559
|
2013 RtMidiIn::RtMidiCallback callback = (RtMidiIn::RtMidiCallback) data->userCallback;
|
Chris@559
|
2014 callback( apiData->message.timeStamp, &apiData->message.bytes, data->userData );
|
Chris@559
|
2015 }
|
Chris@559
|
2016 else {
|
Chris@559
|
2017 // As long as we haven't reached our queue size limit, push the message.
|
Chris@1397
|
2018 if ( data->queue.size < data->queue.ringSize ) {
|
Chris@1397
|
2019 data->queue.ring[data->queue.back++] = apiData->message;
|
Chris@1397
|
2020 if ( data->queue.back == data->queue.ringSize )
|
Chris@1397
|
2021 data->queue.back = 0;
|
Chris@1397
|
2022 data->queue.size++;
|
Chris@1397
|
2023 }
|
Chris@559
|
2024 else
|
Chris@1397
|
2025 std::cerr << "\nRtMidiIn: message queue limit reached!!\n\n";
|
Chris@559
|
2026 }
|
Chris@559
|
2027
|
Chris@565
|
2028 // Clear the vector for the next input message.
|
Chris@559
|
2029 apiData->message.bytes.clear();
|
Chris@559
|
2030 }
|
Chris@559
|
2031
|
Chris@1397
|
2032 MidiInWinMM :: MidiInWinMM( const std::string clientName, unsigned int queueSizeLimit ) : MidiInApi( queueSizeLimit )
|
Chris@1397
|
2033 {
|
Chris@1397
|
2034 initialize( clientName );
|
Chris@1397
|
2035 }
|
Chris@1397
|
2036
|
Chris@1397
|
2037 MidiInWinMM :: ~MidiInWinMM()
|
Chris@1397
|
2038 {
|
Chris@1397
|
2039 // Close a connection if it exists.
|
Chris@1397
|
2040 closePort();
|
Chris@1397
|
2041
|
Chris@1397
|
2042 WinMidiData *data = static_cast<WinMidiData *> (apiData_);
|
Chris@1397
|
2043 DeleteCriticalSection( &(data->_mutex) );
|
Chris@1397
|
2044
|
Chris@1397
|
2045 // Cleanup.
|
Chris@1397
|
2046 delete data;
|
Chris@1397
|
2047 }
|
Chris@1397
|
2048
|
Chris@1397
|
2049 void MidiInWinMM :: initialize( const std::string& /*clientName*/ )
|
Chris@559
|
2050 {
|
Chris@559
|
2051 // We'll issue a warning here if no devices are available but not
|
Chris@559
|
2052 // throw an error since the user can plugin something later.
|
Chris@559
|
2053 unsigned int nDevices = midiInGetNumDevs();
|
Chris@559
|
2054 if ( nDevices == 0 ) {
|
Chris@1397
|
2055 errorString_ = "MidiInWinMM::initialize: no MIDI input devices currently available.";
|
Chris@1397
|
2056 error( RtMidiError::WARNING, errorString_ );
|
Chris@559
|
2057 }
|
Chris@559
|
2058
|
Chris@559
|
2059 // Save our api-specific connection information.
|
Chris@559
|
2060 WinMidiData *data = (WinMidiData *) new WinMidiData;
|
Chris@559
|
2061 apiData_ = (void *) data;
|
Chris@559
|
2062 inputData_.apiData = (void *) data;
|
Chris@559
|
2063 data->message.bytes.clear(); // needs to be empty for first input message
|
Chris@1397
|
2064
|
Chris@1397
|
2065 if ( !InitializeCriticalSectionAndSpinCount(&(data->_mutex), 0x00000400) ) {
|
Chris@1397
|
2066 errorString_ = "MidiInWinMM::initialize: InitializeCriticalSectionAndSpinCount failed.";
|
Chris@1397
|
2067 error( RtMidiError::WARNING, errorString_ );
|
Chris@1397
|
2068 }
|
Chris@559
|
2069 }
|
Chris@559
|
2070
|
Chris@1397
|
2071 void MidiInWinMM :: openPort( unsigned int portNumber, const std::string /*portName*/ )
|
Chris@559
|
2072 {
|
Chris@559
|
2073 if ( connected_ ) {
|
Chris@1397
|
2074 errorString_ = "MidiInWinMM::openPort: a valid connection already exists!";
|
Chris@1397
|
2075 error( RtMidiError::WARNING, errorString_ );
|
Chris@559
|
2076 return;
|
Chris@559
|
2077 }
|
Chris@559
|
2078
|
Chris@559
|
2079 unsigned int nDevices = midiInGetNumDevs();
|
Chris@559
|
2080 if (nDevices == 0) {
|
Chris@1397
|
2081 errorString_ = "MidiInWinMM::openPort: no MIDI input sources found!";
|
Chris@1397
|
2082 error( RtMidiError::NO_DEVICES_FOUND, errorString_ );
|
Chris@1397
|
2083 return;
|
Chris@559
|
2084 }
|
Chris@559
|
2085
|
Chris@559
|
2086 if ( portNumber >= nDevices ) {
|
Chris@1397
|
2087 std::ostringstream ost;
|
Chris@1397
|
2088 ost << "MidiInWinMM::openPort: the 'portNumber' argument (" << portNumber << ") is invalid.";
|
Chris@559
|
2089 errorString_ = ost.str();
|
Chris@1397
|
2090 error( RtMidiError::INVALID_PARAMETER, errorString_ );
|
Chris@1397
|
2091 return;
|
Chris@559
|
2092 }
|
Chris@559
|
2093
|
Chris@559
|
2094 WinMidiData *data = static_cast<WinMidiData *> (apiData_);
|
Chris@559
|
2095 MMRESULT result = midiInOpen( &data->inHandle,
|
Chris@559
|
2096 portNumber,
|
Chris@1397
|
2097 (DWORD_PTR)&midiInputCallback,
|
Chris@1397
|
2098 (DWORD_PTR)&inputData_,
|
Chris@559
|
2099 CALLBACK_FUNCTION );
|
Chris@559
|
2100 if ( result != MMSYSERR_NOERROR ) {
|
Chris@1397
|
2101 errorString_ = "MidiInWinMM::openPort: error creating Windows MM MIDI input port.";
|
Chris@1397
|
2102 error( RtMidiError::DRIVER_ERROR, errorString_ );
|
Chris@1397
|
2103 return;
|
Chris@559
|
2104 }
|
Chris@559
|
2105
|
Chris@1397
|
2106 // Allocate and init the sysex buffers.
|
Chris@1397
|
2107 for ( int i=0; i<RT_SYSEX_BUFFER_COUNT; ++i ) {
|
Chris@1397
|
2108 data->sysexBuffer[i] = (MIDIHDR*) new char[ sizeof(MIDIHDR) ];
|
Chris@1397
|
2109 data->sysexBuffer[i]->lpData = new char[ RT_SYSEX_BUFFER_SIZE ];
|
Chris@1397
|
2110 data->sysexBuffer[i]->dwBufferLength = RT_SYSEX_BUFFER_SIZE;
|
Chris@1397
|
2111 data->sysexBuffer[i]->dwUser = i; // We use the dwUser parameter as buffer indicator
|
Chris@1397
|
2112 data->sysexBuffer[i]->dwFlags = 0;
|
Chris@1397
|
2113
|
Chris@1397
|
2114 result = midiInPrepareHeader( data->inHandle, data->sysexBuffer[i], sizeof(MIDIHDR) );
|
Chris@1397
|
2115 if ( result != MMSYSERR_NOERROR ) {
|
Chris@1397
|
2116 midiInClose( data->inHandle );
|
Chris@1397
|
2117 errorString_ = "MidiInWinMM::openPort: error starting Windows MM MIDI input port (PrepareHeader).";
|
Chris@1397
|
2118 error( RtMidiError::DRIVER_ERROR, errorString_ );
|
Chris@1397
|
2119 return;
|
Chris@1397
|
2120 }
|
Chris@1397
|
2121
|
Chris@1397
|
2122 // Register the buffer.
|
Chris@1397
|
2123 result = midiInAddBuffer( data->inHandle, data->sysexBuffer[i], sizeof(MIDIHDR) );
|
Chris@1397
|
2124 if ( result != MMSYSERR_NOERROR ) {
|
Chris@1397
|
2125 midiInClose( data->inHandle );
|
Chris@1397
|
2126 errorString_ = "MidiInWinMM::openPort: error starting Windows MM MIDI input port (AddBuffer).";
|
Chris@1397
|
2127 error( RtMidiError::DRIVER_ERROR, errorString_ );
|
Chris@1397
|
2128 return;
|
Chris@1397
|
2129 }
|
Chris@559
|
2130 }
|
Chris@559
|
2131
|
Chris@559
|
2132 result = midiInStart( data->inHandle );
|
Chris@559
|
2133 if ( result != MMSYSERR_NOERROR ) {
|
Chris@559
|
2134 midiInClose( data->inHandle );
|
Chris@1397
|
2135 errorString_ = "MidiInWinMM::openPort: error starting Windows MM MIDI input port.";
|
Chris@1397
|
2136 error( RtMidiError::DRIVER_ERROR, errorString_ );
|
Chris@1397
|
2137 return;
|
Chris@559
|
2138 }
|
Chris@559
|
2139
|
Chris@559
|
2140 connected_ = true;
|
Chris@559
|
2141 }
|
Chris@559
|
2142
|
Chris@1397
|
2143 void MidiInWinMM :: openVirtualPort( std::string /*portName*/ )
|
Chris@559
|
2144 {
|
Chris@559
|
2145 // This function cannot be implemented for the Windows MM MIDI API.
|
Chris@1397
|
2146 errorString_ = "MidiInWinMM::openVirtualPort: cannot be implemented in Windows MM MIDI API!";
|
Chris@1397
|
2147 error( RtMidiError::WARNING, errorString_ );
|
Chris@559
|
2148 }
|
Chris@559
|
2149
|
Chris@1397
|
2150 void MidiInWinMM :: closePort( void )
|
Chris@559
|
2151 {
|
Chris@559
|
2152 if ( connected_ ) {
|
Chris@559
|
2153 WinMidiData *data = static_cast<WinMidiData *> (apiData_);
|
Chris@1397
|
2154 EnterCriticalSection( &(data->_mutex) );
|
Chris@559
|
2155 midiInReset( data->inHandle );
|
Chris@559
|
2156 midiInStop( data->inHandle );
|
Chris@559
|
2157
|
Chris@1397
|
2158 for ( int i=0; i<RT_SYSEX_BUFFER_COUNT; ++i ) {
|
Chris@1397
|
2159 int result = midiInUnprepareHeader(data->inHandle, data->sysexBuffer[i], sizeof(MIDIHDR));
|
Chris@1397
|
2160 delete [] data->sysexBuffer[i]->lpData;
|
Chris@1397
|
2161 delete [] data->sysexBuffer[i];
|
Chris@1397
|
2162 if ( result != MMSYSERR_NOERROR ) {
|
Chris@1397
|
2163 midiInClose( data->inHandle );
|
Chris@1397
|
2164 errorString_ = "MidiInWinMM::openPort: error closing Windows MM MIDI input port (midiInUnprepareHeader).";
|
Chris@1397
|
2165 error( RtMidiError::DRIVER_ERROR, errorString_ );
|
Chris@1397
|
2166 return;
|
Chris@1397
|
2167 }
|
Chris@559
|
2168 }
|
Chris@559
|
2169
|
Chris@559
|
2170 midiInClose( data->inHandle );
|
Chris@559
|
2171 connected_ = false;
|
Chris@1397
|
2172 LeaveCriticalSection( &(data->_mutex) );
|
Chris@559
|
2173 }
|
Chris@559
|
2174 }
|
Chris@559
|
2175
|
Chris@1397
|
2176 unsigned int MidiInWinMM :: getPortCount()
|
Chris@1397
|
2177 {
|
Chris@1397
|
2178 return midiInGetNumDevs();
|
Chris@1397
|
2179 }
|
Chris@1397
|
2180
|
Chris@1397
|
2181 std::string MidiInWinMM :: getPortName( unsigned int portNumber )
|
Chris@1397
|
2182 {
|
Chris@1397
|
2183 std::string stringName;
|
Chris@1397
|
2184 unsigned int nDevices = midiInGetNumDevs();
|
Chris@1397
|
2185 if ( portNumber >= nDevices ) {
|
Chris@1397
|
2186 std::ostringstream ost;
|
Chris@1397
|
2187 ost << "MidiInWinMM::getPortName: the 'portNumber' argument (" << portNumber << ") is invalid.";
|
Chris@1397
|
2188 errorString_ = ost.str();
|
Chris@1397
|
2189 error( RtMidiError::WARNING, errorString_ );
|
Chris@1397
|
2190 return stringName;
|
Chris@1397
|
2191 }
|
Chris@1397
|
2192
|
Chris@1397
|
2193 MIDIINCAPS deviceCaps;
|
Chris@1397
|
2194 midiInGetDevCaps( portNumber, &deviceCaps, sizeof(MIDIINCAPS));
|
Chris@1397
|
2195
|
Chris@1397
|
2196 #if defined( UNICODE ) || defined( _UNICODE )
|
Chris@1397
|
2197 int length = WideCharToMultiByte(CP_UTF8, 0, deviceCaps.szPname, -1, NULL, 0, NULL, NULL) - 1;
|
Chris@1397
|
2198 stringName.assign( length, 0 );
|
Chris@1397
|
2199 length = WideCharToMultiByte(CP_UTF8, 0, deviceCaps.szPname, static_cast<int>(wcslen(deviceCaps.szPname)), &stringName[0], length, NULL, NULL);
|
Chris@1397
|
2200 #else
|
Chris@1397
|
2201 stringName = std::string( deviceCaps.szPname );
|
Chris@1397
|
2202 #endif
|
Chris@1397
|
2203
|
Chris@1397
|
2204 // Next lines added to add the portNumber to the name so that
|
Chris@1397
|
2205 // the device's names are sure to be listed with individual names
|
Chris@1397
|
2206 // even when they have the same brand name
|
Chris@1397
|
2207 std::ostringstream os;
|
Chris@1397
|
2208 os << " ";
|
Chris@1397
|
2209 os << portNumber;
|
Chris@1397
|
2210 stringName += os.str();
|
Chris@1397
|
2211
|
Chris@1397
|
2212 return stringName;
|
Chris@1397
|
2213 }
|
Chris@1397
|
2214
|
Chris@1397
|
2215 //*********************************************************************//
|
Chris@1397
|
2216 // API: Windows MM
|
Chris@1397
|
2217 // Class Definitions: MidiOutWinMM
|
Chris@1397
|
2218 //*********************************************************************//
|
Chris@1397
|
2219
|
Chris@1397
|
2220 MidiOutWinMM :: MidiOutWinMM( const std::string clientName ) : MidiOutApi()
|
Chris@1397
|
2221 {
|
Chris@1397
|
2222 initialize( clientName );
|
Chris@1397
|
2223 }
|
Chris@1397
|
2224
|
Chris@1397
|
2225 MidiOutWinMM :: ~MidiOutWinMM()
|
Chris@559
|
2226 {
|
Chris@559
|
2227 // Close a connection if it exists.
|
Chris@559
|
2228 closePort();
|
Chris@559
|
2229
|
Chris@559
|
2230 // Cleanup.
|
Chris@559
|
2231 WinMidiData *data = static_cast<WinMidiData *> (apiData_);
|
Chris@559
|
2232 delete data;
|
Chris@559
|
2233 }
|
Chris@559
|
2234
|
Chris@1397
|
2235 void MidiOutWinMM :: initialize( const std::string& /*clientName*/ )
|
Chris@559
|
2236 {
|
Chris@559
|
2237 // We'll issue a warning here if no devices are available but not
|
Chris@559
|
2238 // throw an error since the user can plug something in later.
|
Chris@559
|
2239 unsigned int nDevices = midiOutGetNumDevs();
|
Chris@559
|
2240 if ( nDevices == 0 ) {
|
Chris@1397
|
2241 errorString_ = "MidiOutWinMM::initialize: no MIDI output devices currently available.";
|
Chris@1397
|
2242 error( RtMidiError::WARNING, errorString_ );
|
Chris@559
|
2243 }
|
Chris@559
|
2244
|
Chris@559
|
2245 // Save our api-specific connection information.
|
Chris@559
|
2246 WinMidiData *data = (WinMidiData *) new WinMidiData;
|
Chris@559
|
2247 apiData_ = (void *) data;
|
Chris@559
|
2248 }
|
Chris@559
|
2249
|
Chris@1397
|
2250 unsigned int MidiOutWinMM :: getPortCount()
|
Chris@1397
|
2251 {
|
Chris@1397
|
2252 return midiOutGetNumDevs();
|
Chris@1397
|
2253 }
|
Chris@1397
|
2254
|
Chris@1397
|
2255 std::string MidiOutWinMM :: getPortName( unsigned int portNumber )
|
Chris@1397
|
2256 {
|
Chris@1397
|
2257 std::string stringName;
|
Chris@1397
|
2258 unsigned int nDevices = midiOutGetNumDevs();
|
Chris@1397
|
2259 if ( portNumber >= nDevices ) {
|
Chris@1397
|
2260 std::ostringstream ost;
|
Chris@1397
|
2261 ost << "MidiOutWinMM::getPortName: the 'portNumber' argument (" << portNumber << ") is invalid.";
|
Chris@1397
|
2262 errorString_ = ost.str();
|
Chris@1397
|
2263 error( RtMidiError::WARNING, errorString_ );
|
Chris@1397
|
2264 return stringName;
|
Chris@1397
|
2265 }
|
Chris@1397
|
2266
|
Chris@1397
|
2267 MIDIOUTCAPS deviceCaps;
|
Chris@1397
|
2268 midiOutGetDevCaps( portNumber, &deviceCaps, sizeof(MIDIOUTCAPS));
|
Chris@1397
|
2269
|
Chris@1397
|
2270 #if defined( UNICODE ) || defined( _UNICODE )
|
Chris@1397
|
2271 int length = WideCharToMultiByte(CP_UTF8, 0, deviceCaps.szPname, -1, NULL, 0, NULL, NULL) - 1;
|
Chris@1397
|
2272 stringName.assign( length, 0 );
|
Chris@1397
|
2273 length = WideCharToMultiByte(CP_UTF8, 0, deviceCaps.szPname, static_cast<int>(wcslen(deviceCaps.szPname)), &stringName[0], length, NULL, NULL);
|
Chris@1397
|
2274 #else
|
Chris@1397
|
2275 stringName = std::string( deviceCaps.szPname );
|
Chris@1397
|
2276 #endif
|
Chris@1397
|
2277
|
Chris@1397
|
2278 // Next lines added to add the portNumber to the name so that
|
Chris@1397
|
2279 // the device's names are sure to be listed with individual names
|
Chris@1397
|
2280 // even when they have the same brand name
|
Chris@1397
|
2281 std::ostringstream os;
|
Chris@1397
|
2282 os << " ";
|
Chris@1397
|
2283 os << portNumber;
|
Chris@1397
|
2284 stringName += os.str();
|
Chris@1397
|
2285
|
Chris@1397
|
2286 return stringName;
|
Chris@1397
|
2287 }
|
Chris@1397
|
2288
|
Chris@1397
|
2289 void MidiOutWinMM :: openPort( unsigned int portNumber, const std::string /*portName*/ )
|
Chris@559
|
2290 {
|
Chris@559
|
2291 if ( connected_ ) {
|
Chris@1397
|
2292 errorString_ = "MidiOutWinMM::openPort: a valid connection already exists!";
|
Chris@1397
|
2293 error( RtMidiError::WARNING, errorString_ );
|
Chris@559
|
2294 return;
|
Chris@559
|
2295 }
|
Chris@559
|
2296
|
Chris@559
|
2297 unsigned int nDevices = midiOutGetNumDevs();
|
Chris@559
|
2298 if (nDevices < 1) {
|
Chris@1397
|
2299 errorString_ = "MidiOutWinMM::openPort: no MIDI output destinations found!";
|
Chris@1397
|
2300 error( RtMidiError::NO_DEVICES_FOUND, errorString_ );
|
Chris@1397
|
2301 return;
|
Chris@559
|
2302 }
|
Chris@559
|
2303
|
Chris@559
|
2304 if ( portNumber >= nDevices ) {
|
Chris@1397
|
2305 std::ostringstream ost;
|
Chris@1397
|
2306 ost << "MidiOutWinMM::openPort: the 'portNumber' argument (" << portNumber << ") is invalid.";
|
Chris@559
|
2307 errorString_ = ost.str();
|
Chris@1397
|
2308 error( RtMidiError::INVALID_PARAMETER, errorString_ );
|
Chris@1397
|
2309 return;
|
Chris@559
|
2310 }
|
Chris@559
|
2311
|
Chris@559
|
2312 WinMidiData *data = static_cast<WinMidiData *> (apiData_);
|
Chris@559
|
2313 MMRESULT result = midiOutOpen( &data->outHandle,
|
Chris@559
|
2314 portNumber,
|
Chris@559
|
2315 (DWORD)NULL,
|
Chris@559
|
2316 (DWORD)NULL,
|
Chris@559
|
2317 CALLBACK_NULL );
|
Chris@559
|
2318 if ( result != MMSYSERR_NOERROR ) {
|
Chris@1397
|
2319 errorString_ = "MidiOutWinMM::openPort: error creating Windows MM MIDI output port.";
|
Chris@1397
|
2320 error( RtMidiError::DRIVER_ERROR, errorString_ );
|
Chris@1397
|
2321 return;
|
Chris@559
|
2322 }
|
Chris@559
|
2323
|
Chris@559
|
2324 connected_ = true;
|
Chris@559
|
2325 }
|
Chris@559
|
2326
|
Chris@1397
|
2327 void MidiOutWinMM :: closePort( void )
|
Chris@559
|
2328 {
|
Chris@559
|
2329 if ( connected_ ) {
|
Chris@559
|
2330 WinMidiData *data = static_cast<WinMidiData *> (apiData_);
|
Chris@559
|
2331 midiOutReset( data->outHandle );
|
Chris@559
|
2332 midiOutClose( data->outHandle );
|
Chris@559
|
2333 connected_ = false;
|
Chris@559
|
2334 }
|
Chris@559
|
2335 }
|
Chris@559
|
2336
|
Chris@1397
|
2337 void MidiOutWinMM :: openVirtualPort( std::string /*portName*/ )
|
Chris@559
|
2338 {
|
Chris@559
|
2339 // This function cannot be implemented for the Windows MM MIDI API.
|
Chris@1397
|
2340 errorString_ = "MidiOutWinMM::openVirtualPort: cannot be implemented in Windows MM MIDI API!";
|
Chris@1397
|
2341 error( RtMidiError::WARNING, errorString_ );
|
Chris@559
|
2342 }
|
Chris@559
|
2343
|
Chris@1397
|
2344 void MidiOutWinMM :: sendMessage( std::vector<unsigned char> *message )
|
Chris@559
|
2345 {
|
Chris@1397
|
2346 if ( !connected_ ) return;
|
Chris@1397
|
2347
|
Chris@1397
|
2348 unsigned int nBytes = static_cast<unsigned int>(message->size());
|
Chris@559
|
2349 if ( nBytes == 0 ) {
|
Chris@1397
|
2350 errorString_ = "MidiOutWinMM::sendMessage: message argument is empty!";
|
Chris@1397
|
2351 error( RtMidiError::WARNING, errorString_ );
|
Chris@559
|
2352 return;
|
Chris@559
|
2353 }
|
Chris@559
|
2354
|
Chris@559
|
2355 MMRESULT result;
|
Chris@559
|
2356 WinMidiData *data = static_cast<WinMidiData *> (apiData_);
|
Chris@559
|
2357 if ( message->at(0) == 0xF0 ) { // Sysex message
|
Chris@559
|
2358
|
Chris@559
|
2359 // Allocate buffer for sysex data.
|
Chris@559
|
2360 char *buffer = (char *) malloc( nBytes );
|
Chris@559
|
2361 if ( buffer == NULL ) {
|
Chris@1397
|
2362 errorString_ = "MidiOutWinMM::sendMessage: error allocating sysex message memory!";
|
Chris@1397
|
2363 error( RtMidiError::MEMORY_ERROR, errorString_ );
|
Chris@1397
|
2364 return;
|
Chris@559
|
2365 }
|
Chris@559
|
2366
|
Chris@559
|
2367 // Copy data to buffer.
|
Chris@1397
|
2368 for ( unsigned int i=0; i<nBytes; ++i ) buffer[i] = message->at(i);
|
Chris@559
|
2369
|
Chris@559
|
2370 // Create and prepare MIDIHDR structure.
|
Chris@559
|
2371 MIDIHDR sysex;
|
Chris@559
|
2372 sysex.lpData = (LPSTR) buffer;
|
Chris@559
|
2373 sysex.dwBufferLength = nBytes;
|
Chris@559
|
2374 sysex.dwFlags = 0;
|
Chris@559
|
2375 result = midiOutPrepareHeader( data->outHandle, &sysex, sizeof(MIDIHDR) );
|
Chris@559
|
2376 if ( result != MMSYSERR_NOERROR ) {
|
Chris@559
|
2377 free( buffer );
|
Chris@1397
|
2378 errorString_ = "MidiOutWinMM::sendMessage: error preparing sysex header.";
|
Chris@1397
|
2379 error( RtMidiError::DRIVER_ERROR, errorString_ );
|
Chris@1397
|
2380 return;
|
Chris@559
|
2381 }
|
Chris@559
|
2382
|
Chris@559
|
2383 // Send the message.
|
Chris@559
|
2384 result = midiOutLongMsg( data->outHandle, &sysex, sizeof(MIDIHDR) );
|
Chris@559
|
2385 if ( result != MMSYSERR_NOERROR ) {
|
Chris@559
|
2386 free( buffer );
|
Chris@1397
|
2387 errorString_ = "MidiOutWinMM::sendMessage: error sending sysex message.";
|
Chris@1397
|
2388 error( RtMidiError::DRIVER_ERROR, errorString_ );
|
Chris@1397
|
2389 return;
|
Chris@559
|
2390 }
|
Chris@559
|
2391
|
Chris@559
|
2392 // Unprepare the buffer and MIDIHDR.
|
Chris@559
|
2393 while ( MIDIERR_STILLPLAYING == midiOutUnprepareHeader( data->outHandle, &sysex, sizeof (MIDIHDR) ) ) Sleep( 1 );
|
Chris@559
|
2394 free( buffer );
|
Chris@559
|
2395 }
|
Chris@559
|
2396 else { // Channel or system message.
|
Chris@559
|
2397
|
Chris@559
|
2398 // Make sure the message size isn't too big.
|
Chris@559
|
2399 if ( nBytes > 3 ) {
|
Chris@1397
|
2400 errorString_ = "MidiOutWinMM::sendMessage: message size is greater than 3 bytes (and not sysex)!";
|
Chris@1397
|
2401 error( RtMidiError::WARNING, errorString_ );
|
Chris@559
|
2402 return;
|
Chris@559
|
2403 }
|
Chris@559
|
2404
|
Chris@559
|
2405 // Pack MIDI bytes into double word.
|
Chris@559
|
2406 DWORD packet;
|
Chris@559
|
2407 unsigned char *ptr = (unsigned char *) &packet;
|
Chris@1397
|
2408 for ( unsigned int i=0; i<nBytes; ++i ) {
|
Chris@559
|
2409 *ptr = message->at(i);
|
Chris@1397
|
2410 ++ptr;
|
Chris@559
|
2411 }
|
Chris@559
|
2412
|
Chris@559
|
2413 // Send the message immediately.
|
Chris@559
|
2414 result = midiOutShortMsg( data->outHandle, packet );
|
Chris@559
|
2415 if ( result != MMSYSERR_NOERROR ) {
|
Chris@1397
|
2416 errorString_ = "MidiOutWinMM::sendMessage: error sending MIDI message.";
|
Chris@1397
|
2417 error( RtMidiError::DRIVER_ERROR, errorString_ );
|
Chris@559
|
2418 }
|
Chris@559
|
2419 }
|
Chris@559
|
2420 }
|
Chris@559
|
2421
|
Chris@559
|
2422 #endif // __WINDOWS_MM__
|
Chris@609
|
2423
|
Chris@1397
|
2424
|
Chris@1397
|
2425 //*********************************************************************//
|
Chris@1397
|
2426 // API: UNIX JACK
|
Chris@1397
|
2427 //
|
Chris@1397
|
2428 // Written primarily by Alexander Svetalkin, with updates for delta
|
Chris@1397
|
2429 // time by Gary Scavone, April 2011.
|
Chris@1397
|
2430 //
|
Chris@1397
|
2431 // *********************************************************************//
|
Chris@1397
|
2432
|
Chris@1397
|
2433 #if defined(__UNIX_JACK__)
|
Chris@1397
|
2434
|
Chris@1397
|
2435 // JACK header files
|
Chris@1397
|
2436 #include <jack/jack.h>
|
Chris@1397
|
2437 #include <jack/midiport.h>
|
Chris@1397
|
2438 #include <jack/ringbuffer.h>
|
Chris@1397
|
2439
|
Chris@1397
|
2440 #define JACK_RINGBUFFER_SIZE 16384 // Default size for ringbuffer
|
Chris@1397
|
2441
|
Chris@1397
|
2442 struct JackMidiData {
|
Chris@1397
|
2443 jack_client_t *client;
|
Chris@1397
|
2444 jack_port_t *port;
|
Chris@1397
|
2445 jack_ringbuffer_t *buffSize;
|
Chris@1397
|
2446 jack_ringbuffer_t *buffMessage;
|
Chris@1397
|
2447 jack_time_t lastTime;
|
Chris@1397
|
2448 MidiInApi :: RtMidiInData *rtMidiIn;
|
Chris@1397
|
2449 };
|
Chris@1397
|
2450
|
Chris@1397
|
2451 //*********************************************************************//
|
Chris@1397
|
2452 // API: JACK
|
Chris@1397
|
2453 // Class Definitions: MidiInJack
|
Chris@1397
|
2454 //*********************************************************************//
|
Chris@1397
|
2455
|
Chris@1397
|
2456 static int jackProcessIn( jack_nframes_t nframes, void *arg )
|
Chris@609
|
2457 {
|
Chris@1397
|
2458 JackMidiData *jData = (JackMidiData *) arg;
|
Chris@1397
|
2459 MidiInApi :: RtMidiInData *rtData = jData->rtMidiIn;
|
Chris@1397
|
2460 jack_midi_event_t event;
|
Chris@1397
|
2461 jack_time_t time;
|
Chris@1397
|
2462
|
Chris@1397
|
2463 // Is port created?
|
Chris@1397
|
2464 if ( jData->port == NULL ) return 0;
|
Chris@1397
|
2465 void *buff = jack_port_get_buffer( jData->port, nframes );
|
Chris@1397
|
2466
|
Chris@1397
|
2467 // We have midi events in buffer
|
Chris@1397
|
2468 int evCount = jack_midi_get_event_count( buff );
|
Chris@1397
|
2469 for (int j = 0; j < evCount; j++) {
|
Chris@1397
|
2470 MidiInApi::MidiMessage message;
|
Chris@1397
|
2471 message.bytes.clear();
|
Chris@1397
|
2472
|
Chris@1397
|
2473 jack_midi_event_get( &event, buff, j );
|
Chris@1397
|
2474
|
Chris@1397
|
2475 for ( unsigned int i = 0; i < event.size; i++ )
|
Chris@1397
|
2476 message.bytes.push_back( event.buffer[i] );
|
Chris@1397
|
2477
|
Chris@1397
|
2478 // Compute the delta time.
|
Chris@1397
|
2479 time = jack_get_time();
|
Chris@1397
|
2480 if ( rtData->firstMessage == true )
|
Chris@1397
|
2481 rtData->firstMessage = false;
|
Chris@1397
|
2482 else
|
Chris@1397
|
2483 message.timeStamp = ( time - jData->lastTime ) * 0.000001;
|
Chris@1397
|
2484
|
Chris@1397
|
2485 jData->lastTime = time;
|
Chris@1397
|
2486
|
Chris@1397
|
2487 if ( !rtData->continueSysex ) {
|
Chris@1397
|
2488 if ( rtData->usingCallback ) {
|
Chris@1397
|
2489 RtMidiIn::RtMidiCallback callback = (RtMidiIn::RtMidiCallback) rtData->userCallback;
|
Chris@1397
|
2490 callback( message.timeStamp, &message.bytes, rtData->userData );
|
Chris@1397
|
2491 }
|
Chris@1397
|
2492 else {
|
Chris@1397
|
2493 // As long as we haven't reached our queue size limit, push the message.
|
Chris@1397
|
2494 if ( rtData->queue.size < rtData->queue.ringSize ) {
|
Chris@1397
|
2495 rtData->queue.ring[rtData->queue.back++] = message;
|
Chris@1397
|
2496 if ( rtData->queue.back == rtData->queue.ringSize )
|
Chris@1397
|
2497 rtData->queue.back = 0;
|
Chris@1397
|
2498 rtData->queue.size++;
|
Chris@1397
|
2499 }
|
Chris@1397
|
2500 else
|
Chris@1397
|
2501 std::cerr << "\nMidiInJack: message queue limit reached!!\n\n";
|
Chris@1397
|
2502 }
|
Chris@1397
|
2503 }
|
Chris@1397
|
2504 }
|
Chris@1397
|
2505
|
Chris@1397
|
2506 return 0;
|
Chris@609
|
2507 }
|
Chris@609
|
2508
|
Chris@1397
|
2509 MidiInJack :: MidiInJack( const std::string clientName, unsigned int queueSizeLimit ) : MidiInApi( queueSizeLimit )
|
Chris@609
|
2510 {
|
Chris@1397
|
2511 initialize( clientName );
|
Chris@609
|
2512 }
|
Chris@609
|
2513
|
Chris@1397
|
2514 void MidiInJack :: initialize( const std::string& clientName )
|
Chris@609
|
2515 {
|
Chris@1397
|
2516 JackMidiData *data = new JackMidiData;
|
Chris@1397
|
2517 apiData_ = (void *) data;
|
Chris@1397
|
2518
|
Chris@1397
|
2519 data->rtMidiIn = &inputData_;
|
Chris@1397
|
2520 data->port = NULL;
|
Chris@1397
|
2521 data->client = NULL;
|
Chris@1397
|
2522 this->clientName = clientName;
|
Chris@1397
|
2523
|
Chris@1397
|
2524 connect();
|
Chris@609
|
2525 }
|
Chris@609
|
2526
|
Chris@1397
|
2527 void MidiInJack :: connect()
|
Chris@609
|
2528 {
|
Chris@1397
|
2529 JackMidiData *data = static_cast<JackMidiData *> (apiData_);
|
Chris@1397
|
2530 if ( data->client )
|
Chris@1397
|
2531 return;
|
Chris@1397
|
2532
|
Chris@1397
|
2533 // Initialize JACK client
|
Chris@1397
|
2534 if (( data->client = jack_client_open( clientName.c_str(), JackNoStartServer, NULL )) == 0) {
|
Chris@1397
|
2535 errorString_ = "MidiInJack::initialize: JACK server not running?";
|
Chris@1397
|
2536 error( RtMidiError::WARNING, errorString_ );
|
Chris@1397
|
2537 return;
|
Chris@1397
|
2538 }
|
Chris@1397
|
2539
|
Chris@1397
|
2540 jack_set_process_callback( data->client, jackProcessIn, data );
|
Chris@1397
|
2541 jack_activate( data->client );
|
Chris@609
|
2542 }
|
Chris@609
|
2543
|
Chris@1397
|
2544 MidiInJack :: ~MidiInJack()
|
Chris@609
|
2545 {
|
Chris@1397
|
2546 JackMidiData *data = static_cast<JackMidiData *> (apiData_);
|
Chris@1397
|
2547 closePort();
|
Chris@1397
|
2548
|
Chris@1397
|
2549 if ( data->client )
|
Chris@1397
|
2550 jack_client_close( data->client );
|
Chris@1397
|
2551 delete data;
|
Chris@609
|
2552 }
|
Chris@609
|
2553
|
Chris@1397
|
2554 void MidiInJack :: openPort( unsigned int portNumber, const std::string portName )
|
Chris@609
|
2555 {
|
Chris@1397
|
2556 JackMidiData *data = static_cast<JackMidiData *> (apiData_);
|
Chris@1397
|
2557
|
Chris@1397
|
2558 connect();
|
Chris@1397
|
2559
|
Chris@1397
|
2560 // Creating new port
|
Chris@1397
|
2561 if ( data->port == NULL)
|
Chris@1397
|
2562 data->port = jack_port_register( data->client, portName.c_str(),
|
Chris@1397
|
2563 JACK_DEFAULT_MIDI_TYPE, JackPortIsInput, 0 );
|
Chris@1397
|
2564
|
Chris@1397
|
2565 if ( data->port == NULL) {
|
Chris@1397
|
2566 errorString_ = "MidiInJack::openPort: JACK error creating port";
|
Chris@1397
|
2567 error( RtMidiError::DRIVER_ERROR, errorString_ );
|
Chris@1397
|
2568 return;
|
Chris@1397
|
2569 }
|
Chris@1397
|
2570
|
Chris@1397
|
2571 // Connecting to the output
|
Chris@1397
|
2572 std::string name = getPortName( portNumber );
|
Chris@1397
|
2573 jack_connect( data->client, name.c_str(), jack_port_name( data->port ) );
|
Chris@1397
|
2574 }
|
Chris@1397
|
2575
|
Chris@1397
|
2576 void MidiInJack :: openVirtualPort( const std::string portName )
|
Chris@1397
|
2577 {
|
Chris@1397
|
2578 JackMidiData *data = static_cast<JackMidiData *> (apiData_);
|
Chris@1397
|
2579
|
Chris@1397
|
2580 connect();
|
Chris@1397
|
2581 if ( data->port == NULL )
|
Chris@1397
|
2582 data->port = jack_port_register( data->client, portName.c_str(),
|
Chris@1397
|
2583 JACK_DEFAULT_MIDI_TYPE, JackPortIsInput, 0 );
|
Chris@1397
|
2584
|
Chris@1397
|
2585 if ( data->port == NULL ) {
|
Chris@1397
|
2586 errorString_ = "MidiInJack::openVirtualPort: JACK error creating virtual port";
|
Chris@1397
|
2587 error( RtMidiError::DRIVER_ERROR, errorString_ );
|
Chris@1397
|
2588 }
|
Chris@1397
|
2589 }
|
Chris@1397
|
2590
|
Chris@1397
|
2591 unsigned int MidiInJack :: getPortCount()
|
Chris@1397
|
2592 {
|
Chris@1397
|
2593 int count = 0;
|
Chris@1397
|
2594 JackMidiData *data = static_cast<JackMidiData *> (apiData_);
|
Chris@1397
|
2595 connect();
|
Chris@1397
|
2596 if ( !data->client )
|
Chris@609
|
2597 return 0;
|
Chris@1397
|
2598
|
Chris@1397
|
2599 // List of available ports
|
Chris@1397
|
2600 const char **ports = jack_get_ports( data->client, NULL, JACK_DEFAULT_MIDI_TYPE, JackPortIsOutput );
|
Chris@1397
|
2601
|
Chris@1397
|
2602 if ( ports == NULL ) return 0;
|
Chris@1397
|
2603 while ( ports[count] != NULL )
|
Chris@1397
|
2604 count++;
|
Chris@1397
|
2605
|
Chris@1397
|
2606 free( ports );
|
Chris@1397
|
2607
|
Chris@1397
|
2608 return count;
|
Chris@609
|
2609 }
|
Chris@609
|
2610
|
Chris@1397
|
2611 std::string MidiInJack :: getPortName( unsigned int portNumber )
|
Chris@609
|
2612 {
|
Chris@1397
|
2613 JackMidiData *data = static_cast<JackMidiData *> (apiData_);
|
Chris@1397
|
2614 std::string retStr("");
|
Chris@1397
|
2615
|
Chris@1397
|
2616 connect();
|
Chris@1397
|
2617
|
Chris@1397
|
2618 // List of available ports
|
Chris@1397
|
2619 const char **ports = jack_get_ports( data->client, NULL,
|
Chris@1397
|
2620 JACK_DEFAULT_MIDI_TYPE, JackPortIsOutput );
|
Chris@1397
|
2621
|
Chris@1397
|
2622 // Check port validity
|
Chris@1397
|
2623 if ( ports == NULL ) {
|
Chris@1397
|
2624 errorString_ = "MidiInJack::getPortName: no ports available!";
|
Chris@1397
|
2625 error( RtMidiError::WARNING, errorString_ );
|
Chris@1397
|
2626 return retStr;
|
Chris@1397
|
2627 }
|
Chris@1397
|
2628
|
Chris@1397
|
2629 if ( ports[portNumber] == NULL ) {
|
Chris@1397
|
2630 std::ostringstream ost;
|
Chris@1397
|
2631 ost << "MidiInJack::getPortName: the 'portNumber' argument (" << portNumber << ") is invalid.";
|
Chris@1397
|
2632 errorString_ = ost.str();
|
Chris@1397
|
2633 error( RtMidiError::WARNING, errorString_ );
|
Chris@1397
|
2634 }
|
Chris@1397
|
2635 else retStr.assign( ports[portNumber] );
|
Chris@1397
|
2636
|
Chris@1397
|
2637 free( ports );
|
Chris@1397
|
2638 return retStr;
|
Chris@609
|
2639 }
|
Chris@609
|
2640
|
Chris@1397
|
2641 void MidiInJack :: closePort()
|
Chris@609
|
2642 {
|
Chris@1397
|
2643 JackMidiData *data = static_cast<JackMidiData *> (apiData_);
|
Chris@1397
|
2644
|
Chris@1397
|
2645 if ( data->port == NULL ) return;
|
Chris@1397
|
2646 jack_port_unregister( data->client, data->port );
|
Chris@1397
|
2647 data->port = NULL;
|
Chris@1397
|
2648 }
|
Chris@1397
|
2649
|
Chris@1397
|
2650 //*********************************************************************//
|
Chris@1397
|
2651 // API: JACK
|
Chris@1397
|
2652 // Class Definitions: MidiOutJack
|
Chris@1397
|
2653 //*********************************************************************//
|
Chris@1397
|
2654
|
Chris@1397
|
2655 // Jack process callback
|
Chris@1397
|
2656 static int jackProcessOut( jack_nframes_t nframes, void *arg )
|
Chris@1397
|
2657 {
|
Chris@1397
|
2658 JackMidiData *data = (JackMidiData *) arg;
|
Chris@1397
|
2659 jack_midi_data_t *midiData;
|
Chris@1397
|
2660 int space;
|
Chris@1397
|
2661
|
Chris@1397
|
2662 // Is port created?
|
Chris@1397
|
2663 if ( data->port == NULL ) return 0;
|
Chris@1397
|
2664
|
Chris@1397
|
2665 void *buff = jack_port_get_buffer( data->port, nframes );
|
Chris@1397
|
2666 jack_midi_clear_buffer( buff );
|
Chris@1397
|
2667
|
Chris@1397
|
2668 while ( jack_ringbuffer_read_space( data->buffSize ) > 0 ) {
|
Chris@1397
|
2669 jack_ringbuffer_read( data->buffSize, (char *) &space, (size_t) sizeof(space) );
|
Chris@1397
|
2670 midiData = jack_midi_event_reserve( buff, 0, space );
|
Chris@1397
|
2671
|
Chris@1397
|
2672 jack_ringbuffer_read( data->buffMessage, (char *) midiData, (size_t) space );
|
Chris@1397
|
2673 }
|
Chris@1397
|
2674
|
Chris@1397
|
2675 return 0;
|
Chris@1397
|
2676 }
|
Chris@1397
|
2677
|
Chris@1397
|
2678 MidiOutJack :: MidiOutJack( const std::string clientName ) : MidiOutApi()
|
Chris@1397
|
2679 {
|
Chris@1397
|
2680 initialize( clientName );
|
Chris@1397
|
2681 }
|
Chris@1397
|
2682
|
Chris@1397
|
2683 void MidiOutJack :: initialize( const std::string& clientName )
|
Chris@1397
|
2684 {
|
Chris@1397
|
2685 JackMidiData *data = new JackMidiData;
|
Chris@1397
|
2686 apiData_ = (void *) data;
|
Chris@1397
|
2687
|
Chris@1397
|
2688 data->port = NULL;
|
Chris@1397
|
2689 data->client = NULL;
|
Chris@1397
|
2690 this->clientName = clientName;
|
Chris@1397
|
2691
|
Chris@1397
|
2692 connect();
|
Chris@1397
|
2693 }
|
Chris@1397
|
2694
|
Chris@1397
|
2695 void MidiOutJack :: connect()
|
Chris@1397
|
2696 {
|
Chris@1397
|
2697 JackMidiData *data = static_cast<JackMidiData *> (apiData_);
|
Chris@1397
|
2698 if ( data->client )
|
Chris@1397
|
2699 return;
|
Chris@1397
|
2700
|
Chris@1397
|
2701 // Initialize output ringbuffers
|
Chris@1397
|
2702 data->buffSize = jack_ringbuffer_create( JACK_RINGBUFFER_SIZE );
|
Chris@1397
|
2703 data->buffMessage = jack_ringbuffer_create( JACK_RINGBUFFER_SIZE );
|
Chris@1397
|
2704
|
Chris@1397
|
2705 // Initialize JACK client
|
Chris@1397
|
2706 if (( data->client = jack_client_open( clientName.c_str(), JackNoStartServer, NULL )) == 0) {
|
Chris@1397
|
2707 errorString_ = "MidiOutJack::initialize: JACK server not running?";
|
Chris@1397
|
2708 error( RtMidiError::WARNING, errorString_ );
|
Chris@1397
|
2709 return;
|
Chris@1397
|
2710 }
|
Chris@1397
|
2711
|
Chris@1397
|
2712 jack_set_process_callback( data->client, jackProcessOut, data );
|
Chris@1397
|
2713 jack_activate( data->client );
|
Chris@1397
|
2714 }
|
Chris@1397
|
2715
|
Chris@1397
|
2716 MidiOutJack :: ~MidiOutJack()
|
Chris@1397
|
2717 {
|
Chris@1397
|
2718 JackMidiData *data = static_cast<JackMidiData *> (apiData_);
|
Chris@1397
|
2719 closePort();
|
Chris@1397
|
2720
|
Chris@1397
|
2721 // Cleanup
|
Chris@1397
|
2722 jack_ringbuffer_free( data->buffSize );
|
Chris@1397
|
2723 jack_ringbuffer_free( data->buffMessage );
|
Chris@1397
|
2724 if ( data->client ) {
|
Chris@1397
|
2725 jack_client_close( data->client );
|
Chris@1397
|
2726 }
|
Chris@1397
|
2727
|
Chris@1397
|
2728 delete data;
|
Chris@1397
|
2729 }
|
Chris@1397
|
2730
|
Chris@1397
|
2731 void MidiOutJack :: openPort( unsigned int portNumber, const std::string portName )
|
Chris@1397
|
2732 {
|
Chris@1397
|
2733 JackMidiData *data = static_cast<JackMidiData *> (apiData_);
|
Chris@1397
|
2734
|
Chris@1397
|
2735 connect();
|
Chris@1397
|
2736
|
Chris@1397
|
2737 // Creating new port
|
Chris@1397
|
2738 if ( data->port == NULL )
|
Chris@1397
|
2739 data->port = jack_port_register( data->client, portName.c_str(),
|
Chris@1397
|
2740 JACK_DEFAULT_MIDI_TYPE, JackPortIsOutput, 0 );
|
Chris@1397
|
2741
|
Chris@1397
|
2742 if ( data->port == NULL ) {
|
Chris@1397
|
2743 errorString_ = "MidiOutJack::openPort: JACK error creating port";
|
Chris@1397
|
2744 error( RtMidiError::DRIVER_ERROR, errorString_ );
|
Chris@1397
|
2745 return;
|
Chris@1397
|
2746 }
|
Chris@1397
|
2747
|
Chris@1397
|
2748 // Connecting to the output
|
Chris@1397
|
2749 std::string name = getPortName( portNumber );
|
Chris@1397
|
2750 jack_connect( data->client, jack_port_name( data->port ), name.c_str() );
|
Chris@1397
|
2751 }
|
Chris@1397
|
2752
|
Chris@1397
|
2753 void MidiOutJack :: openVirtualPort( const std::string portName )
|
Chris@1397
|
2754 {
|
Chris@1397
|
2755 JackMidiData *data = static_cast<JackMidiData *> (apiData_);
|
Chris@1397
|
2756
|
Chris@1397
|
2757 connect();
|
Chris@1397
|
2758 if ( data->port == NULL )
|
Chris@1397
|
2759 data->port = jack_port_register( data->client, portName.c_str(),
|
Chris@1397
|
2760 JACK_DEFAULT_MIDI_TYPE, JackPortIsOutput, 0 );
|
Chris@1397
|
2761
|
Chris@1397
|
2762 if ( data->port == NULL ) {
|
Chris@1397
|
2763 errorString_ = "MidiOutJack::openVirtualPort: JACK error creating virtual port";
|
Chris@1397
|
2764 error( RtMidiError::DRIVER_ERROR, errorString_ );
|
Chris@1397
|
2765 }
|
Chris@1397
|
2766 }
|
Chris@1397
|
2767
|
Chris@1397
|
2768 unsigned int MidiOutJack :: getPortCount()
|
Chris@1397
|
2769 {
|
Chris@1397
|
2770 int count = 0;
|
Chris@1397
|
2771 JackMidiData *data = static_cast<JackMidiData *> (apiData_);
|
Chris@1397
|
2772 connect();
|
Chris@1397
|
2773 if ( !data->client )
|
Chris@609
|
2774 return 0;
|
Chris@1397
|
2775
|
Chris@1397
|
2776 // List of available ports
|
Chris@1397
|
2777 const char **ports = jack_get_ports( data->client, NULL,
|
Chris@1397
|
2778 JACK_DEFAULT_MIDI_TYPE, JackPortIsInput );
|
Chris@1397
|
2779
|
Chris@1397
|
2780 if ( ports == NULL ) return 0;
|
Chris@1397
|
2781 while ( ports[count] != NULL )
|
Chris@1397
|
2782 count++;
|
Chris@1397
|
2783
|
Chris@1397
|
2784 free( ports );
|
Chris@1397
|
2785
|
Chris@1397
|
2786 return count;
|
Chris@609
|
2787 }
|
Chris@609
|
2788
|
Chris@1397
|
2789 std::string MidiOutJack :: getPortName( unsigned int portNumber )
|
Chris@609
|
2790 {
|
Chris@1397
|
2791 JackMidiData *data = static_cast<JackMidiData *> (apiData_);
|
Chris@1397
|
2792 std::string retStr("");
|
Chris@1397
|
2793
|
Chris@1397
|
2794 connect();
|
Chris@1397
|
2795
|
Chris@1397
|
2796 // List of available ports
|
Chris@1397
|
2797 const char **ports = jack_get_ports( data->client, NULL,
|
Chris@1397
|
2798 JACK_DEFAULT_MIDI_TYPE, JackPortIsInput );
|
Chris@1397
|
2799
|
Chris@1397
|
2800 // Check port validity
|
Chris@1397
|
2801 if ( ports == NULL) {
|
Chris@1397
|
2802 errorString_ = "MidiOutJack::getPortName: no ports available!";
|
Chris@1397
|
2803 error( RtMidiError::WARNING, errorString_ );
|
Chris@1397
|
2804 return retStr;
|
Chris@1397
|
2805 }
|
Chris@1397
|
2806
|
Chris@1397
|
2807 if ( ports[portNumber] == NULL) {
|
Chris@1397
|
2808 std::ostringstream ost;
|
Chris@1397
|
2809 ost << "MidiOutJack::getPortName: the 'portNumber' argument (" << portNumber << ") is invalid.";
|
Chris@1397
|
2810 errorString_ = ost.str();
|
Chris@1397
|
2811 error( RtMidiError::WARNING, errorString_ );
|
Chris@1397
|
2812 }
|
Chris@1397
|
2813 else retStr.assign( ports[portNumber] );
|
Chris@1397
|
2814
|
Chris@1397
|
2815 free( ports );
|
Chris@1397
|
2816 return retStr;
|
Chris@609
|
2817 }
|
Chris@609
|
2818
|
Chris@1397
|
2819 void MidiOutJack :: closePort()
|
Chris@609
|
2820 {
|
Chris@1397
|
2821 JackMidiData *data = static_cast<JackMidiData *> (apiData_);
|
Chris@1397
|
2822
|
Chris@1397
|
2823 if ( data->port == NULL ) return;
|
Chris@1397
|
2824 jack_port_unregister( data->client, data->port );
|
Chris@1397
|
2825 data->port = NULL;
|
Chris@609
|
2826 }
|
Chris@609
|
2827
|
Chris@1397
|
2828 void MidiOutJack :: sendMessage( std::vector<unsigned char> *message )
|
Chris@609
|
2829 {
|
Chris@1397
|
2830 int nBytes = message->size();
|
Chris@1397
|
2831 JackMidiData *data = static_cast<JackMidiData *> (apiData_);
|
Chris@1397
|
2832
|
Chris@1397
|
2833 // Write full message to buffer
|
Chris@1397
|
2834 jack_ringbuffer_write( data->buffMessage, ( const char * ) &( *message )[0],
|
Chris@1397
|
2835 message->size() );
|
Chris@1397
|
2836 jack_ringbuffer_write( data->buffSize, ( char * ) &nBytes, sizeof( nBytes ) );
|
Chris@609
|
2837 }
|
Chris@609
|
2838
|
Chris@1397
|
2839 #endif // __UNIX_JACK__
|