*** empty log message ***
[chuck-blob.git] / exile / digitalio_osx.h
blob11573cae9354935b68101cd6514fc0b841fd7314
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 // name: digitalio_osx.h
27 // desc: digitalio
29 // author: Ge Wang (gewang@cs.princeton.edu)
30 // Perry R. Cook (prc@cs.princeton.edu)
31 // Ari Lazier (alazier@cs.princeton.edu)
32 //-----------------------------------------------------------------------------
33 #ifndef __DIGITAL_IO_H__
34 #define __DIGITAL_IO_H__
37 #include <CoreAudio/CoreAudio.h>
38 #include <pthread.h>
39 #include <unistd.h>
44 //-----------------------------------------------------------------------------
45 // define and forward references
46 //-----------------------------------------------------------------------------
47 #define SAFE_DELETE(p) { if(p) { delete (p); (p)=NULL; } }
48 #define SAFE_DELETE_ARRAY(p) { if(p) { delete[] (p); (p)=NULL; } }
50 // defaults
51 #define BUFFER_SIZE_OUT_DEFAULT 512
52 #define BUFFER_SIZE_IN_DEFAULT 512
53 #define BUFFER_SIZE_DEFAULT 512
54 #define NUM_OUT_BUFFERS_DEFAULT 4
55 #define NUM_IN_BUFFERS_DEFAULT 4
56 #define NUM_BUFFERS_DEFAULT 4
57 #define NUM_CHANNELS_DEFAULT 2 // number of channels
58 #define SAMPLING_RATE_DEFAULT 44100 // sampling rate
59 #define BITS_PER_SAMPLE_DEFAULT 32 // sample size
60 #define DEVICE_NUM_OUT_DEFAULT 2
61 #define DEVICE_NUM_IN_DEFAULT 2
63 // sample types
64 #define SAMPLE_SHORT short
65 #define SAMPLE_FLOAT float
66 #define SAMPLE_INTERNAL SAMPLE_FLOAT
67 #define SAMPLE SAMPLE_FLOAT
68 #define S_MAX 1.0f // max value for float
69 #define S_MIN -1.0f // min value for float
71 // external to internal sample conversion
72 #define SAMPLE_TO_INTERNAL(s) ( s )
73 #define INTERNAL_TO_SAMPLE(s) ( s )
75 // types
76 #define DWORD__ unsigned int
77 #define UINT__ DWORD__
78 #define BOOL__ DWORD__
79 #define FLOAT__ float
80 #define BYTE__ unsigned char
82 #ifndef FALSE
83 #define FALSE 0
84 #endif
85 #ifndef TRUE
86 #define TRUE 1
87 #endif
89 // forward references
90 class InternalBuffer;
95 //-----------------------------------------------------------------------------
96 // name: Digitalio
97 // desc: ...
98 //-----------------------------------------------------------------------------
99 class Digitalio
101 public:
102 static BOOL__ initialize( DWORD__ num_channels = NUM_CHANNELS_DEFAULT,
103 DWORD__ sampling_rate = SAMPLING_RATE_DEFAULT,
104 DWORD__ bps = BITS_PER_SAMPLE_DEFAULT,
105 DWORD__ buffer_size = BUFFER_SIZE_DEFAULT,
106 DWORD__ num_buffers = NUM_BUFFERS_DEFAULT );
107 static void shutdown();
109 public:
110 static DWORD__ sampling_rate( ) { return m_sampling_rate; }
111 static DWORD__ num_channels( ) { return m_num_channels; }
112 static DWORD__ bps( ) { return m_bps; }
113 static DWORD__ buffer_size( ) { return m_buffer_size; }
114 static DWORD__ num_buffers( ) { return m_num_buffers; }
116 public: // data
117 static BOOL__ m_init;
118 static DWORD__ m_num_channels;
119 static DWORD__ m_sampling_rate;
120 static DWORD__ m_bps;
121 static DWORD__ m_buffer_size;
122 static DWORD__ m_num_buffers;
128 //-----------------------------------------------------------------------------
129 // name: TickOut
130 // desc: ...
131 //-----------------------------------------------------------------------------
132 class TickOut
134 public:
135 virtual ~TickOut() {}
137 public:
138 virtual BOOL__ reset() { return true; }
139 virtual BOOL__ tick_out( SAMPLE s ) = 0;
140 virtual BOOL__ tick_out( SAMPLE l, SAMPLE r ) = 0;
141 virtual BOOL__ tick_out( const SAMPLE * out, DWORD__ n ) = 0;
147 //-----------------------------------------------------------------------------
148 // name: TickIn
149 // desc: ...
150 //-----------------------------------------------------------------------------
151 class TickIn
153 public:
154 virtual ~TickIn() { }
156 public:
157 virtual BOOL__ reset() { return true; }
158 virtual BOOL__ tick_in( SAMPLE * in ) = 0;
159 virtual BOOL__ tick_in( SAMPLE * l, SAMPLE * r ) = 0;
160 virtual BOOL__ tick_in( SAMPLE * in, DWORD__ n ) = 0;
162 virtual SAMPLE tick( )
163 { SAMPLE in; return ( tick_in( &in ) ? in : (SAMPLE)0.0f ); }
169 //-----------------------------------------------------------------------------
170 // name: ChannelIO
171 // desc: ...
172 //-----------------------------------------------------------------------------
173 class ChannelIO
175 public:
176 virtual ~ChannelIO() { }
178 public:
179 virtual SAMPLE _( SAMPLE x ) = 0;
185 //-----------------------------------------------------------------------------
186 // name: class DigitalOut
187 // desc: ...
188 //-----------------------------------------------------------------------------
189 class DigitalOut : public TickOut
191 public:
192 DigitalOut();
193 ~DigitalOut();
195 public:
196 BOOL__ initialize( DWORD__ device_num = DEVICE_NUM_OUT_DEFAULT,
197 DWORD__ num_channels = NUM_CHANNELS_DEFAULT,
198 DWORD__ sampling_rate = SAMPLING_RATE_DEFAULT,
199 DWORD__ bps = BITS_PER_SAMPLE_DEFAULT,
200 DWORD__ buffer_size = BUFFER_SIZE_OUT_DEFAULT,
201 DWORD__ num_buffers = NUM_OUT_BUFFERS_DEFAULT );
202 void cleanup();
204 BOOL__ start();
205 BOOL__ stop();
207 public:
208 virtual BOOL__ tick_out( SAMPLE s );
209 virtual BOOL__ tick_out( SAMPLE l, SAMPLE r );
210 virtual BOOL__ tick_out( const SAMPLE * samples, DWORD__ n );
212 public:
213 DWORD__ render( AudioBufferList * buffer_list );
215 protected:
216 BOOL__ m_init_out;
217 BOOL__ m_render_start;
219 DWORD__ m_num_channels;
220 DWORD__ m_sampling_rate;
221 DWORD__ m_bps;
223 AudioDeviceID m_device_handle;
224 DWORD__ m_device_num;
225 pthread_mutex_t m_mutex;
226 DWORD__ m_block;
227 DWORD__ m_out2;
229 InternalBuffer * m_out_buffers;
230 DWORD__ m_num_out_buffers;
231 DWORD__ m_out_buffer_size;
232 DWORD__ m_curr_buffer_out;
234 SAMPLE_INTERNAL * m_data_ptr_out;
235 SAMPLE_INTERNAL * m_data_max_out;
236 DWORD__ m_index;
238 inline BOOL__ prepare_tick_out();
244 //-----------------------------------------------------------------------------
245 // name: class DigitalIn
246 // desc: ...
247 //-----------------------------------------------------------------------------
248 class DigitalIn : public TickIn
250 public:
251 DigitalIn();
252 ~DigitalIn();
254 public: // in
255 BOOL__ initialize( DWORD__ device_number = DEVICE_NUM_IN_DEFAULT,
256 DWORD__ num_channels = NUM_CHANNELS_DEFAULT,
257 DWORD__ sampling_rate = SAMPLING_RATE_DEFAULT,
258 DWORD__ bps = BITS_PER_SAMPLE_DEFAULT,
259 DWORD__ buffer_size = BUFFER_SIZE_IN_DEFAULT,
260 DWORD__ num_buffers = NUM_IN_BUFFERS_DEFAULT );
261 void cleanup();
263 BOOL__ start();
264 BOOL__ stop();
266 public:
267 virtual BOOL__ tick_in( SAMPLE * s );
268 virtual BOOL__ tick_in( SAMPLE * l, SAMPLE * r );
269 virtual BOOL__ tick_in( SAMPLE * samples, DWORD__ n );
271 public:
272 DWORD__ capture( const AudioBufferList * buffer_list );
274 protected:
275 BOOL__ m_init_in;
276 BOOL__ m_capture_start;
278 DWORD__ m_num_channels;
279 DWORD__ m_sampling_rate;
280 DWORD__ m_bps;
282 AudioDeviceID m_device_handle;
283 DWORD__ m_device_num;
284 pthread_mutex_t m_mutex;
285 DWORD__ m_block;
286 DWORD__ m_in2;
288 InternalBuffer * m_in_buffers;
289 DWORD__ m_num_in_buffers;
290 DWORD__ m_in_buffer_size;
291 DWORD__ m_curr_buffer_in;
292 SAMPLE_INTERNAL * m_data_ptr_in;
293 SAMPLE_INTERNAL * m_data_max_in;
295 inline BOOL__ prepare_tick_in();
301 //-----------------------------------------------------------------------------
302 // name: AudioBuffer__
303 // desc: ...
304 //-----------------------------------------------------------------------------
305 class AudioBuffer__
307 public:
308 AudioBuffer__( DWORD__ size = 0 );
310 public:
311 BOOL__ initialize( DWORD__ size );
312 void cleanup();
313 SAMPLE * data();
314 DWORD__ size();
316 protected:
317 SAMPLE * m_data;
318 DWORD__ m_size;
320 SAMPLE * m_ptr_curr;
321 SAMPLE * m_ptr_end;
327 //-----------------------------------------------------------------------------
328 // name: AudioBuffer__In
329 // desc: ...
330 //-----------------------------------------------------------------------------
331 class AudioBuffer__In : public TickIn, public AudioBuffer__
333 public:
334 AudioBuffer__In( DWORD__ size = 0 );
335 virtual ~AudioBuffer__In();
337 public:
338 virtual BOOL__ reset();
339 virtual BOOL__ tick_in( SAMPLE * in );
340 virtual BOOL__ tick_in( SAMPLE * l, SAMPLE * r );
341 virtual BOOL__ tick_in( SAMPLE * in, DWORD__ n );
347 //-----------------------------------------------------------------------------
348 // name: AudioBuffer__Out
349 // desc: ...
350 //-----------------------------------------------------------------------------
351 class AudioBuffer__Out : public TickOut, public AudioBuffer__
353 public:
354 AudioBuffer__Out( DWORD__ size = 0 );
355 virtual ~AudioBuffer__Out();
357 public:
358 virtual BOOL__ reset();
359 virtual BOOL__ tick_out( SAMPLE s );
360 virtual BOOL__ tick_out( SAMPLE l, SAMPLE r );
361 virtual BOOL__ tick_out( const SAMPLE * out, DWORD__ n );
367 //-----------------------------------------------------------------------------
368 // name: InternalBuffer
369 // desc: sample buffer
370 //-----------------------------------------------------------------------------
371 class InternalBuffer
373 public:
374 InternalBuffer();
375 ~InternalBuffer();
377 public:
378 BOOL__ initialize( DWORD__ buffer_size );
379 void cleanup();
381 public:
382 BOOL__ clear();
383 void synch();
384 void signal();
385 void reset();
387 public:
388 SAMPLE_INTERNAL * data( );
389 DWORD__ size() const;
391 protected:
392 SAMPLE_INTERNAL * m_data;
393 DWORD__ m_size;
399 #endif