*** empty log message ***
[chuck-blob.git] / exile / digitalio_osx.cpp
blobd919bad43ab267e69adb3feda5de8c4b7f582285
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: digitalio_osx.cpp
27 // desc: digitalio for osx
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 #include "digitalio_osx.h"
34 #include <iostream>
35 using namespace std;
40 // static
41 BOOL__ Digitalio::m_init = FALSE;
42 DWORD__ Digitalio::m_num_channels = NUM_CHANNELS_DEFAULT;
43 DWORD__ Digitalio::m_sampling_rate = SAMPLING_RATE_DEFAULT;
44 DWORD__ Digitalio::m_bps = BITS_PER_SAMPLE_DEFAULT;
45 DWORD__ Digitalio::m_buffer_size = BUFFER_SIZE_DEFAULT;
46 DWORD__ Digitalio::m_num_buffers = NUM_BUFFERS_DEFAULT;
51 //-----------------------------------------------------------------------------
52 // name: initialize()
53 // desc: ...
54 //-----------------------------------------------------------------------------
55 BOOL__ Digitalio::initialize( DWORD__ num_channels, DWORD__ sampling_rate,
56 DWORD__ bps, DWORD__ buffer_size, DWORD__ num_buffers )
58 if( m_init )
59 return FALSE;
61 m_num_channels = num_channels;
62 m_sampling_rate = sampling_rate;
63 m_bps = bps;
64 m_buffer_size = buffer_size;
65 m_num_buffers = num_buffers;
67 return m_init = TRUE;
73 //-----------------------------------------------------------------------------
74 // name: shutdown()
75 // desc: ...
76 //-----------------------------------------------------------------------------
77 void Digitalio::shutdown()
79 m_init = FALSE;
85 //-----------------------------------------------------------------------------
86 // name: DigitalOut()
87 // desc: ...
88 //-----------------------------------------------------------------------------
89 DigitalOut::DigitalOut()
91 m_init_out = FALSE;
92 m_render_start = FALSE;
94 m_num_channels = 0;
95 m_sampling_rate = 0;
96 m_bps = 0;
98 m_device_handle = kAudioDeviceUnknown;
99 m_device_num = 0;
101 m_out_buffers = NULL;
102 m_num_out_buffers = 0;
103 m_out_buffer_size = 0;
104 m_curr_buffer_out = 0;
106 m_data_ptr_out = NULL;
107 m_data_max_out = NULL;
108 m_index = 0;
110 pthread_mutex_init( &m_mutex, NULL );
111 m_block = 0;
112 m_out2 = 0;
118 //-----------------------------------------------------------------------------
119 // name: ~DigitalOut()
120 // desc: ...
121 //-----------------------------------------------------------------------------
122 DigitalOut::~DigitalOut()
124 this->cleanup();
130 //-----------------------------------------------------------------------------
131 // name: callback_handler_output()
132 // desc: ...
133 //-----------------------------------------------------------------------------
134 OSStatus callback_handler_output( AudioDeviceID inDevice,
135 const AudioTimeStamp* inNow,
136 const AudioBufferList* inInputData,
137 const AudioTimeStamp* inInputTime,
138 AudioBufferList* outOutputData,
139 const AudioTimeStamp* inOutputTime,
140 void* infoPointer )
142 DigitalOut * out = (DigitalOut *)infoPointer;
144 // cerr << ".";
146 if( !out->render( outOutputData ) )
147 return kAudioHardwareUnspecifiedError;
149 return kAudioHardwareNoError;
154 //-----------------------------------------------------------------------------
155 // name: initialize()
156 // desc: initialize audio out
157 //-----------------------------------------------------------------------------
158 BOOL__ DigitalOut::initialize( DWORD__ device_number, DWORD__ num_channels,
159 DWORD__ sampling_rate, DWORD__ bps,
160 DWORD__ buffer_size, DWORD__ num_buffers )
162 char msg[1024];
163 OSStatus err = noErr;
164 UInt32 dataSize = 0;
165 UInt32 the_buffer_size = 0;
166 AudioStreamBasicDescription format;
167 AudioBufferList * buffer_list = NULL;
169 // check if already
170 if( m_init_out )
172 sprintf( msg, "error: initialize(); DigitalOut already initialized..." );
173 goto error;
176 // set the parameters
177 m_num_channels = num_channels;
178 m_sampling_rate = sampling_rate;
179 m_bps = bps;
180 m_out_buffer_size = buffer_size;
181 m_num_out_buffers = num_buffers;
182 m_device_num = device_number;
184 // allocate the buffers
185 m_out_buffers = new InternalBuffer[m_num_out_buffers];
186 for( int i = 0; i < m_num_out_buffers; i++ )
187 m_out_buffers[i].initialize( m_out_buffer_size * sizeof(SAMPLE_INTERNAL) *
188 m_num_channels );
190 m_curr_buffer_out = 0;
191 m_data_ptr_out = m_out_buffers[0].data();
192 m_data_max_out = m_data_ptr_out + m_out_buffer_size * m_num_channels;
193 m_index = 0;
195 // get the default device
196 dataSize = sizeof( AudioDeviceID );
197 err = AudioHardwareGetProperty( kAudioHardwarePropertyDefaultOutputDevice,
198 &dataSize, &m_device_handle );
199 if( err != noErr )
201 sprintf( msg, "error opening output device device..." );
202 goto error;
205 // configure the device
206 err = AudioDeviceGetPropertyInfo( m_device_handle, 0, FALSE,
207 kAudioDevicePropertyStreamConfiguration,
208 &dataSize, NULL );
209 if( err != noErr || dataSize <= 0 )
211 sprintf( msg, "error configuring audio device..." );
212 goto error;
214 buffer_list = (AudioBufferList *)malloc(dataSize);
215 err = AudioDeviceGetProperty( m_device_handle, 0, FALSE,
216 kAudioDevicePropertyStreamConfiguration,
217 &dataSize, buffer_list );
218 if( err != noErr )
220 sprintf( msg, "error configuring audio device..." );
221 goto error;
224 AudioValueRange buffer_range;
225 dataSize = sizeof(AudioValueRange);
226 err = AudioDeviceGetProperty( m_device_handle, 0, FALSE,
227 kAudioDevicePropertyBufferSizeRange,
228 &dataSize, &buffer_range);
229 if( err != noErr )
231 sprintf( msg, "error dealing with range" );
232 goto error;
235 the_buffer_size = m_out_buffer_size * m_num_channels * sizeof(SAMPLE_INTERNAL);
237 // set the buffer size
238 dataSize = sizeof(UInt32);
239 err = AudioDeviceSetProperty( m_device_handle, NULL, 0, FALSE,
240 kAudioDevicePropertyBufferSize,
241 dataSize, &the_buffer_size );
242 if( err != noErr )
244 sprintf( msg, "error setting buffer size '%d'", the_buffer_size );
245 goto error;
248 // set the sample rate
249 dataSize = sizeof(AudioStreamBasicDescription);
250 err = AudioDeviceGetProperty( m_device_handle, 0, FALSE,
251 kAudioDevicePropertyStreamFormat,
252 &dataSize, &format );
253 if( err != noErr )
255 sprintf( msg, "error setting sampling rate..." );
256 goto error;
259 format.mSampleRate = m_sampling_rate;
260 format.mFormatID = kAudioFormatLinearPCM;
261 err = AudioDeviceSetProperty( m_device_handle, NULL, 0, FALSE,
262 kAudioDevicePropertyStreamFormat,
263 dataSize, &format );
264 if( err != noErr )
266 sprintf( msg, "error setting sampling rate '%d'", m_sampling_rate );
267 goto error;
270 // register callback handler
271 err = AudioDeviceAddIOProc( m_device_handle, callback_handler_output, this );
272 if( err != noErr )
274 sprintf( msg, "error setting callback" );
275 goto error;
278 // set the flag
279 m_init_out = TRUE;
281 return TRUE;
283 error:
284 // print out the error
285 cerr << msg << endl;
287 // clean
288 this->cleanup();
290 return FALSE;
296 //-----------------------------------------------------------------------------
297 // name: start()
298 // desc: render
299 //-----------------------------------------------------------------------------
300 BOOL__ DigitalOut::start()
302 int err;
303 char msg[1024];
305 pthread_mutex_lock( &m_mutex );
307 if( !m_init_out )
309 sprintf( msg, "error: start(): DigitalOut not initialized..." );
310 goto error;
313 // check to see if started already
314 if( m_render_start )
316 sprintf( msg, "error: start(): DigitalOut already started..." );
317 goto error;
320 // go
321 err = AudioDeviceStart( m_device_handle, callback_handler_output );
322 if( err != noErr )
324 sprintf( msg, "error starting audio device..." );
325 goto error;
328 m_block = 0;
329 m_out2 = 0;
331 // set the flag
332 m_render_start = TRUE;
334 pthread_mutex_unlock( &m_mutex );
335 return TRUE;
337 error:
338 // print out the error
339 cerr << msg << endl;
341 pthread_mutex_unlock( &m_mutex );
342 return FALSE;
348 //-----------------------------------------------------------------------------
349 // name: stop()
350 // desc: render
351 //-----------------------------------------------------------------------------
352 BOOL__ DigitalOut::stop()
354 int err;
355 char msg[1024];
357 // check to see if started
358 if( !m_render_start )
360 sprintf( msg, "error: stop(): DigitalOut not started..." );
361 goto error;
364 pthread_mutex_lock( &m_mutex );
366 err = AudioDeviceStop( m_device_handle, callback_handler_output );
367 if( err != noErr )
369 sprintf( msg, "error stopping audio device..." );
370 goto error;
373 // set the flag
374 m_render_start = FALSE;
376 pthread_mutex_unlock( &m_mutex );
377 return TRUE;
379 error:
380 // print out the error
381 cerr << msg << endl;
383 pthread_mutex_unlock( &m_mutex );
384 return FALSE;
390 //-----------------------------------------------------------------------------
391 // name: cleanup()
392 // desc: ...
393 //-----------------------------------------------------------------------------
394 void DigitalOut::cleanup()
396 // check if started
397 if( m_render_start )
398 stop();
402 // deallocate
403 if( m_out_buffers )
404 SAFE_DELETE_ARRAY( m_out_buffers );
406 // set the flag
407 m_init_out = FALSE;
413 //-----------------------------------------------------------------------------
414 // name: tick_out()
415 // desc: 1 channel
416 //-----------------------------------------------------------------------------
417 BOOL__ DigitalOut::tick_out( SAMPLE sample )
419 if( !prepare_tick_out() )
420 return FALSE;
422 *m_data_ptr_out++ = SAMPLE_TO_INTERNAL( sample );
424 return TRUE;
430 //-----------------------------------------------------------------------------
431 // name: tick_out()
432 // desc: 2 channel
433 //-----------------------------------------------------------------------------
434 BOOL__ DigitalOut::tick_out( SAMPLE sample_l, SAMPLE sample_r )
436 if( !prepare_tick_out() )
437 return FALSE;
439 *m_data_ptr_out++ = SAMPLE_TO_INTERNAL( sample_l );
440 *m_data_ptr_out++ = SAMPLE_TO_INTERNAL( sample_r );
442 return TRUE;
448 //-----------------------------------------------------------------------------
449 // name: tick_out()
450 // desc: all channels
451 //-----------------------------------------------------------------------------
452 BOOL__ DigitalOut::tick_out( const SAMPLE * samples, DWORD__ n )
454 if( !prepare_tick_out() )
455 return FALSE;
457 if( !n ) n = m_num_channels;
458 while( n-- )
459 *m_data_ptr_out++ = SAMPLE_TO_INTERNAL( *samples++ );
461 return TRUE;
467 //-----------------------------------------------------------------------------
468 // name: prepare_tick_out()
469 // desc: data ptr ok
470 //-----------------------------------------------------------------------------
471 inline BOOL__ DigitalOut::prepare_tick_out()
473 if( !m_init_out )
474 return FALSE;
476 if( m_data_ptr_out >= m_data_max_out )
478 if( !m_render_start && !start() )
479 return FALSE;
481 m_block++;
482 while( m_block >= m_num_out_buffers )
483 usleep( 5000 );
485 m_curr_buffer_out = (m_curr_buffer_out + 1) % m_num_out_buffers;
486 m_data_ptr_out = m_out_buffers[m_curr_buffer_out].data();
487 m_data_max_out = m_data_ptr_out + m_out_buffer_size * m_num_channels;
490 return TRUE;
496 //-----------------------------------------------------------------------------
497 // name: render()
498 // desc: the render
499 //-----------------------------------------------------------------------------
500 DWORD__ DigitalOut::render( AudioBufferList * buffer_list )
502 int err;
503 char msg[1024] = "";
505 while( !m_block )
506 usleep( 5000 );
508 // copy
509 memcpy( buffer_list->mBuffers[0].mData,
510 m_out_buffers[m_out2].data(),
511 buffer_list->mBuffers[0].mDataByteSize );
513 m_out2 = (m_out2 + 1) % m_num_out_buffers;
515 m_block--;
517 //cerr << ".";
519 return TRUE;
521 error:
523 cerr << msg << endl;
525 return FALSE;
531 //-----------------------------------------------------------------------------
532 // name: DigitalIn()
533 // desc: ...
534 //-----------------------------------------------------------------------------
535 DigitalIn::DigitalIn()
537 m_init_in = FALSE;
538 m_capture_start = FALSE;
540 m_num_channels = 0;
541 m_sampling_rate = 0;
542 m_bps = 0;
544 m_in_buffers = NULL;
545 m_num_in_buffers = 0;
546 m_in_buffer_size = 0;
547 m_curr_buffer_in = 0;
549 m_data_ptr_in = NULL;
550 m_data_max_in = NULL;
552 m_device_handle = kAudioDeviceUnknown;
553 m_device_num = 0;
555 pthread_mutex_init( &m_mutex, NULL );
556 m_block = 0;
557 m_in2 = 0;
563 //-----------------------------------------------------------------------------
564 // name: ~DigitalIn()
565 // desc: ...
566 //-----------------------------------------------------------------------------
567 DigitalIn::~DigitalIn()
569 this->cleanup();
575 //-----------------------------------------------------------------------------
576 // name: callback_handler_input()
577 // desc: ...
578 //-----------------------------------------------------------------------------
579 OSStatus callback_handler_input( AudioDeviceID inDevice,
580 const AudioTimeStamp* inNow,
581 const AudioBufferList* inInputData,
582 const AudioTimeStamp* inInputTime,
583 AudioBufferList* outOutputData,
584 const AudioTimeStamp* inOutputTime,
585 void* infoPointer )
587 DigitalIn * in = (DigitalIn *)infoPointer;
589 // cerr << "!";
591 if( !in->capture( inInputData ) )
592 return kAudioHardwareUnspecifiedError;
594 return kAudioHardwareNoError;
600 //-----------------------------------------------------------------------------
601 // name: initialize()
602 // desc: initialize audio in
603 //-----------------------------------------------------------------------------
604 BOOL__ DigitalIn::initialize( DWORD__ device_number, DWORD__ num_channels,
605 DWORD__ sampling_rate, DWORD__ bps,
606 DWORD__ buffer_size, DWORD__ num_buffers )
608 char msg[1024];
609 OSStatus err = noErr;
610 UInt32 dataSize = 0;
611 UInt32 the_buffer_size = 0;
612 AudioStreamBasicDescription format;
613 AudioBufferList * buffer_list = NULL;
615 // check if already
616 if( m_init_in )
618 sprintf( msg, "error: initialize(); DigitalIn already initialized..." );
619 goto error;
622 // set the parameters
623 m_num_channels = num_channels;
624 m_sampling_rate = sampling_rate;
625 m_bps = bps;
626 m_in_buffer_size = buffer_size;
627 m_num_in_buffers = num_buffers;
628 m_device_num = device_number;
630 // allocate the buffers
631 m_in_buffers = new InternalBuffer[m_num_in_buffers];
632 for( int i = 0; i < m_num_in_buffers; i++ )
633 m_in_buffers[i].initialize( m_in_buffer_size * sizeof(SAMPLE_INTERNAL) *
634 m_num_channels );
636 m_curr_buffer_in = 0;
637 m_data_ptr_in = m_in_buffers[0].data();
638 m_data_max_in = m_data_ptr_in + m_in_buffer_size * m_num_channels;
640 // get the default device
641 dataSize = sizeof( AudioDeviceID );
642 err = AudioHardwareGetProperty( kAudioHardwarePropertyDefaultOutputDevice,
643 &dataSize, &m_device_handle );
644 if( err != noErr )
646 sprintf( msg, "error opening input device device..." );
647 goto error;
650 // configure the device
651 err = AudioDeviceGetPropertyInfo( m_device_handle, 0, TRUE,
652 kAudioDevicePropertyStreamConfiguration,
653 &dataSize, NULL );
654 if( err != noErr || dataSize <= 0 )
656 sprintf( msg, "error configuring audio device..." );
657 goto error;
659 buffer_list = (AudioBufferList *)malloc(dataSize);
660 err = AudioDeviceGetProperty( m_device_handle, 0, TRUE,
661 kAudioDevicePropertyStreamConfiguration,
662 &dataSize, buffer_list );
663 if( err != noErr )
665 sprintf( msg, "error configuring audio device..." );
666 goto error;
669 AudioValueRange buffer_range;
670 dataSize = sizeof(AudioValueRange);
671 err = AudioDeviceGetProperty( m_device_handle, 0, TRUE,
672 kAudioDevicePropertyBufferSizeRange,
673 &dataSize, &buffer_range);
674 if( err != noErr )
676 sprintf( msg, "error dealing with range" );
677 goto error;
680 the_buffer_size = m_in_buffer_size * m_num_channels * sizeof(SAMPLE_INTERNAL);
682 // set the buffer size
683 dataSize = sizeof(UInt32);
684 err = AudioDeviceSetProperty( m_device_handle, NULL, 0, TRUE,
685 kAudioDevicePropertyBufferSize,
686 dataSize, &the_buffer_size );
687 if( err != noErr )
689 sprintf( msg, "error setting buffer size '%d'", the_buffer_size );
690 goto error;
693 // set the sample rate
694 dataSize = sizeof(AudioStreamBasicDescription);
695 err = AudioDeviceGetProperty( m_device_handle, 0, TRUE,
696 kAudioDevicePropertyStreamFormat,
697 &dataSize, &format );
698 if( err != noErr )
700 sprintf( msg, "error setting sampling rate..." );
701 goto error;
704 format.mSampleRate = m_sampling_rate;
705 format.mFormatID = kAudioFormatLinearPCM;
706 err = AudioDeviceSetProperty( m_device_handle, NULL, 0, TRUE,
707 kAudioDevicePropertyStreamFormat,
708 dataSize, &format );
709 if( err != noErr )
711 sprintf( msg, "error setting sampling rate '%d'", m_sampling_rate );
712 goto error;
715 // register callback handler
716 err = AudioDeviceAddIOProc( m_device_handle, callback_handler_input, this );
717 if( err != noErr )
719 sprintf( msg, "error setting callback" );
720 goto error;
723 // set the flag
724 m_init_in = TRUE;
726 return TRUE;
728 error:
729 // print out the error
730 cerr << msg << endl;
732 // clean
733 this->cleanup();
735 return FALSE;
741 //-----------------------------------------------------------------------------
742 // name: start()
743 // desc: ...
744 //-----------------------------------------------------------------------------
745 BOOL__ DigitalIn::start()
747 int err;
748 char msg[1024];
750 // check if initialized
751 if( !m_init_in )
753 sprintf( msg, "error: start(): DigitalIn not initialized..." );
754 goto error;
757 // check if started already
758 if( m_capture_start )
760 sprintf( msg, "error: start(): DigitalIn already started..." );
761 goto error;
764 // go
765 //err = AudioDeviceStart( m_device_handle, callback_handler_input );
766 //if( err != noErr )
768 // sprintf( msg, "error starting audio device..." );
769 // goto error;
772 m_block = 0;
773 m_in2 = 1;
775 // set the flag
776 m_capture_start = TRUE;
778 return TRUE;
780 error:
781 // print out the error
782 cerr << msg << endl;
784 return FALSE;
790 //-----------------------------------------------------------------------------
791 // name: capture_stop()
792 // desc: ...
793 //-----------------------------------------------------------------------------
794 BOOL__ DigitalIn::stop()
796 int err;
797 char msg[1024];
799 // check to see if started
800 if( !m_capture_start )
802 sprintf( msg, "error: stop(): DigitalIn not started..." );
803 goto error;
806 err = AudioDeviceStop( m_device_handle, callback_handler_input );
807 if( err != noErr )
809 sprintf( msg, "error stopping audio device..." );
810 goto error;
813 // set the flag
814 m_capture_start = FALSE;
816 error:
817 // print out the error
818 cerr << msg << endl;
820 return FALSE;
826 //-----------------------------------------------------------------------------
827 // name: cleanup()
828 // desc: ...
829 //-----------------------------------------------------------------------------
830 void DigitalIn::cleanup()
832 // check if started
833 if( m_capture_start )
835 // stop
836 stop();
839 // deallocate
840 if( m_in_buffers )
841 SAFE_DELETE_ARRAY( m_in_buffers );
843 // set the flag
844 m_init_in = FALSE;
850 //-----------------------------------------------------------------------------
851 // name: tick_in()
852 // desc: ...
853 //-----------------------------------------------------------------------------
854 BOOL__ DigitalIn::tick_in( SAMPLE * s )
856 if( !prepare_tick_in() )
857 return 0;
859 *s = INTERNAL_TO_SAMPLE( *m_data_ptr_in++ );
861 return TRUE;
867 //-----------------------------------------------------------------------------
868 // name: tick_in()
869 // desc: ...
870 //-----------------------------------------------------------------------------
871 BOOL__ DigitalIn::tick_in( SAMPLE * l, SAMPLE * r )
873 if( !prepare_tick_in() )
874 return FALSE;
876 *l = INTERNAL_TO_SAMPLE( *m_data_ptr_in++ );
877 *r = INTERNAL_TO_SAMPLE( *m_data_ptr_in++ );
879 return TRUE;
885 //-----------------------------------------------------------------------------
886 // name: tick_in()
887 // desc: ...
888 //-----------------------------------------------------------------------------
889 BOOL__ DigitalIn::tick_in( SAMPLE * sample, DWORD__ n )
891 if( !prepare_tick_in() )
892 return FALSE;
894 if( !n ) n = m_num_channels;
895 while( n-- )
896 *sample++ = INTERNAL_TO_SAMPLE( *m_data_ptr_in++ );
898 return TRUE;
904 //-----------------------------------------------------------------------------
905 // name: prepare_tick_in()
906 // desc: data ptr ok
907 //-----------------------------------------------------------------------------
908 inline BOOL__ DigitalIn::prepare_tick_in()
910 if( !m_init_in )
911 return FALSE;
913 if( !m_capture_start ) if( !this->start() )
914 return FALSE;
916 if( m_data_ptr_in >= m_data_max_in )
918 while( !m_block )
919 usleep( 5000 );
921 //m_in_buffers[m_curr_buffer_in].clear();
922 m_curr_buffer_in = (m_curr_buffer_in + 1) % m_num_in_buffers;
923 m_data_ptr_in = m_in_buffers[m_curr_buffer_in].data();
924 m_data_max_in = m_data_ptr_in + m_in_buffer_size * m_num_channels;
926 m_block--;
929 return TRUE;
935 //-----------------------------------------------------------------------------
936 // name: capture()
937 // desc: ...
938 //-----------------------------------------------------------------------------
939 DWORD__ DigitalIn::capture( const AudioBufferList * buffer_list )
941 int err;
942 char msg[1024] = "";
944 while( m_block >= m_num_in_buffers )
945 usleep( 5000 );
947 // copy
948 memcpy( m_in_buffers[m_in2].data(),
949 buffer_list->mBuffers[0].mData,
950 buffer_list->mBuffers[0].mDataByteSize );
952 m_in2 = (m_in2 + 1) % m_num_in_buffers;
954 m_block++;
956 cerr << ".";
958 return TRUE;
960 error:
962 cerr << msg << endl;
964 return FALSE;
970 //-----------------------------------------------------------------------------
971 // name: AudioBuffer__()
972 // desc: ...
973 //-----------------------------------------------------------------------------
974 AudioBuffer__::AudioBuffer__( DWORD__ size )
976 if( size )
977 this->initialize( size );
978 else
980 m_size = size;
981 m_data = m_ptr_curr = m_ptr_end = NULL;
988 //-----------------------------------------------------------------------------
989 // name: initialize()
990 // desc: ...
991 //-----------------------------------------------------------------------------
992 BOOL__ AudioBuffer__::initialize( DWORD__ size )
994 m_size = size;
995 m_data = (SAMPLE *)malloc( size * sizeof(SAMPLE) );
997 if( !m_data )
998 return FALSE;
1000 // clear the memory
1001 memset( m_data, 0, size * sizeof(SAMPLE) );
1003 // set the pointers
1004 m_ptr_curr = m_data;
1005 m_ptr_end = m_data + size;
1007 return TRUE;
1013 //-----------------------------------------------------------------------------
1014 // name: cleanup()
1015 // desc: ...
1016 //-----------------------------------------------------------------------------
1017 void AudioBuffer__::cleanup()
1019 if( m_data )
1021 free( m_data );
1022 m_data = NULL;
1025 m_size = 0;
1026 m_ptr_curr = m_ptr_end = NULL;
1032 //-----------------------------------------------------------------------------
1033 // name: size()
1034 // desc: ...
1035 //-----------------------------------------------------------------------------
1036 DWORD__ AudioBuffer__::size()
1038 return m_size;
1044 //-----------------------------------------------------------------------------
1045 // name: data()
1046 // desc: ...
1047 //-----------------------------------------------------------------------------
1048 SAMPLE * AudioBuffer__::data()
1050 return m_data;
1056 //-----------------------------------------------------------------------------
1057 // name: AudioBuffer__In()
1058 // desc: ...
1059 //-----------------------------------------------------------------------------
1060 AudioBuffer__In::AudioBuffer__In( DWORD__ size )
1061 : AudioBuffer__( size )
1067 //-----------------------------------------------------------------------------
1068 // name: ~AudioBuffer__In()
1069 // desc: ...
1070 //-----------------------------------------------------------------------------
1071 AudioBuffer__In::~AudioBuffer__In()
1073 this->cleanup();
1079 //-----------------------------------------------------------------------------
1080 // name: reset()
1081 // desc: ...
1082 //-----------------------------------------------------------------------------
1083 BOOL__ AudioBuffer__In::reset()
1085 m_ptr_curr = m_data;
1087 return TickIn::reset();
1093 //-----------------------------------------------------------------------------
1094 // name: tick_in()
1095 // desc: ...
1096 //-----------------------------------------------------------------------------
1097 BOOL__ AudioBuffer__In::tick_in( SAMPLE * s )
1099 if( !m_data )
1100 return FALSE;
1102 *s = *m_ptr_curr++;
1104 return TRUE;
1110 //-----------------------------------------------------------------------------
1111 // name: tick_in()
1112 // desc: ...
1113 //-----------------------------------------------------------------------------
1114 BOOL__ AudioBuffer__In::tick_in( SAMPLE * l, SAMPLE * r )
1116 if( !m_data || m_ptr_curr + 2 >= m_ptr_end )
1117 return FALSE;
1119 *l = *m_ptr_curr++;
1120 *r = *m_ptr_curr++;
1122 return TRUE;
1128 //-----------------------------------------------------------------------------
1129 // name: tick_in()
1130 // desc: ...
1131 //-----------------------------------------------------------------------------
1132 BOOL__ AudioBuffer__In::tick_in( SAMPLE * in, DWORD__ n )
1134 if( !m_data || m_ptr_curr + n >= m_ptr_end )
1135 return FALSE;
1137 while( n-- )
1138 *in++ = *m_ptr_curr++;
1140 return TRUE;
1146 //-----------------------------------------------------------------------------
1147 // name: AudioBuffer__Out()
1148 // desc: ...
1149 //-----------------------------------------------------------------------------
1150 AudioBuffer__Out::AudioBuffer__Out( DWORD__ size )
1151 : AudioBuffer__( size )
1157 //-----------------------------------------------------------------------------
1158 // name: ~AudioBuffer__Out()
1159 // desc: ...
1160 //-----------------------------------------------------------------------------
1161 AudioBuffer__Out::~AudioBuffer__Out()
1163 this->cleanup();
1169 //-----------------------------------------------------------------------------
1170 // name: reset()
1171 // desc: ...
1172 //-----------------------------------------------------------------------------
1173 BOOL__ AudioBuffer__Out::reset()
1175 m_ptr_curr = m_data;
1177 return TickOut::reset();
1183 //-----------------------------------------------------------------------------
1184 // name: tick_out()
1185 // desc: ...
1186 //-----------------------------------------------------------------------------
1187 BOOL__ AudioBuffer__Out::tick_out( SAMPLE s )
1189 if( !m_data )
1190 return FALSE;
1192 *m_ptr_curr++ = s;
1194 return TRUE;
1200 //-----------------------------------------------------------------------------
1201 // name: tick_out()
1202 // desc: ...
1203 //-----------------------------------------------------------------------------
1204 BOOL__ AudioBuffer__Out::tick_out( SAMPLE l, SAMPLE r )
1206 if( !m_data || m_ptr_curr + 2 >= m_ptr_end )
1207 return FALSE;
1209 *m_ptr_curr++ = l;
1210 *m_ptr_curr++ = r;
1212 return TRUE;
1218 //-----------------------------------------------------------------------------
1219 // name: tick_out()
1220 // desc: ...
1221 //-----------------------------------------------------------------------------
1222 BOOL__ AudioBuffer__Out::tick_out( const SAMPLE * out, DWORD__ n )
1224 if( !m_data || m_ptr_curr + n >= m_ptr_end )
1225 return FALSE;
1227 while( n-- )
1228 *m_ptr_curr++ = *out++;
1230 return TRUE;
1236 //-----------------------------------------------------------------------------
1237 // name: InternalBuffer()
1238 // desc: constructor
1239 //-----------------------------------------------------------------------------
1240 InternalBuffer::InternalBuffer()
1242 m_data = NULL;
1243 m_size = 0;
1249 //-----------------------------------------------------------------------------
1250 // name: ~InternalBuffer()
1251 // desc: destructor
1252 //-----------------------------------------------------------------------------
1253 InternalBuffer::~InternalBuffer()
1255 this->cleanup();
1261 //-----------------------------------------------------------------------------
1262 // name: initialize()
1263 // desc: ...
1264 //-----------------------------------------------------------------------------
1265 BOOL__ InternalBuffer::initialize( DWORD__ buffer_size )
1267 this->cleanup();
1269 // allocate the buffer
1270 if( !(m_data = (SAMPLE_INTERNAL *)malloc(buffer_size)) )
1271 return FALSE;
1273 // set the buffer size
1274 m_size = buffer_size;
1276 // clear the buffer
1277 return this->clear();
1283 //-----------------------------------------------------------------------------
1284 // name: cleanup()
1285 // desc: ...
1286 //-----------------------------------------------------------------------------
1287 BOOL__ InternalBuffer::clear()
1289 if( !m_data )
1290 return FALSE;
1292 // clear the memory
1293 memset( m_data, 0, m_size );
1295 return TRUE;
1301 //-----------------------------------------------------------------------------
1302 // name: cleanup()
1303 // desc: ...
1304 //-----------------------------------------------------------------------------
1305 void InternalBuffer::cleanup()
1307 if( m_data )
1309 free(m_data);
1310 m_data = NULL;
1311 m_size = 0;
1318 //-----------------------------------------------------------------------------
1319 // name: data()
1320 // desc: get the buffer
1321 //-----------------------------------------------------------------------------
1322 SAMPLE_INTERNAL * InternalBuffer::data( )
1324 return m_data;
1330 //-----------------------------------------------------------------------------
1331 // name: size()
1332 // desc: ...
1333 //-----------------------------------------------------------------------------
1334 DWORD__ InternalBuffer::size() const
1336 return m_size;
1342 //-----------------------------------------------------------------------------
1343 // name: synch()
1344 // desc: wait for the buffer to be ready
1345 //-----------------------------------------------------------------------------
1346 void InternalBuffer::synch()
1353 //-----------------------------------------------------------------------------
1354 // name: signal()
1355 // desc: signal the buffer to be used again
1356 //-----------------------------------------------------------------------------
1357 void InternalBuffer::signal()
1364 //-----------------------------------------------------------------------------
1365 // name: reset()
1366 // desc:...
1367 //-----------------------------------------------------------------------------
1368 void InternalBuffer::reset()