| @@ -23,65 +23,31 @@ · 22 unmodified lines |
| 23 | -----------------------------------------------------------------------------*/ | 23 | -----------------------------------------------------------------------------*/ |
| 24 | | 24 | |
| 25 | //----------------------------------------------------------------------------- | 25 | //----------------------------------------------------------------------------- |
| 26 | // name: digiio_rtaudio.cpp | 26 | // name: chuck_audio.cpp |
| 27 | // desc: digitalio over RtAudio (from Gary Scavone) | 27 | // desc: chuck host digital audio I/O, using RtAudio |
| 28 | // | 28 | // |
| 29 | // author: Ge Wang (ge@ccrma.stanford.edu | gewang@cs.princeton.edu) | 29 | // author: Ge Wang (ge@ccrma.stanford.edu | gewang@cs.princeton.edu) |
| 30 | // Spencer Salazar (spencer@ccrma.stanford.edu) | 30 | // Spencer Salazar (spencer@ccrma.stanford.edu) |
| 31 | // RtAudio by: Gary Scavone | 31 | // RtAudio by: Gary Scavone |
| 32 | // date: Spring 2004 | 32 | // |
| | 33 | // date: Spring 2004 -- initial versions |
| | 34 | // Fall 2017 -- REFACTOR-2017: clean up to be more modular |
| 33 | //----------------------------------------------------------------------------- | 35 | //----------------------------------------------------------------------------- |
| 34 | #include "digiio_rtaudio.h" | 36 | #include "chuck_audio.h" |
| 35 | #include "chuck_vm.h" | | |
| 36 | #include "chuck_errmsg.h" | 37 | #include "chuck_errmsg.h" |
| 37 | #include "chuck_globals.h" | 38 | #include "chuck_globals.h" |
| 38 | #include <limits.h> | 39 | #include <limits.h> |
| 39 | #ifndef __DISABLE_RTAUDIO__ | | |
| 40 | #include "RtAudio/RtAudio.h" | | |
| 41 | #endif // __DISABLE_RTAUDIO__ | | |
| 42 | #if defined(__CHIP_MODE__) | | |
| 43 | #include "momu/mo_audio.h" | | |
| 44 | #endif // __CHIP_MODE__ | | |
| 45 | #ifndef __DISABLE_MIDI__ | | |
| 46 | #include "rtmidi.h" | 40 | #include "rtmidi.h" |
| 47 | #endif // __DISABLE_MIDI__ | 41 | |
| 48 | // #include <signal.h> | | |
| 49 | #if (defined(__WINDOWS_DS__) || defined(__WINDOWS_ASIO__)) && !defined(__WINDOWS_PTHREAD__) | 42 | #if (defined(__WINDOWS_DS__) || defined(__WINDOWS_ASIO__)) && !defined(__WINDOWS_PTHREAD__) |
| 50 | #include <windows.h> | 43 | #include <windows.h> |
| 51 | #include <sys/timeb.h> | 44 | #include <sys/timeb.h> |
| 52 | #else | 45 | #else |
| 53 | #include <unistd.h> | 46 | #include <unistd.h> |
| 54 | #include <sys/time.h> | 47 | #include <sys/time.h> |
| 55 | #endif | 48 | #endif |
| 56 | | 49 | |
| 57 | // extern "C" void signal_int( int ); | 50 | |
| 58 | | | |
| 59 | // static | | |
| 60 | BOOL__ Digitalio::m_init = FALSE; | | |
| 61 | DWORD__ Digitalio::m_start = 0; | | |
| 62 | DWORD__ Digitalio::m_tick_count = 0; | | |
| 63 | DWORD__ Digitalio::m_num_channels_out = NUM_CHANNELS_DEFAULT; | | |
| 64 | DWORD__ Digitalio::m_num_channels_in = NUM_CHANNELS_DEFAULT; | | |
| 65 | DWORD__ Digitalio::m_sampling_rate = SAMPLING_RATE_DEFAULT; | | |
| 66 | DWORD__ Digitalio::m_bps = BITS_PER_SAMPLE_DEFAULT; | | |
| 67 | DWORD__ Digitalio::m_buffer_size = BUFFER_SIZE_DEFAULT; | | |
| 68 | DWORD__ Digitalio::m_num_buffers = NUM_BUFFERS_DEFAULT; | | |
| 69 | RtAudio * Digitalio::m_rtaudio = NULL; | | |
| 70 | SAMPLE * Digitalio::m_buffer_out = NULL; | | |
| 71 | SAMPLE * Digitalio::m_buffer_in = NULL; | | |
| 72 | SAMPLE ** Digitalio::m_write_ptr = NULL; | | |
| 73 | SAMPLE ** Digitalio::m_read_ptr = NULL; | | |
| 74 | SAMPLE * Digitalio::m_extern_in = NULL; | | |
| 75 | SAMPLE * Digitalio::m_extern_out = NULL; | | |
| 76 | BOOL__ Digitalio::m_out_ready = FALSE; | | |
| 77 | BOOL__ Digitalio::m_in_ready = FALSE; | | |
| 78 | BOOL__ Digitalio::m_use_cb = USE_CB_DEFAULT; | | |
| 79 | DWORD__ Digitalio::m_go = 0; | | |
| 80 | DWORD__ Digitalio::m_dac_n = 0; | | |
| 81 | DWORD__ Digitalio::m_adc_n = 0; | | |
| 82 | DWORD__ Digitalio::m_end = 0; | | |
| 83 | DWORD__ Digitalio::m_block = TRUE; | | |
| 84 | DWORD__ Digitalio::m_xrun = 0; | | |
| 85 | | 51 | |
| 86 | | 52 | |
| 87 | // sample | 53 | // sample |
| @@ -94,7 +60,29 @@ DWORD__ Digitalio::m_xrun = 0; · 6 unmodified lines |
| 94 | | 60 | |
| 95 | | 61 | |
| 96 | | 62 | |
| 97 | #ifndef __DISABLE_RTAUDIO__ | 63 | // static initialization |
| | 64 | t_CKBOOL ChuckAudio::m_init = FALSE; |
| | 65 | t_CKBOOL ChuckAudio::m_start = FALSE; |
| | 66 | t_CKBOOL ChuckAudio::m_go = FALSE; |
| | 67 | t_CKBOOL ChuckAudio::m_silent = FALSE; |
| | 68 | t_CKUINT ChuckAudio::m_num_channels_out = NUM_CHANNELS_DEFAULT; |
| | 69 | t_CKUINT ChuckAudio::m_num_channels_in = NUM_CHANNELS_DEFAULT; |
| | 70 | t_CKUINT ChuckAudio::m_sample_rate = SAMPLE_RATE_DEFAULT; |
| | 71 | t_CKUINT ChuckAudio::m_buffer_size = BUFFER_SIZE_DEFAULT; |
| | 72 | t_CKUINT ChuckAudio::m_num_buffers = NUM_BUFFERS_DEFAULT; |
| | 73 | SAMPLE * ChuckAudio::m_buffer_out = NULL; |
| | 74 | SAMPLE * ChuckAudio::m_buffer_in = NULL; |
| | 75 | SAMPLE * ChuckAudio::m_extern_in = NULL; |
| | 76 | SAMPLE * ChuckAudio::m_extern_out = NULL; |
| | 77 | t_CKUINT ChuckAudio::m_dac_n = 0; |
| | 78 | t_CKUINT ChuckAudio::m_adc_n = 0; |
| | 79 | RtAudio * ChuckAudio::m_rtaudio = NULL; |
| | 80 | f_audio_cb ChuckAudio::m_audio_cb = NULL; |
| | 81 | void * ChuckAudio::m_cb_user_data = NULL; |
| | 82 | |
| | 83 | |
| | 84 | |
| | 85 | |
| 98 | //----------------------------------------------------------------------------- | 86 | //----------------------------------------------------------------------------- |
| 99 | // name: print() | 87 | // name: print() |
| 100 | // desc: ... | 88 | // desc: ... |
| @@ -134,7 +122,6 @@ void print( const RtAudio::DeviceInfo & info ) · 33 unmodified lines |
| 134 | } | 122 | } |
| 135 | } | 123 | } |
| 136 | } | 124 | } |
| 137 | #endif // __DISABLE_RTAUDIO__ | | |
| 138 | | 125 | |
| 139 | | 126 | |
| 140 | | 127 | |
| @@ -143,14 +130,15 @@ void print( const RtAudio::DeviceInfo & info ) · 2 unmodified lines |
| 143 | // name: probe() | 130 | // name: probe() |
| 144 | // desc: ... | 131 | // desc: ... |
| 145 | //----------------------------------------------------------------------------- | 132 | //----------------------------------------------------------------------------- |
| 146 | void Digitalio::probe() | 133 | void ChuckAudio::probe() |
| 147 | { | 134 | { |
| 148 | #ifndef __DISABLE_RTAUDIO__ | 135 | // rtaduio pointer |
| 149 | RtAudio * rta = NULL; | 136 | RtAudio * audio = NULL; |
| | 137 | // device info struct |
| 150 | RtAudio::DeviceInfo info; | 138 | RtAudio::DeviceInfo info; |
| 151 | | 139 | |
| 152 | // allocate RtAudio | 140 | // allocate RtAudio |
| 153 | try { rta = new RtAudio( ); } | 141 | try { audio = new RtAudio( ); } |
| 154 | catch( RtError err ) | 142 | catch( RtError err ) |
| 155 | { | 143 | { |
| 156 | // problem finding audio devices, most likely | 144 | // problem finding audio devices, most likely |
| @@ -158,17 +146,17 @@ void Digitalio::probe() · 1 unmodified line |
| 158 | return; | 146 | return; |
| 159 | } | 147 | } |
| 160 | | 148 | |
| 161 | // get count | 149 | // get count |
| 162 | int devices = rta->getDeviceCount(); | 150 | int devices = audio->getDeviceCount(); |
| 163 | EM_error2b( 0, "found %d device(s) ...", devices ); | 151 | EM_error2b( 0, "found %d device(s) ...", devices ); |
| 164 | // EM_error2( 0, "--------------------------" ); | 152 | |
| 165 | | 153 | // reset -- what does this do |
| 166 | EM_reset_msg(); | 154 | EM_reset_msg(); |
| 167 | | 155 | |
| 168 | // loop | 156 | // loop over devices |
| 169 | for( int i = 0; i < devices; i++ ) | 157 | for( int i = 0; i < devices; i++ ) |
| 170 | { | 158 | { |
| 171 | try { info = rta->getDeviceInfo(i); } | 159 | try { info = audio->getDeviceInfo(i); } |
| 172 | catch( RtError & error ) | 160 | catch( RtError & error ) |
| 173 | { | 161 | { |
| 174 | error.printMessage(); | 162 | error.printMessage(); |
| @@ -181,11 +169,12 @@ void Digitalio::probe() · 6 unmodified lines |
| 181 | // skip | 169 | // skip |
| 182 | if( i < devices ) EM_error2( 0, "" ); | 170 | if( i < devices ) EM_error2( 0, "" ); |
| 183 | | 171 | |
| | 172 | // reset |
| 184 | EM_reset_msg(); | 173 | EM_reset_msg(); |
| 185 | } | 174 | } |
| 186 | | 175 | |
| 187 | delete rta; | 176 | // done |
| 188 | #endif // __DISABLE_RTAUDIO__ | 177 | SAFE_DELETE( audio ); |
| 189 | | 178 | |
| 190 | return; | 179 | return; |
| 191 | } | 180 | } |
| @@ -195,16 +184,19 @@ void Digitalio::probe() · 3 unmodified lines |
| 195 | | 184 | |
| 196 | //----------------------------------------------------------------------------- | 185 | //----------------------------------------------------------------------------- |
| 197 | // name: device_named() | 186 | // name: device_named() |
| 198 | // desc: ... | 187 | // desc: get device number by name; needs_dac/adc prompts further checks on |
| | 188 | // requested device having > 0 channels |
| 199 | //----------------------------------------------------------------------------- | 189 | //----------------------------------------------------------------------------- |
| 200 | DWORD__ Digitalio::device_named( const std::string & name, t_CKBOOL needs_dac, t_CKBOOL needs_adc ) | 190 | t_CKUINT ChuckAudio::device_named( const std::string & name, t_CKBOOL needs_dac, |
| | 191 | t_CKBOOL needs_adc ) |
| 201 | { | 192 | { |
| 202 | #ifndef __DISABLE_RTAUDIO__ | 193 | // rtaudio pointer |
| 203 | RtAudio * rta = NULL; | 194 | RtAudio * audio = NULL; |
| | 195 | // device info struct |
| 204 | RtAudio::DeviceInfo info; | 196 | RtAudio::DeviceInfo info; |
| 205 | | 197 | |
| 206 | // allocate RtAudio | 198 | // allocate RtAudio |
| 207 | try { rta = new RtAudio( ); } | 199 | try { audio = new RtAudio(); } |
| 208 | catch( RtError err ) | 200 | catch( RtError err ) |
| 209 | { | 201 | { |
| 210 | // problem finding audio devices, most likely | 202 | // problem finding audio devices, most likely |
| @@ -219,26 +211,30 @@ DWORD__ Digitalio::device_named( const std::string & name, t_CKBOOL needs_dac, t · 8 unmodified lines |
| 219 | // loop | 211 | // loop |
| 220 | for( int i = 0; i < devices; i++ ) | 212 | for( int i = 0; i < devices; i++ ) |
| 221 | { | 213 | { |
| 222 | try { info = rta->getDeviceInfo(i); } | 214 | // get info |
| | 215 | try { info = audio->getDeviceInfo(i); } |
| 223 | catch( RtError & error ) | 216 | catch( RtError & error ) |
| 224 | { | 217 | { |
| 225 | error.printMessage(); | 218 | error.printMessage(); |
| 226 | break; | 219 | break; |
| 227 | } | 220 | } |
| 228 | | 221 | |
| | 222 | // compare name |
| 229 | if( info.name.compare(name) == 0 ) | 223 | if( info.name.compare(name) == 0 ) |
| 230 | { | 224 | { |
| | 225 | // found! |
| 231 | device_no = i+1; | 226 | device_no = i+1; |
| 232 | break; | 227 | break; |
| 233 | } | 228 | } |
| 234 | } | 229 | } |
| 235 | | 230 | |
| | 231 | // not found, yet |
| 236 | if( device_no == -1 ) | 232 | if( device_no == -1 ) |
| 237 | { | 233 | { |
| 238 | // no exact match found; try partial match | 234 | // no exact match found; try partial match |
| 239 | for( int i = 0; i < devices; i++ ) | 235 | for( int i = 0; i < devices; i++ ) |
| 240 | { | 236 | { |
| 241 | try { info = rta->getDeviceInfo(i); } | 237 | try { info = audio->getDeviceInfo(i); } |
| 242 | catch( RtError & error ) | 238 | catch( RtError & error ) |
| 243 | { | 239 | { |
| 244 | error.printMessage(); | 240 | error.printMessage(); |
| @@ -247,9 +243,11 @@ DWORD__ Digitalio::device_named( const std::string & name, t_CKBOOL needs_dac, t · 2 unmodified lines |
| 247 | | 243 | |
| 248 | if( info.name.find(name) != std::string::npos ) | 244 | if( info.name.find(name) != std::string::npos ) |
| 249 | { | 245 | { |
| | 246 | // skip over ones with 0 channels, if needed |
| 250 | if( (needs_dac && info.outputChannels == 0) || | 247 | if( (needs_dac && info.outputChannels == 0) || |
| 251 | (needs_adc && info.inputChannels == 0) ) | 248 | (needs_adc && info.inputChannels == 0) ) |
| 252 | continue; | 249 | continue; |
| | 250 | // found |
| 253 | device_no = i+1; | 251 | device_no = i+1; |
| 254 | break; | 252 | break; |
| 255 | } | 253 | } |
| @@ -257,65 +255,13 @@ DWORD__ Digitalio::device_named( const std::string & name, t_CKBOOL needs_dac, t · 1 unmodified line |
| 257 | } | 255 | } |
| 258 | | 256 | |
| 259 | // clean up | 257 | // clean up |
| 260 | SAFE_DELETE( rta ); | 258 | SAFE_DELETE( audio ); |
| 261 | | 259 | |
| 262 | // done | 260 | // done |
| 263 | return device_no; | 261 | return device_no; |
| 264 | | | |
| 265 | #endif // __DISABLE_RTAUDIO__ | | |
| 266 | | | |
| 267 | return -1; | | |
| 268 | } | | |
| 269 | | | |
| 270 | | | |
| 271 | | | |
| 272 | #if !defined(__PLATFORM_WIN32__) || defined(__WINDOWS_PTHREAD__) | | |
| 273 | //----------------------------------------------------------------------------- | | |
| 274 | // name: set_priority() | | |
| 275 | // desc: ... | | |
| 276 | //----------------------------------------------------------------------------- | | |
| 277 | static t_CKBOOL set_priority( CHUCK_THREAD tid, t_CKINT priority ) | | |
| 278 | { | | |
| 279 | struct sched_param param; | | |
| 280 | int policy; | | |
| 281 | | | |
| 282 | // log | | |
| 283 | EM_log( CK_LOG_FINE, "setting thread priority to: %ld...", priority ); | | |
| 284 | | | |
| 285 | // get for thread | | |
| 286 | if( pthread_getschedparam( tid, &policy, ¶m) ) | | |
| 287 | return FALSE; | | |
| 288 | | | |
| 289 | // priority | | |
| 290 | param.sched_priority = priority; | | |
| 291 | // policy | | |
| 292 | policy = SCHED_RR; | | |
| 293 | // set for thread | | |
| 294 | if( pthread_setschedparam( tid, policy, ¶m ) ) | | |
| 295 | return FALSE; | | |
| 296 | | | |
| 297 | return TRUE; | | |
| 298 | } | 262 | } |
| 299 | #else | | |
| 300 | //----------------------------------------------------------------------------- | | |
| 301 | // name: set_priority() | | |
| 302 | // desc: ... | | |
| 303 | //----------------------------------------------------------------------------- | | |
| 304 | static t_CKBOOL set_priority( CHUCK_THREAD tid, t_CKINT priority ) | | |
| 305 | { | | |
| 306 | // if priority is 0 then done | | |
| 307 | if( !priority ) return TRUE; | | |
| 308 | | | |
| 309 | // log | | |
| 310 | EM_log( CK_LOG_FINE, "setting thread priority to: %ld...", priority ); | | |
| 311 | | 263 | |
| 312 | // set the priority the thread | | |
| 313 | if( !SetThreadPriority( tid, priority ) ) | | |
| 314 | return FALSE; | | |
| 315 | | 264 | |
| 316 | return TRUE; | | |
| 317 | } | | |
| 318 | #endif | | |
| 319 | | 265 | |
| 320 | | 266 | |
| 321 | //----------------------------------------------------------------------------- | 267 | //----------------------------------------------------------------------------- |
| @@ -333,11 +279,11 @@ static t_CKFLOAT get_current_time( t_CKBOOL fresh = TRUE ) · 11 unmodified lines |
| 333 | if( fresh ) gettimeofday(&t,NULL); | 279 | if( fresh ) gettimeofday(&t,NULL); |
| 334 | return t.tv_sec + (t_CKFLOAT)t.tv_usec/1000000; | 280 | return t.tv_sec + (t_CKFLOAT)t.tv_usec/1000000; |
| 335 | #endif | 281 | #endif |
| 336 | | | |
| 337 | return 0; | | |
| 338 | } | 282 | } |
| 339 | | 283 | |
| 340 | | 284 | |
| | 285 | |
| | 286 | |
| 341 | // watch dog globals | 287 | // watch dog globals |
| 342 | static CHUCK_THREAD g_tid_synthesis = 0; | 288 | static CHUCK_THREAD g_tid_synthesis = 0; |
| 343 | static XThread * g_watchdog_thread = NULL; | 289 | static XThread * g_watchdog_thread = NULL; |
| @@ -345,9 +291,11 @@ static t_CKBOOL g_watchdog_state = FALSE; · 1 unmodified line |
| 345 | static t_CKFLOAT g_watchdog_time = 0; | 291 | static t_CKFLOAT g_watchdog_time = 0; |
| 346 | | 292 | |
| 347 | | 293 | |
| | 294 | |
| | 295 | |
| 348 | //----------------------------------------------------------------------------- | 296 | //----------------------------------------------------------------------------- |
| 349 | // name: watch_dog() | 297 | // name: watch_dog() |
| 350 | // desc: ... | 298 | // desc: this sniff out possible non-time advancing infinite loops |
| 351 | //----------------------------------------------------------------------------- | 299 | //----------------------------------------------------------------------------- |
| 352 | #if !defined(__PLATFORM_WIN32__) || defined(__WINDOWS_PTHREAD__) | 300 | #if !defined(__PLATFORM_WIN32__) || defined(__WINDOWS_PTHREAD__) |
| 353 | static void * watch_dog( void * ) | 301 | static void * watch_dog( void * ) |
| @@ -355,7 +303,8 @@ static void * watch_dog( void * ) · 1 unmodified line |
| 355 | static unsigned int __stdcall watch_dog( void * ) | 303 | static unsigned int __stdcall watch_dog( void * ) |
| 356 | #endif | 304 | #endif |
| 357 | { | 305 | { |
| 358 | t_CKFLOAT time; | 306 | // time |
| | 307 | t_CKFLOAT time = 0; |
| 359 | | 308 | |
| 360 | // boost priority? | 309 | // boost priority? |
| 361 | t_CKUINT priority = Chuck_VM::our_priority; | 310 | t_CKUINT priority = Chuck_VM::our_priority; |
| @@ -371,43 +320,41 @@ static unsigned int __stdcall watch_dog( void * ) · 9 unmodified lines |
| 371 | EM_poplog(); | 320 | EM_poplog(); |
| 372 | | 321 | |
| 373 | // boost watchdog by same priority | 322 | // boost watchdog by same priority |
| 374 | if( Chuck_VM::our_priority != 0x7fffffff ) | 323 | if( XThreadUtil::our_priority != 0x7fffffff ) |
| 375 | Chuck_VM::set_priority( priority, NULL ); | 324 | XThreadUtil::set_priority( priority ); |
| 376 | | 325 | |
| 377 | // while going | 326 | // while going |
| 378 | while( g_do_watchdog ) | 327 | while( g_do_watchdog ) |
| 379 | { | 328 | { |
| 380 | // get | 329 | // get |
| 381 | time = get_current_time( TRUE ); | 330 | time = get_current_time( TRUE ); |
| 382 | // fprintf( stderr, "last: %f now: %f\n", g_watchdog_time, time ); | 331 | // CK_FPRINTF_STDERR( "last: %f now: %f\n", g_watchdog_time, time ); |
| 383 | | 332 | |
| 384 | // resting | 333 | // resting |
| 385 | if( g_watchdog_state == FALSE ) | 334 | if( g_watchdog_state == FALSE ) |
| 386 | { | 335 | { |
| 387 | // check xrun | 336 | // check time vs timeout |
| 388 | // if( Digitalio::m_xrun > 100 ) | | |
| 389 | if( time - g_watchdog_time > g_watchdog_timeout ) | 337 | if( time - g_watchdog_time > g_watchdog_timeout ) |
| 390 | { | 338 | { |
| 391 | // log | 339 | // log |
| 392 | EM_log( CK_LOG_SEVERE, "real-time watchdog counter-measure activating..." ); | 340 | EM_log( CK_LOG_SEVERE, "real-time watchdog counter-measure activating..." ); |
| 393 | // lowering priority | 341 | // lowering priority |
| 394 | if( g_tid_synthesis && Chuck_VM::our_priority != 0x7fffffff ) | 342 | if( g_tid_synthesis && XThreadUtil::our_priority != 0x7fffffff ) |
| 395 | set_priority( g_tid_synthesis, g_watchdog_countermeasure_priority ); | 343 | XThreadUtil::set_priority( g_tid_synthesis, g_watchdog_countermeasure_priority ); |
| 396 | // set state | 344 | // set state |
| 397 | g_watchdog_state = TRUE; | 345 | g_watchdog_state = TRUE; |
| 398 | } | 346 | } |
| 399 | } | 347 | } |
| 400 | else | 348 | else |
| 401 | { | 349 | { |
| 402 | // check xrun | 350 | // check time vs timeout |
| 403 | // if( Digitalio::m_xrun == 0 ) | | |
| 404 | if( time - g_watchdog_time < g_watchdog_timeout ) | 351 | if( time - g_watchdog_time < g_watchdog_timeout ) |
| 405 | { | 352 | { |
| 406 | // log | 353 | // log |
| 407 | EM_log( CK_LOG_SEVERE, "real-time watchdog resting..." ); | 354 | EM_log( CK_LOG_SEVERE, "real-time watchdog resting..." ); |
| 408 | // raise priority | 355 | // raise priority |
| 409 | if( g_tid_synthesis && Chuck_VM::our_priority != 0x7fffffff ) | 356 | if( g_tid_synthesis && XThreadUtil::our_priority != 0x7fffffff ) |
| 410 | set_priority( g_tid_synthesis, Chuck_VM::our_priority ); | 357 | XThreadUtil::set_priority( g_tid_synthesis, XThreadUtil::our_priority ); |
| 411 | // set state | 358 | // set state |
| 412 | g_watchdog_state = FALSE; | 359 | g_watchdog_state = FALSE; |
| 413 | } | 360 | } |
| @@ -428,9 +375,9 @@ static unsigned int __stdcall watch_dog( void * ) · 14 unmodified lines |
| 428 | | 375 | |
| 429 | //----------------------------------------------------------------------------- | 376 | //----------------------------------------------------------------------------- |
| 430 | // name: watchdog_start() | 377 | // name: watchdog_start() |
| 431 | // desc: ... | 378 | // desc: start watchdog on separate thread |
| 432 | //----------------------------------------------------------------------------- | 379 | //----------------------------------------------------------------------------- |
| 433 | BOOL__ Digitalio::watchdog_start() | 380 | t_CKBOOL ChuckAudio::watchdog_start() |
| 434 | { | 381 | { |
| 435 | // already | 382 | // already |
| 436 | if( g_watchdog_thread ) | 383 | if( g_watchdog_thread ) |
| @@ -448,11 +395,12 @@ BOOL__ Digitalio::watchdog_start() · 11 unmodified lines |
| 448 | | 395 | |
| 449 | | 396 | |
| 450 | | 397 | |
| | 398 | |
| 451 | //----------------------------------------------------------------------------- | 399 | //----------------------------------------------------------------------------- |
| 452 | // name: watchdog_stop() | 400 | // name: watchdog_stop() |
| 453 | // desc: ... | 401 | // desc: stop watchdog thread |
| 454 | //----------------------------------------------------------------------------- | 402 | //----------------------------------------------------------------------------- |
| 455 | BOOL__ Digitalio::watchdog_stop() | 403 | t_CKBOOL ChuckAudio::watchdog_stop() |
| 456 | { | 404 | { |
| 457 | // already | 405 | // already |
| 458 | if( !g_watchdog_thread ) | 406 | if( !g_watchdog_thread ) |
| @@ -460,8 +408,9 @@ BOOL__ Digitalio::watchdog_stop() · 1 unmodified line |
| 460 | | 408 | |
| 461 | // stop things | 409 | // stop things |
| 462 | g_do_watchdog = FALSE; | 410 | g_do_watchdog = FALSE; |
| 463 | // wait a bit | 411 | // wait a bit | TODO: is this needed? |
| 464 | // usleep( 100000 ) | 412 | usleep( 100000 ) |
| | 413 | // delete watchdog thread | TODO: is this safe? |
| 465 | SAFE_DELETE( g_watchdog_thread ); | 414 | SAFE_DELETE( g_watchdog_thread ); |
| 466 | | 415 | |
| 467 | return TRUE; | 416 | return TRUE; |
| @@ -474,369 +423,273 @@ BOOL__ Digitalio::watchdog_stop() · 6 unmodified lines |
| 474 | // name: initialize() | 423 | // name: initialize() |
| 475 | // desc: ... | 424 | // desc: ... |
| 476 | //----------------------------------------------------------------------------- | 425 | //----------------------------------------------------------------------------- |
| 477 | BOOL__ Digitalio::initialize( DWORD__ num_dac_channels, | 426 | t_CKBOOL ChuckAudio::initialize( t_CKUINT num_dac_channels, |
| 478 | DWORD__ num_adc_channels, | 427 | t_CKUINT num_adc_channels, |
| 479 | DWORD__ sampling_rate, | 428 | t_CKUINT sample_rate, |
| 480 | DWORD__ bps, DWORD__ buffer_size, | 429 | t_CKUINT buffer_size, |
| 481 | DWORD__ num_buffers, DWORD__ block, | 430 | t_CKUINT num_buffers, |
| 482 | Chuck_VM * vm_ref, BOOL__ rt_audio, | 431 | f_audio_cb callback, |
| 483 | void * callback, void * data, | 432 | void * data, |
| 484 | BOOL__ force_srate ) | 433 | t_CKBOOL force_srate ) |
| 485 | { | 434 | { |
| 486 | if( m_init ) | 435 | if( m_init ) |
| 487 | return FALSE; | 436 | return FALSE; |
| 488 | | 437 | |
| 489 | m_num_channels_out = num_dac_channels; | 438 | m_num_channels_out = num_dac_channels; |
| 490 | m_num_channels_in = num_adc_channels; | 439 | m_num_channels_in = num_adc_channels; |
| 491 | m_sampling_rate = sampling_rate; | 440 | m_sample_rate = sample_rate; |
| 492 | m_bps = bps; | | |
| 493 | m_buffer_size = buffer_size; | 441 | m_buffer_size = buffer_size; |
| 494 | m_num_buffers = num_buffers; | 442 | m_num_buffers = num_buffers; |
| 495 | m_start = 0; | 443 | m_start = 0; |
| 496 | m_tick_count = 0; | | |
| 497 | m_go = 0; | 444 | m_go = 0; |
| 498 | m_end = 0; | 445 | m_silent = 0; |
| 499 | m_block = block; | 446 | |
| 500 | | 447 | // remember callback |
| 501 | DWORD__ num_channels; | 448 | m_audio_cb = callback; |
| | 449 | // remember user data to pass to callback |
| | 450 | m_cb_user_data = user_data; |
| | 451 | |
| | 452 | // # of channels |
| | 453 | t_CKUINT num_channels = 0; |
| | 454 | // variable to pass by reference into RtAudio |
| 502 | unsigned int bufsize = m_buffer_size; | 455 | unsigned int bufsize = m_buffer_size; |
| 503 | | 456 | |
| 504 | // log | 457 | // log |
| 505 | EM_log( CK_LOG_FINE, "initializing RtAudio..." ); | 458 | EM_log( CK_LOG_FINE, "initializing RtAudio..." ); |
| 506 | // push indent | 459 | // push indent |
| 507 | EM_pushlog(); | 460 | EM_pushlog(); |
| 508 | | | |
| 509 | // if rt_audio is false, then set block to FALSE to avoid deadlock | | |
| 510 | if( !rt_audio ) m_block = FALSE; | | |
| 511 | | 461 | |
| 512 | #ifndef __DISABLE_RTAUDIO__ | 462 | // allocate RtAudio |
| 513 | // if real-time audio | 463 | try { m_rtaudio = new RtAudio( ); } |
| 514 | if( rt_audio ) | 464 | catch( RtError err ) |
| | 465 | { |
| | 466 | // problem finding audio devices, most likely |
| | 467 | EM_error2( 0, "%s", err.getMessage().c_str() ); |
| | 468 | return m_init = FALSE; |
| | 469 | } |
| | 470 | |
| | 471 | // convert 1-based ordinal to 0-based ordinal (added 1.3.0.0) |
| | 472 | // note: this is to preserve previous devices numbering after RtAudio change |
| | 473 | if( m_num_channels_out > 0 ) |
| 515 | { | 474 | { |
| 516 | // allocate RtAudio | 475 | // check output device number; 0 used to mean "default" |
| 517 | try { m_rtaudio = new RtAudio( ); } | 476 | bool useDefault = ( m_dac_n == 0 ); |
| 518 | catch( RtError err ) | 477 | // default (refactor 1.3.1.2) |
| | 478 | if( useDefault ) |
| 519 | { | 479 | { |
| 520 | // problem finding audio devices, most likely | 480 | // get the default |
| 521 | EM_error2( 0, "%s", err.getMessage().c_str() ); | 481 | m_dac_n = m_rtaudio->getDefaultOutputDevice(); |
| 522 | return m_init = FALSE; | | |
| 523 | } | 482 | } |
| 524 | | 483 | else |
| 525 | // convert 1-based ordinal to 0-based ordinal (added 1.3.0.0) | | |
| 526 | // note: this is to preserve previous devices numbering after RtAudio change | | |
| 527 | if( m_num_channels_out > 0 ) | | |
| 528 | { | 484 | { |
| 529 | // check output device number; 0 used to mean "default" | 485 | m_dac_n -= 1; |
| 530 | bool useDefault = ( m_dac_n == 0 ); | 486 | } |
| 531 | | | |
| 532 | // default (refactor 1.3.1.2) | | |
| 533 | if( useDefault ) | | |
| 534 | { | | |
| 535 | // get the default | | |
| 536 | m_dac_n = m_rtaudio->getDefaultOutputDevice(); | | |
| 537 | } | | |
| 538 | else | | |
| 539 | { | | |
| 540 | m_dac_n -= 1; | | |
| 541 | } | | |
| 542 | | 487 | |
| 543 | // get device info | 488 | // get device info |
| 544 | RtAudio::DeviceInfo device_info = m_rtaudio->getDeviceInfo(m_dac_n); | 489 | RtAudio::DeviceInfo device_info = m_rtaudio->getDeviceInfo(m_dac_n); |
| 545 | | 490 | |
| 546 | // ensure correct channel count if default device is requested | 491 | // ensure correct channel count if default device is requested |
| 547 | if( useDefault ) | 492 | if( useDefault ) |
| | 493 | { |
| | 494 | // check |
| | 495 | if( device_info.outputChannels < m_num_channels_out ) |
| 548 | { | 496 | { |
| 549 | // check | 497 | // find first device with at least the requested channel count |
| 550 | if( device_info.outputChannels < m_num_channels_out ) | 498 | m_dac_n = -1; |
| | 499 | int num_devices = m_rtaudio->getDeviceCount(); |
| | 500 | for( int i = 0; i < num_devices; i++ ) |
| 551 | { | 501 | { |
| 552 | // find first device with at least the requested channel count | 502 | device_info = m_rtaudio->getDeviceInfo(i); |
| 553 | m_dac_n = -1; | 503 | if(device_info.outputChannels >= m_num_channels_out) |
| 554 | int num_devices = m_rtaudio->getDeviceCount(); | | |
| 555 | for( int i = 0; i < num_devices; i++ ) | | |
| 556 | { | 504 | { |
| 557 | device_info = m_rtaudio->getDeviceInfo(i); | 505 | m_dac_n = i; |
| 558 | if(device_info.outputChannels >= m_num_channels_out) | 506 | break; |
| 559 | { | | |
| 560 | m_dac_n = i; | | |
| 561 | break; | | |
| 562 | } | | |
| 563 | } | | |
| 564 | | | |
| 565 | // check for error | | |
| 566 | if( m_dac_n == -1 ) | | |
| 567 | { | | |
| 568 | EM_error2( 0, "no audio output device with requested channel count (%i)...", m_num_channels_out ); | | |
| 569 | return m_init = FALSE; | | |
| 570 | } | 507 | } |
| 571 | } | 508 | } |
| | 509 | |
| | 510 | // check for error |
| | 511 | if( m_dac_n == -1 ) |
| | 512 | { |
| | 513 | EM_error2( 0, "no audio output device with requested channel count (%i)...", |
| | 514 | m_num_channels_out ); |
| | 515 | return m_init = FALSE; |
| | 516 | } |
| 572 | } | 517 | } |
| | 518 | } |
| 573 | | 519 | |
| 574 | // index of closest sample rate | 520 | // index of closest sample rate |
| 575 | long closestIndex = -1; | 521 | long closestIndex = -1; |
| 576 | // difference of closest so far | 522 | // difference of closest so far |
| 577 | long closestDiff = LONG_MAX; | 523 | long closestDiff = LONG_MAX; |
| 578 | // the next highest | 524 | // the next highest |
| 579 | long nextHighest = -1; | 525 | long nextHighest = -1; |
| 580 | // diff to next highest so far | 526 | // diff to next highest so far |
| 581 | long diffToNextHighest = LONG_MAX; | 527 | long diffToNextHighest = LONG_MAX; |
| 582 | // check if request sample rate in support rates (added 1.3.1.2) | 528 | // check if request sample rate in support rates (added 1.3.1.2) |
| 583 | for( long i = 0; i < device_info.sampleRates.size(); i++ ) | 529 | for( long i = 0; i < device_info.sampleRates.size(); i++ ) |
| | 530 | { |
| | 531 | // difference |
| | 532 | long diff = device_info.sampleRates[i] - sampling_rate; |
| | 533 | // check // ge: changed from abs to labs, 2015.11 |
| | 534 | if( ::labs(diff) < closestDiff ) |
| 584 | { | 535 | { |
| 585 | // difference | 536 | // remember index |
| 586 | long diff = device_info.sampleRates[i] - sampling_rate; | 537 | closestIndex = i; |
| 587 | // check // ge: changed from abs to labs, 2015.11 | 538 | // update diff |
| 588 | if( ::labs(diff) < closestDiff ) | 539 | closestDiff = ::labs(diff); |
| 589 | { | 540 | } |
| 590 | // remember index | | |
| 591 | closestIndex = i; | | |
| 592 | // update diff | | |
| 593 | closestDiff = ::labs(diff); | | |
| 594 | } | | |
| 595 | | 541 | |
| 596 | // for next highest | 542 | // for next highest |
| 597 | if( diff > 0 && diff < diffToNextHighest ) | 543 | if( diff > 0 && diff < diffToNextHighest ) |
| 598 | { | 544 | { |
| 599 | // remember index | 545 | // remember index |
| 600 | nextHighest = i; | 546 | nextHighest = i; |
| 601 | // update diff | 547 | // update diff |
| 602 | diffToNextHighest = diff; | 548 | diffToNextHighest = diff; |
| 603 | } | | |
| 604 | } | 549 | } |
| | 550 | } |
| 605 | | 551 | |
| 606 | // see if we found exact match (added 1.3.1.2) | 552 | // see if we found exact match (added 1.3.1.2) |
| 607 | if( closestDiff != 0 ) | 553 | if( closestDiff != 0 ) |
| | 554 | { |
| | 555 | // check |
| | 556 | if( force_srate ) |
| 608 | { | 557 | { |
| 609 | // check | 558 | // request sample rate not found, error out |
| 610 | if( force_srate ) | 559 | EM_error2( 0, "unsupported sample rate (%d) requested...", sampling_rate ); |
| 611 | { | 560 | EM_error2( 0, "| (try --probe to enumerate available sample rates)" ); |
| 612 | // request sample rate not found, error out | 561 | return m_init = FALSE; |
| 613 | EM_error2( 0, "unsupported sample rate (%d) requested...", sampling_rate ); | 562 | } |
| 614 | EM_error2( 0, "| (try --probe to enumerate available sample rates)" ); | | |
| 615 | return m_init = FALSE; | | |
| 616 | } | | |
| 617 | | 563 | |
| 618 | // use next highest if available | 564 | // use next highest if available |
| 619 | if( nextHighest >= 0 ) | 565 | if( nextHighest >= 0 ) |
| 620 | { | 566 | { |
| 621 | // log | 567 | // log |
| 622 | EM_log( CK_LOG_SEVERE, "new sample rate (next highest): %d -> %d", | 568 | EM_log( CK_LOG_SEVERE, "new sample rate (next highest): %d -> %d", |
| 623 | sampling_rate, device_info.sampleRates[nextHighest] ); | 569 | sampling_rate, device_info.sampleRates[nextHighest] ); |
| 624 | // update sampling rate | 570 | // update sampling rate |
| 625 | m_sampling_rate = sampling_rate = device_info.sampleRates[nextHighest]; | 571 | m_sampling_rate = sampling_rate = device_info.sampleRates[nextHighest]; |
| 626 | } | 572 | } |
| 627 | else if( closestIndex >= 0 ) // nothing higher | 573 | else if( closestIndex >= 0 ) // nothing higher |
| 628 | { | 574 | { |
| 629 | // log | 575 | // log |
| 630 | EM_log( CK_LOG_SEVERE, "new sample rate (closest): %d -> %d", | 576 | EM_log( CK_LOG_SEVERE, "new sample rate (closest): %d -> %d", |
| 631 | sampling_rate, device_info.sampleRates[closestIndex] ); | 577 | sampling_rate, device_info.sampleRates[closestIndex] ); |
| 632 | // update sampling rate | 578 | // update sampling rate |
| 633 | m_sampling_rate = sampling_rate = device_info.sampleRates[closestIndex]; | 579 | m_sampling_rate = sampling_rate = device_info.sampleRates[closestIndex]; |
| 634 | } | 580 | } |
| 635 | else | 581 | else |
| 636 | { | 582 | { |
| 637 | // nothing to do (will fail and throw error message when opening) | 583 | // nothing to do (will fail and throw error message when opening) |
| 638 | } | | |
| 639 | } | 584 | } |
| 640 | } | 585 | } |
| | 586 | } |
| 641 | | 587 | |
| 642 | // convert 1-based ordinal to 0-based ordinal | 588 | // convert 1-based ordinal to 0-based ordinal |
| 643 | if( m_num_channels_in > 0 ) | 589 | if( m_num_channels_in > 0 ) |
| | 590 | { |
| | 591 | if( m_adc_n == 0 ) |
| 644 | { | 592 | { |
| 645 | if( m_adc_n == 0 ) | 593 | m_adc_n = m_rtaudio->getDefaultInputDevice(); |
| | 594 | |
| | 595 | // ensure correct channel count if default device is requested |
| | 596 | RtAudio::DeviceInfo device_info = m_rtaudio->getDeviceInfo(m_adc_n); |
| | 597 | |
| | 598 | // check if input channels > 0 |
| | 599 | if( device_info.inputChannels < m_num_channels_in ) |
| 646 | { | 600 | { |
| 647 | m_adc_n = m_rtaudio->getDefaultInputDevice(); | 601 | // find first device with at least the requested channel count |
| 648 | | 602 | m_adc_n = -1; |
| 649 | // ensure correct channel count if default device is requested | 603 | int num_devices = m_rtaudio->getDeviceCount(); |
| 650 | RtAudio::DeviceInfo device_info = m_rtaudio->getDeviceInfo(m_adc_n); | 604 | for(int i = 0; i < num_devices; i++) |
| 651 | | | |
| 652 | // check if input channels > 0 | | |
| 653 | if( device_info.inputChannels < m_num_channels_in ) | | |
| 654 | { | 605 | { |
| 655 | // find first device with at least the requested channel count | 606 | device_info = m_rtaudio->getDeviceInfo(i); |
| 656 | m_adc_n = -1; | 607 | if(device_info.inputChannels >= m_num_channels_in) |
| 657 | int num_devices = m_rtaudio->getDeviceCount(); | | |
| 658 | for(int i = 0; i < num_devices; i++) | | |
| 659 | { | 608 | { |
| 660 | device_info = m_rtaudio->getDeviceInfo(i); | 609 | m_adc_n = i; |
| 661 | if(device_info.inputChannels >= m_num_channels_in) | 610 | break; |
| 662 | { | | |
| 663 | m_adc_n = i; | | |
| 664 | break; | | |
| 665 | } | | |
| 666 | } | 611 | } |
| | 612 | } |
| 667 | | 613 | |
| 668 | // changed 1.3.1.2 (ge): for input, if nothing found, we just gonna try to open half-duplex | 614 | // changed 1.3.1.2 (ge): for input, if nothing found, we just gonna try to open half-duplex |
| 669 | if( m_adc_n == -1 ) | 615 | if( m_adc_n == -1 ) |
| 670 | { | 616 | { |
| 671 | // set to 0 | 617 | // set to 0 |
| 672 | m_num_channels_in = 0; | 618 | m_num_channels_in = 0; |
| 673 | // problem finding audio devices, most likely | 619 | // problem finding audio devices, most likely |
| 674 | // EM_error2( 0, "unable to find audio input device with requested channel count (%i)...", m_num_channels_in); | 620 | // EM_error2( 0, "unable to find audio input device with requested channel count (%i)...", |
| 675 | // return m_init = FALSE; | 621 | // m_num_channels_in); |
| 676 | } | 622 | // return m_init = FALSE; |
| 677 | } | 623 | } |
| 678 | } | 624 | } |
| 679 | else | | |
| 680 | { | | |
| 681 | m_adc_n -= 1; | | |
| 682 | } | | |
| 683 | } | 625 | } |
| 684 | | 626 | else |
| 685 | // open device | | |
| 686 | try { | | |
| 687 | // log | | |
| 688 | EM_log( CK_LOG_FINE, "trying %d input %d output...", | | |
| 689 | m_num_channels_in, m_num_channels_out ); | | |
| 690 | | | |
| 691 | RtAudio::StreamParameters output_parameters; | | |
| 692 | output_parameters.deviceId = m_dac_n; | | |
| 693 | output_parameters.nChannels = m_num_channels_out; | | |
| 694 | output_parameters.firstChannel = 0; | | |
| 695 | | | |
| 696 | RtAudio::StreamParameters input_parameters; | | |
| 697 | input_parameters.deviceId = m_adc_n; | | |
| 698 | input_parameters.nChannels = m_num_channels_in; | | |
| 699 | input_parameters.firstChannel = 0; | | |
| 700 | | | |
| 701 | RtAudio::StreamOptions stream_options; | | |
| 702 | stream_options.flags = 0; | | |
| 703 | stream_options.numberOfBuffers = num_buffers; | | |
| 704 | stream_options.streamName = "ChucK"; | | |
| 705 | stream_options.priority = 0; | | |
| 706 | | | |
| 707 | // open RtAudio | | |
| 708 | m_rtaudio->openStream( | | |
| 709 | m_num_channels_out > 0 ? &output_parameters : NULL, | | |
| 710 | m_num_channels_in > 0 ? &input_parameters : NULL, | | |
| 711 | CK_RTAUDIO_FORMAT, sampling_rate, &bufsize, | | |
| 712 | m_use_cb ? ( block ? &cb : &cb2 ) : NULL, vm_ref, | | |
| 713 | &stream_options ); | | |
| 714 | } catch( RtError err ) { | | |
| 715 | // log | | |
| 716 | EM_log( CK_LOG_INFO, "exception caught: '%s'...", err.getMessage().c_str() ); | | |
| 717 | EM_error2( 0, "%s", err.getMessage().c_str() ); | | |
| 718 | SAFE_DELETE( m_rtaudio ); | | |
| 719 | return m_init = FALSE; | | |
| 720 | } | | |
| 721 | | | |
| 722 | // check bufsize | | |
| 723 | if( bufsize != (int)m_buffer_size ) | | |
| 724 | { | 627 | { |
| 725 | EM_log( CK_LOG_SEVERE, "new buffer size: %d -> %i", m_buffer_size, bufsize ); | 628 | m_adc_n -= 1; |
| 726 | m_buffer_size = bufsize; | | |
| 727 | } | 629 | } |
| 728 | | | |
| 729 | // pop indent | | |
| 730 | EM_poplog(); | | |
| 731 | } | | |
| 732 | #endif // __DISABLE_RTAUDIO__ | | |
| 733 | | | |
| 734 | #if defined(__CHIP_MODE__) | | |
| 735 | if( !MoAudio::init( sampling_rate, buffer_size, 2 ) ) | | |
| 736 | { | | |
| 737 | EM_error2( 0, "%s", "(chuck)error: unable to initialize MoAudio..." ); | | |
| 738 | return m_init = FALSE; | | |
| 739 | } | 630 | } |
| 740 | #endif // __CHIP_MODE__ | | |
| 741 | | 631 | |
| 742 | if( m_use_cb ) | 632 | // open device |
| 743 | { | 633 | try { |
| 744 | num_channels = num_dac_channels > num_adc_channels ? | | |
| 745 | num_dac_channels : num_adc_channels; | | |
| 746 | // log | 634 | // log |
| 747 | EM_log( CK_LOG_SEVERE, "allocating buffers for %d x %d samples...", | 635 | EM_log( CK_LOG_FINE, "trying %d input %d output...", |
| 748 | m_buffer_size, num_channels ); | 636 | m_num_channels_in, m_num_channels_out ); |
| 749 | // allocate buffers | | |
| 750 | m_buffer_in = new SAMPLE[m_buffer_size * num_channels]; | | |
| 751 | m_buffer_out = new SAMPLE[m_buffer_size * num_channels]; | | |
| 752 | memset( m_buffer_in, 0, m_buffer_size * sizeof(SAMPLE) * num_channels ); | | |
| 753 | memset( m_buffer_out, 0, m_buffer_size * sizeof(SAMPLE) * num_channels ); | | |
| 754 | m_read_ptr = NULL; | | |
| 755 | m_write_ptr = NULL; | | |
| 756 | } | | |
| 757 | | | |
| 758 | m_in_ready = FALSE; | | |
| 759 | m_out_ready = FALSE; | | |
| 760 | | 637 | |
| 761 | return m_init = TRUE; | 638 | RtAudio::StreamParameters output_parameters; |
| 762 | } | 639 | output_parameters.deviceId = m_dac_n; |
| 763 | | 640 | output_parameters.nChannels = m_num_channels_out; |
| 764 | | 641 | output_parameters.firstChannel = 0; |
| 765 | | 642 | |
| 766 | | 643 | RtAudio::StreamParameters input_parameters; |
| 767 | //----------------------------------------------------------------------------- | 644 | input_parameters.deviceId = m_adc_n; |
| 768 | // name: cb() | 645 | input_parameters.nChannels = m_num_channels_in; |
| 769 | // desc: ... | 646 | input_parameters.firstChannel = 0; |
| 770 | //----------------------------------------------------------------------------- | 647 | |
| 771 | //int Digitalio::cb( char * buffer, int buffer_size, void * user_data ) | 648 | RtAudio::StreamOptions stream_options; |
| 772 | int Digitalio::cb( void *output_buffer, void *input_buffer, | 649 | stream_options.flags = 0; |
| 773 | unsigned int buffer_size, | 650 | stream_options.numberOfBuffers = num_buffers; |
| 774 | double streamTime, | 651 | stream_options.streamName = "ChucK"; |
| 775 | RtAudioStreamStatus status, | 652 | stream_options.priority = 0; |
| 776 | void *user_data ) | 653 | |
| 777 | { | 654 | // open RtAudio |
| 778 | DWORD__ len = buffer_size * sizeof(SAMPLE) * m_num_channels_out; | 655 | m_rtaudio->openStream( |
| 779 | DWORD__ n = 20; | 656 | m_num_channels_out > 0 ? &output_parameters : NULL, |
| 780 | DWORD__ start = 50; | 657 | m_num_channels_in > 0 ? &input_parameters : NULL, |
| 781 | | 658 | CK_RTAUDIO_FORMAT, sampling_rate, &bufsize, |
| 782 | // copy input to local buffer | 659 | m_use_cb ? ( block ? &cb : &cb2 ) : NULL, vm_ref, |
| 783 | if( m_num_channels_in ) | 660 | &stream_options ); |
| 784 | { | 661 | } catch( RtError err ) { |
| 785 | memcpy( m_buffer_in, input_buffer, len ); | 662 | // log |
| 786 | // copy to extern | 663 | EM_log( CK_LOG_INFO, "exception caught: '%s'...", err.getMessage().c_str() ); |
| 787 | if( m_extern_in ) memcpy( m_extern_in, input_buffer, len ); | 664 | EM_error2( 0, "%s", err.getMessage().c_str() ); |
| 788 | } | 665 | SAFE_DELETE( m_rtaudio ); |
| 789 | // flag ready | 666 | return m_init = FALSE; |
| 790 | m_in_ready = TRUE; | | |
| 791 | // out is ready early | | |
| 792 | if( m_go < start && m_go > 1 && m_out_ready ) m_go = start; | | |
| 793 | // copy output into local buffer | | |
| 794 | if( m_go >= start ) | | |
| 795 | { | | |
| 796 | while( !m_out_ready && n-- ) usleep( 250 ); | | |
| 797 | if( m_out_ready && g_do_watchdog ) | | |
| 798 | g_watchdog_time = get_current_time( TRUE ); | | |
| 799 | // copy local buffer to be rendered | | |
| 800 | if( m_out_ready && !m_end ) memcpy( output_buffer, m_buffer_out, len ); | | |
| 801 | // set all elements of local buffer to silence | | |
| 802 | else memset( output_buffer, 0, len ); | | |
| 803 | } | 667 | } |
| 804 | else // initial condition | 668 | |
| | 669 | // check bufsize |
| | 670 | if( bufsize != (int)m_buffer_size ) |
| 805 | { | 671 | { |
| 806 | // priority boost | 672 | EM_log( CK_LOG_SEVERE, "new buffer size: %d -> %i", m_buffer_size, bufsize ); |
| 807 | if( !m_go && Chuck_VM::our_priority != 0x7fffffff ) | 673 | m_buffer_size = bufsize; |
| 808 | Chuck_VM::set_priority( Chuck_VM::our_priority, NULL ); | | |
| 809 | // catch SIGINT | | |
| 810 | // signal( SIGINT, signal_int ); | | |
| 811 | | | |
| 812 | // timestamp | | |
| 813 | if( g_do_watchdog ) g_watchdog_time = get_current_time( TRUE ); | | |
| 814 | | | |
| 815 | memset( output_buffer, 0, len ); | | |
| 816 | m_go++; | | |
| 817 | return 0; | | |
| 818 | } | 674 | } |
| 819 | | 675 | |
| 820 | // 2nd buffer | 676 | // pop indent |
| 821 | // RtAudio4 TODO | 677 | EM_poplog(); |
| 822 | // if( m_go == start ) | | |
| 823 | // { | | |
| 824 | // n = 8; while( !m_out_ready && n-- ) usleep( 250 ); | | |
| 825 | // len /= sizeof(SAMPLE); DWORD__ i = 0; | | |
| 826 | // SAMPLE * s = (SAMPLE *) buffer; | | |
| 827 | // while( i < len ) *s++ *= (SAMPLE)i++/len; | | |
| 828 | // m_go++; | | |
| 829 | // } | | |
| 830 | | 678 | |
| 831 | // copy to extern | 679 | // greater of dac/adc channels |
| 832 | if( m_extern_out ) memcpy( m_extern_out, output_buffer, len ); | 680 | num_channels = num_dac_channels > num_adc_channels ? |
| | 681 | num_dac_channels : num_adc_channels; |
| | 682 | // log |
| | 683 | EM_log( CK_LOG_SEVERE, "allocating buffers for %d x %d samples...", |
| | 684 | m_buffer_size, num_channels ); |
| 833 | | 685 | |
| 834 | // set pointer to the beginning - if not ready, then too late anyway | 686 | // allocate buffers |
| 835 | //*m_write_ptr = (SAMPLE *)m_buffer_out; | 687 | m_buffer_in = new SAMPLE[m_buffer_size * num_channels]; |
| 836 | //*m_read_ptr = (SAMPLE *)m_buffer_in; | 688 | m_buffer_out = new SAMPLE[m_buffer_size * num_channels]; |
| 837 | m_out_ready = FALSE; | 689 | memset( m_buffer_in, 0, m_buffer_size * sizeof(SAMPLE) * num_channels ); |
| | 690 | memset( m_buffer_out, 0, m_buffer_size * sizeof(SAMPLE) * num_channels ); |
| 838 | | 691 | |
| 839 | return 0; | 692 | return m_init = TRUE; |
| 840 | } | 693 | } |
| 841 | | 694 | |
| 842 | | 695 | |
| @@ -846,15 +699,11 @@ int Digitalio::cb( void *output_buffer, void *input_buffer, · 3 unmodified lines |
| 846 | // name: cb2() | 699 | // name: cb2() |
| 847 | // desc: ... | 700 | // desc: ... |
| 848 | //----------------------------------------------------------------------------- | 701 | //----------------------------------------------------------------------------- |
| 849 | //int Digitalio::cb2( char * buffer, int buffer_size, void * user_data ) | 702 | int ChuckAudio::cb( void * output_buffer, void * input_buffer, unsigned int buffer_size, |
| 850 | int Digitalio::cb2( void *output_buffer, void *input_buffer, | 703 | double streamTime, RtAudioStreamStatus status, void * user_data ) |
| 851 | unsigned int buffer_size, | | |
| 852 | double streamTime, | | |
| 853 | RtAudioStreamStatus status, | | |
| 854 | void *user_data ) | | |
| 855 | { | 704 | { |
| 856 | DWORD__ len = buffer_size * sizeof(SAMPLE) * m_num_channels_out; | 705 | // length, in bytes of output |
| 857 | Chuck_VM * vm_ref = (Chuck_VM *)user_data; | 706 | t_CKUINT len = buffer_size * sizeof(SAMPLE) * m_num_channels_out; |
| 858 | | 707 | |
| 859 | // priority boost | 708 | // priority boost |
| 860 | if( !m_go && Chuck_VM::our_priority != 0x7fffffff ) | 709 | if( !m_go && Chuck_VM::our_priority != 0x7fffffff ) |
| @@ -876,7 +725,7 @@ int Digitalio::cb2( void *output_buffer, void *input_buffer, · 15 unmodified lines |
| 876 | | 725 | |
| 877 | // TODO: close the duplicate handle? | 726 | // TODO: close the duplicate handle? |
| 878 | #endif | 727 | #endif |
| 879 | Chuck_VM::set_priority( Chuck_VM::our_priority, NULL ); | 728 | XThreadUtil::set_priority( XThreadUtil::our_priority ); |
| 880 | memset( output_buffer, 0, len ); | 729 | memset( output_buffer, 0, len ); |
| 881 | m_go = TRUE; | 730 | m_go = TRUE; |
| 882 | | 731 | |
| @@ -901,21 +750,11 @@ int Digitalio::cb2( void *output_buffer, void *input_buffer, · 18 unmodified lines |
| 901 | if( m_extern_in ) memcpy( m_extern_in, input_buffer, len ); | 750 | if( m_extern_in ) memcpy( m_extern_in, input_buffer, len ); |
| 902 | } | 751 | } |
| 903 | | 752 | |
| 904 | // check xrun | 753 | // timestamp |
| 905 | if( m_xrun < 6 ) | 754 | if( g_do_watchdog ) g_watchdog_time = get_current_time( TRUE ); |
| 906 | { | 755 | // call the audio cb |
| 907 | // timestamp | 756 | m_audio_cb( m_buffer_in, m_buffer_out, buffer_size, |
| 908 | if( g_do_watchdog ) g_watchdog_time = get_current_time( TRUE ); | 757 | m_num_channels_in, m_num_channels_out, m_cb_user_data ); |
| 909 | // get samples from output | | |
| 910 | vm_ref->run( buffer_size, m_buffer_in, m_buffer_out ); | | |
| 911 | // ... | | |
| 912 | if( m_xrun ) m_xrun--; | | |
| 913 | } | | |
| 914 | else | | |
| 915 | { | | |
| 916 | // reset | | |
| 917 | m_xrun /= 2; | | |
| 918 | } | | |
| 919 | | 758 | |
| 920 | // copy local buffer to be rendered | 759 | // copy local buffer to be rendered |
| 921 | if( !m_end ) memcpy( output_buffer, m_buffer_out, len ); | 760 | if( !m_end ) memcpy( output_buffer, m_buffer_out, len ); |
| @@ -925,44 +764,27 @@ int Digitalio::cb2( void *output_buffer, void *input_buffer, · 3 unmodified lines |
| 925 | // copy to extern | 764 | // copy to extern |
| 926 | if( m_extern_out ) memcpy( m_extern_out, output_buffer, len ); | 765 | if( m_extern_out ) memcpy( m_extern_out, output_buffer, len ); |
| 927 | | 766 | |
| 928 | | | |
| 929 | return 0; | 767 | return 0; |
| 930 | } | 768 | } |
| 931 | | 769 | |
| 932 | | 770 | |
| 933 | | 771 | |
| 934 | | 772 | |
| 935 | #ifdef __CHIP_MODE__ | | |
| 936 | //----------------------------------------------------------------------------- | | |
| 937 | // name: MoAudio_cb() | | |
| 938 | // desc: ... | | |
| 939 | //----------------------------------------------------------------------------- | | |
| 940 | void MoAudio_cb( Float32 * buffer, UInt32 numFrames, void * userData ) | | |
| 941 | { | | |
| 942 | Digitalio::cb2( (char *)buffer, (char *)buffer, numFrames, 0, 0, userData ); | | |
| 943 | } | | |
| 944 | #endif // __CHIP_MODE__ | | |
| 945 | | | |
| 946 | | | |
| 947 | | | |
| 948 | | | |
| 949 | //----------------------------------------------------------------------------- | 773 | //----------------------------------------------------------------------------- |
| 950 | // name: start() | 774 | // name: start() |
| 951 | // desc: ... | 775 | // desc: start host audio |
| 952 | //----------------------------------------------------------------------------- | 776 | //----------------------------------------------------------------------------- |
| 953 | BOOL__ Digitalio::start( ) | 777 | t_CKBOOL ChuckAudio::start( ) |
| 954 | { | 778 | { |
| 955 | #ifndef __DISABLE_RTAUDIO__ | 779 | try { |
| 956 | try{ if( !m_start ) | 780 | if( !m_start ) |
| 957 | m_rtaudio->startStream(); | 781 | m_rtaudio->startStream(); |
| 958 | m_start = TRUE; | 782 | m_start = TRUE; |
| 959 | } catch( RtError err ){ return FALSE; } | 783 | } |
| 960 | #endif // __DISABLE_RTAUDIO__ | 784 | catch( RtError err ) |
| 961 | | 785 | { |
| 962 | #if defined(__CHIP_MODE__) | 786 | return FALSE; |
| 963 | if( !m_start ) | 787 | } |
| 964 | m_start = MoAudio::start( MoAudio_cb, g_vm ); | | |
| 965 | #endif // __CHIP_MODE__ | | |
| 966 | | 788 | |
| 967 | return m_start; | 789 | return m_start; |
| 968 | } | 790 | } |
| @@ -972,22 +794,22 @@ BOOL__ Digitalio::start( ) · 3 unmodified lines |
| 972 | | 794 | |
| 973 | //----------------------------------------------------------------------------- | 795 | //----------------------------------------------------------------------------- |
| 974 | // name: stop() | 796 | // name: stop() |
| 975 | // desc: ... | 797 | // desc: stop host audio |
| 976 | //----------------------------------------------------------------------------- | 798 | //----------------------------------------------------------------------------- |
| 977 | BOOL__ Digitalio::stop( ) | 799 | t_CKBOOL ChuckAudio::stop( ) |
| 978 | { | 800 | { |
| 979 | #ifndef __DISABLE_RTAUDIO__ | 801 | try { |
| 980 | try{ if( m_start ) | 802 | if( m_start ) |
| 981 | m_rtaudio->stopStream(); | 803 | m_rtaudio->stopStream(); |
| 982 | m_start = FALSE; | 804 | m_start = FALSE; |
| 983 | } catch( RtError err ){ return FALSE; } | 805 | } |
| 984 | #endif // __DISABLE_RTAUDIO__ | 806 | catch( RtError err ) |
| 985 | | 807 | { |
| 986 | #if defined(__CHIP_MODE__) | 808 | return FALSE; |
| 987 | if( m_start ) | 809 | } |
| 988 | MoAudio::stop(); | 810 | |
| 989 | m_start = FALSE; | 811 | // REFACTOR-2017: set end flag |
| 990 | #endif | 812 | m_silent = TRUE; |
| 991 | | 813 | |
| 992 | return !m_start; | 814 | return !m_start; |
| 993 | } | 815 | } |
| @@ -995,701 +817,33 @@ BOOL__ Digitalio::stop( ) · 1 unmodified line |
| 995 | | 817 | |
| 996 | | 818 | |
| 997 | | 819 | |
| 998 | //----------------------------------------------------------------------------- | | |
| 999 | // name: tick() | | |
| 1000 | // desc: ... | | |
| 1001 | //----------------------------------------------------------------------------- | | |
| 1002 | //BOOL__ Digitalio::tick( ) | | |
| 1003 | //{ | | |
| 1004 | //#ifndef __DISABLE_RTAUDIO__ | | |
| 1005 | // try | | |
| 1006 | // { | | |
| 1007 | // if( ++m_tick_count >= m_start ) | | |
| 1008 | // { | | |
| 1009 | // m_rtaudio->tickStream(); | | |
| 1010 | // m_tick_count = 0; | | |
| 1011 | // m_out_ready = TRUE; | | |
| 1012 | // m_in_ready = TRUE; | | |
| 1013 | // } | | |
| 1014 | // | | |
| 1015 | // return TRUE; | | |
| 1016 | // } catch( RtError err ){ return FALSE; } | | |
| 1017 | //#endif // __DISABLE_RTAUDIO__ | | |
| 1018 | // | | |
| 1019 | // return FALSE; | | |
| 1020 | //} | | |
| 1021 | | | |
| 1022 | | | |
| 1023 | | | |
| 1024 | | | |
| 1025 | //----------------------------------------------------------------------------- | 820 | //----------------------------------------------------------------------------- |
| 1026 | // name: shutdown() | 821 | // name: shutdown() |
| 1027 | // desc: ... | 822 | // desc: ... |
| 1028 | //----------------------------------------------------------------------------- | 823 | //----------------------------------------------------------------------------- |
| 1029 | void Digitalio::shutdown() | 824 | void ChuckAudio::shutdown() |
| 1030 | { | 825 | { |
| | 826 | // check flag |
| 1031 | if( !m_init ) return; | 827 | if( !m_init ) return; |
| 1032 | | 828 | |
| 1033 | #ifndef __DISABLE_RTAUDIO__ | 829 | // stop watchdog | REFACTOR-2017: moved this earlier to give watchdog |
| | 830 | // a head start in stopping, to avoid unneeded countermeasure deployment |
| | 831 | watchdog_stop(); |
| | 832 | |
| | 833 | // check if started |
| 1034 | if( m_start ) | 834 | if( m_start ) |
| 1035 | { | 835 | { |
| 1036 | //if( m_use_cb ) m_rtaudio->cancelStreamCallback(); | 836 | // m_rtaudio->cancelStreamCallback(); |
| 1037 | m_rtaudio->stopStream(); | 837 | // m_rtaudio->stopStream(); |
| | 838 | stop(); |
| 1038 | } | 839 | } |
| 1039 | | 840 | |
| | 841 | // close stream |
| 1040 | m_rtaudio->closeStream(); | 842 | m_rtaudio->closeStream(); |
| | 843 | // cleanup |
| 1041 | SAFE_DELETE( m_rtaudio ); | 844 | SAFE_DELETE( m_rtaudio ); |
| 1042 | #endif // __DISABLE_RTAUDIO__ | | |
| 1043 | | 845 | |
| | 846 | // unflag |
| 1044 | m_init = FALSE; | 847 | m_init = FALSE; |
| 1045 | m_start = FALSE; | 848 | m_start = FALSE; |
| 1046 | | | |
| 1047 | // stop watchdog | | |
| 1048 | watchdog_stop(); | | |
| 1049 | } | | |
| 1050 | | | |
| 1051 | | | |
| 1052 | | | |
| 1053 | | | |
| 1054 | //----------------------------------------------------------------------------- | | |
| 1055 | // name: DigitalOut() | | |
| 1056 | // desc: ... | | |
| 1057 | //----------------------------------------------------------------------------- | | |
| 1058 | DigitalOut::DigitalOut() | | |
| 1059 | { | | |
| 1060 | m_data_ptr_out = NULL; | | |
| 1061 | m_data_max_out = NULL; | | |
| 1062 | } | | |
| 1063 | | | |
| 1064 | | | |
| 1065 | | | |
| 1066 | | | |
| 1067 | //----------------------------------------------------------------------------- | | |
| 1068 | // name: ~DigitalOut() | | |
| 1069 | // desc: ... | | |
| 1070 | //----------------------------------------------------------------------------- | | |
| 1071 | DigitalOut::~DigitalOut() | | |
| 1072 | { | | |
| 1073 | this->cleanup(); | | |
| 1074 | } | | |
| 1075 | | | |
| 1076 | | | |
| 1077 | | | |
| 1078 | | | |
| 1079 | //----------------------------------------------------------------------------- | | |
| 1080 | // name: initialize() | | |
| 1081 | // desc: initialize audio out | | |
| 1082 | //----------------------------------------------------------------------------- | | |
| 1083 | BOOL__ DigitalOut::initialize( ) | | |
| 1084 | { | | |
| 1085 | #ifndef __DISABLE_RTAUDIO__ | | |
| 1086 | // set pointer to beginning of local buffer | | |
| 1087 | // RtAudio4 TODO | | |
| 1088 | m_data_ptr_out = Digitalio::m_use_cb ? Digitalio::m_buffer_out : NULL; | | |
| 1089 | // : (SAMPLE *)Digitalio::audio()->getStreamBuffer(); | | |
| 1090 | #else | | |
| 1091 | // set pointer | | |
| 1092 | assert( Digitalio::m_use_cb ); | | |
| 1093 | m_data_ptr_out = Digitalio::m_buffer_out; | | |
| 1094 | #endif // __DISABLE_RTAUDIO__ | | |
| 1095 | // calculate the end of the buffer | | |
| 1096 | m_data_max_out = m_data_ptr_out + | | |
| 1097 | Digitalio::buffer_size() * Digitalio::num_channels_out(); | | |
| 1098 | // set the writer pointer to our write pointer | | |
| 1099 | Digitalio::m_write_ptr = &m_data_ptr_out; | | |
| 1100 | | | |
| 1101 | return TRUE; | | |
| 1102 | } | | |
| 1103 | | | |
| 1104 | | | |
| 1105 | | | |
| 1106 | | | |
| 1107 | //----------------------------------------------------------------------------- | | |
| 1108 | // name: start() | | |
| 1109 | // desc: render | | |
| 1110 | //----------------------------------------------------------------------------- | | |
| 1111 | BOOL__ DigitalOut::start() | | |
| 1112 | { | | |
| 1113 | // start stream | | |
| 1114 | return ( Digitalio::start() != 0 ); | | |
| 1115 | } | | |
| 1116 | | | |
| 1117 | | | |
| 1118 | | | |
| 1119 | | | |
| 1120 | //----------------------------------------------------------------------------- | | |
| 1121 | // name: stop() | | |
| 1122 | // desc: render | | |
| 1123 | //----------------------------------------------------------------------------- | | |
| 1124 | BOOL__ DigitalOut::stop() | | |
| 1125 | { | | |
| 1126 | // well | | |
| 1127 | Digitalio::stop(); | | |
| 1128 | | | |
| 1129 | return TRUE; | | |
| 1130 | } | | |
| 1131 | | | |
| 1132 | | | |
| 1133 | | | |
| 1134 | | | |
| 1135 | //----------------------------------------------------------------------------- | | |
| 1136 | // name: cleanup() | | |
| 1137 | // desc: ... | | |
| 1138 | //----------------------------------------------------------------------------- | | |
| 1139 | void DigitalOut::cleanup() | | |
| 1140 | { | | |
| 1141 | } | | |
| 1142 | | | |
| 1143 | | | |
| 1144 | | | |
| 1145 | | | |
| 1146 | //----------------------------------------------------------------------------- | | |
| 1147 | // name: tick_out() | | |
| 1148 | // desc: 1 channel | | |
| 1149 | //----------------------------------------------------------------------------- | | |
| 1150 | BOOL__ DigitalOut::tick_out( SAMPLE sample ) | | |
| 1151 | { | | |
| 1152 | if( !prepare_tick_out() ) | | |
| 1153 | return FALSE; | | |
| 1154 | | | |
| 1155 | *m_data_ptr_out++ = sample; | | |
| 1156 | | | |
| 1157 | return TRUE; | | |
| 1158 | } | | |
| 1159 | | | |
| 1160 | | | |
| 1161 | | | |
| 1162 | | | |
| 1163 | //----------------------------------------------------------------------------- | | |
| 1164 | // name: tick_out() | | |
| 1165 | // desc: 2 channel | | |
| 1166 | //----------------------------------------------------------------------------- | | |
| 1167 | BOOL__ DigitalOut::tick_out( SAMPLE sample_l, SAMPLE sample_r ) | | |
| 1168 | { | | |
| 1169 | if( !prepare_tick_out() ) | | |
| 1170 | return FALSE; | | |
| 1171 | | | |
| 1172 | *m_data_ptr_out++ = sample_l; | | |
| 1173 | *m_data_ptr_out++ = sample_r; | | |
| 1174 | | | |
| 1175 | return TRUE; | | |
| 1176 | } | | |
| 1177 | | | |
| 1178 | | | |
| 1179 | | | |
| 1180 | | | |
| 1181 | //----------------------------------------------------------------------------- | | |
| 1182 | // name: tick_out() | | |
| 1183 | // desc: all channels | | |
| 1184 | //----------------------------------------------------------------------------- | | |
| 1185 | BOOL__ DigitalOut::tick_out( const SAMPLE * samples, DWORD__ n ) | | |
| 1186 | { | | |
| 1187 | if( !prepare_tick_out() ) | | |
| 1188 | return FALSE; | | |
| 1189 | | | |
| 1190 | if( !n ) n = Digitalio::m_num_channels_out; | | |
| 1191 | while( n-- ) | | |
| 1192 | *m_data_ptr_out++ = *samples++; | | |
| 1193 | | | |
| 1194 | return TRUE; | | |
| 1195 | } | | |
| 1196 | | | |
| 1197 | | | |
| 1198 | | | |
| 1199 | | | |
| 1200 | //----------------------------------------------------------------------------- | | |
| 1201 | // name: prepare_tick_out() | | |
| 1202 | // desc: data ptr ok | | |
| 1203 | //----------------------------------------------------------------------------- | | |
| 1204 | inline BOOL__ DigitalOut::prepare_tick_out() | | |
| 1205 | { | | |
| 1206 | if( m_data_ptr_out >= m_data_max_out ) | | |
| 1207 | { | | |
| 1208 | this->render(); | | |
| 1209 | } | | |
| 1210 | | | |
| 1211 | return TRUE; | | |
| 1212 | } | | |
| 1213 | | | |
| 1214 | | | |
| 1215 | | | |
| 1216 | | | |
| 1217 | //----------------------------------------------------------------------------- | | |
| 1218 | // name: render() | | |
| 1219 | // desc: the render | | |
| 1220 | //----------------------------------------------------------------------------- | | |
| 1221 | DWORD__ DigitalOut::render() | | |
| 1222 | { | | |
| 1223 | //if( !Digitalio::m_use_cb && !Digitalio::tick() ) return FALSE; | | |
| 1224 | | | |
| 1225 | if( Digitalio::m_block ) | | |
| 1226 | { | | |
| 1227 | // synchronize | | |
| 1228 | Digitalio::m_out_ready = TRUE; | | |
| 1229 | // synchronize | | |
| 1230 | while( Digitalio::m_out_ready ) | | |
| 1231 | usleep( 250 ); | | |
| 1232 | } | | |
| 1233 | | | |
| 1234 | // set pointer to the beginning - if not ready, then too late anyway | | |
| 1235 | *Digitalio::m_write_ptr = (SAMPLE *)Digitalio::m_buffer_out; | | |
| 1236 | | | |
| 1237 | return TRUE; | | |
| 1238 | } | | |
| 1239 | | | |
| 1240 | | | |
| 1241 | | | |
| 1242 | | | |
| 1243 | //----------------------------------------------------------------------------- | | |
| 1244 | // name: DigitalIn() | | |
| 1245 | // desc: ... | | |
| 1246 | //----------------------------------------------------------------------------- | | |
| 1247 | DigitalIn::DigitalIn() | | |
| 1248 | { | | |
| 1249 | m_data_ptr_in = NULL; | | |
| 1250 | m_data_max_in = NULL; | | |
| 1251 | } | | |
| 1252 | | | |
| 1253 | | | |
| 1254 | | | |
| 1255 | | | |
| 1256 | //----------------------------------------------------------------------------- | | |
| 1257 | // name: ~DigitalIn() | | |
| 1258 | // desc: ... | | |
| 1259 | //----------------------------------------------------------------------------- | | |
| 1260 | DigitalIn::~DigitalIn() | | |
| 1261 | { | | |
| 1262 | this->cleanup(); | | |
| 1263 | } | | |
| 1264 | | | |
| 1265 | | | |
| 1266 | | | |
| 1267 | | | |
| 1268 | //----------------------------------------------------------------------------- | | |
| 1269 | // name: initialize() | | |
| 1270 | // desc: initialize audio in | | |
| 1271 | //----------------------------------------------------------------------------- | | |
| 1272 | BOOL__ DigitalIn::initialize( ) | | |
| 1273 | { | | |
| 1274 | m_data = new SAMPLE[Digitalio::buffer_size() * Digitalio::num_channels_in()]; | | |
| 1275 | memset( m_data, 0, Digitalio::buffer_size() * Digitalio::num_channels_in() * sizeof(SAMPLE) ); | | |
| 1276 | #ifndef __DISABLE_RTAUDIO__ | | |
| 1277 | // set the buffer to the beginning of the local buffer | | |
| 1278 | // RtAudio4 TODO | | |
| 1279 | m_data_ptr_in = Digitalio::m_use_cb ? m_data : NULL; | | |
| 1280 | // : (SAMPLE *)Digitalio::audio()->getStreamBuffer(); | | |
| 1281 | #else | | |
| 1282 | // set the buffer pointer | | |
| 1283 | assert( Digitalio::m_use_cb ); | | |
| 1284 | m_data_ptr_in = m_data; | | |
| 1285 | #endif // __DISABLE_RTAUDIO__ | | |
| 1286 | // calculate past buffer | | |
| 1287 | m_data_max_in = m_data_ptr_in + | | |
| 1288 | Digitalio::buffer_size() * Digitalio::num_channels_in(); | | |
| 1289 | // set the read pointer to the local pointer | | |
| 1290 | Digitalio::m_read_ptr = &m_data_ptr_in; | | |
| 1291 | // invalidate | | |
| 1292 | m_data_ptr_in = m_data_max_in; | | |
| 1293 | | | |
| 1294 | return TRUE; | | |
| 1295 | } | | |
| 1296 | | | |
| 1297 | | | |
| 1298 | | | |
| 1299 | | | |
| 1300 | //----------------------------------------------------------------------------- | | |
| 1301 | // name: start() | | |
| 1302 | // desc: ... | | |
| 1303 | //----------------------------------------------------------------------------- | | |
| 1304 | BOOL__ DigitalIn::start() | | |
| 1305 | { | | |
| 1306 | return ( Digitalio::start() != 0 ); | | |
| 1307 | } | | |
| 1308 | | | |
| 1309 | | | |
| 1310 | | | |
| 1311 | | | |
| 1312 | //----------------------------------------------------------------------------- | | |
| 1313 | // name: capture_stop() | | |
| 1314 | // desc: ... | | |
| 1315 | //----------------------------------------------------------------------------- | | |
| 1316 | BOOL__ DigitalIn::stop() | | |
| 1317 | { | | |
| 1318 | Digitalio::stop(); | | |
| 1319 | return TRUE; | | |
| 1320 | } | | |
| 1321 | | | |
| 1322 | | | |
| 1323 | | | |
| 1324 | | | |
| 1325 | //----------------------------------------------------------------------------- | | |
| 1326 | // name: cleanup() | | |
| 1327 | // desc: ... | | |
| 1328 | //----------------------------------------------------------------------------- | | |
| 1329 | void DigitalIn::cleanup() | | |
| 1330 | { | | |
| 1331 | } | | |
| 1332 | | | |
| 1333 | | | |
| 1334 | | | |
| 1335 | | | |
| 1336 | //----------------------------------------------------------------------------- | | |
| 1337 | // name: tick_in() | | |
| 1338 | // desc: ... | | |
| 1339 | //----------------------------------------------------------------------------- | | |
| 1340 | BOOL__ DigitalIn::tick_in( SAMPLE * s ) | | |
| 1341 | { | | |
| 1342 | if( !prepare_tick_in() ) | | |
| 1343 | return 0; | | |
| 1344 | *s = *m_data_ptr_in++; | | |
| 1345 | | | |
| 1346 | return TRUE; | | |
| 1347 | } | | |
| 1348 | | | |
| 1349 | | | |
| 1350 | | | |
| 1351 | | | |
| 1352 | //----------------------------------------------------------------------------- | | |
| 1353 | // name: tick_in() | | |
| 1354 | // desc: ... | | |
| 1355 | //----------------------------------------------------------------------------- | | |
| 1356 | BOOL__ DigitalIn::tick_in( SAMPLE * l, SAMPLE * r ) | | |
| 1357 | { | | |
| 1358 | if( !prepare_tick_in() ) | | |
| 1359 | return FALSE; | | |
| 1360 | | | |
| 1361 | *l = *m_data_ptr_in++; | | |
| 1362 | *r = *m_data_ptr_in++; | | |
| 1363 | | | |
| 1364 | return TRUE; | | |
| 1365 | } | | |
| 1366 | | | |
| 1367 | | | |
| 1368 | | | |
| 1369 | | | |
| 1370 | //----------------------------------------------------------------------------- | | |
| 1371 | // name: tick_in() | | |
| 1372 | // desc: ... | | |
| 1373 | //----------------------------------------------------------------------------- | | |
| 1374 | BOOL__ DigitalIn::tick_in( SAMPLE * sample, DWORD__ n ) | | |
| 1375 | { | | |
| 1376 | if( !prepare_tick_in() ) | | |
| 1377 | return FALSE; | | |
| 1378 | | | |
| 1379 | if( !n ) n = Digitalio::m_num_channels_in; | | |
| 1380 | while( n-- ) | | |
| 1381 | *sample++ = *m_data_ptr_in++; | | |
| 1382 | | | |
| 1383 | return TRUE; | | |
| 1384 | } | | |
| 1385 | | | |
| 1386 | | | |
| 1387 | | | |
| 1388 | | | |
| 1389 | //----------------------------------------------------------------------------- | | |
| 1390 | // name: prepare_tick_in() | | |
| 1391 | // desc: data ptr ok | | |
| 1392 | //----------------------------------------------------------------------------- | | |
| 1393 | inline BOOL__ DigitalIn::prepare_tick_in() | | |
| 1394 | { | | |
| 1395 | if( m_data_ptr_in >= m_data_max_in ) | | |
| 1396 | { | | |
| 1397 | this->capture(); | | |
| 1398 | } | | |
| 1399 | | | |
| 1400 | return TRUE; | | |
| 1401 | } | | |
| 1402 | | | |
| 1403 | | | |
| 1404 | | | |
| 1405 | | | |
| 1406 | //----------------------------------------------------------------------------- | | |
| 1407 | // name: cb_capture() | | |
| 1408 | // desc: ... | | |
| 1409 | //----------------------------------------------------------------------------- | | |
| 1410 | DWORD__ DigitalIn::capture( ) | | |
| 1411 | { | | |
| 1412 | // if( !Digitalio::m_use_cb && !Digitalio::tick() ) return FALSE; | | |
| 1413 | | | |
| 1414 | if( Digitalio::m_block ) | | |
| 1415 | { | | |
| 1416 | t_CKUINT n = 20; | | |
| 1417 | while( !Digitalio::m_in_ready && n-- ) | | |
| 1418 | usleep( 250 ); | | |
| 1419 | } | | |
| 1420 | | | |
| 1421 | // copy data | | |
| 1422 | memcpy( m_data, Digitalio::m_buffer_in, Digitalio::buffer_size() * | | |
| 1423 | Digitalio::num_channels_in() * sizeof(SAMPLE) ); | | |
| 1424 | Digitalio::m_in_ready = FALSE; | | |
| 1425 | // set pointer to the beginning - if not ready, then too late anyway | | |
| 1426 | *Digitalio::m_read_ptr = (SAMPLE *)m_data; | | |
| 1427 | | | |
| 1428 | return TRUE; | | |
| 1429 | } | | |
| 1430 | | | |
| 1431 | | | |
| 1432 | | | |
| 1433 | | | |
| 1434 | //----------------------------------------------------------------------------- | | |
| 1435 | // name: AudioBufferX() | | |
| 1436 | // desc: ... | | |
| 1437 | //----------------------------------------------------------------------------- | | |
| 1438 | AudioBufferX::AudioBufferX( DWORD__ size ) | | |
| 1439 | { | | |
| 1440 | if( size ) | | |
| 1441 | this->initialize( size ); | | |
| 1442 | else | | |
| 1443 | { | | |
| 1444 | m_size = size; | | |
| 1445 | m_data = m_ptr_curr = m_ptr_end = NULL; | | |
| 1446 | } | | |
| 1447 | } | | |
| 1448 | | | |
| 1449 | | | |
| 1450 | | | |
| 1451 | | | |
| 1452 | //----------------------------------------------------------------------------- | | |
| 1453 | // name: initialize() | | |
| 1454 | // desc: ... | | |
| 1455 | //----------------------------------------------------------------------------- | | |
| 1456 | BOOL__ AudioBufferX::initialize( DWORD__ size ) | | |
| 1457 | { | | |
| 1458 | m_size = size; | | |
| 1459 | m_data = (SAMPLE *)malloc( size * sizeof(SAMPLE) ); | | |
| 1460 | | | |
| 1461 | if( !m_data ) | | |
| 1462 | return FALSE; | | |
| 1463 | | | |
| 1464 | // clear the memory | | |
| 1465 | memset( m_data, 0, size * sizeof(SAMPLE) ); | | |
| 1466 | | | |
| 1467 | // set the pointers | | |
| 1468 | m_ptr_curr = m_data; | | |
| 1469 | m_ptr_end = m_data + size; | | |
| 1470 | | | |
| 1471 | return TRUE; | | |
| 1472 | } | | |
| 1473 | | | |
| 1474 | | | |
| 1475 | | | |
| 1476 | | | |
| 1477 | //----------------------------------------------------------------------------- | | |
| 1478 | // name: cleanup() | | |
| 1479 | // desc: ... | | |
| 1480 | //----------------------------------------------------------------------------- | | |
| 1481 | void AudioBufferX::cleanup() | | |
| 1482 | { | | |
| 1483 | if( m_data ) | | |
| 1484 | { | | |
| 1485 | free( m_data ); | | |
| 1486 | m_data = NULL; | | |
| 1487 | } | | |
| 1488 | | | |
| 1489 | m_size = 0; | | |
| 1490 | m_ptr_curr = m_ptr_end = NULL; | | |
| 1491 | } | | |
| 1492 | | | |
| 1493 | | | |
| 1494 | | | |
| 1495 | | | |
| 1496 | //----------------------------------------------------------------------------- | | |
| 1497 | // name: size() | | |
| 1498 | // desc: ... | | |
| 1499 | //----------------------------------------------------------------------------- | | |
| 1500 | DWORD__ AudioBufferX::size() | | |
| 1501 | { | | |
| 1502 | return m_size; | | |
| 1503 | } | | |
| 1504 | | | |
| 1505 | | | |
| 1506 | | | |
| 1507 | | | |
| 1508 | //----------------------------------------------------------------------------- | | |
| 1509 | // name: data() | | |
| 1510 | // desc: ... | | |
| 1511 | //----------------------------------------------------------------------------- | | |
| 1512 | SAMPLE * AudioBufferX::data() | | |
| 1513 | { | | |
| 1514 | return m_data; | | |
| 1515 | } | | |
| 1516 | | | |
| 1517 | | | |
| 1518 | | | |
| 1519 | | | |
| 1520 | //----------------------------------------------------------------------------- | | |
| 1521 | // name: AudioBufferIn() | | |
| 1522 | // desc: ... | | |
| 1523 | //----------------------------------------------------------------------------- | | |
| 1524 | AudioBufferIn::AudioBufferIn( DWORD__ size ) | | |
| 1525 | : AudioBufferX( size ) | | |
| 1526 | { } | | |
| 1527 | | | |
| 1528 | | | |
| 1529 | | | |
| 1530 | | | |
| 1531 | //----------------------------------------------------------------------------- | | |
| 1532 | // name: ~AudioBufferIn() | | |
| 1533 | // desc: ... | | |
| 1534 | //----------------------------------------------------------------------------- | | |
| 1535 | AudioBufferIn::~AudioBufferIn() | | |
| 1536 | { | | |
| 1537 | this->cleanup(); | | |
| 1538 | } | | |
| 1539 | | | |
| 1540 | | | |
| 1541 | | | |
| 1542 | | | |
| 1543 | //----------------------------------------------------------------------------- | | |
| 1544 | // name: reset() | | |
| 1545 | // desc: ... | | |
| 1546 | //----------------------------------------------------------------------------- | | |
| 1547 | BOOL__ AudioBufferIn::reset() | | |
| 1548 | { | | |
| 1549 | m_ptr_curr = m_data; | | |
| 1550 | | | |
| 1551 | return TickIn::reset(); | | |
| 1552 | } | | |
| 1553 | | | |
| 1554 | | | |
| 1555 | | | |
| 1556 | | | |
| 1557 | //----------------------------------------------------------------------------- | | |
| 1558 | // name: tick_in() | | |
| 1559 | // desc: ... | | |
| 1560 | //----------------------------------------------------------------------------- | | |
| 1561 | BOOL__ AudioBufferIn::tick_in( SAMPLE * s ) | | |
| 1562 | { | | |
| 1563 | if( !m_data ) | | |
| 1564 | return FALSE; | | |
| 1565 | | | |
| 1566 | *s = *m_ptr_curr++; | | |
| 1567 | | | |
| 1568 | return TRUE; | | |
| 1569 | } | | |
| 1570 | | | |
| 1571 | | | |
| 1572 | | | |
| 1573 | | | |
| 1574 | //----------------------------------------------------------------------------- | | |
| 1575 | // name: tick_in() | | |
| 1576 | // desc: ... | | |
| 1577 | //----------------------------------------------------------------------------- | | |
| 1578 | BOOL__ AudioBufferIn::tick_in( SAMPLE * l, SAMPLE * r ) | | |
| 1579 | { | | |
| 1580 | if( !m_data || m_ptr_curr + 2 >= m_ptr_end ) | | |
| 1581 | return FALSE; | | |
| 1582 | | | |
| 1583 | *l = *m_ptr_curr++; | | |
| 1584 | *r = *m_ptr_curr++; | | |
| 1585 | | | |
| 1586 | return TRUE; | | |
| 1587 | } | | |
| 1588 | | | |
| 1589 | | | |
| 1590 | | | |
| 1591 | | | |
| 1592 | //----------------------------------------------------------------------------- | | |
| 1593 | // name: tick_in() | | |
| 1594 | // desc: ... | | |
| 1595 | //----------------------------------------------------------------------------- | | |
| 1596 | BOOL__ AudioBufferIn::tick_in( SAMPLE * in, DWORD__ n ) | | |
| 1597 | { | | |
| 1598 | if( !m_data || m_ptr_curr + n >= m_ptr_end ) | | |
| 1599 | return FALSE; | | |
| 1600 | | | |
| 1601 | while( n-- ) | | |
| 1602 | *in++ = *m_ptr_curr++; | | |
| 1603 | | | |
| 1604 | return TRUE; | | |
| 1605 | } | | |
| 1606 | | | |
| 1607 | | | |
| 1608 | | | |
| 1609 | | | |
| 1610 | //----------------------------------------------------------------------------- | | |
| 1611 | // name: AudioBufferOut() | | |
| 1612 | // desc: ... | | |
| 1613 | //----------------------------------------------------------------------------- | | |
| 1614 | AudioBufferOut::AudioBufferOut( DWORD__ size ) | | |
| 1615 | : AudioBufferX( size ) | | |
| 1616 | { } | | |
| 1617 | | | |
| 1618 | | | |
| 1619 | | | |
| 1620 | | | |
| 1621 | //----------------------------------------------------------------------------- | | |
| 1622 | // name: ~AudioBufferOut() | | |
| 1623 | // desc: ... | | |
| 1624 | //----------------------------------------------------------------------------- | | |
| 1625 | AudioBufferOut::~AudioBufferOut() | | |
| 1626 | { | | |
| 1627 | this->cleanup(); | | |
| 1628 | } | | |
| 1629 | | | |
| 1630 | | | |
| 1631 | | | |
| 1632 | | | |
| 1633 | //----------------------------------------------------------------------------- | | |
| 1634 | // name: reset() | | |
| 1635 | // desc: ... | | |
| 1636 | //----------------------------------------------------------------------------- | | |
| 1637 | BOOL__ AudioBufferOut::reset() | | |
| 1638 | { | | |
| 1639 | m_ptr_curr = m_data; | | |
| 1640 | | | |
| 1641 | return TickOut::reset(); | | |
| 1642 | } | | |
| 1643 | | | |
| 1644 | | | |
| 1645 | | | |
| 1646 | | | |
| 1647 | //----------------------------------------------------------------------------- | | |
| 1648 | // name: tick_out() | | |
| 1649 | // desc: ... | | |
| 1650 | //----------------------------------------------------------------------------- | | |
| 1651 | BOOL__ AudioBufferOut::tick_out( SAMPLE s ) | | |
| 1652 | { | | |
| 1653 | if( !m_data ) | | |
| 1654 | return FALSE; | | |
| 1655 | | | |
| 1656 | *m_ptr_curr++ = s; | | |
| 1657 | | | |
| 1658 | return TRUE; | | |
| 1659 | } | | |
| 1660 | | | |
| 1661 | | | |
| 1662 | | | |
| 1663 | | | |
| 1664 | //----------------------------------------------------------------------------- | | |
| 1665 | // name: tick_out() | | |
| 1666 | // desc: ... | | |
| 1667 | //----------------------------------------------------------------------------- | | |
| 1668 | BOOL__ AudioBufferOut::tick_out( SAMPLE l, SAMPLE r ) | | |
| 1669 | { | | |
| 1670 | if( !m_data || m_ptr_curr + 2 >= m_ptr_end ) | | |
| 1671 | return FALSE; | | |
| 1672 | | | |
| 1673 | *m_ptr_curr++ = l; | | |
| 1674 | *m_ptr_curr++ = r; | | |
| 1675 | | | |
| 1676 | return TRUE; | | |
| 1677 | } | | |
| 1678 | | | |
| 1679 | | | |
| 1680 | | | |
| 1681 | | | |
| 1682 | //----------------------------------------------------------------------------- | | |
| 1683 | // name: tick_out() | | |
| 1684 | // desc: ... | | |
| 1685 | //----------------------------------------------------------------------------- | | |
| 1686 | BOOL__ AudioBufferOut::tick_out( const SAMPLE * out, DWORD__ n ) | | |
| 1687 | { | | |
| 1688 | if( !m_data || m_ptr_curr + n >= m_ptr_end ) | | |
| 1689 | return FALSE; | | |
| 1690 | | | |
| 1691 | while( n-- ) | | |
| 1692 | *m_ptr_curr++ = *out++; | | |
| 1693 | | | |
| 1694 | return TRUE; | | |
| 1695 | } | 849 | } |