*** empty log message ***
[chuck-blob.git] / exile / v1 / src / midiio_oss.cpp
blobd644ca8302592e74316883944092c54c2cbe2bb8
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
22 U.S.A.
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"
33 #include <stdio.h>
34 #include <stdlib.h>
39 //-----------------------------------------------------------------------------
40 // name: MidiOut()
41 // desc: constructor
42 //-----------------------------------------------------------------------------
43 MidiOut::MidiOut()
45 UINT__ m_device_num = 0;
46 // m_midi_out = NULL;
52 //-----------------------------------------------------------------------------
53 // name: ~MidiOut()
54 // desc: destructor
55 //-----------------------------------------------------------------------------
56 MidiOut::~MidiOut()
58 this->close();
64 //-----------------------------------------------------------------------------
65 // name: open()
66 // desc: open a device
67 //-----------------------------------------------------------------------------
68 BOOL__ MidiOut::open( int device_num )
70 int err = 0;
71 char buffer[256];
73 m_device_num = device_num;
75 // open the midi
76 sprintf( buffer, "hw:0,%d", m_device_num );
78 return err == 0;
84 //-----------------------------------------------------------------------------
85 // name: close()
86 // desc: close the device
87 //-----------------------------------------------------------------------------
88 BOOL__ MidiOut::close( )
90 // send everything in buffer
92 // close midi out
94 return true;
100 //-----------------------------------------------------------------------------
101 // name: drain()
102 // desc: ...
103 //-----------------------------------------------------------------------------
104 BOOL__ MidiOut::drain()
106 return true;
112 //-----------------------------------------------------------------------------
113 // name: send()
114 // desc: send 1 BYTE__ midi message
115 //-----------------------------------------------------------------------------
116 UINT__ MidiOut::send( BYTE__ status )
118 // send
120 return true;
126 //-----------------------------------------------------------------------------
127 // name: send()
128 // desc: send 2 BYTE__ midi message
129 //-----------------------------------------------------------------------------
130 UINT__ MidiOut::send( BYTE__ status, BYTE__ data1 )
132 // send
134 return true;
140 //-----------------------------------------------------------------------------
141 // name: send()
142 // desc: send 3 BYTE__ midi message
143 //-----------------------------------------------------------------------------
144 UINT__ MidiOut::send( BYTE__ status, BYTE__ data1, BYTE__ data2 )
146 // send the three BYTE
148 return true;
154 //-----------------------------------------------------------------------------
155 // name: send()
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 //-----------------------------------------------------------------------------
167 // name: MidiIn()
168 // desc: constructor
169 //-----------------------------------------------------------------------------
170 MidiIn::MidiIn()
172 // m_midi_in = NULL;
173 m_device_num = 0;
174 pthread_mutex_init( &m_mutex, NULL );
180 //-----------------------------------------------------------------------------
181 // name: ~MidiIn()
182 // desc: destructor
183 //-----------------------------------------------------------------------------
184 MidiIn::~MidiIn( )
186 this->close();
192 //-----------------------------------------------------------------------------
193 // name: open()
194 // desc: open
195 //-----------------------------------------------------------------------------
196 BOOL__ MidiIn::open( int device_num )
198 int err = 0;
199 char buffer[256];
201 m_device_num = device_num;
203 // open the raw midi
204 sprintf( buffer, "hw:0,%d", m_device_num );
206 // initialize the buffer
207 m_buffer.initialize( 1024, sizeof( MidiMsg ) );
209 // thread
210 pthread_create( &m_cb_thread_id, NULL, midi_in_cb, this );
212 return err == 0;
218 //-----------------------------------------------------------------------------
219 // name: close()
220 // desc: close
221 //-----------------------------------------------------------------------------
222 BOOL__ MidiIn::close()
224 pthread_cancel( m_cb_thread_id );
225 pthread_mutex_destroy( &m_mutex );
227 return true;
233 //-----------------------------------------------------------------------------
234 // name: get()
235 // desc: get message
236 //-----------------------------------------------------------------------------
237 UINT__ MidiIn::recv( MidiMsg * msg )
239 UINT__ r = 0;
240 // pthread_mutex_lock( &m_mutex );
241 r = m_buffer.get( msg, 1 );
242 // pthread_mutex_unlock( &m_mutex );
244 return r;
250 //-----------------------------------------------------------------------------
251 // name: midi_in_cb()
252 // desc: ...
253 //-----------------------------------------------------------------------------
254 void * MidiIn::midi_in_cb( void * arg )
256 MidiIn * min = (MidiIn *)arg;
257 BYTE__ byte = 0;
258 int n = 0, num_args = 0, num_left = 0;
259 MidiMsg msg;
261 while( true )
263 // get the next BYTE
265 if( n < 0 )
267 // encounter error
268 fprintf( stdout, "error: rawmidi_read...\n" );
269 continue;
272 while( n > 0 )
274 if( byte & 0x80 ) // status byte
276 if( (byte & 0xf0) == 0xf0 ) // system msg
278 n--;
279 continue;
282 if( ( (byte & 0xf0) == 0xc0 ) || ( (byte & 0xf0) == 0xd0 ) )
283 num_args = 1;
284 else
285 num_args = 2;
287 msg.data[3] = 0;
288 msg.data[2] = byte;
289 msg.data[1] = 0;
290 msg.data[0] = 0;
291 num_left = num_args;
293 else // data byte
295 if( num_left == num_args )
296 msg.data[1] = byte;
297 else
298 msg.data[0] = byte;
300 num_left--;
302 if( !num_left )
304 if( ((msg.data[2] & 0xf0) == 0xc0) || ((msg.data[2] & 0xf0) == 0xd0) )
305 num_left = 1;
306 else
307 num_left = 2;
309 //pthread_mutex_lock( &min->m_mutex );
310 min->m_buffer.put( &msg, 1 );
311 //pthread_mutex_unlock( &min->m_mutex );
314 n--;
318 return NULL;