1 /*----------------------------------------------------------------------------
2 ChucK Concurrent, On-the-fly Audio Programming Language
3 Compiler and Virtual Machine
5 Copyright (c) 2004 Ge Wang and Perry R. Cook. All rights reserved.
6 http://chuck.cs.princeton.edu/
7 http://soundlab.cs.princeton.edu/
9 This program is free software; you can redistribute it and/or modify
10 it under the terms of the GNU General Public License as published by
11 the Free Software Foundation; either version 2 of the License, or
12 (at your option) any later version.
14 This program is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 GNU General Public License for more details.
19 You should have received a copy of the GNU General Public License
20 along with this program; if not, write to the Free Software
21 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
23 -----------------------------------------------------------------------------*/
25 //-----------------------------------------------------------------------------
26 // file: midiio_oss.cpp
27 // desc: midi io oss implementation
29 // author: Ge Wang (gewang@cs.princeton.edu)
30 // Perry R. Cook (prc@cs.princeton.edu)
31 //-----------------------------------------------------------------------------
32 #include "midiio_oss.h"
39 //-----------------------------------------------------------------------------
42 //-----------------------------------------------------------------------------
45 UINT__ m_device_num
= 0;
52 //-----------------------------------------------------------------------------
55 //-----------------------------------------------------------------------------
64 //-----------------------------------------------------------------------------
66 // desc: open a device
67 //-----------------------------------------------------------------------------
68 BOOL__
MidiOut::open( int device_num
)
73 m_device_num
= device_num
;
76 sprintf( buffer
, "hw:0,%d", m_device_num
);
84 //-----------------------------------------------------------------------------
86 // desc: close the device
87 //-----------------------------------------------------------------------------
88 BOOL__
MidiOut::close( )
90 // send everything in buffer
100 //-----------------------------------------------------------------------------
103 //-----------------------------------------------------------------------------
104 BOOL__
MidiOut::drain()
112 //-----------------------------------------------------------------------------
114 // desc: send 1 BYTE__ midi message
115 //-----------------------------------------------------------------------------
116 UINT__
MidiOut::send( BYTE__ status
)
126 //-----------------------------------------------------------------------------
128 // desc: send 2 BYTE__ midi message
129 //-----------------------------------------------------------------------------
130 UINT__
MidiOut::send( BYTE__ status
, BYTE__ data1
)
140 //-----------------------------------------------------------------------------
142 // desc: send 3 BYTE__ midi message
143 //-----------------------------------------------------------------------------
144 UINT__
MidiOut::send( BYTE__ status
, BYTE__ data1
, BYTE__ data2
)
146 // send the three BYTE
154 //-----------------------------------------------------------------------------
156 // desc: send midi message
157 //-----------------------------------------------------------------------------
158 UINT__
MidiOut::send( const MidiMsg
* msg
)
160 return this->send( msg
->data
[0], msg
->data
[1], msg
->data
[2] );
166 //-----------------------------------------------------------------------------
169 //-----------------------------------------------------------------------------
174 pthread_mutex_init( &m_mutex
, NULL
);
180 //-----------------------------------------------------------------------------
183 //-----------------------------------------------------------------------------
192 //-----------------------------------------------------------------------------
195 //-----------------------------------------------------------------------------
196 BOOL__
MidiIn::open( int device_num
)
201 m_device_num
= device_num
;
204 sprintf( buffer
, "hw:0,%d", m_device_num
);
206 // initialize the buffer
207 m_buffer
.initialize( 1024, sizeof( MidiMsg
) );
210 pthread_create( &m_cb_thread_id
, NULL
, midi_in_cb
, this );
218 //-----------------------------------------------------------------------------
221 //-----------------------------------------------------------------------------
222 BOOL__
MidiIn::close()
224 pthread_cancel( m_cb_thread_id
);
225 pthread_mutex_destroy( &m_mutex
);
233 //-----------------------------------------------------------------------------
236 //-----------------------------------------------------------------------------
237 UINT__
MidiIn::recv( MidiMsg
* msg
)
240 // pthread_mutex_lock( &m_mutex );
241 r
= m_buffer
.get( msg
, 1 );
242 // pthread_mutex_unlock( &m_mutex );
250 //-----------------------------------------------------------------------------
251 // name: midi_in_cb()
253 //-----------------------------------------------------------------------------
254 void * MidiIn::midi_in_cb( void * arg
)
256 MidiIn
* min
= (MidiIn
*)arg
;
258 int n
= 0, num_args
= 0, num_left
= 0;
268 fprintf( stdout
, "error: rawmidi_read...\n" );
274 if( byte
& 0x80 ) // status byte
276 if( (byte
& 0xf0) == 0xf0 ) // system msg
282 if( ( (byte
& 0xf0) == 0xc0 ) || ( (byte
& 0xf0) == 0xd0 ) )
295 if( num_left
== num_args
)
304 if( ((msg
.data
[2] & 0xf0) == 0xc0) || ((msg
.data
[2] & 0xf0) == 0xd0) )
309 //pthread_mutex_lock( &min->m_mutex );
310 min
->m_buffer
.put( &msg
, 1 );
311 //pthread_mutex_unlock( &min->m_mutex );