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 // name: digitalio_osx.h
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>
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; } }
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
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 )
76 #define DWORD__ unsigned int
77 #define UINT__ DWORD__
78 #define BOOL__ DWORD__
80 #define BYTE__ unsigned char
95 //-----------------------------------------------------------------------------
98 //-----------------------------------------------------------------------------
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();
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
; }
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 //-----------------------------------------------------------------------------
131 //-----------------------------------------------------------------------------
135 virtual ~TickOut() {}
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 //-----------------------------------------------------------------------------
150 //-----------------------------------------------------------------------------
154 virtual ~TickIn() { }
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 //-----------------------------------------------------------------------------
172 //-----------------------------------------------------------------------------
176 virtual ~ChannelIO() { }
179 virtual SAMPLE
_( SAMPLE x
) = 0;
185 //-----------------------------------------------------------------------------
186 // name: class DigitalOut
188 //-----------------------------------------------------------------------------
189 class DigitalOut
: public TickOut
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
);
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
);
213 DWORD__
render( AudioBufferList
* buffer_list
);
217 BOOL__ m_render_start
;
219 DWORD__ m_num_channels
;
220 DWORD__ m_sampling_rate
;
223 AudioDeviceID m_device_handle
;
224 DWORD__ m_device_num
;
225 pthread_mutex_t m_mutex
;
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
;
238 inline BOOL__
prepare_tick_out();
244 //-----------------------------------------------------------------------------
245 // name: class DigitalIn
247 //-----------------------------------------------------------------------------
248 class DigitalIn
: public TickIn
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
);
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
);
272 DWORD__
capture( const AudioBufferList
* buffer_list
);
276 BOOL__ m_capture_start
;
278 DWORD__ m_num_channels
;
279 DWORD__ m_sampling_rate
;
282 AudioDeviceID m_device_handle
;
283 DWORD__ m_device_num
;
284 pthread_mutex_t m_mutex
;
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__
304 //-----------------------------------------------------------------------------
308 AudioBuffer__( DWORD__ size
= 0 );
311 BOOL__
initialize( DWORD__ size
);
327 //-----------------------------------------------------------------------------
328 // name: AudioBuffer__In
330 //-----------------------------------------------------------------------------
331 class AudioBuffer__In
: public TickIn
, public AudioBuffer__
334 AudioBuffer__In( DWORD__ size
= 0 );
335 virtual ~AudioBuffer__In();
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
350 //-----------------------------------------------------------------------------
351 class AudioBuffer__Out
: public TickOut
, public AudioBuffer__
354 AudioBuffer__Out( DWORD__ size
= 0 );
355 virtual ~AudioBuffer__Out();
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 //-----------------------------------------------------------------------------
378 BOOL__
initialize( DWORD__ buffer_size
);
388 SAMPLE_INTERNAL
* data( );
389 DWORD__
size() const;
392 SAMPLE_INTERNAL
* m_data
;