| 1 | #ifdef _WIN32 |
| 2 | /* |
| 3 | * $Id$ |
| 4 | * Portable Audio I/O Library for ASIO Drivers |
| 5 | * |
| 6 | * Author: Stephane Letz |
| 7 | * Based on the Open Source API proposed by Ross Bencina |
| 8 | * Copyright (c) 2000-2002 Stephane Letz, Phil Burk, Ross Bencina |
| 9 | * Blocking i/o implementation by Sven Fischer, Institute of Hearing |
| 10 | * Technology and Audiology (www.hoertechnik-audiologie.de) |
| 11 | * |
| 12 | * Permission is hereby granted, free of charge, to any person obtaining |
| 13 | * a copy of this software and associated documentation files |
| 14 | * (the "Software"), to deal in the Software without restriction, |
| 15 | * including without limitation the rights to use, copy, modify, merge, |
| 16 | * publish, distribute, sublicense, and/or sell copies of the Software, |
| 17 | * and to permit persons to whom the Software is furnished to do so, |
| 18 | * subject to the following conditions: |
| 19 | * |
| 20 | * The above copyright notice and this permission notice shall be |
| 21 | * included in all copies or substantial portions of the Software. |
| 22 | * |
| 23 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, |
| 24 | * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF |
| 25 | * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. |
| 26 | * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR |
| 27 | * ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF |
| 28 | * CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION |
| 29 | * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. |
| 30 | */ |
| 31 | |
| 32 | /* |
| 33 | * The text above constitutes the entire PortAudio license; however, |
| 34 | * the PortAudio community also makes the following non-binding requests: |
| 35 | * |
| 36 | * Any person wishing to distribute modifications to the Software is |
| 37 | * requested to send the modifications to the original developer so that |
| 38 | * they can be incorporated into the canonical version. It is also |
| 39 | * requested that these non-binding requests be included along with the |
| 40 | * license above. |
| 41 | */ |
| 42 | |
| 43 | /* Modification History |
| 44 | |
| 45 | 08-03-01 First version : Stephane Letz |
| 46 | 08-06-01 Tweaks for PC, use C++, buffer allocation, Float32 to Int32 conversion : Phil Burk |
| 47 | 08-20-01 More conversion, PA_StreamTime, Pa_GetHostError : Stephane Letz |
| 48 | 08-21-01 PaUInt8 bug correction, implementation of ASIOSTFloat32LSB and ASIOSTFloat32MSB native formats : Stephane Letz |
| 49 | 08-24-01 MAX_INT32_FP hack, another Uint8 fix : Stephane and Phil |
| 50 | 08-27-01 Implementation of hostBufferSize < userBufferSize case, better management of the output buffer when |
| 51 | the stream is stopped : Stephane Letz |
| 52 | 08-28-01 Check the stream pointer for null in bufferSwitchTimeInfo, correct bug in bufferSwitchTimeInfo when |
| 53 | the stream is stopped : Stephane Letz |
| 54 | 10-12-01 Correct the PaHost_CalcNumHostBuffers function: computes FramesPerHostBuffer to be the lowest that |
| 55 | respect requested FramesPerUserBuffer and userBuffersPerHostBuffer : Stephane Letz |
| 56 | 10-26-01 Management of hostBufferSize and userBufferSize of any size : Stephane Letz |
| 57 | 10-27-01 Improve calculus of hostBufferSize to be multiple or divisor of userBufferSize if possible : Stephane and Phil |
| 58 | 10-29-01 Change MAX_INT32_FP to (2147483520.0f) to prevent roundup to 0x80000000 : Phil Burk |
| 59 | 10-31-01 Clear the output buffer and user buffers in PaHost_StartOutput, correct bug in GetFirstMultiple : Stephane Letz |
| 60 | 11-06-01 Rename functions : Stephane Letz |
| 61 | 11-08-01 New Pa_ASIO_Adaptor_Init function to init Callback adpatation variables, cleanup of Pa_ASIO_Callback_Input: Stephane Letz |
| 62 | 11-29-01 Break apart device loading to debug random failure in Pa_ASIO_QueryDeviceInfo ; Phil Burk |
| 63 | 01-03-02 Deallocate all resources in PaHost_Term for cases where Pa_CloseStream is not called properly : Stephane Letz |
| 64 | 02-01-02 Cleanup, test of multiple-stream opening : Stephane Letz |
| 65 | 19-02-02 New Pa_ASIO_loadDriver that calls CoInitialize on each thread on Windows : Stephane Letz |
| 66 | 09-04-02 Correct error code management in PaHost_Term, removes various compiler warning : Stephane Letz |
| 67 | 12-04-02 Add Mac includes for <Devices.h> and <Timer.h> : Phil Burk |
| 68 | 13-04-02 Removes another compiler warning : Stephane Letz |
| 69 | 30-04-02 Pa_ASIO_QueryDeviceInfo bug correction, memory allocation checking, better error handling : D Viens, P Burk, S Letz |
| 70 | 12-06-02 Rehashed into new multi-api infrastructure, added support for all ASIO sample formats : Ross Bencina |
| 71 | 18-06-02 Added pa_asio.h, PaAsio_GetAvailableLatencyValues() : Ross B. |
| 72 | 21-06-02 Added SelectHostBufferSize() which selects host buffer size based on user latency parameters : Ross Bencina |
| 73 | ** NOTE maintenance history is now stored in CVS ** |
| 74 | */ |
| 75 | |
| 76 | /** @file |
| 77 | @ingroup hostapi_src |
| 78 | |
| 79 | Note that specific support for paInputUnderflow, paOutputOverflow and |
| 80 | paNeverDropInput is not necessary or possible with this driver due to the |
| 81 | synchronous full duplex double-buffered architecture of ASIO. |
| 82 | */ |
| 83 | |
| 84 | |
| 85 | #include <stdio.h> |
| 86 | #include <assert.h> |
| 87 | #include <string.h> |
| 88 | //#include <values.h> |
| 89 | #include <new> |
| 90 | |
| 91 | #include <windows.h> |
| 92 | #include <mmsystem.h> |
| 93 | |
| 94 | #include "portaudio.h" |
| 95 | #include "pa_asio.h" |
| 96 | #include "pa_util.h" |
| 97 | #include "pa_allocation.h" |
| 98 | #include "pa_hostapi.h" |
| 99 | #include "pa_stream.h" |
| 100 | #include "pa_cpuload.h" |
| 101 | #include "pa_process.h" |
| 102 | #include "pa_debugprint.h" |
| 103 | #include "pa_ringbuffer.h" |
| 104 | |
| 105 | #include "pa_win_coinitialize.h" |
| 106 | |
| 107 | /* This version of pa_asio.cpp is currently only targeted at Win32, |
| 108 | It would require a few tweaks to work with pre-OS X Macintosh. |
| 109 | To make configuration easier, we define WIN32 here to make sure |
| 110 | that the ASIO SDK knows this is Win32. |
| 111 | */ |
| 112 | #ifndef WIN32 |
| 113 | #define WIN32 |
| 114 | #endif |
| 115 | |
| 116 | #include "asiosys.h" |
| 117 | #include "asio.h" |
| 118 | #include "asiodrivers.h" |
| 119 | #include "iasiothiscallresolver.h" |
| 120 | |
| 121 | /* |
| 122 | #if MAC |
| 123 | #include <Devices.h> |
| 124 | #include <Timer.h> |
| 125 | #include <Math64.h> |
| 126 | #else |
| 127 | */ |
| 128 | /* |
| 129 | #include <math.h> |
| 130 | #include <windows.h> |
| 131 | #include <mmsystem.h> |
| 132 | */ |
| 133 | /* |
| 134 | #endif |
| 135 | */ |
| 136 | |
| 137 | |
| 138 | /* winmm.lib is needed for timeGetTime() (this is in winmm.a if you're using gcc) */ |
| 139 | #if (defined(WIN32) && (defined(_MSC_VER) && (_MSC_VER >= 1200))) /* MSC version 6 and above */ |
| 140 | #pragma comment(lib, "winmm.lib") |
| 141 | #endif |
| 142 | |
| 143 | |
| 144 | /* external reference to ASIO SDK's asioDrivers. |
| 145 | |
| 146 | This is a bit messy because we want to explicitly manage |
| 147 | allocation/deallocation of this structure, but some layers of the SDK |
| 148 | which we currently use (eg the implementation in asio.cpp) still |
| 149 | use this global version. |
| 150 | |
| 151 | For now we keep it in sync with our local instance in the host |
| 152 | API representation structure, but later we should be able to remove |
| 153 | all dependence on it. |
| 154 | */ |
| 155 | extern AsioDrivers* asioDrivers; |
| 156 | |
| 157 | |
| 158 | /* We are trying to be compatible with CARBON but this has not been thoroughly tested. */ |
| 159 | /* not tested at all since new V19 code was introduced. */ |
| 160 | #define CARBON_COMPATIBLE (0) |
| 161 | |
| 162 | |
| 163 | /* prototypes for functions declared in this file */ |
| 164 | |
| 165 | extern "C" PaError PaAsio_Initialize( PaUtilHostApiRepresentation **hostApi, PaHostApiIndex hostApiIndex ); |
| 166 | static void Terminate( struct PaUtilHostApiRepresentation *hostApi ); |
| 167 | static PaError OpenStream( struct PaUtilHostApiRepresentation *hostApi, |
| 168 | PaStream** s, |
| 169 | const PaStreamParameters *inputParameters, |
| 170 | const PaStreamParameters *outputParameters, |
| 171 | double sampleRate, |
| 172 | unsigned long framesPerBuffer, |
| 173 | PaStreamFlags streamFlags, |
| 174 | PaStreamCallback *streamCallback, |
| 175 | void *userData ); |
| 176 | static PaError IsFormatSupported( struct PaUtilHostApiRepresentation *hostApi, |
| 177 | const PaStreamParameters *inputParameters, |
| 178 | const PaStreamParameters *outputParameters, |
| 179 | double sampleRate ); |
| 180 | static PaError CloseStream( PaStream* stream ); |
| 181 | static PaError StartStream( PaStream *stream ); |
| 182 | static PaError StopStream( PaStream *stream ); |
| 183 | static PaError AbortStream( PaStream *stream ); |
| 184 | static PaError IsStreamStopped( PaStream *s ); |
| 185 | static PaError IsStreamActive( PaStream *stream ); |
| 186 | static PaTime GetStreamTime( PaStream *stream ); |
| 187 | static double GetStreamCpuLoad( PaStream* stream ); |
| 188 | static PaError ReadStream( PaStream* stream, void *buffer, unsigned long frames ); |
| 189 | static PaError WriteStream( PaStream* stream, const void *buffer, unsigned long frames ); |
| 190 | static signed long GetStreamReadAvailable( PaStream* stream ); |
| 191 | static signed long GetStreamWriteAvailable( PaStream* stream ); |
| 192 | |
| 193 | /* Blocking i/o callback function. */ |
| 194 | static int BlockingIoPaCallback(const void *inputBuffer , |
| 195 | void *outputBuffer , |
| 196 | unsigned long framesPerBuffer, |
| 197 | const PaStreamCallbackTimeInfo *timeInfo , |
| 198 | PaStreamCallbackFlags statusFlags , |
| 199 | void *userData ); |
| 200 | |
| 201 | /* our ASIO callback functions */ |
| 202 | |
| 203 | static void bufferSwitch(long index, ASIOBool processNow); |
| 204 | static ASIOTime *bufferSwitchTimeInfo(ASIOTime *timeInfo, long index, ASIOBool processNow); |
| 205 | static void sampleRateChanged(ASIOSampleRate sRate); |
| 206 | static long asioMessages(long selector, long value, void* message, double* opt); |
| 207 | |
| 208 | static ASIOCallbacks asioCallbacks_ = |
| 209 | { bufferSwitch, sampleRateChanged, asioMessages, bufferSwitchTimeInfo }; |
| 210 | |
| 211 | |
| 212 | #define PA_ASIO_SET_LAST_HOST_ERROR( errorCode, errorText ) \ |
| 213 | PaUtil_SetLastHostErrorInfo( paASIO, errorCode, errorText ) |
| 214 | |
| 215 | |
| 216 | static void PaAsio_SetLastSystemError( DWORD errorCode ) |
| 217 | { |
| 218 | LPVOID lpMsgBuf; |
| 219 | FormatMessage( |
| 220 | FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM, |
| 221 | NULL, |
| 222 | errorCode, |
| 223 | MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), |
| 224 | (LPTSTR) &lpMsgBuf, |
| 225 | 0, |
| 226 | NULL |
| 227 | ); |
| 228 | PaUtil_SetLastHostErrorInfo( paASIO, errorCode, (const char*)lpMsgBuf ); |
| 229 | LocalFree( lpMsgBuf ); |
| 230 | } |
| 231 | |
| 232 | #define PA_ASIO_SET_LAST_SYSTEM_ERROR( errorCode ) \ |
| 233 | PaAsio_SetLastSystemError( errorCode ) |
| 234 | |
| 235 | |
| 236 | static const char* PaAsio_GetAsioErrorText( ASIOError asioError ) |
| 237 | { |
| 238 | const char *result; |
| 239 | |
| 240 | switch( asioError ){ |
| 241 | case ASE_OK: |
| 242 | case ASE_SUCCESS: result = "Success"; break; |
| 243 | case ASE_NotPresent: result = "Hardware input or output is not present or available"; break; |
| 244 | case ASE_HWMalfunction: result = "Hardware is malfunctioning"; break; |
| 245 | case ASE_InvalidParameter: result = "Input parameter invalid"; break; |
| 246 | case ASE_InvalidMode: result = "Hardware is in a bad mode or used in a bad mode"; break; |
| 247 | case ASE_SPNotAdvancing: result = "Hardware is not running when sample position is inquired"; break; |
| 248 | case ASE_NoClock: result = "Sample clock or rate cannot be determined or is not present"; break; |
| 249 | case ASE_NoMemory: result = "Not enough memory for completing the request"; break; |
| 250 | default: result = "Unknown ASIO error"; break; |
| 251 | } |
| 252 | |
| 253 | return result; |
| 254 | } |
| 255 | |
| 256 | |
| 257 | #define PA_ASIO_SET_LAST_ASIO_ERROR( asioError ) \ |
| 258 | PaUtil_SetLastHostErrorInfo( paASIO, asioError, PaAsio_GetAsioErrorText( asioError ) ) |
| 259 | |
| 260 | |
| 261 | |
| 262 | |
| 263 | // Atomic increment and decrement operations |
| 264 | #if MAC |
| 265 | /* need to be implemented on Mac */ |
| 266 | inline long PaAsio_AtomicIncrement(volatile long* v) {return ++(*const_cast<long*>(v));} |
| 267 | inline long PaAsio_AtomicDecrement(volatile long* v) {return --(*const_cast<long*>(v));} |
| 268 | #elif WINDOWS |
| 269 | inline long PaAsio_AtomicIncrement(volatile long* v) {return InterlockedIncrement(const_cast<long*>(v));} |
| 270 | inline long PaAsio_AtomicDecrement(volatile long* v) {return InterlockedDecrement(const_cast<long*>(v));} |
| 271 | #endif |
| 272 | |
| 273 | |
| 274 | |
| 275 | typedef struct PaAsioDriverInfo |
| 276 | { |
| 277 | ASIODriverInfo asioDriverInfo; |
| 278 | long inputChannelCount, outputChannelCount; |
| 279 | long bufferMinSize, bufferMaxSize, bufferPreferredSize, bufferGranularity; |
| 280 | bool postOutput; |
| 281 | } |
| 282 | PaAsioDriverInfo; |
| 283 | |
| 284 | |
| 285 | /* PaAsioHostApiRepresentation - host api datastructure specific to this implementation */ |
| 286 | |
| 287 | typedef struct |
| 288 | { |
| 289 | PaUtilHostApiRepresentation inheritedHostApiRep; |
| 290 | PaUtilStreamInterface callbackStreamInterface; |
| 291 | PaUtilStreamInterface blockingStreamInterface; |
| 292 | |
| 293 | PaUtilAllocationGroup *allocations; |
| 294 | |
| 295 | PaWinUtilComInitializationResult comInitializationResult; |
| 296 | |
| 297 | AsioDrivers *asioDrivers; |
| 298 | void *systemSpecific; |
| 299 | |
| 300 | /* the ASIO C API only allows one ASIO driver to be open at a time, |
| 301 | so we keep track of whether we have the driver open here, and |
| 302 | use this information to return errors from OpenStream if the |
| 303 | driver is already open. |
| 304 | |
| 305 | openAsioDeviceIndex will be PaNoDevice if there is no device open |
| 306 | and a valid pa_asio (not global) device index otherwise. |
| 307 | |
| 308 | openAsioDriverInfo is populated with the driver info for the |
| 309 | currently open device (if any) |
| 310 | */ |
| 311 | PaDeviceIndex openAsioDeviceIndex; |
| 312 | PaAsioDriverInfo openAsioDriverInfo; |
| 313 | } |
| 314 | PaAsioHostApiRepresentation; |
| 315 | |
| 316 | |
| 317 | /* |
| 318 | Retrieve <driverCount> driver names from ASIO, returned in a char** |
| 319 | allocated in <group>. |
| 320 | */ |
| 321 | static char **GetAsioDriverNames( PaAsioHostApiRepresentation *asioHostApi, PaUtilAllocationGroup *group, long driverCount ) |
| 322 | { |
| 323 | char **result = 0; |
| 324 | int i; |
| 325 | |
| 326 | result =(char**)PaUtil_GroupAllocateMemory( |
| 327 | group, sizeof(char*) * driverCount ); |
| 328 | if( !result ) |
| 329 | goto error; |
| 330 | |
| 331 | result[0] = (char*)PaUtil_GroupAllocateMemory( |
| 332 | group, 32 * driverCount ); |
| 333 | if( !result[0] ) |
| 334 | goto error; |
| 335 | |
| 336 | for( i=0; i<driverCount; ++i ) |
| 337 | result[i] = result[0] + (32 * i); |
| 338 | |
| 339 | asioHostApi->asioDrivers->getDriverNames( result, driverCount ); |
| 340 | |
| 341 | error: |
| 342 | return result; |
| 343 | } |
| 344 | |
| 345 | |
| 346 | static PaSampleFormat AsioSampleTypeToPaNativeSampleFormat(ASIOSampleType type) |
| 347 | { |
| 348 | switch (type) { |
| 349 | case ASIOSTInt16MSB: |
| 350 | case ASIOSTInt16LSB: |
| 351 | return paInt16; |
| 352 | |
| 353 | case ASIOSTFloat32MSB: |
| 354 | case ASIOSTFloat32LSB: |
| 355 | case ASIOSTFloat64MSB: |
| 356 | case ASIOSTFloat64LSB: |
| 357 | return paFloat32; |
| 358 | |
| 359 | case ASIOSTInt32MSB: |
| 360 | case ASIOSTInt32LSB: |
| 361 | case ASIOSTInt32MSB16: |
| 362 | case ASIOSTInt32LSB16: |
| 363 | case ASIOSTInt32MSB18: |
| 364 | case ASIOSTInt32MSB20: |
| 365 | case ASIOSTInt32MSB24: |
| 366 | case ASIOSTInt32LSB18: |
| 367 | case ASIOSTInt32LSB20: |
| 368 | case ASIOSTInt32LSB24: |
| 369 | return paInt32; |
| 370 | |
| 371 | case ASIOSTInt24MSB: |
| 372 | case ASIOSTInt24LSB: |
| 373 | return paInt24; |
| 374 | |
| 375 | default: |
| 376 | return paCustomFormat; |
| 377 | } |
| 378 | } |
| 379 | |
| 380 | void AsioSampleTypeLOG(ASIOSampleType type) |
| 381 | { |
| 382 | switch (type) { |
| 383 | case ASIOSTInt16MSB: PA_DEBUG(("ASIOSTInt16MSB\n")); break; |
| 384 | case ASIOSTInt16LSB: PA_DEBUG(("ASIOSTInt16LSB\n")); break; |
| 385 | case ASIOSTFloat32MSB:PA_DEBUG(("ASIOSTFloat32MSB\n"));break; |
| 386 | case ASIOSTFloat32LSB:PA_DEBUG(("ASIOSTFloat32LSB\n"));break; |
| 387 | case ASIOSTFloat64MSB:PA_DEBUG(("ASIOSTFloat64MSB\n"));break; |
| 388 | case ASIOSTFloat64LSB:PA_DEBUG(("ASIOSTFloat64LSB\n"));break; |
| 389 | case ASIOSTInt32MSB: PA_DEBUG(("ASIOSTInt32MSB\n")); break; |
| 390 | case ASIOSTInt32LSB: PA_DEBUG(("ASIOSTInt32LSB\n")); break; |
| 391 | case ASIOSTInt32MSB16:PA_DEBUG(("ASIOSTInt32MSB16\n"));break; |
| 392 | case ASIOSTInt32LSB16:PA_DEBUG(("ASIOSTInt32LSB16\n"));break; |
| 393 | case ASIOSTInt32MSB18:PA_DEBUG(("ASIOSTInt32MSB18\n"));break; |
| 394 | case ASIOSTInt32MSB20:PA_DEBUG(("ASIOSTInt32MSB20\n"));break; |
| 395 | case ASIOSTInt32MSB24:PA_DEBUG(("ASIOSTInt32MSB24\n"));break; |
| 396 | case ASIOSTInt32LSB18:PA_DEBUG(("ASIOSTInt32LSB18\n"));break; |
| 397 | case ASIOSTInt32LSB20:PA_DEBUG(("ASIOSTInt32LSB20\n"));break; |
| 398 | case ASIOSTInt32LSB24:PA_DEBUG(("ASIOSTInt32LSB24\n"));break; |
| 399 | case ASIOSTInt24MSB: PA_DEBUG(("ASIOSTInt24MSB\n")); break; |
| 400 | case ASIOSTInt24LSB: PA_DEBUG(("ASIOSTInt24LSB\n")); break; |
| 401 | default: PA_DEBUG(("Custom Format%d\n",type));break; |
| 402 | |
| 403 | } |
| 404 | } |
| 405 | |
| 406 | static int BytesPerAsioSample( ASIOSampleType sampleType ) |
| 407 | { |
| 408 | switch (sampleType) { |
| 409 | case ASIOSTInt16MSB: |
| 410 | case ASIOSTInt16LSB: |
| 411 | return 2; |
| 412 | |
| 413 | case ASIOSTFloat64MSB: |
| 414 | case ASIOSTFloat64LSB: |
| 415 | return 8; |
| 416 | |
| 417 | case ASIOSTFloat32MSB: |
| 418 | case ASIOSTFloat32LSB: |
| 419 | case ASIOSTInt32MSB: |
| 420 | case ASIOSTInt32LSB: |
| 421 | case ASIOSTInt32MSB16: |
| 422 | case ASIOSTInt32LSB16: |
| 423 | case ASIOSTInt32MSB18: |
| 424 | case ASIOSTInt32MSB20: |
| 425 | case ASIOSTInt32MSB24: |
| 426 | case ASIOSTInt32LSB18: |
| 427 | case ASIOSTInt32LSB20: |
| 428 | case ASIOSTInt32LSB24: |
| 429 | return 4; |
| 430 | |
| 431 | case ASIOSTInt24MSB: |
| 432 | case ASIOSTInt24LSB: |
| 433 | return 3; |
| 434 | |
| 435 | default: |
| 436 | return 0; |
| 437 | } |
| 438 | } |
| 439 | |
| 440 | |
| 441 | static void Swap16( void *buffer, long shift, long count ) |
| 442 | { |
| 443 | unsigned short *p = (unsigned short*)buffer; |
| 444 | unsigned short temp; |
| 445 | (void) shift; /* unused parameter */ |
| 446 | |
| 447 | while( count-- ) |
| 448 | { |
| 449 | temp = *p; |
| 450 | *p++ = (unsigned short)((temp<<8) | (temp>>8)); |
| 451 | } |
| 452 | } |
| 453 | |
| 454 | static void Swap24( void *buffer, long shift, long count ) |
| 455 | { |
| 456 | unsigned char *p = (unsigned char*)buffer; |
| 457 | unsigned char temp; |
| 458 | (void) shift; /* unused parameter */ |
| 459 | |
| 460 | while( count-- ) |
| 461 | { |
| 462 | temp = *p; |
| 463 | *p = *(p+2); |
| 464 | *(p+2) = temp; |
| 465 | p += 3; |
| 466 | } |
| 467 | } |
| 468 | |
| 469 | #define PA_SWAP32_( x ) ((x>>24) | ((x>>8)&0xFF00) | ((x<<8)&0xFF0000) | (x<<24)); |
| 470 | |
| 471 | static void Swap32( void *buffer, long shift, long count ) |
| 472 | { |
| 473 | unsigned long *p = (unsigned long*)buffer; |
| 474 | unsigned long temp; |
| 475 | (void) shift; /* unused parameter */ |
| 476 | |
| 477 | while( count-- ) |
| 478 | { |
| 479 | temp = *p; |
| 480 | *p++ = PA_SWAP32_( temp); |
| 481 | } |
| 482 | } |
| 483 | |
| 484 | static void SwapShiftLeft32( void *buffer, long shift, long count ) |
| 485 | { |
| 486 | unsigned long *p = (unsigned long*)buffer; |
| 487 | unsigned long temp; |
| 488 | |
| 489 | while( count-- ) |
| 490 | { |
| 491 | temp = *p; |
| 492 | temp = PA_SWAP32_( temp); |
| 493 | *p++ = temp << shift; |
| 494 | } |
| 495 | } |
| 496 | |
| 497 | static void ShiftRightSwap32( void *buffer, long shift, long count ) |
| 498 | { |
| 499 | unsigned long *p = (unsigned long*)buffer; |
| 500 | unsigned long temp; |
| 501 | |
| 502 | while( count-- ) |
| 503 | { |
| 504 | temp = *p >> shift; |
| 505 | *p++ = PA_SWAP32_( temp); |
| 506 | } |
| 507 | } |
| 508 | |
| 509 | static void ShiftLeft32( void *buffer, long shift, long count ) |
| 510 | { |
| 511 | unsigned long *p = (unsigned long*)buffer; |
| 512 | unsigned long temp; |
| 513 | |
| 514 | while( count-- ) |
| 515 | { |
| 516 | temp = *p; |
| 517 | *p++ = temp << shift; |
| 518 | } |
| 519 | } |
| 520 | |
| 521 | static void ShiftRight32( void *buffer, long shift, long count ) |
| 522 | { |
| 523 | unsigned long *p = (unsigned long*)buffer; |
| 524 | unsigned long temp; |
| 525 | |
| 526 | while( count-- ) |
| 527 | { |
| 528 | temp = *p; |
| 529 | *p++ = temp >> shift; |
| 530 | } |
| 531 | } |
| 532 | |
| 533 | #define PA_SWAP_( x, y ) temp=x; x = y; y = temp; |
| 534 | |
| 535 | static void Swap64ConvertFloat64ToFloat32( void *buffer, long shift, long count ) |
| 536 | { |
| 537 | double *in = (double*)buffer; |
| 538 | float *out = (float*)buffer; |
| 539 | unsigned char *p; |
| 540 | unsigned char temp; |
| 541 | (void) shift; /* unused parameter */ |
| 542 | |
| 543 | while( count-- ) |
| 544 | { |
| 545 | p = (unsigned char*)in; |
| 546 | PA_SWAP_( p[0], p[7] ); |
| 547 | PA_SWAP_( p[1], p[6] ); |
| 548 | PA_SWAP_( p[2], p[5] ); |
| 549 | PA_SWAP_( p[3], p[4] ); |
| 550 | |
| 551 | *out++ = (float) (*in++); |
| 552 | } |
| 553 | } |
| 554 | |
| 555 | static void ConvertFloat64ToFloat32( void *buffer, long shift, long count ) |
| 556 | { |
| 557 | double *in = (double*)buffer; |
| 558 | float *out = (float*)buffer; |
| 559 | (void) shift; /* unused parameter */ |
| 560 | |
| 561 | while( count-- ) |
| 562 | *out++ = (float) (*in++); |
| 563 | } |
| 564 | |
| 565 | static void ConvertFloat32ToFloat64Swap64( void *buffer, long shift, long count ) |
| 566 | { |
| 567 | float *in = ((float*)buffer) + (count-1); |
| 568 | double *out = ((double*)buffer) + (count-1); |
| 569 | unsigned char *p; |
| 570 | unsigned char temp; |
| 571 | (void) shift; /* unused parameter */ |
| 572 | |
| 573 | while( count-- ) |
| 574 | { |
| 575 | *out = *in--; |
| 576 | |
| 577 | p = (unsigned char*)out; |
| 578 | PA_SWAP_( p[0], p[7] ); |
| 579 | PA_SWAP_( p[1], p[6] ); |
| 580 | PA_SWAP_( p[2], p[5] ); |
| 581 | PA_SWAP_( p[3], p[4] ); |
| 582 | |
| 583 | out--; |
| 584 | } |
| 585 | } |
| 586 | |
| 587 | static void ConvertFloat32ToFloat64( void *buffer, long shift, long count ) |
| 588 | { |
| 589 | float *in = ((float*)buffer) + (count-1); |
| 590 | double *out = ((double*)buffer) + (count-1); |
| 591 | (void) shift; /* unused parameter */ |
| 592 | |
| 593 | while( count-- ) |
| 594 | *out-- = *in--; |
| 595 | } |
| 596 | |
| 597 | #ifdef MAC |
| 598 | #define PA_MSB_IS_NATIVE_ |
| 599 | #undef PA_LSB_IS_NATIVE_ |
| 600 | #endif |
| 601 | |
| 602 | #ifdef WINDOWS |
| 603 | #undef PA_MSB_IS_NATIVE_ |
| 604 | #define PA_LSB_IS_NATIVE_ |
| 605 | #endif |
| 606 | |
| 607 | typedef void PaAsioBufferConverter( void *, long, long ); |
| 608 | |
| 609 | static void SelectAsioToPaConverter( ASIOSampleType type, PaAsioBufferConverter **converter, long *shift ) |
| 610 | { |
| 611 | *shift = 0; |
| 612 | *converter = 0; |
| 613 | |
| 614 | switch (type) { |
| 615 | case ASIOSTInt16MSB: |
| 616 | /* dest: paInt16, no conversion necessary, possible byte swap*/ |
| 617 | #ifdef PA_LSB_IS_NATIVE_ |
| 618 | *converter = Swap16; |
| 619 | #endif |
| 620 | break; |
| 621 | case ASIOSTInt16LSB: |
| 622 | /* dest: paInt16, no conversion necessary, possible byte swap*/ |
| 623 | #ifdef PA_MSB_IS_NATIVE_ |
| 624 | *converter = Swap16; |
| 625 | #endif |
| 626 | break; |
| 627 | case ASIOSTFloat32MSB: |
| 628 | /* dest: paFloat32, no conversion necessary, possible byte swap*/ |
| 629 | #ifdef PA_LSB_IS_NATIVE_ |
| 630 | *converter = Swap32; |
| 631 | #endif |
| 632 | break; |
| 633 | case ASIOSTFloat32LSB: |
| 634 | /* dest: paFloat32, no conversion necessary, possible byte swap*/ |
| 635 | #ifdef PA_MSB_IS_NATIVE_ |
| 636 | *converter = Swap32; |
| 637 | #endif |
| 638 | break; |
| 639 | case ASIOSTFloat64MSB: |
| 640 | /* dest: paFloat32, in-place conversion to/from float32, possible byte swap*/ |
| 641 | #ifdef PA_LSB_IS_NATIVE_ |
| 642 | *converter = Swap64ConvertFloat64ToFloat32; |
| 643 | #else |
| 644 | *converter = ConvertFloat64ToFloat32; |
| 645 | #endif |
| 646 | break; |
| 647 | case ASIOSTFloat64LSB: |
| 648 | /* dest: paFloat32, in-place conversion to/from float32, possible byte swap*/ |
| 649 | #ifdef PA_MSB_IS_NATIVE_ |
| 650 | *converter = Swap64ConvertFloat64ToFloat32; |
| 651 | #else |
| 652 | *converter = ConvertFloat64ToFloat32; |
| 653 | #endif |
| 654 | break; |
| 655 | case ASIOSTInt32MSB: |
| 656 | /* dest: paInt32, no conversion necessary, possible byte swap */ |
| 657 | #ifdef PA_LSB_IS_NATIVE_ |
| 658 | *converter = Swap32; |
| 659 | #endif |
| 660 | break; |
| 661 | case ASIOSTInt32LSB: |
| 662 | /* dest: paInt32, no conversion necessary, possible byte swap */ |
| 663 | #ifdef PA_MSB_IS_NATIVE_ |
| 664 | *converter = Swap32; |
| 665 | #endif |
| 666 | break; |
| 667 | case ASIOSTInt32MSB16: |
| 668 | /* dest: paInt32, 16 bit shift, possible byte swap */ |
| 669 | #ifdef PA_LSB_IS_NATIVE_ |
| 670 | *converter = SwapShiftLeft32; |
| 671 | #else |
| 672 | *converter = ShiftLeft32; |
| 673 | #endif |
| 674 | *shift = 16; |
| 675 | break; |
| 676 | case ASIOSTInt32MSB18: |
| 677 | /* dest: paInt32, 14 bit shift, possible byte swap */ |
| 678 | #ifdef PA_LSB_IS_NATIVE_ |
| 679 | *converter = SwapShiftLeft32; |
| 680 | #else |
| 681 | *converter = ShiftLeft32; |
| 682 | #endif |
| 683 | *shift = 14; |
| 684 | break; |
| 685 | case ASIOSTInt32MSB20: |
| 686 | /* dest: paInt32, 12 bit shift, possible byte swap */ |
| 687 | #ifdef PA_LSB_IS_NATIVE_ |
| 688 | *converter = SwapShiftLeft32; |
| 689 | #else |
| 690 | *converter = ShiftLeft32; |
| 691 | #endif |
| 692 | *shift = 12; |
| 693 | break; |
| 694 | case ASIOSTInt32MSB24: |
| 695 | /* dest: paInt32, 8 bit shift, possible byte swap */ |
| 696 | #ifdef PA_LSB_IS_NATIVE_ |
| 697 | *converter = SwapShiftLeft32; |
| 698 | #else |
| 699 | *converter = ShiftLeft32; |
| 700 | #endif |
| 701 | *shift = 8; |
| 702 | break; |
| 703 | case ASIOSTInt32LSB16: |
| 704 | /* dest: paInt32, 16 bit shift, possible byte swap */ |
| 705 | #ifdef PA_MSB_IS_NATIVE_ |
| 706 | *converter = SwapShiftLeft32; |
| 707 | #else |
| 708 | *converter = ShiftLeft32; |
| 709 | #endif |
| 710 | *shift = 16; |
| 711 | break; |
| 712 | case ASIOSTInt32LSB18: |
| 713 | /* dest: paInt32, 14 bit shift, possible byte swap */ |
| 714 | #ifdef PA_MSB_IS_NATIVE_ |
| 715 | *converter = SwapShiftLeft32; |
| 716 | #else |
| 717 | *converter = ShiftLeft32; |
| 718 | #endif |
| 719 | *shift = 14; |
| 720 | break; |
| 721 | case ASIOSTInt32LSB20: |
| 722 | /* dest: paInt32, 12 bit shift, possible byte swap */ |
| 723 | #ifdef PA_MSB_IS_NATIVE_ |
| 724 | *converter = SwapShiftLeft32; |
| 725 | #else |
| 726 | *converter = ShiftLeft32; |
| 727 | #endif |
| 728 | *shift = 12; |
| 729 | break; |
| 730 | case ASIOSTInt32LSB24: |
| 731 | /* dest: paInt32, 8 bit shift, possible byte swap */ |
| 732 | #ifdef PA_MSB_IS_NATIVE_ |
| 733 | *converter = SwapShiftLeft32; |
| 734 | #else |
| 735 | *converter = ShiftLeft32; |
| 736 | #endif |
| 737 | *shift = 8; |
| 738 | break; |
| 739 | case ASIOSTInt24MSB: |
| 740 | /* dest: paInt24, no conversion necessary, possible byte swap */ |
| 741 | #ifdef PA_LSB_IS_NATIVE_ |
| 742 | *converter = Swap24; |
| 743 | #endif |
| 744 | break; |
| 745 | case ASIOSTInt24LSB: |
| 746 | /* dest: paInt24, no conversion necessary, possible byte swap */ |
| 747 | #ifdef PA_MSB_IS_NATIVE_ |
| 748 | *converter = Swap24; |
| 749 | #endif |
| 750 | break; |
| 751 | } |
| 752 | } |
| 753 | |
| 754 | |
| 755 | static void SelectPaToAsioConverter( ASIOSampleType type, PaAsioBufferConverter **converter, long *shift ) |
| 756 | { |
| 757 | *shift = 0; |
| 758 | *converter = 0; |
| 759 | |
| 760 | switch (type) { |
| 761 | case ASIOSTInt16MSB: |
| 762 | /* src: paInt16, no conversion necessary, possible byte swap*/ |
| 763 | #ifdef PA_LSB_IS_NATIVE_ |
| 764 | *converter = Swap16; |
| 765 | #endif |
| 766 | break; |
| 767 | case ASIOSTInt16LSB: |
| 768 | /* src: paInt16, no conversion necessary, possible byte swap*/ |
| 769 | #ifdef PA_MSB_IS_NATIVE_ |
| 770 | *converter = Swap16; |
| 771 | #endif |
| 772 | break; |
| 773 | case ASIOSTFloat32MSB: |
| 774 | /* src: paFloat32, no conversion necessary, possible byte swap*/ |
| 775 | #ifdef PA_LSB_IS_NATIVE_ |
| 776 | *converter = Swap32; |
| 777 | #endif |
| 778 | break; |
| 779 | case ASIOSTFloat32LSB: |
| 780 | /* src: paFloat32, no conversion necessary, possible byte swap*/ |
| 781 | #ifdef PA_MSB_IS_NATIVE_ |
| 782 | *converter = Swap32; |
| 783 | #endif |
| 784 | break; |
| 785 | case ASIOSTFloat64MSB: |
| 786 | /* src: paFloat32, in-place conversion to/from float32, possible byte swap*/ |
| 787 | #ifdef PA_LSB_IS_NATIVE_ |
| 788 | *converter = ConvertFloat32ToFloat64Swap64; |
| 789 | #else |
| 790 | *converter = ConvertFloat32ToFloat64; |
| 791 | #endif |
| 792 | break; |
| 793 | case ASIOSTFloat64LSB: |
| 794 | /* src: paFloat32, in-place conversion to/from float32, possible byte swap*/ |
| 795 | #ifdef PA_MSB_IS_NATIVE_ |
| 796 | *converter = ConvertFloat32ToFloat64Swap64; |
| 797 | #else |
| 798 | *converter = ConvertFloat32ToFloat64; |
| 799 | #endif |
| 800 | break; |
| 801 | case ASIOSTInt32MSB: |
| 802 | /* src: paInt32, no conversion necessary, possible byte swap */ |
| 803 | #ifdef PA_LSB_IS_NATIVE_ |
| 804 | *converter = Swap32; |
| 805 | #endif |
| 806 | break; |
| 807 | case ASIOSTInt32LSB: |
| 808 | /* src: paInt32, no conversion necessary, possible byte swap */ |
| 809 | #ifdef PA_MSB_IS_NATIVE_ |
| 810 | *converter = Swap32; |
| 811 | #endif |
| 812 | break; |
| 813 | case ASIOSTInt32MSB16: |
| 814 | /* src: paInt32, 16 bit shift, possible byte swap */ |
| 815 | #ifdef PA_LSB_IS_NATIVE_ |
| 816 | *converter = ShiftRightSwap32; |
| 817 | #else |
| 818 | *converter = ShiftRight32; |
| 819 | #endif |
| 820 | *shift = 16; |
| 821 | break; |
| 822 | case ASIOSTInt32MSB18: |
| 823 | /* src: paInt32, 14 bit shift, possible byte swap */ |
| 824 | #ifdef PA_LSB_IS_NATIVE_ |
| 825 | *converter = ShiftRightSwap32; |
| 826 | #else |
| 827 | *converter = ShiftRight32; |
| 828 | #endif |
| 829 | *shift = 14; |
| 830 | break; |
| 831 | case ASIOSTInt32MSB20: |
| 832 | /* src: paInt32, 12 bit shift, possible byte swap */ |
| 833 | #ifdef PA_LSB_IS_NATIVE_ |
| 834 | *converter = ShiftRightSwap32; |
| 835 | #else |
| 836 | *converter = ShiftRight32; |
| 837 | #endif |
| 838 | *shift = 12; |
| 839 | break; |
| 840 | case ASIOSTInt32MSB24: |
| 841 | /* src: paInt32, 8 bit shift, possible byte swap */ |
| 842 | #ifdef PA_LSB_IS_NATIVE_ |
| 843 | *converter = ShiftRightSwap32; |
| 844 | #else |
| 845 | *converter = ShiftRight32; |
| 846 | #endif |
| 847 | *shift = 8; |
| 848 | break; |
| 849 | case ASIOSTInt32LSB16: |
| 850 | /* src: paInt32, 16 bit shift, possible byte swap */ |
| 851 | #ifdef PA_MSB_IS_NATIVE_ |
| 852 | *converter = ShiftRightSwap32; |
| 853 | #else |
| 854 | *converter = ShiftRight32; |
| 855 | #endif |
| 856 | *shift = 16; |
| 857 | break; |
| 858 | case ASIOSTInt32LSB18: |
| 859 | /* src: paInt32, 14 bit shift, possible byte swap */ |
| 860 | #ifdef PA_MSB_IS_NATIVE_ |
| 861 | *converter = ShiftRightSwap32; |
| 862 | #else |
| 863 | *converter = ShiftRight32; |
| 864 | #endif |
| 865 | *shift = 14; |
| 866 | break; |
| 867 | case ASIOSTInt32LSB20: |
| 868 | /* src: paInt32, 12 bit shift, possible byte swap */ |
| 869 | #ifdef PA_MSB_IS_NATIVE_ |
| 870 | *converter = ShiftRightSwap32; |
| 871 | #else |
| 872 | *converter = ShiftRight32; |
| 873 | #endif |
| 874 | *shift = 12; |
| 875 | break; |
| 876 | case ASIOSTInt32LSB24: |
| 877 | /* src: paInt32, 8 bit shift, possible byte swap */ |
| 878 | #ifdef PA_MSB_IS_NATIVE_ |
| 879 | *converter = ShiftRightSwap32; |
| 880 | #else |
| 881 | *converter = ShiftRight32; |
| 882 | #endif |
| 883 | *shift = 8; |
| 884 | break; |
| 885 | case ASIOSTInt24MSB: |
| 886 | /* src: paInt24, no conversion necessary, possible byte swap */ |
| 887 | #ifdef PA_LSB_IS_NATIVE_ |
| 888 | *converter = Swap24; |
| 889 | #endif |
| 890 | break; |
| 891 | case ASIOSTInt24LSB: |
| 892 | /* src: paInt24, no conversion necessary, possible byte swap */ |
| 893 | #ifdef PA_MSB_IS_NATIVE_ |
| 894 | *converter = Swap24; |
| 895 | #endif |
| 896 | break; |
| 897 | } |
| 898 | } |
| 899 | |
| 900 | |
| 901 | typedef struct PaAsioDeviceInfo |
| 902 | { |
| 903 | PaDeviceInfo commonDeviceInfo; |
| 904 | long minBufferSize; |
| 905 | long maxBufferSize; |
| 906 | long preferredBufferSize; |
| 907 | long bufferGranularity; |
| 908 | |
| 909 | ASIOChannelInfo *asioChannelInfos; |
| 910 | } |
| 911 | PaAsioDeviceInfo; |
| 912 | |
| 913 | |
| 914 | PaError PaAsio_GetAvailableBufferSizes( PaDeviceIndex device, |
| 915 | long *minBufferSizeFrames, long *maxBufferSizeFrames, long *preferredBufferSizeFrames, long *granularity ) |
| 916 | { |
| 917 | PaError result; |
| 918 | PaUtilHostApiRepresentation *hostApi; |
| 919 | PaDeviceIndex hostApiDevice; |
| 920 | |
| 921 | result = PaUtil_GetHostApiRepresentation( &hostApi, paASIO ); |
| 922 | |
| 923 | if( result == paNoError ) |
| 924 | { |
| 925 | result = PaUtil_DeviceIndexToHostApiDeviceIndex( &hostApiDevice, device, hostApi ); |
| 926 | |
| 927 | if( result == paNoError ) |
| 928 | { |
| 929 | PaAsioDeviceInfo *asioDeviceInfo = |
| 930 | (PaAsioDeviceInfo*)hostApi->deviceInfos[hostApiDevice]; |
| 931 | |
| 932 | *minBufferSizeFrames = asioDeviceInfo->minBufferSize; |
| 933 | *maxBufferSizeFrames = asioDeviceInfo->maxBufferSize; |
| 934 | *preferredBufferSizeFrames = asioDeviceInfo->preferredBufferSize; |
| 935 | *granularity = asioDeviceInfo->bufferGranularity; |
| 936 | } |
| 937 | } |
| 938 | |
| 939 | return result; |
| 940 | } |
| 941 | |
| 942 | /* Unload whatever we loaded in LoadAsioDriver(). |
| 943 | */ |
| 944 | static void UnloadAsioDriver( void ) |
| 945 | { |
| 946 | ASIOExit(); |
| 947 | } |
| 948 | |
| 949 | /* |
| 950 | load the asio driver named by <driverName> and return statistics about |
| 951 | the driver in info. If no error occurred, the driver will remain open |
| 952 | and must be closed by the called by calling UnloadAsioDriver() - if an error |
| 953 | is returned the driver will already be unloaded. |
| 954 | */ |
| 955 | static PaError LoadAsioDriver( PaAsioHostApiRepresentation *asioHostApi, const char *driverName, |
| 956 | PaAsioDriverInfo *driverInfo, void *systemSpecific ) |
| 957 | { |
| 958 | PaError result = paNoError; |
| 959 | ASIOError asioError; |
| 960 | int asioIsInitialized = 0; |
| 961 | |
| 962 | if( !asioHostApi->asioDrivers->loadDriver( const_cast<char*>(driverName) ) ) |
| 963 | { |
| 964 | result = paUnanticipatedHostError; |
| 965 | PA_ASIO_SET_LAST_HOST_ERROR( 0, "Failed to load ASIO driver" ); |
| 966 | goto error; |
| 967 | } |
| 968 | |
| 969 | memset( &driverInfo->asioDriverInfo, 0, sizeof(ASIODriverInfo) ); |
| 970 | driverInfo->asioDriverInfo.asioVersion = 2; |
| 971 | driverInfo->asioDriverInfo.sysRef = systemSpecific; |
| 972 | if( (asioError = ASIOInit( &driverInfo->asioDriverInfo )) != ASE_OK ) |
| 973 | { |
| 974 | result = paUnanticipatedHostError; |
| 975 | PA_ASIO_SET_LAST_ASIO_ERROR( asioError ); |
| 976 | goto error; |
| 977 | } |
| 978 | else |
| 979 | { |
| 980 | asioIsInitialized = 1; |
| 981 | } |
| 982 | |
| 983 | if( (asioError = ASIOGetChannels(&driverInfo->inputChannelCount, |
| 984 | &driverInfo->outputChannelCount)) != ASE_OK ) |
| 985 | { |
| 986 | result = paUnanticipatedHostError; |
| 987 | PA_ASIO_SET_LAST_ASIO_ERROR( asioError ); |
| 988 | goto error; |
| 989 | } |
| 990 | |
| 991 | if( (asioError = ASIOGetBufferSize(&driverInfo->bufferMinSize, |
| 992 | &driverInfo->bufferMaxSize, &driverInfo->bufferPreferredSize, |
| 993 | &driverInfo->bufferGranularity)) != ASE_OK ) |
| 994 | { |
| 995 | result = paUnanticipatedHostError; |
| 996 | PA_ASIO_SET_LAST_ASIO_ERROR( asioError ); |
| 997 | goto error; |
| 998 | } |
| 999 | |
| 1000 | if( ASIOOutputReady() == ASE_OK ) |
| 1001 | driverInfo->postOutput = true; |
| 1002 | else |
| 1003 | driverInfo->postOutput = false; |
| 1004 | |
| 1005 | return result; |
| 1006 | |
| 1007 | error: |
| 1008 | if( asioIsInitialized ) |
| 1009 | { |
| 1010 | ASIOExit(); |
| 1011 | } |
| 1012 | |
| 1013 | return result; |
| 1014 | } |
| 1015 | |
| 1016 | |
| 1017 | #define PA_DEFAULTSAMPLERATESEARCHORDER_COUNT_ 13 /* must be the same number of elements as in the array below */ |
| 1018 | static ASIOSampleRate defaultSampleRateSearchOrder_[] |
| 1019 | = {44100.0, 48000.0, 32000.0, 24000.0, 22050.0, 88200.0, 96000.0, |
| 1020 | 192000.0, 16000.0, 12000.0, 11025.0, 9600.0, 8000.0 }; |
| 1021 | |
| 1022 | |
| 1023 | static PaError InitPaDeviceInfoFromAsioDriver( PaAsioHostApiRepresentation *asioHostApi, |
| 1024 | const char *driverName, int driverIndex, |
| 1025 | PaDeviceInfo *deviceInfo, PaAsioDeviceInfo *asioDeviceInfo ) |
| 1026 | { |
| 1027 | PaError result = paNoError; |
| 1028 | |
| 1029 | /* Due to the headless design of the ASIO API, drivers are free to write over data given to them (like M-Audio |
| 1030 | drivers f.i.). This is an attempt to overcome that. */ |
| 1031 | union _tag_local { |
| 1032 | PaAsioDriverInfo info; |
| 1033 | char _padding[4096]; |
| 1034 | } paAsioDriver; |
| 1035 | |
| 1036 | asioDeviceInfo->asioChannelInfos = 0; /* we check this below to handle error cleanup */ |
| 1037 | |
| 1038 | result = LoadAsioDriver( asioHostApi, driverName, &paAsioDriver.info, asioHostApi->systemSpecific ); |
| 1039 | if( result == paNoError ) |
| 1040 | { |
| 1041 | PA_DEBUG(("PaAsio_Initialize: drv:%d name = %s\n", driverIndex,deviceInfo->name)); |
| 1042 | PA_DEBUG(("PaAsio_Initialize: drv:%d inputChannels = %d\n", driverIndex, paAsioDriver.info.inputChannelCount)); |
| 1043 | PA_DEBUG(("PaAsio_Initialize: drv:%d outputChannels = %d\n", driverIndex, paAsioDriver.info.outputChannelCount)); |
| 1044 | PA_DEBUG(("PaAsio_Initialize: drv:%d bufferMinSize = %d\n", driverIndex, paAsioDriver.info.bufferMinSize)); |
| 1045 | PA_DEBUG(("PaAsio_Initialize: drv:%d bufferMaxSize = %d\n", driverIndex, paAsioDriver.info.bufferMaxSize)); |
| 1046 | PA_DEBUG(("PaAsio_Initialize: drv:%d bufferPreferredSize = %d\n", driverIndex, paAsioDriver.info.bufferPreferredSize)); |
| 1047 | PA_DEBUG(("PaAsio_Initialize: drv:%d bufferGranularity = %d\n", driverIndex, paAsioDriver.info.bufferGranularity)); |
| 1048 | |
| 1049 | deviceInfo->maxInputChannels = paAsioDriver.info.inputChannelCount; |
| 1050 | deviceInfo->maxOutputChannels = paAsioDriver.info.outputChannelCount; |
| 1051 | |
| 1052 | deviceInfo->defaultSampleRate = 0.; |
| 1053 | bool foundDefaultSampleRate = false; |
| 1054 | for( int j=0; j < PA_DEFAULTSAMPLERATESEARCHORDER_COUNT_; ++j ) |
| 1055 | { |
| 1056 | ASIOError asioError = ASIOCanSampleRate( defaultSampleRateSearchOrder_[j] ); |
| 1057 | if( asioError != ASE_NoClock && asioError != ASE_NotPresent ) |
| 1058 | { |
| 1059 | deviceInfo->defaultSampleRate = defaultSampleRateSearchOrder_[j]; |
| 1060 | foundDefaultSampleRate = true; |
| 1061 | break; |
| 1062 | } |
| 1063 | } |
| 1064 | |
| 1065 | PA_DEBUG(("PaAsio_Initialize: drv:%d defaultSampleRate = %f\n", driverIndex, deviceInfo->defaultSampleRate)); |
| 1066 | |
| 1067 | if( foundDefaultSampleRate ){ |
| 1068 | |
| 1069 | /* calculate default latency values from bufferPreferredSize |
| 1070 | for default low latency, and bufferMaxSize |
| 1071 | for default high latency. |
| 1072 | use the default sample rate to convert from samples to |
| 1073 | seconds. Without knowing what sample rate the user will |
| 1074 | use this is the best we can do. |
| 1075 | */ |
| 1076 | |
| 1077 | double defaultLowLatency = |
| 1078 | paAsioDriver.info.bufferPreferredSize / deviceInfo->defaultSampleRate; |
| 1079 | |
| 1080 | deviceInfo->defaultLowInputLatency = defaultLowLatency; |
| 1081 | deviceInfo->defaultLowOutputLatency = defaultLowLatency; |
| 1082 | |
| 1083 | double defaultHighLatency = |
| 1084 | paAsioDriver.info.bufferMaxSize / deviceInfo->defaultSampleRate; |
| 1085 | |
| 1086 | if( defaultHighLatency < defaultLowLatency ) |
| 1087 | defaultHighLatency = defaultLowLatency; /* just in case the driver returns something strange */ |
| 1088 | |
| 1089 | deviceInfo->defaultHighInputLatency = defaultHighLatency; |
| 1090 | deviceInfo->defaultHighOutputLatency = defaultHighLatency; |
| 1091 | |
| 1092 | }else{ |
| 1093 | |
| 1094 | deviceInfo->defaultLowInputLatency = 0.; |
| 1095 | deviceInfo->defaultLowOutputLatency = 0.; |
| 1096 | deviceInfo->defaultHighInputLatency = 0.; |
| 1097 | deviceInfo->defaultHighOutputLatency = 0.; |
| 1098 | } |
| 1099 | |
| 1100 | PA_DEBUG(("PaAsio_Initialize: drv:%d defaultLowInputLatency = %f\n", driverIndex, deviceInfo->defaultLowInputLatency)); |
| 1101 | PA_DEBUG(("PaAsio_Initialize: drv:%d defaultLowOutputLatency = %f\n", driverIndex, deviceInfo->defaultLowOutputLatency)); |
| 1102 | PA_DEBUG(("PaAsio_Initialize: drv:%d defaultHighInputLatency = %f\n", driverIndex, deviceInfo->defaultHighInputLatency)); |
| 1103 | PA_DEBUG(("PaAsio_Initialize: drv:%d defaultHighOutputLatency = %f\n", driverIndex, deviceInfo->defaultHighOutputLatency)); |
| 1104 | |
| 1105 | asioDeviceInfo->minBufferSize = paAsioDriver.info.bufferMinSize; |
| 1106 | asioDeviceInfo->maxBufferSize = paAsioDriver.info.bufferMaxSize; |
| 1107 | asioDeviceInfo->preferredBufferSize = paAsioDriver.info.bufferPreferredSize; |
| 1108 | asioDeviceInfo->bufferGranularity = paAsioDriver.info.bufferGranularity; |
| 1109 | |
| 1110 | |
| 1111 | asioDeviceInfo->asioChannelInfos = (ASIOChannelInfo*)PaUtil_GroupAllocateMemory( |
| 1112 | asioHostApi->allocations, |
| 1113 | sizeof(ASIOChannelInfo) * (deviceInfo->maxInputChannels |
| 1114 | + deviceInfo->maxOutputChannels) ); |
| 1115 | if( !asioDeviceInfo->asioChannelInfos ) |
| 1116 | { |
| 1117 | result = paInsufficientMemory; |
| 1118 | goto error_unload; |
| 1119 | } |
| 1120 | |
| 1121 | int a; |
| 1122 | |
| 1123 | for( a=0; a < deviceInfo->maxInputChannels; ++a ){ |
| 1124 | asioDeviceInfo->asioChannelInfos[a].channel = a; |
| 1125 | asioDeviceInfo->asioChannelInfos[a].isInput = ASIOTrue; |
| 1126 | ASIOError asioError = ASIOGetChannelInfo( &asioDeviceInfo->asioChannelInfos[a] ); |
| 1127 | if( asioError != ASE_OK ) |
| 1128 | { |
| 1129 | result = paUnanticipatedHostError; |
| 1130 | PA_ASIO_SET_LAST_ASIO_ERROR( asioError ); |
| 1131 | goto error_unload; |
| 1132 | } |
| 1133 | } |
| 1134 | |
| 1135 | for( a=0; a < deviceInfo->maxOutputChannels; ++a ){ |
| 1136 | int b = deviceInfo->maxInputChannels + a; |
| 1137 | asioDeviceInfo->asioChannelInfos[b].channel = a; |
| 1138 | asioDeviceInfo->asioChannelInfos[b].isInput = ASIOFalse; |
| 1139 | ASIOError asioError = ASIOGetChannelInfo( &asioDeviceInfo->asioChannelInfos[b] ); |
| 1140 | if( asioError != ASE_OK ) |
| 1141 | { |
| 1142 | result = paUnanticipatedHostError; |
| 1143 | PA_ASIO_SET_LAST_ASIO_ERROR( asioError ); |
| 1144 | goto error_unload; |
| 1145 | } |
| 1146 | } |
| 1147 | |
| 1148 | /* unload the driver */ |
| 1149 | UnloadAsioDriver(); |
| 1150 | } |
| 1151 | |
| 1152 | return result; |
| 1153 | |
| 1154 | error_unload: |
| 1155 | UnloadAsioDriver(); |
| 1156 | |
| 1157 | if( asioDeviceInfo->asioChannelInfos ){ |
| 1158 | PaUtil_GroupFreeMemory( asioHostApi->allocations, asioDeviceInfo->asioChannelInfos ); |
| 1159 | asioDeviceInfo->asioChannelInfos = 0; |
| 1160 | } |
| 1161 | |
| 1162 | return result; |
| 1163 | } |
| 1164 | |
| 1165 | |
| 1166 | /* we look up IsDebuggerPresent at runtime incase it isn't present (on Win95 for example) */ |
| 1167 | typedef BOOL (WINAPI *IsDebuggerPresentPtr)(VOID); |
| 1168 | IsDebuggerPresentPtr IsDebuggerPresent_ = 0; |
| 1169 | //FARPROC IsDebuggerPresent_ = 0; // this is the current way to do it apparently according to davidv |
| 1170 | |
| 1171 | PaError PaAsio_Initialize( PaUtilHostApiRepresentation **hostApi, PaHostApiIndex hostApiIndex ) |
| 1172 | { |
| 1173 | PaError result = paNoError; |
| 1174 | int i, driverCount; |
| 1175 | PaAsioHostApiRepresentation *asioHostApi; |
| 1176 | PaAsioDeviceInfo *deviceInfoArray; |
| 1177 | char **names; |
| 1178 | asioHostApi = (PaAsioHostApiRepresentation*)PaUtil_AllocateMemory( sizeof(PaAsioHostApiRepresentation) ); |
| 1179 | if( !asioHostApi ) |
| 1180 | { |
| 1181 | result = paInsufficientMemory; |
| 1182 | goto error; |
| 1183 | } |
| 1184 | |
| 1185 | memset( asioHostApi, 0, sizeof(PaAsioHostApiRepresentation) ); /* ensure all fields are zeroed. especially asioHostApi->allocations */ |
| 1186 | |
| 1187 | /* |
| 1188 | We initialize COM ourselves here and uninitialize it in Terminate(). |
| 1189 | This should be the only COM initialization needed in this module. |
| 1190 | |
| 1191 | The ASIO SDK may also initialize COM but since we want to reduce dependency |
| 1192 | on the ASIO SDK we manage COM initialization ourselves. |
| 1193 | |
| 1194 | There used to be code that initialized COM in other situations |
| 1195 | such as when creating a Stream. This made PA work when calling Pa_CreateStream |
| 1196 | from a non-main thread. However we currently consider initialization |
| 1197 | of COM in non-main threads to be the caller's responsibility. |
| 1198 | */ |
| 1199 | result = PaWinUtil_CoInitialize( paASIO, &asioHostApi->comInitializationResult ); |
| 1200 | if( result != paNoError ) |
| 1201 | { |
| 1202 | goto error; |
| 1203 | } |
| 1204 | |
| 1205 | asioHostApi->asioDrivers = 0; /* avoid surprises in our error handler below */ |
| 1206 | |
| 1207 | asioHostApi->allocations = PaUtil_CreateAllocationGroup(); |
| 1208 | if( !asioHostApi->allocations ) |
| 1209 | { |
| 1210 | result = paInsufficientMemory; |
| 1211 | goto error; |
| 1212 | } |
| 1213 | |
| 1214 | /* Allocate the AsioDrivers() driver list (class from ASIO SDK) */ |
| 1215 | try |
| 1216 | { |
| 1217 | asioHostApi->asioDrivers = new AsioDrivers(); /* invokes CoInitialize(0) in AsioDriverList::AsioDriverList */ |
| 1218 | } |
| 1219 | catch (std::bad_alloc) |
| 1220 | { |
| 1221 | asioHostApi->asioDrivers = 0; |
| 1222 | } |
| 1223 | /* some implementations of new (ie MSVC, see http://support.microsoft.com/?kbid=167733) |
| 1224 | don't throw std::bad_alloc, so we also explicitly test for a null return. */ |
| 1225 | if( asioHostApi->asioDrivers == 0 ) |
| 1226 | { |
| 1227 | result = paInsufficientMemory; |
| 1228 | goto error; |
| 1229 | } |
| 1230 | |
| 1231 | asioDrivers = asioHostApi->asioDrivers; /* keep SDK global in sync until we stop depending on it */ |
| 1232 | |
| 1233 | asioHostApi->systemSpecific = 0; |
| 1234 | asioHostApi->openAsioDeviceIndex = paNoDevice; |
| 1235 | |
| 1236 | *hostApi = &asioHostApi->inheritedHostApiRep; |
| 1237 | (*hostApi)->info.structVersion = 1; |
| 1238 | |
| 1239 | (*hostApi)->info.type = paASIO; |
| 1240 | (*hostApi)->info.name = "ASIO"; |
| 1241 | (*hostApi)->info.deviceCount = 0; |
| 1242 | |
| 1243 | #ifdef WINDOWS |
| 1244 | /* use desktop window as system specific ptr */ |
| 1245 | asioHostApi->systemSpecific = GetDesktopWindow(); |
| 1246 | #endif |
| 1247 | |
| 1248 | /* driverCount is the number of installed drivers - not necessarily |
| 1249 | the number of installed physical devices. */ |
| 1250 | #if MAC |
| 1251 | driverCount = asioHostApi->asioDrivers->getNumFragments(); |
| 1252 | #elif WINDOWS |
| 1253 | driverCount = asioHostApi->asioDrivers->asioGetNumDev(); |
| 1254 | #endif |
| 1255 | |
| 1256 | if( driverCount > 0 ) |
| 1257 | { |
| 1258 | names = GetAsioDriverNames( asioHostApi, asioHostApi->allocations, driverCount ); |
| 1259 | if( !names ) |
| 1260 | { |
| 1261 | result = paInsufficientMemory; |
| 1262 | goto error; |
| 1263 | } |
| 1264 | |
| 1265 | |
| 1266 | /* allocate enough space for all drivers, even if some aren't installed */ |
| 1267 | |
| 1268 | (*hostApi)->deviceInfos = (PaDeviceInfo**)PaUtil_GroupAllocateMemory( |
| 1269 | asioHostApi->allocations, sizeof(PaDeviceInfo*) * driverCount ); |
| 1270 | if( !(*hostApi)->deviceInfos ) |
| 1271 | { |
| 1272 | result = paInsufficientMemory; |
| 1273 | goto error; |
| 1274 | } |
| 1275 | |
| 1276 | /* allocate all device info structs in a contiguous block */ |
| 1277 | deviceInfoArray = (PaAsioDeviceInfo*)PaUtil_GroupAllocateMemory( |
| 1278 | asioHostApi->allocations, sizeof(PaAsioDeviceInfo) * driverCount ); |
| 1279 | if( !deviceInfoArray ) |
| 1280 | { |
| 1281 | result = paInsufficientMemory; |
| 1282 | goto error; |
| 1283 | } |
| 1284 | |
| 1285 | IsDebuggerPresent_ = (IsDebuggerPresentPtr)GetProcAddress( LoadLibraryA( "Kernel32.dll" ), "IsDebuggerPresent" ); |
| 1286 | |
| 1287 | for( i=0; i < driverCount; ++i ) |
| 1288 | { |
| 1289 | PA_DEBUG(("ASIO names[%d]:%s\n",i,names[i])); |
| 1290 | |
| 1291 | // Since portaudio opens ALL ASIO drivers, and no one else does that, |
| 1292 | // we face fact that some drivers were not meant for it, drivers which act |
| 1293 | // like shells on top of REAL drivers, for instance. |
| 1294 | // so we get duplicate handles, locks and other problems. |
| 1295 | // so lets NOT try to load any such wrappers. |
| 1296 | // The ones i [davidv] know of so far are: |
| 1297 | |
| 1298 | if ( strcmp (names[i],"ASIO DirectX Full Duplex Driver") == 0 |
| 1299 | || strcmp (names[i],"ASIO Multimedia Driver") == 0 |
| 1300 | || strncmp(names[i],"Premiere",8) == 0 //"Premiere Elements Windows Sound 1.0" |
| 1301 | || strncmp(names[i],"Adobe",5) == 0 //"Adobe Default Windows Sound 1.5" |
| 1302 | ) |
| 1303 | { |
| 1304 | PA_DEBUG(("BLACKLISTED!!!\n")); |
| 1305 | continue; |
| 1306 | } |
| 1307 | |
| 1308 | |
| 1309 | if( IsDebuggerPresent_ && IsDebuggerPresent_() ) |
| 1310 | { |
| 1311 | /* ASIO Digidesign Driver uses PACE copy protection which quits out |
| 1312 | if a debugger is running. So we don't load it if a debugger is running. */ |
| 1313 | if( strcmp(names[i], "ASIO Digidesign Driver") == 0 ) |
| 1314 | { |
| 1315 | PA_DEBUG(("BLACKLISTED!!! ASIO Digidesign Driver would quit the debugger\n")); |
| 1316 | continue; |
| 1317 | } |
| 1318 | } |
| 1319 | |
| 1320 | |
| 1321 | /* Attempt to init device info from the asio driver... */ |
| 1322 | { |
| 1323 | PaAsioDeviceInfo *asioDeviceInfo = &deviceInfoArray[ (*hostApi)->info.deviceCount ]; |
| 1324 | PaDeviceInfo *deviceInfo = &asioDeviceInfo->commonDeviceInfo; |
| 1325 | |
| 1326 | deviceInfo->structVersion = 2; |
| 1327 | deviceInfo->hostApi = hostApiIndex; |
| 1328 | |
| 1329 | deviceInfo->name = names[i]; |
| 1330 | |
| 1331 | if( InitPaDeviceInfoFromAsioDriver( asioHostApi, names[i], i, deviceInfo, asioDeviceInfo ) == paNoError ) |
| 1332 | { |
| 1333 | (*hostApi)->deviceInfos[ (*hostApi)->info.deviceCount ] = deviceInfo; |
| 1334 | ++(*hostApi)->info.deviceCount; |
| 1335 | } |
| 1336 | else |
| 1337 | { |
| 1338 | PA_DEBUG(("Skipping ASIO device:%s\n",names[i])); |
| 1339 | continue; |
| 1340 | } |
| 1341 | } |
| 1342 | } |
| 1343 | } |
| 1344 | |
| 1345 | if( (*hostApi)->info.deviceCount > 0 ) |
| 1346 | { |
| 1347 | (*hostApi)->info.defaultInputDevice = 0; |
| 1348 | (*hostApi)->info.defaultOutputDevice = 0; |
| 1349 | } |
| 1350 | else |
| 1351 | { |
| 1352 | (*hostApi)->info.defaultInputDevice = paNoDevice; |
| 1353 | (*hostApi)->info.defaultOutputDevice = paNoDevice; |
| 1354 | } |
| 1355 | |
| 1356 | |
| 1357 | (*hostApi)->Terminate = Terminate; |
| 1358 | (*hostApi)->OpenStream = OpenStream; |
| 1359 | (*hostApi)->IsFormatSupported = IsFormatSupported; |
| 1360 | |
| 1361 | PaUtil_InitializeStreamInterface( &asioHostApi->callbackStreamInterface, CloseStream, StartStream, |
| 1362 | StopStream, AbortStream, IsStreamStopped, IsStreamActive, |
| 1363 | GetStreamTime, GetStreamCpuLoad, |
| 1364 | PaUtil_DummyRead, PaUtil_DummyWrite, |
| 1365 | PaUtil_DummyGetReadAvailable, PaUtil_DummyGetWriteAvailable ); |
| 1366 | |
| 1367 | PaUtil_InitializeStreamInterface( &asioHostApi->blockingStreamInterface, CloseStream, StartStream, |
| 1368 | StopStream, AbortStream, IsStreamStopped, IsStreamActive, |
| 1369 | GetStreamTime, PaUtil_DummyGetCpuLoad, |
| 1370 | ReadStream, WriteStream, GetStreamReadAvailable, GetStreamWriteAvailable ); |
| 1371 | |
| 1372 | return result; |
| 1373 | |
| 1374 | error: |
| 1375 | if( asioHostApi ) |
| 1376 | { |
| 1377 | if( asioHostApi->allocations ) |
| 1378 | { |
| 1379 | PaUtil_FreeAllAllocations( asioHostApi->allocations ); |
| 1380 | PaUtil_DestroyAllocationGroup( asioHostApi->allocations ); |
| 1381 | } |
| 1382 | |
| 1383 | delete asioHostApi->asioDrivers; |
| 1384 | asioDrivers = 0; /* keep SDK global in sync until we stop depending on it */ |
| 1385 | |
| 1386 | PaWinUtil_CoUninitialize( paASIO, &asioHostApi->comInitializationResult ); |
| 1387 | |
| 1388 | PaUtil_FreeMemory( asioHostApi ); |
| 1389 | } |
| 1390 | |
| 1391 | return result; |
| 1392 | } |
| 1393 | |
| 1394 | |
| 1395 | static void Terminate( struct PaUtilHostApiRepresentation *hostApi ) |
| 1396 | { |
| 1397 | PaAsioHostApiRepresentation *asioHostApi = (PaAsioHostApiRepresentation*)hostApi; |
| 1398 | |
| 1399 | /* |
| 1400 | IMPLEMENT ME: |
| 1401 | - clean up any resources not handled by the allocation group (need to review if there are any) |
| 1402 | */ |
| 1403 | |
| 1404 | if( asioHostApi->allocations ) |
| 1405 | { |
| 1406 | PaUtil_FreeAllAllocations( asioHostApi->allocations ); |
| 1407 | PaUtil_DestroyAllocationGroup( asioHostApi->allocations ); |
| 1408 | } |
| 1409 | |
| 1410 | delete asioHostApi->asioDrivers; |
| 1411 | asioDrivers = 0; /* keep SDK global in sync until we stop depending on it */ |
| 1412 | |
| 1413 | PaWinUtil_CoUninitialize( paASIO, &asioHostApi->comInitializationResult ); |
| 1414 | |
| 1415 | PaUtil_FreeMemory( asioHostApi ); |
| 1416 | } |
| 1417 | |
| 1418 | |
| 1419 | static PaError IsFormatSupported( struct PaUtilHostApiRepresentation *hostApi, |
| 1420 | const PaStreamParameters *inputParameters, |
| 1421 | const PaStreamParameters *outputParameters, |
| 1422 | double sampleRate ) |
| 1423 | { |
| 1424 | PaError result = paNoError; |
| 1425 | PaAsioHostApiRepresentation *asioHostApi = (PaAsioHostApiRepresentation*)hostApi; |
| 1426 | PaAsioDriverInfo *driverInfo = &asioHostApi->openAsioDriverInfo; |
| 1427 | int inputChannelCount, outputChannelCount; |
| 1428 | PaSampleFormat inputSampleFormat, outputSampleFormat; |
| 1429 | PaDeviceIndex asioDeviceIndex; |
| 1430 | ASIOError asioError; |
| 1431 | |
| 1432 | if( inputParameters && outputParameters ) |
| 1433 | { |
| 1434 | /* full duplex ASIO stream must use the same device for input and output */ |
| 1435 | |
| 1436 | if( inputParameters->device != outputParameters->device ) |
| 1437 | return paBadIODeviceCombination; |
| 1438 | } |
| 1439 | |
| 1440 | if( inputParameters ) |
| 1441 | { |
| 1442 | inputChannelCount = inputParameters->channelCount; |
| 1443 | inputSampleFormat = inputParameters->sampleFormat; |
| 1444 | |
| 1445 | /* all standard sample formats are supported by the buffer adapter, |
| 1446 | this implementation doesn't support any custom sample formats */ |
| 1447 | if( inputSampleFormat & paCustomFormat ) |
| 1448 | return paSampleFormatNotSupported; |
| 1449 | |
| 1450 | /* unless alternate device specification is supported, reject the use of |
| 1451 | paUseHostApiSpecificDeviceSpecification */ |
| 1452 | |
| 1453 | if( inputParameters->device == paUseHostApiSpecificDeviceSpecification ) |
| 1454 | return paInvalidDevice; |
| 1455 | |
| 1456 | asioDeviceIndex = inputParameters->device; |
| 1457 | |
| 1458 | /* validate inputStreamInfo */ |
| 1459 | /** @todo do more validation here */ |
| 1460 | // if( inputParameters->hostApiSpecificStreamInfo ) |
| 1461 | // return paIncompatibleHostApiSpecificStreamInfo; /* this implementation doesn't use custom stream info */ |
| 1462 | } |
| 1463 | else |
| 1464 | { |
| 1465 | inputChannelCount = 0; |
| 1466 | } |
| 1467 | |
| 1468 | if( outputParameters ) |
| 1469 | { |
| 1470 | outputChannelCount = outputParameters->channelCount; |
| 1471 | outputSampleFormat = outputParameters->sampleFormat; |
| 1472 | |
| 1473 | /* all standard sample formats are supported by the buffer adapter, |
| 1474 | this implementation doesn't support any custom sample formats */ |
| 1475 | if( outputSampleFormat & paCustomFormat ) |
| 1476 | return paSampleFormatNotSupported; |
| 1477 | |
| 1478 | /* unless alternate device specification is supported, reject the use of |
| 1479 | paUseHostApiSpecificDeviceSpecification */ |
| 1480 | |
| 1481 | if( outputParameters->device == paUseHostApiSpecificDeviceSpecification ) |
| 1482 | return paInvalidDevice; |
| 1483 | |
| 1484 | asioDeviceIndex = outputParameters->device; |
| 1485 | |
| 1486 | /* validate outputStreamInfo */ |
| 1487 | /** @todo do more validation here */ |
| 1488 | // if( outputParameters->hostApiSpecificStreamInfo ) |
| 1489 | // return paIncompatibleHostApiSpecificStreamInfo; /* this implementation doesn't use custom stream info */ |
| 1490 | } |
| 1491 | else |
| 1492 | { |
| 1493 | outputChannelCount = 0; |
| 1494 | } |
| 1495 | |
| 1496 | |
| 1497 | |
| 1498 | /* if an ASIO device is open we can only get format information for the currently open device */ |
| 1499 | |
| 1500 | if( asioHostApi->openAsioDeviceIndex != paNoDevice |
| 1501 | && asioHostApi->openAsioDeviceIndex != asioDeviceIndex ) |
| 1502 | { |
| 1503 | return paDeviceUnavailable; |
| 1504 | } |
| 1505 | |
| 1506 | |
| 1507 | /* NOTE: we load the driver and use its current settings |
| 1508 | rather than the ones in our device info structure which may be stale */ |
| 1509 | |
| 1510 | /* open the device if it's not already open */ |
| 1511 | if( asioHostApi->openAsioDeviceIndex == paNoDevice ) |
| 1512 | { |
| 1513 | result = LoadAsioDriver( asioHostApi, asioHostApi->inheritedHostApiRep.deviceInfos[ asioDeviceIndex ]->name, |
| 1514 | driverInfo, asioHostApi->systemSpecific ); |
| 1515 | if( result != paNoError ) |
| 1516 | return result; |
| 1517 | } |
| 1518 | |
| 1519 | /* check that input device can support inputChannelCount */ |
| 1520 | if( inputChannelCount > 0 ) |
| 1521 | { |
| 1522 | if( inputChannelCount > driverInfo->inputChannelCount ) |
| 1523 | { |
| 1524 | result = paInvalidChannelCount; |
| 1525 | goto done; |
| 1526 | } |
| 1527 | } |
| 1528 | |
| 1529 | /* check that output device can support outputChannelCount */ |
| 1530 | if( outputChannelCount ) |
| 1531 | { |
| 1532 | if( outputChannelCount > driverInfo->outputChannelCount ) |
| 1533 | { |
| 1534 | result = paInvalidChannelCount; |
| 1535 | goto done; |
| 1536 | } |
| 1537 | } |
| 1538 | |
| 1539 | /* query for sample rate support */ |
| 1540 | asioError = ASIOCanSampleRate( sampleRate ); |
| 1541 | if( asioError == ASE_NoClock || asioError == ASE_NotPresent ) |
| 1542 | { |
| 1543 | result = paInvalidSampleRate; |
| 1544 | goto done; |
| 1545 | } |
| 1546 | |
| 1547 | done: |
| 1548 | /* close the device if it wasn't already open */ |
| 1549 | if( asioHostApi->openAsioDeviceIndex == paNoDevice ) |
| 1550 | { |
| 1551 | UnloadAsioDriver(); /* not sure if we should check for errors here */ |
| 1552 | } |
| 1553 | |
| 1554 | if( result == paNoError ) |
| 1555 | return paFormatIsSupported; |
| 1556 | else |
| 1557 | return result; |
| 1558 | } |
| 1559 | |
| 1560 | |
| 1561 | |
| 1562 | /** A data structure specifically for storing blocking i/o related data. */ |
| 1563 | typedef struct PaAsioStreamBlockingState |
| 1564 | { |
| 1565 | int stopFlag; /**< Flag indicating that block processing is to be stopped. */ |
| 1566 | |
| 1567 | unsigned long writeBuffersRequested; /**< The number of available output buffers, requested by the #WriteStream() function. */ |
| 1568 | unsigned long readFramesRequested; /**< The number of available input frames, requested by the #ReadStream() function. */ |
| 1569 | |
| 1570 | int writeBuffersRequestedFlag; /**< Flag to indicate that #WriteStream() has requested more output buffers to be available. */ |
| 1571 | int readFramesRequestedFlag; /**< Flag to indicate that #ReadStream() requires more input frames to be available. */ |
| 1572 | |
| 1573 | HANDLE writeBuffersReadyEvent; /**< Event to signal that requested output buffers are available. */ |
| 1574 | HANDLE readFramesReadyEvent; /**< Event to signal that requested input frames are available. */ |
| 1575 | |
| 1576 | void *writeRingBufferData; /**< The actual ring buffer memory, used by the output ring buffer. */ |
| 1577 | void *readRingBufferData; /**< The actual ring buffer memory, used by the input ring buffer. */ |
| 1578 | |
| 1579 | PaUtilRingBuffer writeRingBuffer; /**< Frame-aligned blocking i/o ring buffer to store output data (interleaved user format). */ |
| 1580 | PaUtilRingBuffer readRingBuffer; /**< Frame-aligned blocking i/o ring buffer to store input data (interleaved user format). */ |
| 1581 | |
| 1582 | long writeRingBufferInitialFrames; /**< The initial number of silent frames within the output ring buffer. */ |
| 1583 | |
| 1584 | const void **writeStreamBuffer; /**< Temp buffer, used by #WriteStream() for handling non-interleaved data. */ |
| 1585 | void **readStreamBuffer; /**< Temp buffer, used by #ReadStream() for handling non-interleaved data. */ |
| 1586 | |
| 1587 | PaUtilBufferProcessor bufferProcessor; /**< Buffer processor, used to handle the blocking i/o ring buffers. */ |
| 1588 | |
| 1589 | int outputUnderflowFlag; /**< Flag to signal an output underflow from within the callback function. */ |
| 1590 | int inputOverflowFlag; /**< Flag to signal an input overflow from within the callback function. */ |
| 1591 | } |
| 1592 | PaAsioStreamBlockingState; |
| 1593 | |
| 1594 | |
| 1595 | |
| 1596 | /* PaAsioStream - a stream data structure specifically for this implementation */ |
| 1597 | |
| 1598 | typedef struct PaAsioStream |
| 1599 | { |
| 1600 | PaUtilStreamRepresentation streamRepresentation; |
| 1601 | PaUtilCpuLoadMeasurer cpuLoadMeasurer; |
| 1602 | PaUtilBufferProcessor bufferProcessor; |
| 1603 | |
| 1604 | PaAsioHostApiRepresentation *asioHostApi; |
| 1605 | unsigned long framesPerHostCallback; |
| 1606 | |
| 1607 | /* ASIO driver info - these may not be needed for the life of the stream, |
| 1608 | but store them here until we work out how format conversion is going |
| 1609 | to work. */ |
| 1610 | |
| 1611 | ASIOBufferInfo *asioBufferInfos; |
| 1612 | ASIOChannelInfo *asioChannelInfos; |
| 1613 | long asioInputLatencyFrames, asioOutputLatencyFrames; // actual latencies returned by asio |
| 1614 | |
| 1615 | long inputChannelCount, outputChannelCount; |
| 1616 | bool postOutput; |
| 1617 | |
| 1618 | void **bufferPtrs; /* this is carved up for inputBufferPtrs and outputBufferPtrs */ |
| 1619 | void **inputBufferPtrs[2]; |
| 1620 | void **outputBufferPtrs[2]; |
| 1621 | |
| 1622 | PaAsioBufferConverter *inputBufferConverter; |
| 1623 | long inputShift; |
| 1624 | PaAsioBufferConverter *outputBufferConverter; |
| 1625 | long outputShift; |
| 1626 | |
| 1627 | volatile bool stopProcessing; |
| 1628 | int stopPlayoutCount; |
| 1629 | HANDLE completedBuffersPlayedEvent; |
| 1630 | |
| 1631 | bool streamFinishedCallbackCalled; |
| 1632 | int isStopped; |
| 1633 | volatile int isActive; |
| 1634 | volatile bool zeroOutput; /* all future calls to the callback will output silence */ |
| 1635 | |
| 1636 | volatile long reenterCount; |
| 1637 | volatile long reenterError; |
| 1638 | |
| 1639 | PaStreamCallbackFlags callbackFlags; |
| 1640 | |
| 1641 | PaAsioStreamBlockingState *blockingState; /**< Blocking i/o data struct, or NULL when using callback interface. */ |
| 1642 | } |
| 1643 | PaAsioStream; |
| 1644 | |
| 1645 | static PaAsioStream *theAsioStream = 0; /* due to ASIO sdk limitations there can be only one stream */ |
| 1646 | |
| 1647 | |
| 1648 | static void ZeroOutputBuffers( PaAsioStream *stream, long index ) |
| 1649 | { |
| 1650 | int i; |
| 1651 | |
| 1652 | for( i=0; i < stream->outputChannelCount; ++i ) |
| 1653 | { |
| 1654 | void *buffer = stream->asioBufferInfos[ i + stream->inputChannelCount ].buffers[index]; |
| 1655 | |
| 1656 | int bytesPerSample = BytesPerAsioSample( stream->asioChannelInfos[ i + stream->inputChannelCount ].type ); |
| 1657 | |
| 1658 | memset( buffer, 0, stream->framesPerHostCallback * bytesPerSample ); |
| 1659 | } |
| 1660 | } |
| 1661 | |
| 1662 | |
| 1663 | /* return the next power of two >= x. |
| 1664 | Returns the input parameter if it is already a power of two. |
| 1665 | http://stackoverflow.com/questions/364985/algorithm-for-finding-the-smallest-power-of-two-thats-greater-or-equal-to-a-giv |
| 1666 | */ |
| 1667 | static unsigned long NextPowerOfTwo( unsigned long x ) |
| 1668 | { |
| 1669 | --x; |
| 1670 | x |= x >> 1; |
| 1671 | x |= x >> 2; |
| 1672 | x |= x >> 4; |
| 1673 | x |= x >> 8; |
| 1674 | x |= x >> 16; |
| 1675 | /* If you needed to deal with numbers > 2^32 the following would be needed. |
| 1676 | For latencies, we don't deal with values this large. |
| 1677 | x |= x >> 16; |
| 1678 | */ |
| 1679 | |
| 1680 | return x + 1; |
| 1681 | } |
| 1682 | |
| 1683 | |
| 1684 | static unsigned long SelectHostBufferSizeForUnspecifiedUserFramesPerBuffer( |
| 1685 | unsigned long targetBufferingLatencyFrames, PaAsioDriverInfo *driverInfo ) |
| 1686 | { |
| 1687 | /* Choose a host buffer size based only on targetBufferingLatencyFrames and the |
| 1688 | device's supported buffer sizes. Always returns a valid value. |
| 1689 | */ |
| 1690 | |
| 1691 | unsigned long result; |
| 1692 | |
| 1693 | if( targetBufferingLatencyFrames <= (unsigned long)driverInfo->bufferMinSize ) |
| 1694 | { |
| 1695 | result = driverInfo->bufferMinSize; |
| 1696 | } |
| 1697 | else if( targetBufferingLatencyFrames >= (unsigned long)driverInfo->bufferMaxSize ) |
| 1698 | { |
| 1699 | result = driverInfo->bufferMaxSize; |
| 1700 | } |
| 1701 | else |
| 1702 | { |
| 1703 | if( driverInfo->bufferGranularity == 0 ) /* single fixed host buffer size */ |
| 1704 | { |
| 1705 | /* The documentation states that bufferGranularity should be zero |
| 1706 | when bufferMinSize, bufferMaxSize and bufferPreferredSize are the |
| 1707 | same. We assume that is the case. |
| 1708 | */ |
| 1709 | |
| 1710 | result = driverInfo->bufferPreferredSize; |
| 1711 | } |
| 1712 | else if( driverInfo->bufferGranularity == -1 ) /* power-of-two */ |
| 1713 | { |
| 1714 | /* We assume bufferMinSize and bufferMaxSize are powers of two. */ |
| 1715 | |
| 1716 | result = NextPowerOfTwo( targetBufferingLatencyFrames ); |
| 1717 | |
| 1718 | if( result < (unsigned long)driverInfo->bufferMinSize ) |
| 1719 | result = driverInfo->bufferMinSize; |
| 1720 | |
| 1721 | if( result > (unsigned long)driverInfo->bufferMaxSize ) |
| 1722 | result = driverInfo->bufferMaxSize; |
| 1723 | } |
| 1724 | else /* modulo bufferGranularity */ |
| 1725 | { |
| 1726 | /* round up to the next multiple of granularity */ |
| 1727 | unsigned long n = (targetBufferingLatencyFrames + driverInfo->bufferGranularity - 1) |
| 1728 | / driverInfo->bufferGranularity; |
| 1729 | |
| 1730 | result = n * driverInfo->bufferGranularity; |
| 1731 | |
| 1732 | if( result < (unsigned long)driverInfo->bufferMinSize ) |
| 1733 | result = driverInfo->bufferMinSize; |
| 1734 | |
| 1735 | if( result > (unsigned long)driverInfo->bufferMaxSize ) |
| 1736 | result = driverInfo->bufferMaxSize; |
| 1737 | } |
| 1738 | } |
| 1739 | |
| 1740 | return result; |
| 1741 | } |
| 1742 | |
| 1743 | |
| 1744 | static unsigned long SelectHostBufferSizeForSpecifiedUserFramesPerBuffer( |
| 1745 | unsigned long targetBufferingLatencyFrames, unsigned long userFramesPerBuffer, |
| 1746 | PaAsioDriverInfo *driverInfo ) |
| 1747 | { |
| 1748 | /* Select a host buffer size conforming to targetBufferingLatencyFrames |
| 1749 | and the device's supported buffer sizes. |
| 1750 | The return value will always be a multiple of userFramesPerBuffer. |
| 1751 | If a valid buffer size can not be found the function returns 0. |
| 1752 | |
| 1753 | The current implementation uses a simple iterative search for clarity. |
| 1754 | Feel free to suggest a closed form solution. |
| 1755 | */ |
| 1756 | unsigned long result = 0; |
| 1757 | |
| 1758 | assert( userFramesPerBuffer != 0 ); |
| 1759 | |
| 1760 | if( driverInfo->bufferGranularity == 0 ) /* single fixed host buffer size */ |
| 1761 | { |
| 1762 | /* The documentation states that bufferGranularity should be zero |
| 1763 | when bufferMinSize, bufferMaxSize and bufferPreferredSize are the |
| 1764 | same. We assume that is the case. |
| 1765 | */ |
| 1766 | |
| 1767 | if( (driverInfo->bufferPreferredSize % userFramesPerBuffer) == 0 ) |
| 1768 | result = driverInfo->bufferPreferredSize; |
| 1769 | } |
| 1770 | else if( driverInfo->bufferGranularity == -1 ) /* power-of-two */ |
| 1771 | { |
| 1772 | /* We assume bufferMinSize and bufferMaxSize are powers of two. */ |
| 1773 | |
| 1774 | /* Search all powers of two in the range [bufferMinSize,bufferMaxSize] |
| 1775 | for multiples of userFramesPerBuffer. We prefer the first multiple |
| 1776 | that is equal or greater than targetBufferingLatencyFrames, or |
| 1777 | failing that, the largest multiple less than |
| 1778 | targetBufferingLatencyFrames. |
| 1779 | */ |
| 1780 | unsigned long x = (unsigned long)driverInfo->bufferMinSize; |
| 1781 | do { |
| 1782 | if( (x % userFramesPerBuffer) == 0 ) |
| 1783 | { |
| 1784 | /* any multiple of userFramesPerBuffer is acceptable */ |
| 1785 | result = x; |
| 1786 | if( result >= targetBufferingLatencyFrames ) |
| 1787 | break; /* stop. a value >= to targetBufferingLatencyFrames is ideal. */ |
| 1788 | } |
| 1789 | |
| 1790 | x *= 2; |
| 1791 | } while( x <= (unsigned long)driverInfo->bufferMaxSize ); |
| 1792 | } |
| 1793 | else /* modulo granularity */ |
| 1794 | { |
| 1795 | /* We assume bufferMinSize is a multiple of bufferGranularity. */ |
| 1796 | |
| 1797 | /* Search all multiples of bufferGranularity in the range |
| 1798 | [bufferMinSize,bufferMaxSize] for multiples of userFramesPerBuffer. |
| 1799 | We prefer the first multiple that is equal or greater than |
| 1800 | targetBufferingLatencyFrames, or failing that, the largest multiple |
| 1801 | less than targetBufferingLatencyFrames. |
| 1802 | */ |
| 1803 | unsigned long x = (unsigned long)driverInfo->bufferMinSize; |
| 1804 | do { |
| 1805 | if( (x % userFramesPerBuffer) == 0 ) |
| 1806 | { |
| 1807 | /* any multiple of userFramesPerBuffer is acceptable */ |
| 1808 | result = x; |
| 1809 | if( result >= targetBufferingLatencyFrames ) |
| 1810 | break; /* stop. a value >= to targetBufferingLatencyFrames is ideal. */ |
| 1811 | } |
| 1812 | |
| 1813 | x += driverInfo->bufferGranularity; |
| 1814 | } while( x <= (unsigned long)driverInfo->bufferMaxSize ); |
| 1815 | } |
| 1816 | |
| 1817 | return result; |
| 1818 | } |
| 1819 | |
| 1820 | |
| 1821 | static unsigned long SelectHostBufferSize( |
| 1822 | unsigned long targetBufferingLatencyFrames, |
| 1823 | unsigned long userFramesPerBuffer, PaAsioDriverInfo *driverInfo ) |
| 1824 | { |
| 1825 | unsigned long result = 0; |
| 1826 | |
| 1827 | /* We select a host buffer size based on the following requirements |
| 1828 | (in priority order): |
| 1829 | |
| 1830 | 1. The host buffer size must be permissible according to the ASIO |
| 1831 | driverInfo buffer size constraints (min, max, granularity or |
| 1832 | powers-of-two). |
| 1833 | |
| 1834 | 2. If the user specifies a non-zero framesPerBuffer parameter |
| 1835 | (userFramesPerBuffer here) the host buffer should be a multiple of |
| 1836 | this (subject to the constraints in (1) above). |
| 1837 | |
| 1838 | [NOTE: Where no permissible host buffer size is a multiple of |
| 1839 | userFramesPerBuffer, we choose a value as if userFramesPerBuffer were |
| 1840 | zero (i.e. we ignore it). This strategy is open for review ~ perhaps |
| 1841 | there are still "more optimal" buffer sizes related to |
| 1842 | userFramesPerBuffer that we could use.] |
| 1843 | |
| 1844 | 3. The host buffer size should be greater than or equal to |
| 1845 | targetBufferingLatencyFrames, subject to (1) and (2) above. Where it |
| 1846 | is not possible to select a host buffer size equal or greater than |
| 1847 | targetBufferingLatencyFrames, the highest buffer size conforming to |
| 1848 | (1) and (2) should be chosen. |
| 1849 | */ |
| 1850 | |
| 1851 | if( userFramesPerBuffer != 0 ) |
| 1852 | { |
| 1853 | /* userFramesPerBuffer is specified, try to find a buffer size that's |
| 1854 | a multiple of it */ |
| 1855 | result = SelectHostBufferSizeForSpecifiedUserFramesPerBuffer( |
| 1856 | targetBufferingLatencyFrames, userFramesPerBuffer, driverInfo ); |
| 1857 | } |
| 1858 | |
| 1859 | if( result == 0 ) |
| 1860 | { |
| 1861 | /* either userFramesPerBuffer was not specified, or we couldn't find a |
| 1862 | host buffer size that is a multiple of it. Select a host buffer size |
| 1863 | according to targetBufferingLatencyFrames and the ASIO driverInfo |
| 1864 | buffer size constraints. |
| 1865 | */ |
| 1866 | result = SelectHostBufferSizeForUnspecifiedUserFramesPerBuffer( |
| 1867 | targetBufferingLatencyFrames, driverInfo ); |
| 1868 | } |
| 1869 | |
| 1870 | return result; |
| 1871 | } |
| 1872 | |
| 1873 | |
| 1874 | /* returns channelSelectors if present */ |
| 1875 | |
| 1876 | static PaError ValidateAsioSpecificStreamInfo( |
| 1877 | const PaStreamParameters *streamParameters, |
| 1878 | const PaAsioStreamInfo *streamInfo, |
| 1879 | int deviceChannelCount, |
| 1880 | int **channelSelectors ) |
| 1881 | { |
| 1882 | if( streamInfo ) |
| 1883 | { |
| 1884 | if( streamInfo->size != sizeof( PaAsioStreamInfo ) |
| 1885 | || streamInfo->version != 1 ) |
| 1886 | { |
| 1887 | return paIncompatibleHostApiSpecificStreamInfo; |
| 1888 | } |
| 1889 | |
| 1890 | if( streamInfo->flags & paAsioUseChannelSelectors ) |
| 1891 | *channelSelectors = streamInfo->channelSelectors; |
| 1892 | |
| 1893 | if( !(*channelSelectors) ) |
| 1894 | return paIncompatibleHostApiSpecificStreamInfo; |
| 1895 | |
| 1896 | for( int i=0; i < streamParameters->channelCount; ++i ){ |
| 1897 | if( (*channelSelectors)[i] < 0 |
| 1898 | || (*channelSelectors)[i] >= deviceChannelCount ){ |
| 1899 | return paInvalidChannelCount; |
| 1900 | } |
| 1901 | } |
| 1902 | } |
| 1903 | |
| 1904 | return paNoError; |
| 1905 | } |
| 1906 | |
| 1907 | |
| 1908 | static bool IsUsingExternalClockSource() |
| 1909 | { |
| 1910 | bool result = false; |
| 1911 | ASIOError asioError; |
| 1912 | ASIOClockSource clocks[32]; |
| 1913 | long numSources=32; |
| 1914 | |
| 1915 | /* davidv: listing ASIO Clock sources. there is an ongoing investigation by |
| 1916 | me about whether or not to call ASIOSetSampleRate if an external Clock is |
| 1917 | used. A few drivers expected different things here */ |
| 1918 | |
| 1919 | asioError = ASIOGetClockSources(clocks, &numSources); |
| 1920 | if( asioError != ASE_OK ){ |
| 1921 | PA_DEBUG(("ERROR: ASIOGetClockSources: %s\n", PaAsio_GetAsioErrorText(asioError) )); |
| 1922 | }else{ |
| 1923 | PA_DEBUG(("INFO ASIOGetClockSources listing %d clocks\n", numSources )); |
| 1924 | for (int i=0;i<numSources;++i){ |
| 1925 | PA_DEBUG(("ASIOClockSource%d %s current:%d\n", i, clocks[i].name, clocks[i].isCurrentSource )); |
| 1926 | |
| 1927 | if (clocks[i].isCurrentSource) |
| 1928 | result = true; |
| 1929 | } |
| 1930 | } |
| 1931 | |
| 1932 | return result; |
| 1933 | } |
| 1934 | |
| 1935 | |
| 1936 | static PaError ValidateAndSetSampleRate( double sampleRate ) |
| 1937 | { |
| 1938 | PaError result = paNoError; |
| 1939 | ASIOError asioError; |
| 1940 | |
| 1941 | // check that the device supports the requested sample rate |
| 1942 | |
| 1943 | asioError = ASIOCanSampleRate( sampleRate ); |
| 1944 | PA_DEBUG(("ASIOCanSampleRate(%f):%d\n", sampleRate, asioError )); |
| 1945 | |
| 1946 | if( asioError != ASE_OK ) |
| 1947 | { |
| 1948 | result = paInvalidSampleRate; |
| 1949 | PA_DEBUG(("ERROR: ASIOCanSampleRate: %s\n", PaAsio_GetAsioErrorText(asioError) )); |
| 1950 | goto error; |
| 1951 | } |
| 1952 | |
| 1953 | // retrieve the current sample rate, we only change to the requested |
| 1954 | // sample rate if the device is not already in that rate. |
| 1955 | |
| 1956 | ASIOSampleRate oldRate; |
| 1957 | asioError = ASIOGetSampleRate(&oldRate); |
| 1958 | if( asioError != ASE_OK ) |
| 1959 | { |
| 1960 | result = paInvalidSampleRate; |
| 1961 | PA_DEBUG(("ERROR: ASIOGetSampleRate: %s\n", PaAsio_GetAsioErrorText(asioError) )); |
| 1962 | goto error; |
| 1963 | } |
| 1964 | PA_DEBUG(("ASIOGetSampleRate:%f\n",oldRate)); |
| 1965 | |
| 1966 | if (oldRate != sampleRate){ |
| 1967 | /* Set sample rate */ |
| 1968 | |
| 1969 | PA_DEBUG(("before ASIOSetSampleRate(%f)\n",sampleRate)); |
| 1970 | |
| 1971 | /* |
| 1972 | If you have problems with some drivers when externally clocked, |
| 1973 | try switching on the following line and commenting out the one after it. |
| 1974 | See IsUsingExternalClockSource() for more info. |
| 1975 | */ |
| 1976 | //if( IsUsingExternalClockSource() ){ |
| 1977 | if( false ){ |
| 1978 | asioError = ASIOSetSampleRate( 0 ); |
| 1979 | }else{ |
| 1980 | asioError = ASIOSetSampleRate( sampleRate ); |
| 1981 | } |
| 1982 | if( asioError != ASE_OK ) |
| 1983 | { |
| 1984 | result = paInvalidSampleRate; |
| 1985 | PA_DEBUG(("ERROR: ASIOSetSampleRate: %s\n", PaAsio_GetAsioErrorText(asioError) )); |
| 1986 | goto error; |
| 1987 | } |
| 1988 | PA_DEBUG(("after ASIOSetSampleRate(%f)\n",sampleRate)); |
| 1989 | } |
| 1990 | else |
| 1991 | { |
| 1992 | PA_DEBUG(("No Need to change SR\n")); |
| 1993 | } |
| 1994 | |
| 1995 | error: |
| 1996 | return result; |
| 1997 | } |
| 1998 | |
| 1999 | |
| 2000 | /* see pa_hostapi.h for a list of validity guarantees made about OpenStream parameters */ |
| 2001 | |
| 2002 | static PaError OpenStream( struct PaUtilHostApiRepresentation *hostApi, |
| 2003 | PaStream** s, |
| 2004 | const PaStreamParameters *inputParameters, |
| 2005 | const PaStreamParameters *outputParameters, |
| 2006 | double sampleRate, |
| 2007 | unsigned long framesPerBuffer, |
| 2008 | PaStreamFlags streamFlags, |
| 2009 | PaStreamCallback *streamCallback, |
| 2010 | void *userData ) |
| 2011 | { |
| 2012 | PaError result = paNoError; |
| 2013 | PaAsioHostApiRepresentation *asioHostApi = (PaAsioHostApiRepresentation*)hostApi; |
| 2014 | PaAsioStream *stream = 0; |
| 2015 | PaAsioStreamInfo *inputStreamInfo, *outputStreamInfo; |
| 2016 | unsigned long framesPerHostBuffer; |
| 2017 | int inputChannelCount, outputChannelCount; |
| 2018 | PaSampleFormat inputSampleFormat, outputSampleFormat; |
| 2019 | PaSampleFormat hostInputSampleFormat, hostOutputSampleFormat; |
| 2020 | unsigned long suggestedInputLatencyFrames; |
| 2021 | unsigned long suggestedOutputLatencyFrames; |
| 2022 | PaDeviceIndex asioDeviceIndex; |
| 2023 | ASIOError asioError; |
| 2024 | int asioIsInitialized = 0; |
| 2025 | int asioBuffersCreated = 0; |
| 2026 | int completedBuffersPlayedEventInited = 0; |
| 2027 | int i; |
| 2028 | PaAsioDriverInfo *driverInfo; |
| 2029 | int *inputChannelSelectors = 0; |
| 2030 | int *outputChannelSelectors = 0; |
| 2031 | |
| 2032 | /* Are we using blocking i/o interface? */ |
| 2033 | int usingBlockingIo = ( !streamCallback ) ? TRUE : FALSE; |
| 2034 | /* Blocking i/o stuff */ |
| 2035 | long lBlockingBufferSize = 0; /* Desired ring buffer size in samples. */ |
| 2036 | long lBlockingBufferSizePow2 = 0; /* Power-of-2 rounded ring buffer size. */ |
| 2037 | long lBytesPerFrame = 0; /* Number of bytes per input/output frame. */ |
| 2038 | int blockingWriteBuffersReadyEventInitialized = 0; /* Event init flag. */ |
| 2039 | int blockingReadFramesReadyEventInitialized = 0; /* Event init flag. */ |
| 2040 | |
| 2041 | int callbackBufferProcessorInited = FALSE; |
| 2042 | int blockingBufferProcessorInited = FALSE; |
| 2043 | |
| 2044 | /* unless we move to using lower level ASIO calls, we can only have |
| 2045 | one device open at a time */ |
| 2046 | if( asioHostApi->openAsioDeviceIndex != paNoDevice ) |
| 2047 | { |
| 2048 | PA_DEBUG(("OpenStream paDeviceUnavailable\n")); |
| 2049 | return paDeviceUnavailable; |
| 2050 | } |
| 2051 | |
| 2052 | assert( theAsioStream == 0 ); |
| 2053 | |
| 2054 | if( inputParameters && outputParameters ) |
| 2055 | { |
| 2056 | /* full duplex ASIO stream must use the same device for input and output */ |
| 2057 | |
| 2058 | if( inputParameters->device != outputParameters->device ) |
| 2059 | { |
| 2060 | PA_DEBUG(("OpenStream paBadIODeviceCombination\n")); |
| 2061 | return paBadIODeviceCombination; |
| 2062 | } |
| 2063 | } |
| 2064 | |
| 2065 | if( inputParameters ) |
| 2066 | { |
| 2067 | inputChannelCount = inputParameters->channelCount; |
| 2068 | inputSampleFormat = inputParameters->sampleFormat; |
| 2069 | suggestedInputLatencyFrames = (unsigned long)((inputParameters->suggestedLatency * sampleRate)+0.5f); |
| 2070 | |
| 2071 | /* unless alternate device specification is supported, reject the use of |
| 2072 | paUseHostApiSpecificDeviceSpecification */ |
| 2073 | if( inputParameters->device == paUseHostApiSpecificDeviceSpecification ) |
| 2074 | return paInvalidDevice; |
| 2075 | |
| 2076 | asioDeviceIndex = inputParameters->device; |
| 2077 | |
| 2078 | PaAsioDeviceInfo *asioDeviceInfo = (PaAsioDeviceInfo*)hostApi->deviceInfos[asioDeviceIndex]; |
| 2079 | |
| 2080 | /* validate hostApiSpecificStreamInfo */ |
| 2081 | inputStreamInfo = (PaAsioStreamInfo*)inputParameters->hostApiSpecificStreamInfo; |
| 2082 | result = ValidateAsioSpecificStreamInfo( inputParameters, inputStreamInfo, |
| 2083 | asioDeviceInfo->commonDeviceInfo.maxInputChannels, |
| 2084 | &inputChannelSelectors |
| 2085 | ); |
| 2086 | if( result != paNoError ) return result; |
| 2087 | } |
| 2088 | else |
| 2089 | { |
| 2090 | inputChannelCount = 0; |
| 2091 | inputSampleFormat = 0; |
| 2092 | suggestedInputLatencyFrames = 0; |
| 2093 | } |
| 2094 | |
| 2095 | if( outputParameters ) |
| 2096 | { |
| 2097 | outputChannelCount = outputParameters->channelCount; |
| 2098 | outputSampleFormat = outputParameters->sampleFormat; |
| 2099 | suggestedOutputLatencyFrames = (unsigned long)((outputParameters->suggestedLatency * sampleRate)+0.5f); |
| 2100 | |
| 2101 | /* unless alternate device specification is supported, reject the use of |
| 2102 | paUseHostApiSpecificDeviceSpecification */ |
| 2103 | if( outputParameters->device == paUseHostApiSpecificDeviceSpecification ) |
| 2104 | return paInvalidDevice; |
| 2105 | |
| 2106 | asioDeviceIndex = outputParameters->device; |
| 2107 | |
| 2108 | PaAsioDeviceInfo *asioDeviceInfo = (PaAsioDeviceInfo*)hostApi->deviceInfos[asioDeviceIndex]; |
| 2109 | |
| 2110 | /* validate hostApiSpecificStreamInfo */ |
| 2111 | outputStreamInfo = (PaAsioStreamInfo*)outputParameters->hostApiSpecificStreamInfo; |
| 2112 | result = ValidateAsioSpecificStreamInfo( outputParameters, outputStreamInfo, |
| 2113 | asioDeviceInfo->commonDeviceInfo.maxOutputChannels, |
| 2114 | &outputChannelSelectors |
| 2115 | ); |
| 2116 | if( result != paNoError ) return result; |
| 2117 | } |
| 2118 | else |
| 2119 | { |
| 2120 | outputChannelCount = 0; |
| 2121 | outputSampleFormat = 0; |
| 2122 | suggestedOutputLatencyFrames = 0; |
| 2123 | } |
| 2124 | |
| 2125 | driverInfo = &asioHostApi->openAsioDriverInfo; |
| 2126 | |
| 2127 | /* NOTE: we load the driver and use its current settings |
| 2128 | rather than the ones in our device info structure which may be stale */ |
| 2129 | |
| 2130 | result = LoadAsioDriver( asioHostApi, asioHostApi->inheritedHostApiRep.deviceInfos[ asioDeviceIndex ]->name, |
| 2131 | driverInfo, asioHostApi->systemSpecific ); |
| 2132 | if( result == paNoError ) |
| 2133 | asioIsInitialized = 1; |
| 2134 | else{ |
| 2135 | PA_DEBUG(("OpenStream ERROR1 - LoadAsioDriver returned %d\n", result)); |
| 2136 | goto error; |
| 2137 | } |
| 2138 | |
| 2139 | /* check that input device can support inputChannelCount */ |
| 2140 | if( inputChannelCount > 0 ) |
| 2141 | { |
| 2142 | if( inputChannelCount > driverInfo->inputChannelCount ) |
| 2143 | { |
| 2144 | result = paInvalidChannelCount; |
| 2145 | PA_DEBUG(("OpenStream ERROR2\n")); |
| 2146 | goto error; |
| 2147 | } |
| 2148 | } |
| 2149 | |
| 2150 | /* check that output device can support outputChannelCount */ |
| 2151 | if( outputChannelCount ) |
| 2152 | { |
| 2153 | if( outputChannelCount > driverInfo->outputChannelCount ) |
| 2154 | { |
| 2155 | result = paInvalidChannelCount; |
| 2156 | PA_DEBUG(("OpenStream ERROR3\n")); |
| 2157 | goto error; |
| 2158 | } |
| 2159 | } |
| 2160 | |
| 2161 | result = ValidateAndSetSampleRate( sampleRate ); |
| 2162 | if( result != paNoError ) |
| 2163 | goto error; |
| 2164 | |
| 2165 | /* |
| 2166 | IMPLEMENT ME: |
| 2167 | - if a full duplex stream is requested, check that the combination |
| 2168 | of input and output parameters is supported |
| 2169 | */ |
| 2170 | |
| 2171 | /* validate platform specific flags */ |
| 2172 | if( (streamFlags & paPlatformSpecificFlags) != 0 ){ |
| 2173 | PA_DEBUG(("OpenStream invalid flags!!\n")); |
| 2174 | return paInvalidFlag; /* unexpected platform specific flag */ |
| 2175 | } |
| 2176 | |
| 2177 | |
| 2178 | stream = (PaAsioStream*)PaUtil_AllocateMemory( sizeof(PaAsioStream) ); |
| 2179 | if( !stream ) |
| 2180 | { |
| 2181 | result = paInsufficientMemory; |
| 2182 | PA_DEBUG(("OpenStream ERROR5\n")); |
| 2183 | goto error; |
| 2184 | } |
| 2185 | stream->blockingState = NULL; /* Blocking i/o not initialized, yet. */ |
| 2186 | |
| 2187 | |
| 2188 | stream->completedBuffersPlayedEvent = CreateEvent( NULL, TRUE, FALSE, NULL ); |
| 2189 | if( stream->completedBuffersPlayedEvent == NULL ) |
| 2190 | { |
| 2191 | result = paUnanticipatedHostError; |
| 2192 | PA_ASIO_SET_LAST_SYSTEM_ERROR( GetLastError() ); |
| 2193 | PA_DEBUG(("OpenStream ERROR6\n")); |
| 2194 | goto error; |
| 2195 | } |
| 2196 | completedBuffersPlayedEventInited = 1; |
| 2197 | |
| 2198 | |
| 2199 | stream->asioBufferInfos = 0; /* for deallocation in error */ |
| 2200 | stream->asioChannelInfos = 0; /* for deallocation in error */ |
| 2201 | stream->bufferPtrs = 0; /* for deallocation in error */ |
| 2202 | |
| 2203 | /* Using blocking i/o interface... */ |
| 2204 | if( usingBlockingIo ) |
| 2205 | { |
| 2206 | /* Blocking i/o is implemented by running callback mode, using a special blocking i/o callback. */ |
| 2207 | streamCallback = BlockingIoPaCallback; /* Setup PA to use the ASIO blocking i/o callback. */ |
| 2208 | userData = &theAsioStream; /* The callback user data will be the PA ASIO stream. */ |
| 2209 | PaUtil_InitializeStreamRepresentation( &stream->streamRepresentation, |
| 2210 | &asioHostApi->blockingStreamInterface, streamCallback, userData ); |
| 2211 | } |
| 2212 | else /* Using callback interface... */ |
| 2213 | { |
| 2214 | PaUtil_InitializeStreamRepresentation( &stream->streamRepresentation, |
| 2215 | &asioHostApi->callbackStreamInterface, streamCallback, userData ); |
| 2216 | } |
| 2217 | |
| 2218 | |
| 2219 | PaUtil_InitializeCpuLoadMeasurer( &stream->cpuLoadMeasurer, sampleRate ); |
| 2220 | |
| 2221 | |
| 2222 | stream->asioBufferInfos = (ASIOBufferInfo*)PaUtil_AllocateMemory( |
| 2223 | sizeof(ASIOBufferInfo) * (inputChannelCount + outputChannelCount) ); |
| 2224 | if( !stream->asioBufferInfos ) |
| 2225 | { |
| 2226 | result = paInsufficientMemory; |
| 2227 | PA_DEBUG(("OpenStream ERROR7\n")); |
| 2228 | goto error; |
| 2229 | } |
| 2230 | |
| 2231 | |
| 2232 | for( i=0; i < inputChannelCount; ++i ) |
| 2233 | { |
| 2234 | ASIOBufferInfo *info = &stream->asioBufferInfos[i]; |
| 2235 | |
| 2236 | info->isInput = ASIOTrue; |
| 2237 | |
| 2238 | if( inputChannelSelectors ){ |
| 2239 | // inputChannelSelectors values have already been validated in |
| 2240 | // ValidateAsioSpecificStreamInfo() above |
| 2241 | info->channelNum = inputChannelSelectors[i]; |
| 2242 | }else{ |
| 2243 | info->channelNum = i; |
| 2244 | } |
| 2245 | |
| 2246 | info->buffers[0] = info->buffers[1] = 0; |
| 2247 | } |
| 2248 | |
| 2249 | for( i=0; i < outputChannelCount; ++i ){ |
| 2250 | ASIOBufferInfo *info = &stream->asioBufferInfos[inputChannelCount+i]; |
| 2251 | |
| 2252 | info->isInput = ASIOFalse; |
| 2253 | |
| 2254 | if( outputChannelSelectors ){ |
| 2255 | // outputChannelSelectors values have already been validated in |
| 2256 | // ValidateAsioSpecificStreamInfo() above |
| 2257 | info->channelNum = outputChannelSelectors[i]; |
| 2258 | }else{ |
| 2259 | info->channelNum = i; |
| 2260 | } |
| 2261 | |
| 2262 | info->buffers[0] = info->buffers[1] = 0; |
| 2263 | } |
| 2264 | |
| 2265 | |
| 2266 | /* Using blocking i/o interface... */ |
| 2267 | if( usingBlockingIo ) |
| 2268 | { |
| 2269 | /** @todo REVIEW selection of host buffer size for blocking i/o */ |
| 2270 | |
| 2271 | framesPerHostBuffer = SelectHostBufferSize( 0, framesPerBuffer, driverInfo ); |
| 2272 | |
| 2273 | } |
| 2274 | else /* Using callback interface... */ |
| 2275 | { |
| 2276 | /* Select the host buffer size based on user framesPerBuffer and the |
| 2277 | maximum of suggestedInputLatencyFrames and |
| 2278 | suggestedOutputLatencyFrames. |
| 2279 | |
| 2280 | We should subtract any fixed known driver latency from |
| 2281 | suggestedLatencyFrames before computing the host buffer size. |
| 2282 | However, the ASIO API doesn't provide a method for determining fixed |
| 2283 | latencies independent of the host buffer size. ASIOGetLatencies() |
| 2284 | only returns latencies after the buffer size has been configured, so |
| 2285 | we can't reliably use it to determine fixed latencies here. |
| 2286 | |
| 2287 | We could set the preferred buffer size and then subtract it from |
| 2288 | the values returned from ASIOGetLatencies, but this would not be 100% |
| 2289 | reliable, so we don't do it. |
| 2290 | */ |
| 2291 | |
| 2292 | unsigned long targetBufferingLatencyFrames = |
| 2293 | (( suggestedInputLatencyFrames > suggestedOutputLatencyFrames ) |
| 2294 | ? suggestedInputLatencyFrames |
| 2295 | : suggestedOutputLatencyFrames); |
| 2296 | |
| 2297 | framesPerHostBuffer = SelectHostBufferSize( targetBufferingLatencyFrames, |
| 2298 | framesPerBuffer, driverInfo ); |
| 2299 | } |
| 2300 | |
| 2301 | |
| 2302 | PA_DEBUG(("PaAsioOpenStream: framesPerHostBuffer :%d\n", framesPerHostBuffer)); |
| 2303 | |
| 2304 | asioError = ASIOCreateBuffers( stream->asioBufferInfos, |
| 2305 | inputChannelCount+outputChannelCount, |
| 2306 | framesPerHostBuffer, &asioCallbacks_ ); |
| 2307 | |
| 2308 | if( asioError != ASE_OK |
| 2309 | && framesPerHostBuffer != (unsigned long)driverInfo->bufferPreferredSize ) |
| 2310 | { |
| 2311 | PA_DEBUG(("ERROR: ASIOCreateBuffers: %s\n", PaAsio_GetAsioErrorText(asioError) )); |
| 2312 | /* |
| 2313 | Some buggy drivers (like the Hoontech DSP24) give incorrect |
| 2314 | [min, preferred, max] values They should work with the preferred size |
| 2315 | value, thus if Pa_ASIO_CreateBuffers fails with the hostBufferSize |
| 2316 | computed in SelectHostBufferSize, we try again with the preferred size. |
| 2317 | */ |
| 2318 | |
| 2319 | framesPerHostBuffer = driverInfo->bufferPreferredSize; |
| 2320 | |
| 2321 | PA_DEBUG(("PaAsioOpenStream: CORRECTED framesPerHostBuffer :%d\n", framesPerHostBuffer)); |
| 2322 | |
| 2323 | ASIOError asioError2 = ASIOCreateBuffers( stream->asioBufferInfos, |
| 2324 | inputChannelCount+outputChannelCount, |
| 2325 | framesPerHostBuffer, &asioCallbacks_ ); |
| 2326 | if( asioError2 == ASE_OK ) |
| 2327 | asioError = ASE_OK; |
| 2328 | } |
| 2329 | |
| 2330 | if( asioError != ASE_OK ) |
| 2331 | { |
| 2332 | result = paUnanticipatedHostError; |
| 2333 | PA_ASIO_SET_LAST_ASIO_ERROR( asioError ); |
| 2334 | PA_DEBUG(("OpenStream ERROR9\n")); |
| 2335 | goto error; |
| 2336 | } |
| 2337 | |
| 2338 | asioBuffersCreated = 1; |
| 2339 | |
| 2340 | stream->asioChannelInfos = (ASIOChannelInfo*)PaUtil_AllocateMemory( |
| 2341 | sizeof(ASIOChannelInfo) * (inputChannelCount + outputChannelCount) ); |
| 2342 | if( !stream->asioChannelInfos ) |
| 2343 | { |
| 2344 | result = paInsufficientMemory; |
| 2345 | PA_DEBUG(("OpenStream ERROR10\n")); |
| 2346 | goto error; |
| 2347 | } |
| 2348 | |
| 2349 | for( i=0; i < inputChannelCount + outputChannelCount; ++i ) |
| 2350 | { |
| 2351 | stream->asioChannelInfos[i].channel = stream->asioBufferInfos[i].channelNum; |
| 2352 | stream->asioChannelInfos[i].isInput = stream->asioBufferInfos[i].isInput; |
| 2353 | asioError = ASIOGetChannelInfo( &stream->asioChannelInfos[i] ); |
| 2354 | if( asioError != ASE_OK ) |
| 2355 | { |
| 2356 | result = paUnanticipatedHostError; |
| 2357 | PA_ASIO_SET_LAST_ASIO_ERROR( asioError ); |
| 2358 | PA_DEBUG(("OpenStream ERROR11\n")); |
| 2359 | goto error; |
| 2360 | } |
| 2361 | } |
| 2362 | |
| 2363 | stream->bufferPtrs = (void**)PaUtil_AllocateMemory( |
| 2364 | 2 * sizeof(void*) * (inputChannelCount + outputChannelCount) ); |
| 2365 | if( !stream->bufferPtrs ) |
| 2366 | { |
| 2367 | result = paInsufficientMemory; |
| 2368 | PA_DEBUG(("OpenStream ERROR12\n")); |
| 2369 | goto error; |
| 2370 | } |
| 2371 | |
| 2372 | if( inputChannelCount > 0 ) |
| 2373 | { |
| 2374 | stream->inputBufferPtrs[0] = stream-> bufferPtrs; |
| 2375 | stream->inputBufferPtrs[1] = &stream->bufferPtrs[inputChannelCount]; |
| 2376 | |
| 2377 | for( i=0; i<inputChannelCount; ++i ) |
| 2378 | { |
| 2379 | stream->inputBufferPtrs[0][i] = stream->asioBufferInfos[i].buffers[0]; |
| 2380 | stream->inputBufferPtrs[1][i] = stream->asioBufferInfos[i].buffers[1]; |
| 2381 | } |
| 2382 | } |
| 2383 | else |
| 2384 | { |
| 2385 | stream->inputBufferPtrs[0] = 0; |
| 2386 | stream->inputBufferPtrs[1] = 0; |
| 2387 | } |
| 2388 | |
| 2389 | if( outputChannelCount > 0 ) |
| 2390 | { |
| 2391 | stream->outputBufferPtrs[0] = &stream->bufferPtrs[inputChannelCount*2]; |
| 2392 | stream->outputBufferPtrs[1] = &stream->bufferPtrs[inputChannelCount*2 + outputChannelCount]; |
| 2393 | |
| 2394 | for( i=0; i<outputChannelCount; ++i ) |
| 2395 | { |
| 2396 | stream->outputBufferPtrs[0][i] = stream->asioBufferInfos[inputChannelCount+i].buffers[0]; |
| 2397 | stream->outputBufferPtrs[1][i] = stream->asioBufferInfos[inputChannelCount+i].buffers[1]; |
| 2398 | } |
| 2399 | } |
| 2400 | else |
| 2401 | { |
| 2402 | stream->outputBufferPtrs[0] = 0; |
| 2403 | stream->outputBufferPtrs[1] = 0; |
| 2404 | } |
| 2405 | |
| 2406 | if( inputChannelCount > 0 ) |
| 2407 | { |
| 2408 | /* FIXME: assume all channels use the same type for now |
| 2409 | |
| 2410 | see: "ASIO devices with multiple sample formats are unsupported" |
| 2411 | http://www.portaudio.com/trac/ticket/106 |
| 2412 | */ |
| 2413 | ASIOSampleType inputType = stream->asioChannelInfos[0].type; |
| 2414 | |
| 2415 | PA_DEBUG(("ASIO Input type:%d",inputType)); |
| 2416 | AsioSampleTypeLOG(inputType); |
| 2417 | hostInputSampleFormat = AsioSampleTypeToPaNativeSampleFormat( inputType ); |
| 2418 | |
| 2419 | SelectAsioToPaConverter( inputType, &stream->inputBufferConverter, &stream->inputShift ); |
| 2420 | } |
| 2421 | else |
| 2422 | { |
| 2423 | hostInputSampleFormat = 0; |
| 2424 | stream->inputBufferConverter = 0; |
| 2425 | } |
| 2426 | |
| 2427 | if( outputChannelCount > 0 ) |
| 2428 | { |
| 2429 | /* FIXME: assume all channels use the same type for now |
| 2430 | |
| 2431 | see: "ASIO devices with multiple sample formats are unsupported" |
| 2432 | http://www.portaudio.com/trac/ticket/106 |
| 2433 | */ |
| 2434 | ASIOSampleType outputType = stream->asioChannelInfos[inputChannelCount].type; |
| 2435 | |
| 2436 | PA_DEBUG(("ASIO Output type:%d",outputType)); |
| 2437 | AsioSampleTypeLOG(outputType); |
| 2438 | hostOutputSampleFormat = AsioSampleTypeToPaNativeSampleFormat( outputType ); |
| 2439 | |
| 2440 | SelectPaToAsioConverter( outputType, &stream->outputBufferConverter, &stream->outputShift ); |
| 2441 | } |
| 2442 | else |
| 2443 | { |
| 2444 | hostOutputSampleFormat = 0; |
| 2445 | stream->outputBufferConverter = 0; |
| 2446 | } |
| 2447 | |
| 2448 | /* Values returned by ASIOGetLatencies() include the latency introduced by |
| 2449 | the ASIO double buffer. */ |
| 2450 | ASIOGetLatencies( &stream->asioInputLatencyFrames, &stream->asioOutputLatencyFrames ); |
| 2451 | |
| 2452 | |
| 2453 | /* Using blocking i/o interface... */ |
| 2454 | if( usingBlockingIo ) |
| 2455 | { |
| 2456 | /* Allocate the blocking i/o input ring buffer memory. */ |
| 2457 | stream->blockingState = (PaAsioStreamBlockingState*)PaUtil_AllocateMemory( sizeof(PaAsioStreamBlockingState) ); |
| 2458 | if( !stream->blockingState ) |
| 2459 | { |
| 2460 | result = paInsufficientMemory; |
| 2461 | PA_DEBUG(("ERROR! Blocking i/o interface struct allocation failed in OpenStream()\n")); |
| 2462 | goto error; |
| 2463 | } |
| 2464 | |
| 2465 | /* Initialize blocking i/o interface struct. */ |
| 2466 | stream->blockingState->readFramesReadyEvent = NULL; /* Uninitialized, yet. */ |
| 2467 | stream->blockingState->writeBuffersReadyEvent = NULL; /* Uninitialized, yet. */ |
| 2468 | stream->blockingState->readRingBufferData = NULL; /* Uninitialized, yet. */ |
| 2469 | stream->blockingState->writeRingBufferData = NULL; /* Uninitialized, yet. */ |
| 2470 | stream->blockingState->readStreamBuffer = NULL; /* Uninitialized, yet. */ |
| 2471 | stream->blockingState->writeStreamBuffer = NULL; /* Uninitialized, yet. */ |
| 2472 | stream->blockingState->stopFlag = TRUE; /* Not started, yet. */ |
| 2473 | |
| 2474 | |
| 2475 | /* If the user buffer is unspecified */ |
| 2476 | if( framesPerBuffer == paFramesPerBufferUnspecified ) |
| 2477 | { |
| 2478 | /* Make the user buffer the same size as the host buffer. */ |
| 2479 | framesPerBuffer = framesPerHostBuffer; |
| 2480 | } |
| 2481 | |
| 2482 | |
| 2483 | /* Initialize callback buffer processor. */ |
| 2484 | result = PaUtil_InitializeBufferProcessor( &stream->bufferProcessor , |
| 2485 | inputChannelCount , |
| 2486 | inputSampleFormat & ~paNonInterleaved , /* Ring buffer. */ |
| 2487 | (hostInputSampleFormat | paNonInterleaved), /* Host format. */ |
| 2488 | outputChannelCount , |
| 2489 | outputSampleFormat & ~paNonInterleaved, /* Ring buffer. */ |
| 2490 | (hostOutputSampleFormat | paNonInterleaved), /* Host format. */ |
| 2491 | sampleRate , |
| 2492 | streamFlags , |
| 2493 | framesPerBuffer , /* Frames per ring buffer block. */ |
| 2494 | framesPerHostBuffer , /* Frames per asio buffer. */ |
| 2495 | paUtilFixedHostBufferSize , |
| 2496 | streamCallback , |
| 2497 | userData ); |
| 2498 | if( result != paNoError ){ |
| 2499 | PA_DEBUG(("OpenStream ERROR13\n")); |
| 2500 | goto error; |
| 2501 | } |
| 2502 | callbackBufferProcessorInited = TRUE; |
| 2503 | |
| 2504 | /* Initialize the blocking i/o buffer processor. */ |
| 2505 | result = PaUtil_InitializeBufferProcessor(&stream->blockingState->bufferProcessor, |
| 2506 | inputChannelCount , |
| 2507 | inputSampleFormat , /* User format. */ |
| 2508 | inputSampleFormat & ~paNonInterleaved , /* Ring buffer. */ |
| 2509 | outputChannelCount , |
| 2510 | outputSampleFormat , /* User format. */ |
| 2511 | outputSampleFormat & ~paNonInterleaved, /* Ring buffer. */ |
| 2512 | sampleRate , |
| 2513 | paClipOff | paDitherOff , /* Don't use dither nor clipping. */ |
| 2514 | framesPerBuffer , /* Frames per user buffer. */ |
| 2515 | framesPerBuffer , /* Frames per ring buffer block. */ |
| 2516 | paUtilBoundedHostBufferSize , |
| 2517 | NULL, NULL );/* No callback! */ |
| 2518 | if( result != paNoError ){ |
| 2519 | PA_DEBUG(("ERROR! Blocking i/o buffer processor initialization failed in OpenStream()\n")); |
| 2520 | goto error; |
| 2521 | } |
| 2522 | blockingBufferProcessorInited = TRUE; |
| 2523 | |
| 2524 | /* If input is requested. */ |
| 2525 | if( inputChannelCount ) |
| 2526 | { |
| 2527 | /* Create the callback sync-event. */ |
| 2528 | stream->blockingState->readFramesReadyEvent = CreateEvent( NULL, FALSE, FALSE, NULL ); |
| 2529 | if( stream->blockingState->readFramesReadyEvent == NULL ) |
| 2530 | { |
| 2531 | result = paUnanticipatedHostError; |
| 2532 | PA_ASIO_SET_LAST_SYSTEM_ERROR( GetLastError() ); |
| 2533 | PA_DEBUG(("ERROR! Blocking i/o \"read frames ready\" event creation failed in OpenStream()\n")); |
| 2534 | goto error; |
| 2535 | } |
| 2536 | blockingReadFramesReadyEventInitialized = 1; |
| 2537 | |
| 2538 | |
| 2539 | /* Create pointer buffer to access non-interleaved data in ReadStream() */ |
| 2540 | stream->blockingState->readStreamBuffer = (void**)PaUtil_AllocateMemory( sizeof(void*) * inputChannelCount ); |
| 2541 | if( !stream->blockingState->readStreamBuffer ) |
| 2542 | { |
| 2543 | result = paInsufficientMemory; |
| 2544 | PA_DEBUG(("ERROR! Blocking i/o read stream buffer allocation failed in OpenStream()\n")); |
| 2545 | goto error; |
| 2546 | } |
| 2547 | |
| 2548 | /* The ring buffer should store as many data blocks as needed |
| 2549 | to achieve the requested latency. Whereas it must be large |
| 2550 | enough to store at least two complete data blocks. |
| 2551 | |
| 2552 | 1) Determine the amount of latency to be added to the |
| 2553 | preferred ASIO latency. |
| 2554 | 2) Make sure we have at lest one additional latency frame. |
| 2555 | 3) Divide the number of frames by the desired block size to |
| 2556 | get the number (rounded up to pure integer) of blocks to |
| 2557 | be stored in the buffer. |
| 2558 | 4) Add one additional block for block processing and convert |
| 2559 | to samples frames. |
| 2560 | 5) Get the next larger (or equal) power-of-two buffer size. |
| 2561 | */ |
| 2562 | lBlockingBufferSize = suggestedInputLatencyFrames - stream->asioInputLatencyFrames; |
| 2563 | lBlockingBufferSize = (lBlockingBufferSize > 0) ? lBlockingBufferSize : 1; |
| 2564 | lBlockingBufferSize = (lBlockingBufferSize + framesPerBuffer - 1) / framesPerBuffer; |
| 2565 | lBlockingBufferSize = (lBlockingBufferSize + 1) * framesPerBuffer; |
| 2566 | |
| 2567 | /* Get the next larger or equal power-of-two buffersize. */ |
| 2568 | lBlockingBufferSizePow2 = 1; |
| 2569 | while( lBlockingBufferSize > (lBlockingBufferSizePow2<<=1) ); |
| 2570 | lBlockingBufferSize = lBlockingBufferSizePow2; |
| 2571 | |
| 2572 | /* Compute total input latency in seconds */ |
| 2573 | stream->streamRepresentation.streamInfo.inputLatency = |
| 2574 | (double)( PaUtil_GetBufferProcessorInputLatencyFrames(&stream->bufferProcessor ) |
| 2575 | + PaUtil_GetBufferProcessorInputLatencyFrames(&stream->blockingState->bufferProcessor) |
| 2576 | + (lBlockingBufferSize / framesPerBuffer - 1) * framesPerBuffer |
| 2577 | + stream->asioInputLatencyFrames ) |
| 2578 | / sampleRate; |
| 2579 | |
| 2580 | /* The code below prints the ASIO latency which doesn't include |
| 2581 | the buffer processor latency nor the blocking i/o latency. It |
| 2582 | reports the added latency separately. |
| 2583 | */ |
| 2584 | PA_DEBUG(("PaAsio : ASIO InputLatency = %ld (%ld ms),\n added buffProc:%ld (%ld ms),\n added blocking:%ld (%ld ms)\n", |
| 2585 | stream->asioInputLatencyFrames, |
| 2586 | (long)( stream->asioInputLatencyFrames * (1000.0 / sampleRate) ), |
| 2587 | PaUtil_GetBufferProcessorInputLatencyFrames(&stream->bufferProcessor), |
| 2588 | (long)( PaUtil_GetBufferProcessorInputLatencyFrames(&stream->bufferProcessor) * (1000.0 / sampleRate) ), |
| 2589 | PaUtil_GetBufferProcessorInputLatencyFrames(&stream->blockingState->bufferProcessor) + (lBlockingBufferSize / framesPerBuffer - 1) * framesPerBuffer, |
| 2590 | (long)( (PaUtil_GetBufferProcessorInputLatencyFrames(&stream->blockingState->bufferProcessor) + (lBlockingBufferSize / framesPerBuffer - 1) * framesPerBuffer) * (1000.0 / sampleRate) ) |
| 2591 | )); |
| 2592 | |
| 2593 | /* Determine the size of ring buffer in bytes. */ |
| 2594 | lBytesPerFrame = inputChannelCount * Pa_GetSampleSize(inputSampleFormat ); |
| 2595 | |
| 2596 | /* Allocate the blocking i/o input ring buffer memory. */ |
| 2597 | stream->blockingState->readRingBufferData = (void*)PaUtil_AllocateMemory( lBlockingBufferSize * lBytesPerFrame ); |
| 2598 | if( !stream->blockingState->readRingBufferData ) |
| 2599 | { |
| 2600 | result = paInsufficientMemory; |
| 2601 | PA_DEBUG(("ERROR! Blocking i/o input ring buffer allocation failed in OpenStream()\n")); |
| 2602 | goto error; |
| 2603 | } |
| 2604 | |
| 2605 | /* Initialize the input ring buffer struct. */ |
| 2606 | PaUtil_InitializeRingBuffer( &stream->blockingState->readRingBuffer , |
| 2607 | lBytesPerFrame , |
| 2608 | lBlockingBufferSize , |
| 2609 | stream->blockingState->readRingBufferData ); |
| 2610 | } |
| 2611 | |
| 2612 | /* If output is requested. */ |
| 2613 | if( outputChannelCount ) |
| 2614 | { |
| 2615 | stream->blockingState->writeBuffersReadyEvent = CreateEvent( NULL, FALSE, FALSE, NULL ); |
| 2616 | if( stream->blockingState->writeBuffersReadyEvent == NULL ) |
| 2617 | { |
| 2618 | result = paUnanticipatedHostError; |
| 2619 | PA_ASIO_SET_LAST_SYSTEM_ERROR( GetLastError() ); |
| 2620 | PA_DEBUG(("ERROR! Blocking i/o \"write buffers ready\" event creation failed in OpenStream()\n")); |
| 2621 | goto error; |
| 2622 | } |
| 2623 | blockingWriteBuffersReadyEventInitialized = 1; |
| 2624 | |
| 2625 | /* Create pointer buffer to access non-interleaved data in WriteStream() */ |
| 2626 | stream->blockingState->writeStreamBuffer = (const void**)PaUtil_AllocateMemory( sizeof(const void*) * outputChannelCount ); |
| 2627 | if( !stream->blockingState->writeStreamBuffer ) |
| 2628 | { |
| 2629 | result = paInsufficientMemory; |
| 2630 | PA_DEBUG(("ERROR! Blocking i/o write stream buffer allocation failed in OpenStream()\n")); |
| 2631 | goto error; |
| 2632 | } |
| 2633 | |
| 2634 | /* The ring buffer should store as many data blocks as needed |
| 2635 | to achieve the requested latency. Whereas it must be large |
| 2636 | enough to store at least two complete data blocks. |
| 2637 | |
| 2638 | 1) Determine the amount of latency to be added to the |
| 2639 | preferred ASIO latency. |
| 2640 | 2) Make sure we have at lest one additional latency frame. |
| 2641 | 3) Divide the number of frames by the desired block size to |
| 2642 | get the number (rounded up to pure integer) of blocks to |
| 2643 | be stored in the buffer. |
| 2644 | 4) Add one additional block for block processing and convert |
| 2645 | to samples frames. |
| 2646 | 5) Get the next larger (or equal) power-of-two buffer size. |
| 2647 | */ |
| 2648 | lBlockingBufferSize = suggestedOutputLatencyFrames - stream->asioOutputLatencyFrames; |
| 2649 | lBlockingBufferSize = (lBlockingBufferSize > 0) ? lBlockingBufferSize : 1; |
| 2650 | lBlockingBufferSize = (lBlockingBufferSize + framesPerBuffer - 1) / framesPerBuffer; |
| 2651 | lBlockingBufferSize = (lBlockingBufferSize + 1) * framesPerBuffer; |
| 2652 | |
| 2653 | /* The buffer size (without the additional block) corresponds |
| 2654 | to the initial number of silent samples in the output ring |
| 2655 | buffer. */ |
| 2656 | stream->blockingState->writeRingBufferInitialFrames = lBlockingBufferSize - framesPerBuffer; |
| 2657 | |
| 2658 | /* Get the next larger or equal power-of-two buffersize. */ |
| 2659 | lBlockingBufferSizePow2 = 1; |
| 2660 | while( lBlockingBufferSize > (lBlockingBufferSizePow2<<=1) ); |
| 2661 | lBlockingBufferSize = lBlockingBufferSizePow2; |
| 2662 | |
| 2663 | /* Compute total output latency in seconds */ |
| 2664 | stream->streamRepresentation.streamInfo.outputLatency = |
| 2665 | (double)( PaUtil_GetBufferProcessorOutputLatencyFrames(&stream->bufferProcessor) |
| 2666 | + PaUtil_GetBufferProcessorOutputLatencyFrames(&stream->blockingState->bufferProcessor) |
| 2667 | + (lBlockingBufferSize / framesPerBuffer - 1) * framesPerBuffer |
| 2668 | + stream->asioOutputLatencyFrames ) |
| 2669 | / sampleRate; |
| 2670 | |
| 2671 | /* The code below prints the ASIO latency which doesn't include |
| 2672 | the buffer processor latency nor the blocking i/o latency. It |
| 2673 | reports the added latency separately. |
| 2674 | */ |
| 2675 | PA_DEBUG(("PaAsio : ASIO OutputLatency = %ld (%ld ms),\n added buffProc:%ld (%ld ms),\n added blocking:%ld (%ld ms)\n", |
| 2676 | stream->asioOutputLatencyFrames, |
| 2677 | (long)( stream->asioOutputLatencyFrames * (1000.0 / sampleRate) ), |
| 2678 | PaUtil_GetBufferProcessorOutputLatencyFrames(&stream->bufferProcessor), |
| 2679 | (long)( PaUtil_GetBufferProcessorOutputLatencyFrames(&stream->bufferProcessor) * (1000.0 / sampleRate) ), |
| 2680 | PaUtil_GetBufferProcessorOutputLatencyFrames(&stream->blockingState->bufferProcessor) + (lBlockingBufferSize / framesPerBuffer - 1) * framesPerBuffer, |
| 2681 | (long)( (PaUtil_GetBufferProcessorOutputLatencyFrames(&stream->blockingState->bufferProcessor) + (lBlockingBufferSize / framesPerBuffer - 1) * framesPerBuffer) * (1000.0 / sampleRate) ) |
| 2682 | )); |
| 2683 | |
| 2684 | /* Determine the size of ring buffer in bytes. */ |
| 2685 | lBytesPerFrame = outputChannelCount * Pa_GetSampleSize(outputSampleFormat); |
| 2686 | |
| 2687 | /* Allocate the blocking i/o output ring buffer memory. */ |
| 2688 | stream->blockingState->writeRingBufferData = (void*)PaUtil_AllocateMemory( lBlockingBufferSize * lBytesPerFrame ); |
| 2689 | if( !stream->blockingState->writeRingBufferData ) |
| 2690 | { |
| 2691 | result = paInsufficientMemory; |
| 2692 | PA_DEBUG(("ERROR! Blocking i/o output ring buffer allocation failed in OpenStream()\n")); |
| 2693 | goto error; |
| 2694 | } |
| 2695 | |
| 2696 | /* Initialize the output ring buffer struct. */ |
| 2697 | PaUtil_InitializeRingBuffer( &stream->blockingState->writeRingBuffer , |
| 2698 | lBytesPerFrame , |
| 2699 | lBlockingBufferSize , |
| 2700 | stream->blockingState->writeRingBufferData ); |
| 2701 | } |
| 2702 | |
| 2703 | stream->streamRepresentation.streamInfo.sampleRate = sampleRate; |
| 2704 | |
| 2705 | |
| 2706 | } |
| 2707 | else /* Using callback interface... */ |
| 2708 | { |
| 2709 | result = PaUtil_InitializeBufferProcessor( &stream->bufferProcessor, |
| 2710 | inputChannelCount, inputSampleFormat, (hostInputSampleFormat | paNonInterleaved), |
| 2711 | outputChannelCount, outputSampleFormat, (hostOutputSampleFormat | paNonInterleaved), |
| 2712 | sampleRate, streamFlags, framesPerBuffer, |
| 2713 | framesPerHostBuffer, paUtilFixedHostBufferSize, |
| 2714 | streamCallback, userData ); |
| 2715 | if( result != paNoError ){ |
| 2716 | PA_DEBUG(("OpenStream ERROR13\n")); |
| 2717 | goto error; |
| 2718 | } |
| 2719 | callbackBufferProcessorInited = TRUE; |
| 2720 | |
| 2721 | stream->streamRepresentation.streamInfo.inputLatency = |
| 2722 | (double)( PaUtil_GetBufferProcessorInputLatencyFrames(&stream->bufferProcessor) |
| 2723 | + stream->asioInputLatencyFrames) / sampleRate; // seconds |
| 2724 | stream->streamRepresentation.streamInfo.outputLatency = |
| 2725 | (double)( PaUtil_GetBufferProcessorOutputLatencyFrames(&stream->bufferProcessor) |
| 2726 | + stream->asioOutputLatencyFrames) / sampleRate; // seconds |
| 2727 | stream->streamRepresentation.streamInfo.sampleRate = sampleRate; |
| 2728 | |
| 2729 | // the code below prints the ASIO latency which doesn't include the |
| 2730 | // buffer processor latency. it reports the added latency separately |
| 2731 | PA_DEBUG(("PaAsio : ASIO InputLatency = %ld (%ld ms), added buffProc:%ld (%ld ms)\n", |
| 2732 | stream->asioInputLatencyFrames, |
| 2733 | (long)((stream->asioInputLatencyFrames*1000)/ sampleRate), |
| 2734 | PaUtil_GetBufferProcessorInputLatencyFrames(&stream->bufferProcessor), |
| 2735 | (long)((PaUtil_GetBufferProcessorInputLatencyFrames(&stream->bufferProcessor)*1000)/ sampleRate) |
| 2736 | )); |
| 2737 | |
| 2738 | PA_DEBUG(("PaAsio : ASIO OuputLatency = %ld (%ld ms), added buffProc:%ld (%ld ms)\n", |
| 2739 | stream->asioOutputLatencyFrames, |
| 2740 | (long)((stream->asioOutputLatencyFrames*1000)/ sampleRate), |
| 2741 | PaUtil_GetBufferProcessorOutputLatencyFrames(&stream->bufferProcessor), |
| 2742 | (long)((PaUtil_GetBufferProcessorOutputLatencyFrames(&stream->bufferProcessor)*1000)/ sampleRate) |
| 2743 | )); |
| 2744 | } |
| 2745 | |
| 2746 | stream->asioHostApi = asioHostApi; |
| 2747 | stream->framesPerHostCallback = framesPerHostBuffer; |
| 2748 | |
| 2749 | stream->inputChannelCount = inputChannelCount; |
| 2750 | stream->outputChannelCount = outputChannelCount; |
| 2751 | stream->postOutput = driverInfo->postOutput; |
| 2752 | stream->isStopped = 1; |
| 2753 | stream->isActive = 0; |
| 2754 | |
| 2755 | asioHostApi->openAsioDeviceIndex = asioDeviceIndex; |
| 2756 | |
| 2757 | theAsioStream = stream; |
| 2758 | *s = (PaStream*)stream; |
| 2759 | |
| 2760 | return result; |
| 2761 | |
| 2762 | error: |
| 2763 | PA_DEBUG(("goto errored\n")); |
| 2764 | if( stream ) |
| 2765 | { |
| 2766 | if( stream->blockingState ) |
| 2767 | { |
| 2768 | if( blockingBufferProcessorInited ) |
| 2769 | PaUtil_TerminateBufferProcessor( &stream->blockingState->bufferProcessor ); |
| 2770 | |
| 2771 | if( stream->blockingState->writeRingBufferData ) |
| 2772 | PaUtil_FreeMemory( stream->blockingState->writeRingBufferData ); |
| 2773 | if( stream->blockingState->writeStreamBuffer ) |
| 2774 | PaUtil_FreeMemory( stream->blockingState->writeStreamBuffer ); |
| 2775 | if( blockingWriteBuffersReadyEventInitialized ) |
| 2776 | CloseHandle( stream->blockingState->writeBuffersReadyEvent ); |
| 2777 | |
| 2778 | if( stream->blockingState->readRingBufferData ) |
| 2779 | PaUtil_FreeMemory( stream->blockingState->readRingBufferData ); |
| 2780 | if( stream->blockingState->readStreamBuffer ) |
| 2781 | PaUtil_FreeMemory( stream->blockingState->readStreamBuffer ); |
| 2782 | if( blockingReadFramesReadyEventInitialized ) |
| 2783 | CloseHandle( stream->blockingState->readFramesReadyEvent ); |
| 2784 | |
| 2785 | PaUtil_FreeMemory( stream->blockingState ); |
| 2786 | } |
| 2787 | |
| 2788 | if( callbackBufferProcessorInited ) |
| 2789 | PaUtil_TerminateBufferProcessor( &stream->bufferProcessor ); |
| 2790 | |
| 2791 | if( completedBuffersPlayedEventInited ) |
| 2792 | CloseHandle( stream->completedBuffersPlayedEvent ); |
| 2793 | |
| 2794 | if( stream->asioBufferInfos ) |
| 2795 | PaUtil_FreeMemory( stream->asioBufferInfos ); |
| 2796 | |
| 2797 | if( stream->asioChannelInfos ) |
| 2798 | PaUtil_FreeMemory( stream->asioChannelInfos ); |
| 2799 | |
| 2800 | if( stream->bufferPtrs ) |
| 2801 | PaUtil_FreeMemory( stream->bufferPtrs ); |
| 2802 | |
| 2803 | PaUtil_FreeMemory( stream ); |
| 2804 | } |
| 2805 | |
| 2806 | if( asioBuffersCreated ) |
| 2807 | ASIODisposeBuffers(); |
| 2808 | |
| 2809 | if( asioIsInitialized ) |
| 2810 | { |
| 2811 | UnloadAsioDriver(); |
| 2812 | } |
| 2813 | return result; |
| 2814 | } |
| 2815 | |
| 2816 | |
| 2817 | /* |
| 2818 | When CloseStream() is called, the multi-api layer ensures that |
| 2819 | the stream has already been stopped or aborted. |
| 2820 | */ |
| 2821 | static PaError CloseStream( PaStream* s ) |
| 2822 | { |
| 2823 | PaError result = paNoError; |
| 2824 | PaAsioStream *stream = (PaAsioStream*)s; |
| 2825 | |
| 2826 | /* |
| 2827 | IMPLEMENT ME: |
| 2828 | - additional stream closing + cleanup |
| 2829 | */ |
| 2830 | |
| 2831 | PaUtil_TerminateBufferProcessor( &stream->bufferProcessor ); |
| 2832 | PaUtil_TerminateStreamRepresentation( &stream->streamRepresentation ); |
| 2833 | |
| 2834 | stream->asioHostApi->openAsioDeviceIndex = paNoDevice; |
| 2835 | |
| 2836 | CloseHandle( stream->completedBuffersPlayedEvent ); |
| 2837 | |
| 2838 | /* Using blocking i/o interface... */ |
| 2839 | if( stream->blockingState ) |
| 2840 | { |
| 2841 | PaUtil_TerminateBufferProcessor( &stream->blockingState->bufferProcessor ); |
| 2842 | |
| 2843 | if( stream->inputChannelCount ) { |
| 2844 | PaUtil_FreeMemory( stream->blockingState->readRingBufferData ); |
| 2845 | PaUtil_FreeMemory( stream->blockingState->readStreamBuffer ); |
| 2846 | CloseHandle( stream->blockingState->readFramesReadyEvent ); |
| 2847 | } |
| 2848 | if( stream->outputChannelCount ) { |
| 2849 | PaUtil_FreeMemory( stream->blockingState->writeRingBufferData ); |
| 2850 | PaUtil_FreeMemory( stream->blockingState->writeStreamBuffer ); |
| 2851 | CloseHandle( stream->blockingState->writeBuffersReadyEvent ); |
| 2852 | } |
| 2853 | |
| 2854 | PaUtil_FreeMemory( stream->blockingState ); |
| 2855 | } |
| 2856 | |
| 2857 | PaUtil_FreeMemory( stream->asioBufferInfos ); |
| 2858 | PaUtil_FreeMemory( stream->asioChannelInfos ); |
| 2859 | PaUtil_FreeMemory( stream->bufferPtrs ); |
| 2860 | PaUtil_FreeMemory( stream ); |
| 2861 | |
| 2862 | ASIODisposeBuffers(); |
| 2863 | UnloadAsioDriver(); |
| 2864 | |
| 2865 | theAsioStream = 0; |
| 2866 | |
| 2867 | return result; |
| 2868 | } |
| 2869 | |
| 2870 | |
| 2871 | static void bufferSwitch(long index, ASIOBool directProcess) |
| 2872 | { |
| 2873 | //TAKEN FROM THE ASIO SDK |
| 2874 | |
| 2875 | // the actual processing callback. |
| 2876 | // Beware that this is normally in a separate thread, hence be sure that |
| 2877 | // you take care about thread synchronization. This is omitted here for |
| 2878 | // simplicity. |
| 2879 | |
| 2880 | // as this is a "back door" into the bufferSwitchTimeInfo a timeInfo needs |
| 2881 | // to be created though it will only set the timeInfo.samplePosition and |
| 2882 | // timeInfo.systemTime fields and the according flags |
| 2883 | |
| 2884 | ASIOTime timeInfo; |
| 2885 | memset( &timeInfo, 0, sizeof (timeInfo) ); |
| 2886 | |
| 2887 | // get the time stamp of the buffer, not necessary if no |
| 2888 | // synchronization to other media is required |
| 2889 | if( ASIOGetSamplePosition(&timeInfo.timeInfo.samplePosition, &timeInfo.timeInfo.systemTime) == ASE_OK) |
| 2890 | timeInfo.timeInfo.flags = kSystemTimeValid | kSamplePositionValid; |
| 2891 | |
| 2892 | // Call the real callback |
| 2893 | bufferSwitchTimeInfo( &timeInfo, index, directProcess ); |
| 2894 | } |
| 2895 | |
| 2896 | |
| 2897 | // conversion from 64 bit ASIOSample/ASIOTimeStamp to double float |
| 2898 | #if NATIVE_INT64 |
| 2899 | #define ASIO64toDouble(a) (a) |
| 2900 | #else |
| 2901 | const double twoRaisedTo32 = 4294967296.; |
| 2902 | #define ASIO64toDouble(a) ((a).lo + (a).hi * twoRaisedTo32) |
| 2903 | #endif |
| 2904 | |
| 2905 | static ASIOTime *bufferSwitchTimeInfo( ASIOTime *timeInfo, long index, ASIOBool directProcess ) |
| 2906 | { |
| 2907 | // the actual processing callback. |
| 2908 | // Beware that this is normally in a separate thread, hence be sure that |
| 2909 | // you take care about thread synchronization. |
| 2910 | |
| 2911 | |
| 2912 | /* The SDK says the following about the directProcess flag: |
| 2913 | suggests to the host whether it should immediately start processing |
| 2914 | (directProcess == ASIOTrue), or whether its process should be deferred |
| 2915 | because the call comes from a very low level (for instance, a high level |
| 2916 | priority interrupt), and direct processing would cause timing instabilities for |
| 2917 | the rest of the system. If in doubt, directProcess should be set to ASIOFalse. |
| 2918 | |
| 2919 | We just ignore directProcess. This could cause incompatibilities with |
| 2920 | drivers which really don't want the audio processing to occur in this |
| 2921 | callback, but none have been identified yet. |
| 2922 | */ |
| 2923 | |
| 2924 | (void) directProcess; /* suppress unused parameter warning */ |
| 2925 | |
| 2926 | #if 0 |
| 2927 | // store the timeInfo for later use |
| 2928 | asioDriverInfo.tInfo = *timeInfo; |
| 2929 | |
| 2930 | // get the time stamp of the buffer, not necessary if no |
| 2931 | // synchronization to other media is required |
| 2932 | |
| 2933 | if (timeInfo->timeInfo.flags & kSystemTimeValid) |
| 2934 | asioDriverInfo.nanoSeconds = ASIO64toDouble(timeInfo->timeInfo.systemTime); |
| 2935 | else |
| 2936 | asioDriverInfo.nanoSeconds = 0; |
| 2937 | |
| 2938 | if (timeInfo->timeInfo.flags & kSamplePositionValid) |
| 2939 | asioDriverInfo.samples = ASIO64toDouble(timeInfo->timeInfo.samplePosition); |
| 2940 | else |
| 2941 | asioDriverInfo.samples = 0; |
| 2942 | |
| 2943 | if (timeInfo->timeCode.flags & kTcValid) |
| 2944 | asioDriverInfo.tcSamples = ASIO64toDouble(timeInfo->timeCode.timeCodeSamples); |
| 2945 | else |
| 2946 | asioDriverInfo.tcSamples = 0; |
| 2947 | |
| 2948 | // get the system reference time |
| 2949 | asioDriverInfo.sysRefTime = get_sys_reference_time(); |
| 2950 | #endif |
| 2951 | |
| 2952 | #if 0 |
| 2953 | // a few debug messages for the Windows device driver developer |
| 2954 | // tells you the time when driver got its interrupt and the delay until the app receives |
| 2955 | // the event notification. |
| 2956 | static double last_samples = 0; |
| 2957 | char tmp[128]; |
| 2958 | sprintf (tmp, "diff: %d / %d ms / %d ms / %d samples \n", asioDriverInfo.sysRefTime - (long)(asioDriverInfo.nanoSeconds / 1000000.0), asioDriverInfo.sysRefTime, (long)(asioDriverInfo.nanoSeconds / 1000000.0), (long)(asioDriverInfo.samples - last_samples)); |
| 2959 | OutputDebugString (tmp); |
| 2960 | last_samples = asioDriverInfo.samples; |
| 2961 | #endif |
| 2962 | |
| 2963 | |
| 2964 | if( !theAsioStream ) |
| 2965 | return 0L; |
| 2966 | |
| 2967 | // protect against reentrancy |
| 2968 | if( PaAsio_AtomicIncrement(&theAsioStream->reenterCount) ) |
| 2969 | { |
| 2970 | theAsioStream->reenterError++; |
| 2971 | //DBUG(("bufferSwitchTimeInfo : reentrancy detection = %d\n", asioDriverInfo.reenterError)); |
| 2972 | return 0L; |
| 2973 | } |
| 2974 | |
| 2975 | int buffersDone = 0; |
| 2976 | |
| 2977 | do |
| 2978 | { |
| 2979 | if( buffersDone > 0 ) |
| 2980 | { |
| 2981 | // this is a reentered buffer, we missed processing it on time |
| 2982 | // set the input overflow and output underflow flags as appropriate |
| 2983 | |
| 2984 | if( theAsioStream->inputChannelCount > 0 ) |
| 2985 | theAsioStream->callbackFlags |= paInputOverflow; |
| 2986 | |
| 2987 | if( theAsioStream->outputChannelCount > 0 ) |
| 2988 | theAsioStream->callbackFlags |= paOutputUnderflow; |
| 2989 | } |
| 2990 | else |
| 2991 | { |
| 2992 | if( theAsioStream->zeroOutput ) |
| 2993 | { |
| 2994 | ZeroOutputBuffers( theAsioStream, index ); |
| 2995 | |
| 2996 | // Finally if the driver supports the ASIOOutputReady() optimization, |
| 2997 | // do it here, all data are in place |
| 2998 | if( theAsioStream->postOutput ) |
| 2999 | ASIOOutputReady(); |
| 3000 | |
| 3001 | if( theAsioStream->stopProcessing ) |
| 3002 | { |
| 3003 | if( theAsioStream->stopPlayoutCount < 2 ) |
| 3004 | { |
| 3005 | ++theAsioStream->stopPlayoutCount; |
| 3006 | if( theAsioStream->stopPlayoutCount == 2 ) |
| 3007 | { |
| 3008 | theAsioStream->isActive = 0; |
| 3009 | if( theAsioStream->streamRepresentation.streamFinishedCallback != 0 ) |
| 3010 | theAsioStream->streamRepresentation.streamFinishedCallback( theAsioStream->streamRepresentation.userData ); |
| 3011 | theAsioStream->streamFinishedCallbackCalled = true; |
| 3012 | SetEvent( theAsioStream->completedBuffersPlayedEvent ); |
| 3013 | } |
| 3014 | } |
| 3015 | } |
| 3016 | } |
| 3017 | else |
| 3018 | { |
| 3019 | |
| 3020 | #if 0 |
| 3021 | /* |
| 3022 | see: "ASIO callback underflow/overflow buffer slip detection doesn't work" |
| 3023 | http://www.portaudio.com/trac/ticket/110 |
| 3024 | */ |
| 3025 | |
| 3026 | // test code to try to detect slip conditions... these may work on some systems |
| 3027 | // but neither of them work on the RME Digi96 |
| 3028 | |
| 3029 | // check that sample delta matches buffer size (otherwise we must have skipped |
| 3030 | // a buffer. |
| 3031 | static double last_samples = -512; |
| 3032 | double samples; |
| 3033 | //if( timeInfo->timeCode.flags & kTcValid ) |
| 3034 | // samples = ASIO64toDouble(timeInfo->timeCode.timeCodeSamples); |
| 3035 | //else |
| 3036 | samples = ASIO64toDouble(timeInfo->timeInfo.samplePosition); |
| 3037 | int delta = samples - last_samples; |
| 3038 | //printf( "%d\n", delta); |
| 3039 | last_samples = samples; |
| 3040 | |
| 3041 | if( delta > theAsioStream->framesPerHostCallback ) |
| 3042 | { |
| 3043 | if( theAsioStream->inputChannelCount > 0 ) |
| 3044 | theAsioStream->callbackFlags |= paInputOverflow; |
| 3045 | |
| 3046 | if( theAsioStream->outputChannelCount > 0 ) |
| 3047 | theAsioStream->callbackFlags |= paOutputUnderflow; |
| 3048 | } |
| 3049 | |
| 3050 | // check that the buffer index is not the previous index (which would indicate |
| 3051 | // that a buffer was skipped. |
| 3052 | static int previousIndex = 1; |
| 3053 | if( index == previousIndex ) |
| 3054 | { |
| 3055 | if( theAsioStream->inputChannelCount > 0 ) |
| 3056 | theAsioStream->callbackFlags |= paInputOverflow; |
| 3057 | |
| 3058 | if( theAsioStream->outputChannelCount > 0 ) |
| 3059 | theAsioStream->callbackFlags |= paOutputUnderflow; |
| 3060 | } |
| 3061 | previousIndex = index; |
| 3062 | #endif |
| 3063 | |
| 3064 | int i; |
| 3065 | |
| 3066 | PaUtil_BeginCpuLoadMeasurement( &theAsioStream->cpuLoadMeasurer ); |
| 3067 | |
| 3068 | PaStreamCallbackTimeInfo paTimeInfo; |
| 3069 | |
| 3070 | // asio systemTime is supposed to be measured according to the same |
| 3071 | // clock as timeGetTime |
| 3072 | paTimeInfo.currentTime = (ASIO64toDouble( timeInfo->timeInfo.systemTime ) * .000000001); |
| 3073 | |
| 3074 | /* patch from Paul Boege */ |
| 3075 | paTimeInfo.inputBufferAdcTime = paTimeInfo.currentTime - |
| 3076 | ((double)theAsioStream->asioInputLatencyFrames/theAsioStream->streamRepresentation.streamInfo.sampleRate); |
| 3077 | |
| 3078 | paTimeInfo.outputBufferDacTime = paTimeInfo.currentTime + |
| 3079 | ((double)theAsioStream->asioOutputLatencyFrames/theAsioStream->streamRepresentation.streamInfo.sampleRate); |
| 3080 | |
| 3081 | /* old version is buggy because the buffer processor also adds in its latency to the time parameters |
| 3082 | paTimeInfo.inputBufferAdcTime = paTimeInfo.currentTime - theAsioStream->streamRepresentation.streamInfo.inputLatency; |
| 3083 | paTimeInfo.outputBufferDacTime = paTimeInfo.currentTime + theAsioStream->streamRepresentation.streamInfo.outputLatency; |
| 3084 | */ |
| 3085 | |
| 3086 | /* Disabled! Stopping and re-starting the stream causes an input overflow / output underflow. S.Fischer */ |
| 3087 | #if 0 |
| 3088 | // detect underflows by checking inter-callback time > 2 buffer period |
| 3089 | static double previousTime = -1; |
| 3090 | if( previousTime > 0 ){ |
| 3091 | |
| 3092 | double delta = paTimeInfo.currentTime - previousTime; |
| 3093 | |
| 3094 | if( delta >= 2. * (theAsioStream->framesPerHostCallback / theAsioStream->streamRepresentation.streamInfo.sampleRate) ){ |
| 3095 | if( theAsioStream->inputChannelCount > 0 ) |
| 3096 | theAsioStream->callbackFlags |= paInputOverflow; |
| 3097 | |
| 3098 | if( theAsioStream->outputChannelCount > 0 ) |
| 3099 | theAsioStream->callbackFlags |= paOutputUnderflow; |
| 3100 | } |
| 3101 | } |
| 3102 | previousTime = paTimeInfo.currentTime; |
| 3103 | #endif |
| 3104 | |
| 3105 | // note that the above input and output times do not need to be |
| 3106 | // adjusted for the latency of the buffer processor -- the buffer |
| 3107 | // processor handles that. |
| 3108 | |
| 3109 | if( theAsioStream->inputBufferConverter ) |
| 3110 | { |
| 3111 | for( i=0; i<theAsioStream->inputChannelCount; i++ ) |
| 3112 | { |
| 3113 | theAsioStream->inputBufferConverter( theAsioStream->inputBufferPtrs[index][i], |
| 3114 | theAsioStream->inputShift, theAsioStream->framesPerHostCallback ); |
| 3115 | } |
| 3116 | } |
| 3117 | |
| 3118 | PaUtil_BeginBufferProcessing( &theAsioStream->bufferProcessor, &paTimeInfo, theAsioStream->callbackFlags ); |
| 3119 | |
| 3120 | /* reset status flags once they've been passed to the callback */ |
| 3121 | theAsioStream->callbackFlags = 0; |
| 3122 | |
| 3123 | PaUtil_SetInputFrameCount( &theAsioStream->bufferProcessor, 0 /* default to host buffer size */ ); |
| 3124 | for( i=0; i<theAsioStream->inputChannelCount; ++i ) |
| 3125 | PaUtil_SetNonInterleavedInputChannel( &theAsioStream->bufferProcessor, i, theAsioStream->inputBufferPtrs[index][i] ); |
| 3126 | |
| 3127 | PaUtil_SetOutputFrameCount( &theAsioStream->bufferProcessor, 0 /* default to host buffer size */ ); |
| 3128 | for( i=0; i<theAsioStream->outputChannelCount; ++i ) |
| 3129 | PaUtil_SetNonInterleavedOutputChannel( &theAsioStream->bufferProcessor, i, theAsioStream->outputBufferPtrs[index][i] ); |
| 3130 | |
| 3131 | int callbackResult; |
| 3132 | if( theAsioStream->stopProcessing ) |
| 3133 | callbackResult = paComplete; |
| 3134 | else |
| 3135 | callbackResult = paContinue; |
| 3136 | unsigned long framesProcessed = PaUtil_EndBufferProcessing( &theAsioStream->bufferProcessor, &callbackResult ); |
| 3137 | |
| 3138 | if( theAsioStream->outputBufferConverter ) |
| 3139 | { |
| 3140 | for( i=0; i<theAsioStream->outputChannelCount; i++ ) |
| 3141 | { |
| 3142 | theAsioStream->outputBufferConverter( theAsioStream->outputBufferPtrs[index][i], |
| 3143 | theAsioStream->outputShift, theAsioStream->framesPerHostCallback ); |
| 3144 | } |
| 3145 | } |
| 3146 | |
| 3147 | PaUtil_EndCpuLoadMeasurement( &theAsioStream->cpuLoadMeasurer, framesProcessed ); |
| 3148 | |
| 3149 | // Finally if the driver supports the ASIOOutputReady() optimization, |
| 3150 | // do it here, all data are in place |
| 3151 | if( theAsioStream->postOutput ) |
| 3152 | ASIOOutputReady(); |
| 3153 | |
| 3154 | if( callbackResult == paContinue ) |
| 3155 | { |
| 3156 | /* nothing special to do */ |
| 3157 | } |
| 3158 | else if( callbackResult == paAbort ) |
| 3159 | { |
| 3160 | /* finish playback immediately */ |
| 3161 | theAsioStream->isActive = 0; |
| 3162 | if( theAsioStream->streamRepresentation.streamFinishedCallback != 0 ) |
| 3163 | theAsioStream->streamRepresentation.streamFinishedCallback( theAsioStream->streamRepresentation.userData ); |
| 3164 | theAsioStream->streamFinishedCallbackCalled = true; |
| 3165 | SetEvent( theAsioStream->completedBuffersPlayedEvent ); |
| 3166 | theAsioStream->zeroOutput = true; |
| 3167 | } |
| 3168 | else /* paComplete or other non-zero value indicating complete */ |
| 3169 | { |
| 3170 | /* Finish playback once currently queued audio has completed. */ |
| 3171 | theAsioStream->stopProcessing = true; |
| 3172 | |
| 3173 | if( PaUtil_IsBufferProcessorOutputEmpty( &theAsioStream->bufferProcessor ) ) |
| 3174 | { |
| 3175 | theAsioStream->zeroOutput = true; |
| 3176 | theAsioStream->stopPlayoutCount = 0; |
| 3177 | } |
| 3178 | } |
| 3179 | } |
| 3180 | } |
| 3181 | |
| 3182 | ++buffersDone; |
| 3183 | }while( PaAsio_AtomicDecrement(&theAsioStream->reenterCount) >= 0 ); |
| 3184 | |
| 3185 | return 0L; |
| 3186 | } |
| 3187 | |
| 3188 | |
| 3189 | static void sampleRateChanged(ASIOSampleRate sRate) |
| 3190 | { |
| 3191 | // TAKEN FROM THE ASIO SDK |
| 3192 | // do whatever you need to do if the sample rate changed |
| 3193 | // usually this only happens during external sync. |
| 3194 | // Audio processing is not stopped by the driver, actual sample rate |
| 3195 | // might not have even changed, maybe only the sample rate status of an |
| 3196 | // AES/EBU or S/PDIF digital input at the audio device. |
| 3197 | // You might have to update time/sample related conversion routines, etc. |
| 3198 | |
| 3199 | (void) sRate; /* unused parameter */ |
| 3200 | PA_DEBUG( ("sampleRateChanged : %d \n", sRate)); |
| 3201 | } |
| 3202 | |
| 3203 | static long asioMessages(long selector, long value, void* message, double* opt) |
| 3204 | { |
| 3205 | // TAKEN FROM THE ASIO SDK |
| 3206 | // currently the parameters "value", "message" and "opt" are not used. |
| 3207 | long ret = 0; |
| 3208 | |
| 3209 | (void) message; /* unused parameters */ |
| 3210 | (void) opt; |
| 3211 | |
| 3212 | PA_DEBUG( ("asioMessages : %d , %d \n", selector, value)); |
| 3213 | |
| 3214 | switch(selector) |
| 3215 | { |
| 3216 | case kAsioSelectorSupported: |
| 3217 | if(value == kAsioResetRequest |
| 3218 | || value == kAsioEngineVersion |
| 3219 | || value == kAsioResyncRequest |
| 3220 | || value == kAsioLatenciesChanged |
| 3221 | // the following three were added for ASIO 2.0, you don't necessarily have to support them |
| 3222 | || value == kAsioSupportsTimeInfo |
| 3223 | || value == kAsioSupportsTimeCode |
| 3224 | || value == kAsioSupportsInputMonitor) |
| 3225 | ret = 1L; |
| 3226 | break; |
| 3227 | |
| 3228 | case kAsioBufferSizeChange: |
| 3229 | //printf("kAsioBufferSizeChange \n"); |
| 3230 | break; |
| 3231 | |
| 3232 | case kAsioResetRequest: |
| 3233 | // defer the task and perform the reset of the driver during the next "safe" situation |
| 3234 | // You cannot reset the driver right now, as this code is called from the driver. |
| 3235 | // Reset the driver is done by completely destruct is. I.e. ASIOStop(), ASIODisposeBuffers(), Destruction |
| 3236 | // Afterwards you initialize the driver again. |
| 3237 | |
| 3238 | /*FIXME: commented the next line out |
| 3239 | |
| 3240 | see: "PA/ASIO ignores some driver notifications it probably shouldn't" |
| 3241 | http://www.portaudio.com/trac/ticket/108 |
| 3242 | */ |
| 3243 | //asioDriverInfo.stopped; // In this sample the processing will just stop |
| 3244 | ret = 1L; |
| 3245 | break; |
| 3246 | |
| 3247 | case kAsioResyncRequest: |
| 3248 | // This informs the application, that the driver encountered some non fatal data loss. |
| 3249 | // It is used for synchronization purposes of different media. |
| 3250 | // Added mainly to work around the Win16Mutex problems in Windows 95/98 with the |
| 3251 | // Windows Multimedia system, which could loose data because the Mutex was hold too long |
| 3252 | // by another thread. |
| 3253 | // However a driver can issue it in other situations, too. |
| 3254 | ret = 1L; |
| 3255 | break; |
| 3256 | |
| 3257 | case kAsioLatenciesChanged: |
| 3258 | // This will inform the host application that the drivers were latencies changed. |
| 3259 | // Beware, it this does not mean that the buffer sizes have changed! |
| 3260 | // You might need to update internal delay data. |
| 3261 | ret = 1L; |
| 3262 | //printf("kAsioLatenciesChanged \n"); |
| 3263 | break; |
| 3264 | |
| 3265 | case kAsioEngineVersion: |
| 3266 | // return the supported ASIO version of the host application |
| 3267 | // If a host applications does not implement this selector, ASIO 1.0 is assumed |
| 3268 | // by the driver |
| 3269 | ret = 2L; |
| 3270 | break; |
| 3271 | |
| 3272 | case kAsioSupportsTimeInfo: |
| 3273 | // informs the driver whether the asioCallbacks.bufferSwitchTimeInfo() callback |
| 3274 | // is supported. |
| 3275 | // For compatibility with ASIO 1.0 drivers the host application should always support |
| 3276 | // the "old" bufferSwitch method, too. |
| 3277 | ret = 1; |
| 3278 | break; |
| 3279 | |
| 3280 | case kAsioSupportsTimeCode: |
| 3281 | // informs the driver whether application is interested in time code info. |
| 3282 | // If an application does not need to know about time code, the driver has less work |
| 3283 | // to do. |
| 3284 | ret = 0; |
| 3285 | break; |
| 3286 | } |
| 3287 | return ret; |
| 3288 | } |
| 3289 | |
| 3290 | |
| 3291 | static PaError StartStream( PaStream *s ) |
| 3292 | { |
| 3293 | PaError result = paNoError; |
| 3294 | PaAsioStream *stream = (PaAsioStream*)s; |
| 3295 | PaAsioStreamBlockingState *blockingState = stream->blockingState; |
| 3296 | ASIOError asioError; |
| 3297 | |
| 3298 | if( stream->outputChannelCount > 0 ) |
| 3299 | { |
| 3300 | ZeroOutputBuffers( stream, 0 ); |
| 3301 | ZeroOutputBuffers( stream, 1 ); |
| 3302 | } |
| 3303 | |
| 3304 | PaUtil_ResetBufferProcessor( &stream->bufferProcessor ); |
| 3305 | stream->stopProcessing = false; |
| 3306 | stream->zeroOutput = false; |
| 3307 | |
| 3308 | /* Reentrancy counter initialisation */ |
| 3309 | stream->reenterCount = -1; |
| 3310 | stream->reenterError = 0; |
| 3311 | |
| 3312 | stream->callbackFlags = 0; |
| 3313 | |
| 3314 | if( ResetEvent( stream->completedBuffersPlayedEvent ) == 0 ) |
| 3315 | { |
| 3316 | result = paUnanticipatedHostError; |
| 3317 | PA_ASIO_SET_LAST_SYSTEM_ERROR( GetLastError() ); |
| 3318 | } |
| 3319 | |
| 3320 | |
| 3321 | /* Using blocking i/o interface... */ |
| 3322 | if( blockingState ) |
| 3323 | { |
| 3324 | /* Reset blocking i/o buffer processor. */ |
| 3325 | PaUtil_ResetBufferProcessor( &blockingState->bufferProcessor ); |
| 3326 | |
| 3327 | /* If we're about to process some input data. */ |
| 3328 | if( stream->inputChannelCount ) |
| 3329 | { |
| 3330 | /* Reset callback-ReadStream sync event. */ |
| 3331 | if( ResetEvent( blockingState->readFramesReadyEvent ) == 0 ) |
| 3332 | { |
| 3333 | result = paUnanticipatedHostError; |
| 3334 | PA_ASIO_SET_LAST_SYSTEM_ERROR( GetLastError() ); |
| 3335 | } |
| 3336 | |
| 3337 | /* Flush blocking i/o ring buffer. */ |
| 3338 | PaUtil_FlushRingBuffer( &blockingState->readRingBuffer ); |
| 3339 | (*blockingState->bufferProcessor.inputZeroer)( blockingState->readRingBuffer.buffer, 1, blockingState->bufferProcessor.inputChannelCount * blockingState->readRingBuffer.bufferSize ); |
| 3340 | } |
| 3341 | |
| 3342 | /* If we're about to process some output data. */ |
| 3343 | if( stream->outputChannelCount ) |
| 3344 | { |
| 3345 | /* Reset callback-WriteStream sync event. */ |
| 3346 | if( ResetEvent( blockingState->writeBuffersReadyEvent ) == 0 ) |
| 3347 | { |
| 3348 | result = paUnanticipatedHostError; |
| 3349 | PA_ASIO_SET_LAST_SYSTEM_ERROR( GetLastError() ); |
| 3350 | } |
| 3351 | |
| 3352 | /* Flush blocking i/o ring buffer. */ |
| 3353 | PaUtil_FlushRingBuffer( &blockingState->writeRingBuffer ); |
| 3354 | (*blockingState->bufferProcessor.outputZeroer)( blockingState->writeRingBuffer.buffer, 1, blockingState->bufferProcessor.outputChannelCount * blockingState->writeRingBuffer.bufferSize ); |
| 3355 | |
| 3356 | /* Initialize the output ring buffer to "silence". */ |
| 3357 | PaUtil_AdvanceRingBufferWriteIndex( &blockingState->writeRingBuffer, blockingState->writeRingBufferInitialFrames ); |
| 3358 | } |
| 3359 | |
| 3360 | /* Clear requested frames / buffers count. */ |
| 3361 | blockingState->writeBuffersRequested = 0; |
| 3362 | blockingState->readFramesRequested = 0; |
| 3363 | blockingState->writeBuffersRequestedFlag = FALSE; |
| 3364 | blockingState->readFramesRequestedFlag = FALSE; |
| 3365 | blockingState->outputUnderflowFlag = FALSE; |
| 3366 | blockingState->inputOverflowFlag = FALSE; |
| 3367 | blockingState->stopFlag = FALSE; |
| 3368 | } |
| 3369 | |
| 3370 | |
| 3371 | if( result == paNoError ) |
| 3372 | { |
| 3373 | assert( theAsioStream == stream ); /* theAsioStream should be set correctly in OpenStream */ |
| 3374 | |
| 3375 | /* initialize these variables before the callback has a chance to be invoked */ |
| 3376 | stream->isStopped = 0; |
| 3377 | stream->isActive = 1; |
| 3378 | stream->streamFinishedCallbackCalled = false; |
| 3379 | |
| 3380 | asioError = ASIOStart(); |
| 3381 | if( asioError != ASE_OK ) |
| 3382 | { |
| 3383 | stream->isStopped = 1; |
| 3384 | stream->isActive = 0; |
| 3385 | |
| 3386 | result = paUnanticipatedHostError; |
| 3387 | PA_ASIO_SET_LAST_ASIO_ERROR( asioError ); |
| 3388 | } |
| 3389 | } |
| 3390 | |
| 3391 | return result; |
| 3392 | } |
| 3393 | |
| 3394 | static void EnsureCallbackHasCompleted( PaAsioStream *stream ) |
| 3395 | { |
| 3396 | // make sure that the callback is not still in-flight after ASIOStop() |
| 3397 | // returns. This has been observed to happen on the Hoontech DSP24 for |
| 3398 | // example. |
| 3399 | int count = 2000; // only wait for 2 seconds, rather than hanging. |
| 3400 | while( stream->reenterCount != -1 && count > 0 ) |
| 3401 | { |
| 3402 | Sleep(1); |
| 3403 | --count; |
| 3404 | } |
| 3405 | } |
| 3406 | |
| 3407 | static PaError StopStream( PaStream *s ) |
| 3408 | { |
| 3409 | PaError result = paNoError; |
| 3410 | PaAsioStream *stream = (PaAsioStream*)s; |
| 3411 | PaAsioStreamBlockingState *blockingState = stream->blockingState; |
| 3412 | ASIOError asioError; |
| 3413 | |
| 3414 | if( stream->isActive ) |
| 3415 | { |
| 3416 | /* If blocking i/o output is in use */ |
| 3417 | if( blockingState && stream->outputChannelCount ) |
| 3418 | { |
| 3419 | /* Request the whole output buffer to be available. */ |
| 3420 | blockingState->writeBuffersRequested = blockingState->writeRingBuffer.bufferSize; |
| 3421 | /* Signalize that additional buffers are need. */ |
| 3422 | blockingState->writeBuffersRequestedFlag = TRUE; |
| 3423 | /* Set flag to indicate the playback is to be stopped. */ |
| 3424 | blockingState->stopFlag = TRUE; |
| 3425 | |
| 3426 | /* Wait until requested number of buffers has been freed. Time |
| 3427 | out after twice the blocking i/o output buffer could have |
| 3428 | been consumed. */ |
| 3429 | DWORD timeout = (DWORD)( 2 * blockingState->writeRingBuffer.bufferSize * 1000 |
| 3430 | / stream->streamRepresentation.streamInfo.sampleRate ); |
| 3431 | DWORD waitResult = WaitForSingleObject( blockingState->writeBuffersReadyEvent, timeout ); |
| 3432 | |
| 3433 | /* If something seriously went wrong... */ |
| 3434 | if( waitResult == WAIT_FAILED ) |
| 3435 | { |
| 3436 | PA_DEBUG(("WaitForSingleObject() failed in StopStream()\n")); |
| 3437 | result = paUnanticipatedHostError; |
| 3438 | PA_ASIO_SET_LAST_SYSTEM_ERROR( GetLastError() ); |
| 3439 | } |
| 3440 | else if( waitResult == WAIT_TIMEOUT ) |
| 3441 | { |
| 3442 | PA_DEBUG(("WaitForSingleObject() timed out in StopStream()\n")); |
| 3443 | result = paTimedOut; |
| 3444 | } |
| 3445 | } |
| 3446 | |
| 3447 | stream->stopProcessing = true; |
| 3448 | |
| 3449 | /* wait for the stream to finish playing out enqueued buffers. |
| 3450 | timeout after four times the stream latency. |
| 3451 | |
| 3452 | @todo should use a better time out value - if the user buffer |
| 3453 | length is longer than the asio buffer size then that should |
| 3454 | be taken into account. |
| 3455 | */ |
| 3456 | if( WaitForSingleObject( stream->completedBuffersPlayedEvent, |
| 3457 | (DWORD)(stream->streamRepresentation.streamInfo.outputLatency * 1000. * 4.) ) |
| 3458 | == WAIT_TIMEOUT ) |
| 3459 | { |
| 3460 | PA_DEBUG(("WaitForSingleObject() timed out in StopStream()\n" )); |
| 3461 | } |
| 3462 | } |
| 3463 | |
| 3464 | asioError = ASIOStop(); |
| 3465 | if( asioError == ASE_OK ) |
| 3466 | { |
| 3467 | EnsureCallbackHasCompleted( stream ); |
| 3468 | } |
| 3469 | else |
| 3470 | { |
| 3471 | result = paUnanticipatedHostError; |
| 3472 | PA_ASIO_SET_LAST_ASIO_ERROR( asioError ); |
| 3473 | } |
| 3474 | |
| 3475 | stream->isStopped = 1; |
| 3476 | stream->isActive = 0; |
| 3477 | |
| 3478 | if( !stream->streamFinishedCallbackCalled ) |
| 3479 | { |
| 3480 | if( stream->streamRepresentation.streamFinishedCallback != 0 ) |
| 3481 | stream->streamRepresentation.streamFinishedCallback( stream->streamRepresentation.userData ); |
| 3482 | } |
| 3483 | |
| 3484 | return result; |
| 3485 | } |
| 3486 | |
| 3487 | static PaError AbortStream( PaStream *s ) |
| 3488 | { |
| 3489 | PaError result = paNoError; |
| 3490 | PaAsioStream *stream = (PaAsioStream*)s; |
| 3491 | ASIOError asioError; |
| 3492 | |
| 3493 | stream->zeroOutput = true; |
| 3494 | |
| 3495 | asioError = ASIOStop(); |
| 3496 | if( asioError == ASE_OK ) |
| 3497 | { |
| 3498 | EnsureCallbackHasCompleted( stream ); |
| 3499 | } |
| 3500 | else |
| 3501 | { |
| 3502 | result = paUnanticipatedHostError; |
| 3503 | PA_ASIO_SET_LAST_ASIO_ERROR( asioError ); |
| 3504 | } |
| 3505 | |
| 3506 | stream->isStopped = 1; |
| 3507 | stream->isActive = 0; |
| 3508 | |
| 3509 | if( !stream->streamFinishedCallbackCalled ) |
| 3510 | { |
| 3511 | if( stream->streamRepresentation.streamFinishedCallback != 0 ) |
| 3512 | stream->streamRepresentation.streamFinishedCallback( stream->streamRepresentation.userData ); |
| 3513 | } |
| 3514 | |
| 3515 | return result; |
| 3516 | } |
| 3517 | |
| 3518 | |
| 3519 | static PaError IsStreamStopped( PaStream *s ) |
| 3520 | { |
| 3521 | PaAsioStream *stream = (PaAsioStream*)s; |
| 3522 | |
| 3523 | return stream->isStopped; |
| 3524 | } |
| 3525 | |
| 3526 | |
| 3527 | static PaError IsStreamActive( PaStream *s ) |
| 3528 | { |
| 3529 | PaAsioStream *stream = (PaAsioStream*)s; |
| 3530 | |
| 3531 | return stream->isActive; |
| 3532 | } |
| 3533 | |
| 3534 | |
| 3535 | static PaTime GetStreamTime( PaStream *s ) |
| 3536 | { |
| 3537 | (void) s; /* unused parameter */ |
| 3538 | |
| 3539 | return (double)timeGetTime() * .001; |
| 3540 | } |
| 3541 | |
| 3542 | |
| 3543 | static double GetStreamCpuLoad( PaStream* s ) |
| 3544 | { |
| 3545 | PaAsioStream *stream = (PaAsioStream*)s; |
| 3546 | |
| 3547 | return PaUtil_GetCpuLoad( &stream->cpuLoadMeasurer ); |
| 3548 | } |
| 3549 | |
| 3550 | |
| 3551 | /* |
| 3552 | As separate stream interfaces are used for blocking and callback |
| 3553 | streams, the following functions can be guaranteed to only be called |
| 3554 | for blocking streams. |
| 3555 | */ |
| 3556 | |
| 3557 | static PaError ReadStream( PaStream *s , |
| 3558 | void *buffer, |
| 3559 | unsigned long frames ) |
| 3560 | { |
| 3561 | PaError result = paNoError; /* Initial return value. */ |
| 3562 | PaAsioStream *stream = (PaAsioStream*)s; /* The PA ASIO stream. */ |
| 3563 | |
| 3564 | /* Pointer to the blocking i/o data struct. */ |
| 3565 | PaAsioStreamBlockingState *blockingState = stream->blockingState; |
| 3566 | |
| 3567 | /* Get blocking i/o buffer processor and ring buffer pointers. */ |
| 3568 | PaUtilBufferProcessor *pBp = &blockingState->bufferProcessor; |
| 3569 | PaUtilRingBuffer *pRb = &blockingState->readRingBuffer; |
| 3570 | |
| 3571 | /* Ring buffer segment(s) used for writing. */ |
| 3572 | void *pRingBufferData1st = NULL; /* First segment. (Mandatory) */ |
| 3573 | void *pRingBufferData2nd = NULL; /* Second segment. (Optional) */ |
| 3574 | |
| 3575 | /* Number of frames per ring buffer segment. */ |
| 3576 | long lRingBufferSize1st = 0; /* First segment. (Mandatory) */ |
| 3577 | long lRingBufferSize2nd = 0; /* Second segment. (Optional) */ |
| 3578 | |
| 3579 | /* Get number of frames to be processed per data block. */ |
| 3580 | unsigned long lFramesPerBlock = stream->bufferProcessor.framesPerUserBuffer; |
| 3581 | /* Actual number of frames that has been copied into the ring buffer. */ |
| 3582 | unsigned long lFramesCopied = 0; |
| 3583 | /* The number of remaining unprocessed dtat frames. */ |
| 3584 | unsigned long lFramesRemaining = frames; |
| 3585 | |
| 3586 | /* Copy the input argument to avoid pointer increment! */ |
| 3587 | const void *userBuffer; |
| 3588 | unsigned int i; /* Just a counter. */ |
| 3589 | |
| 3590 | /* About the time, needed to process 8 data blocks. */ |
| 3591 | DWORD timeout = (DWORD)( 8 * lFramesPerBlock * 1000 / stream->streamRepresentation.streamInfo.sampleRate ); |
| 3592 | DWORD waitResult = 0; |
| 3593 | |
| 3594 | |
| 3595 | /* Check if the stream is still available ready to gather new data. */ |
| 3596 | if( blockingState->stopFlag || !stream->isActive ) |
| 3597 | { |
| 3598 | PA_DEBUG(("Warning! Stream no longer available for reading in ReadStream()\n")); |
| 3599 | result = paStreamIsStopped; |
| 3600 | return result; |
| 3601 | } |
| 3602 | |
| 3603 | /* If the stream is a input stream. */ |
| 3604 | if( stream->inputChannelCount ) |
| 3605 | { |
| 3606 | /* Prepare buffer access. */ |
| 3607 | if( !pBp->userOutputIsInterleaved ) |
| 3608 | { |
| 3609 | userBuffer = blockingState->readStreamBuffer; |
| 3610 | for( i = 0; i<pBp->inputChannelCount; ++i ) |
| 3611 | { |
| 3612 | ((void**)userBuffer)[i] = ((void**)buffer)[i]; |
| 3613 | } |
| 3614 | } /* Use the unchanged buffer. */ |
| 3615 | else { userBuffer = buffer; } |
| 3616 | |
| 3617 | do /* Internal block processing for too large user data buffers. */ |
| 3618 | { |
| 3619 | /* Get the size of the current data block to be processed. */ |
| 3620 | lFramesPerBlock =(lFramesPerBlock < lFramesRemaining) |
| 3621 | ? lFramesPerBlock : lFramesRemaining; |
| 3622 | /* Use predefined block size for as long there are enough |
| 3623 | buffers available, thereafter reduce the processing block |
| 3624 | size to match the number of remaining buffers. So the final |
| 3625 | data block is processed although it may be incomplete. */ |
| 3626 | |
| 3627 | /* If the available amount of data frames is insufficient. */ |
| 3628 | if( PaUtil_GetRingBufferReadAvailable(pRb) < (long) lFramesPerBlock ) |
| 3629 | { |
| 3630 | /* Make sure, the event isn't already set! */ |
| 3631 | /* ResetEvent( blockingState->readFramesReadyEvent ); */ |
| 3632 | |
| 3633 | /* Set the number of requested buffers. */ |
| 3634 | blockingState->readFramesRequested = lFramesPerBlock; |
| 3635 | |
| 3636 | /* Signalize that additional buffers are need. */ |
| 3637 | blockingState->readFramesRequestedFlag = TRUE; |
| 3638 | |
| 3639 | /* Wait until requested number of buffers has been freed. */ |
| 3640 | waitResult = WaitForSingleObject( blockingState->readFramesReadyEvent, timeout ); |
| 3641 | |
| 3642 | /* If something seriously went wrong... */ |
| 3643 | if( waitResult == WAIT_FAILED ) |
| 3644 | { |
| 3645 | PA_DEBUG(("WaitForSingleObject() failed in ReadStream()\n")); |
| 3646 | result = paUnanticipatedHostError; |
| 3647 | PA_ASIO_SET_LAST_SYSTEM_ERROR( GetLastError() ); |
| 3648 | return result; |
| 3649 | } |
| 3650 | else if( waitResult == WAIT_TIMEOUT ) |
| 3651 | { |
| 3652 | PA_DEBUG(("WaitForSingleObject() timed out in ReadStream()\n")); |
| 3653 | |
| 3654 | /* If block processing has stopped, abort! */ |
| 3655 | if( blockingState->stopFlag ) { return result = paStreamIsStopped; } |
| 3656 | |
| 3657 | /* If a timeout is encountered, give up eventually. */ |
| 3658 | return result = paTimedOut; |
| 3659 | } |
| 3660 | } |
| 3661 | /* Now, the ring buffer contains the required amount of data |
| 3662 | frames. |
| 3663 | (Therefor we don't need to check the return argument of |
| 3664 | PaUtil_GetRingBufferReadRegions(). ;-) ) |
| 3665 | */ |
| 3666 | |
| 3667 | /* Retrieve pointer(s) to the ring buffer's current write |
| 3668 | position(s). If the first buffer segment is too small to |
| 3669 | store the requested number of bytes, an additional second |
| 3670 | segment is returned. Otherwise, i.e. if the first segment |
| 3671 | is large enough, the second segment's pointer will be NULL. |
| 3672 | */ |
| 3673 | PaUtil_GetRingBufferReadRegions(pRb , |
| 3674 | lFramesPerBlock , |
| 3675 | &pRingBufferData1st, |
| 3676 | &lRingBufferSize1st, |
| 3677 | &pRingBufferData2nd, |
| 3678 | &lRingBufferSize2nd); |
| 3679 | |
| 3680 | /* Set number of frames to be copied from the ring buffer. */ |
| 3681 | PaUtil_SetInputFrameCount( pBp, lRingBufferSize1st ); |
| 3682 | /* Setup ring buffer access. */ |
| 3683 | PaUtil_SetInterleavedInputChannels(pBp , /* Buffer processor. */ |
| 3684 | 0 , /* The first channel's index. */ |
| 3685 | pRingBufferData1st, /* First ring buffer segment. */ |
| 3686 | 0 ); /* Use all available channels. */ |
| 3687 | |
| 3688 | /* If a second ring buffer segment is required. */ |
| 3689 | if( lRingBufferSize2nd ) { |
| 3690 | /* Set number of frames to be copied from the ring buffer. */ |
| 3691 | PaUtil_Set2ndInputFrameCount( pBp, lRingBufferSize2nd ); |
| 3692 | /* Setup ring buffer access. */ |
| 3693 | PaUtil_Set2ndInterleavedInputChannels(pBp , /* Buffer processor. */ |
| 3694 | 0 , /* The first channel's index. */ |
| 3695 | pRingBufferData2nd, /* Second ring buffer segment. */ |
| 3696 | 0 ); /* Use all available channels. */ |
| 3697 | } |
| 3698 | |
| 3699 | /* Let the buffer processor handle "copy and conversion" and |
| 3700 | update the ring buffer indices manually. */ |
| 3701 | lFramesCopied = PaUtil_CopyInput( pBp, &buffer, lFramesPerBlock ); |
| 3702 | PaUtil_AdvanceRingBufferReadIndex( pRb, lFramesCopied ); |
| 3703 | |
| 3704 | /* Decrease number of unprocessed frames. */ |
| 3705 | lFramesRemaining -= lFramesCopied; |
| 3706 | |
| 3707 | } /* Continue with the next data chunk. */ |
| 3708 | while( lFramesRemaining ); |
| 3709 | |
| 3710 | |
| 3711 | /* If there has been an input overflow within the callback */ |
| 3712 | if( blockingState->inputOverflowFlag ) |
| 3713 | { |
| 3714 | blockingState->inputOverflowFlag = FALSE; |
| 3715 | |
| 3716 | /* Return the corresponding error code. */ |
| 3717 | result = paInputOverflowed; |
| 3718 | } |
| 3719 | |
| 3720 | } /* If this is not an input stream. */ |
| 3721 | else { |
| 3722 | result = paCanNotReadFromAnOutputOnlyStream; |
| 3723 | } |
| 3724 | |
| 3725 | return result; |
| 3726 | } |
| 3727 | |
| 3728 | static PaError WriteStream( PaStream *s , |
| 3729 | const void *buffer, |
| 3730 | unsigned long frames ) |
| 3731 | { |
| 3732 | PaError result = paNoError; /* Initial return value. */ |
| 3733 | PaAsioStream *stream = (PaAsioStream*)s; /* The PA ASIO stream. */ |
| 3734 | |
| 3735 | /* Pointer to the blocking i/o data struct. */ |
| 3736 | PaAsioStreamBlockingState *blockingState = stream->blockingState; |
| 3737 | |
| 3738 | /* Get blocking i/o buffer processor and ring buffer pointers. */ |
| 3739 | PaUtilBufferProcessor *pBp = &blockingState->bufferProcessor; |
| 3740 | PaUtilRingBuffer *pRb = &blockingState->writeRingBuffer; |
| 3741 | |
| 3742 | /* Ring buffer segment(s) used for writing. */ |
| 3743 | void *pRingBufferData1st = NULL; /* First segment. (Mandatory) */ |
| 3744 | void *pRingBufferData2nd = NULL; /* Second segment. (Optional) */ |
| 3745 | |
| 3746 | /* Number of frames per ring buffer segment. */ |
| 3747 | long lRingBufferSize1st = 0; /* First segment. (Mandatory) */ |
| 3748 | long lRingBufferSize2nd = 0; /* Second segment. (Optional) */ |
| 3749 | |
| 3750 | /* Get number of frames to be processed per data block. */ |
| 3751 | unsigned long lFramesPerBlock = stream->bufferProcessor.framesPerUserBuffer; |
| 3752 | /* Actual number of frames that has been copied into the ring buffer. */ |
| 3753 | unsigned long lFramesCopied = 0; |
| 3754 | /* The number of remaining unprocessed dtat frames. */ |
| 3755 | unsigned long lFramesRemaining = frames; |
| 3756 | |
| 3757 | /* About the time, needed to process 8 data blocks. */ |
| 3758 | DWORD timeout = (DWORD)( 8 * lFramesPerBlock * 1000 / stream->streamRepresentation.streamInfo.sampleRate ); |
| 3759 | DWORD waitResult = 0; |
| 3760 | |
| 3761 | /* Copy the input argument to avoid pointer increment! */ |
| 3762 | const void *userBuffer; |
| 3763 | unsigned int i; /* Just a counter. */ |
| 3764 | |
| 3765 | |
| 3766 | /* Check if the stream is still available ready to receive new data. */ |
| 3767 | if( blockingState->stopFlag || !stream->isActive ) |
| 3768 | { |
| 3769 | PA_DEBUG(("Warning! Stream no longer available for writing in WriteStream()\n")); |
| 3770 | result = paStreamIsStopped; |
| 3771 | return result; |
| 3772 | } |
| 3773 | |
| 3774 | /* If the stream is a output stream. */ |
| 3775 | if( stream->outputChannelCount ) |
| 3776 | { |
| 3777 | /* Prepare buffer access. */ |
| 3778 | if( !pBp->userOutputIsInterleaved ) |
| 3779 | { |
| 3780 | userBuffer = blockingState->writeStreamBuffer; |
| 3781 | for( i = 0; i<pBp->outputChannelCount; ++i ) |
| 3782 | { |
| 3783 | ((const void**)userBuffer)[i] = ((const void**)buffer)[i]; |
| 3784 | } |
| 3785 | } /* Use the unchanged buffer. */ |
| 3786 | else { userBuffer = buffer; } |
| 3787 | |
| 3788 | |
| 3789 | do /* Internal block processing for too large user data buffers. */ |
| 3790 | { |
| 3791 | /* Get the size of the current data block to be processed. */ |
| 3792 | lFramesPerBlock =(lFramesPerBlock < lFramesRemaining) |
| 3793 | ? lFramesPerBlock : lFramesRemaining; |
| 3794 | /* Use predefined block size for as long there are enough |
| 3795 | frames available, thereafter reduce the processing block |
| 3796 | size to match the number of remaining frames. So the final |
| 3797 | data block is processed although it may be incomplete. */ |
| 3798 | |
| 3799 | /* If the available amount of buffers is insufficient. */ |
| 3800 | if( PaUtil_GetRingBufferWriteAvailable(pRb) < (long) lFramesPerBlock ) |
| 3801 | { |
| 3802 | /* Make sure, the event isn't already set! */ |
| 3803 | /* ResetEvent( blockingState->writeBuffersReadyEvent ); */ |
| 3804 | |
| 3805 | /* Set the number of requested buffers. */ |
| 3806 | blockingState->writeBuffersRequested = lFramesPerBlock; |
| 3807 | |
| 3808 | /* Signalize that additional buffers are need. */ |
| 3809 | blockingState->writeBuffersRequestedFlag = TRUE; |
| 3810 | |
| 3811 | /* Wait until requested number of buffers has been freed. */ |
| 3812 | waitResult = WaitForSingleObject( blockingState->writeBuffersReadyEvent, timeout ); |
| 3813 | |
| 3814 | /* If something seriously went wrong... */ |
| 3815 | if( waitResult == WAIT_FAILED ) |
| 3816 | { |
| 3817 | PA_DEBUG(("WaitForSingleObject() failed in WriteStream()\n")); |
| 3818 | result = paUnanticipatedHostError; |
| 3819 | PA_ASIO_SET_LAST_SYSTEM_ERROR( GetLastError() ); |
| 3820 | return result; |
| 3821 | } |
| 3822 | else if( waitResult == WAIT_TIMEOUT ) |
| 3823 | { |
| 3824 | PA_DEBUG(("WaitForSingleObject() timed out in WriteStream()\n")); |
| 3825 | |
| 3826 | /* If block processing has stopped, abort! */ |
| 3827 | if( blockingState->stopFlag ) { return result = paStreamIsStopped; } |
| 3828 | |
| 3829 | /* If a timeout is encountered, give up eventually. */ |
| 3830 | return result = paTimedOut; |
| 3831 | } |
| 3832 | } |
| 3833 | /* Now, the ring buffer contains the required amount of free |
| 3834 | space to store the provided number of data frames. |
| 3835 | (Therefor we don't need to check the return argument of |
| 3836 | PaUtil_GetRingBufferWriteRegions(). ;-) ) |
| 3837 | */ |
| 3838 | |
| 3839 | /* Retrieve pointer(s) to the ring buffer's current write |
| 3840 | position(s). If the first buffer segment is too small to |
| 3841 | store the requested number of bytes, an additional second |
| 3842 | segment is returned. Otherwise, i.e. if the first segment |
| 3843 | is large enough, the second segment's pointer will be NULL. |
| 3844 | */ |
| 3845 | PaUtil_GetRingBufferWriteRegions(pRb , |
| 3846 | lFramesPerBlock , |
| 3847 | &pRingBufferData1st, |
| 3848 | &lRingBufferSize1st, |
| 3849 | &pRingBufferData2nd, |
| 3850 | &lRingBufferSize2nd); |
| 3851 | |
| 3852 | /* Set number of frames to be copied to the ring buffer. */ |
| 3853 | PaUtil_SetOutputFrameCount( pBp, lRingBufferSize1st ); |
| 3854 | /* Setup ring buffer access. */ |
| 3855 | PaUtil_SetInterleavedOutputChannels(pBp , /* Buffer processor. */ |
| 3856 | 0 , /* The first channel's index. */ |
| 3857 | pRingBufferData1st, /* First ring buffer segment. */ |
| 3858 | 0 ); /* Use all available channels. */ |
| 3859 | |
| 3860 | /* If a second ring buffer segment is required. */ |
| 3861 | if( lRingBufferSize2nd ) { |
| 3862 | /* Set number of frames to be copied to the ring buffer. */ |
| 3863 | PaUtil_Set2ndOutputFrameCount( pBp, lRingBufferSize2nd ); |
| 3864 | /* Setup ring buffer access. */ |
| 3865 | PaUtil_Set2ndInterleavedOutputChannels(pBp , /* Buffer processor. */ |
| 3866 | 0 , /* The first channel's index. */ |
| 3867 | pRingBufferData2nd, /* Second ring buffer segment. */ |
| 3868 | 0 ); /* Use all available channels. */ |
| 3869 | } |
| 3870 | |
| 3871 | /* Let the buffer processor handle "copy and conversion" and |
| 3872 | update the ring buffer indices manually. */ |
| 3873 | lFramesCopied = PaUtil_CopyOutput( pBp, &userBuffer, lFramesPerBlock ); |
| 3874 | PaUtil_AdvanceRingBufferWriteIndex( pRb, lFramesCopied ); |
| 3875 | |
| 3876 | /* Decrease number of unprocessed frames. */ |
| 3877 | lFramesRemaining -= lFramesCopied; |
| 3878 | |
| 3879 | } /* Continue with the next data chunk. */ |
| 3880 | while( lFramesRemaining ); |
| 3881 | |
| 3882 | |
| 3883 | /* If there has been an output underflow within the callback */ |
| 3884 | if( blockingState->outputUnderflowFlag ) |
| 3885 | { |
| 3886 | blockingState->outputUnderflowFlag = FALSE; |
| 3887 | |
| 3888 | /* Return the corresponding error code. */ |
| 3889 | result = paOutputUnderflowed; |
| 3890 | } |
| 3891 | |
| 3892 | } /* If this is not an output stream. */ |
| 3893 | else |
| 3894 | { |
| 3895 | result = paCanNotWriteToAnInputOnlyStream; |
| 3896 | } |
| 3897 | |
| 3898 | return result; |
| 3899 | } |
| 3900 | |
| 3901 | |
| 3902 | static signed long GetStreamReadAvailable( PaStream* s ) |
| 3903 | { |
| 3904 | PaAsioStream *stream = (PaAsioStream*)s; |
| 3905 | |
| 3906 | /* Call buffer utility routine to get the number of available frames. */ |
| 3907 | return PaUtil_GetRingBufferReadAvailable( &stream->blockingState->readRingBuffer ); |
| 3908 | } |
| 3909 | |
| 3910 | |
| 3911 | static signed long GetStreamWriteAvailable( PaStream* s ) |
| 3912 | { |
| 3913 | PaAsioStream *stream = (PaAsioStream*)s; |
| 3914 | |
| 3915 | /* Call buffer utility routine to get the number of empty buffers. */ |
| 3916 | return PaUtil_GetRingBufferWriteAvailable( &stream->blockingState->writeRingBuffer ); |
| 3917 | } |
| 3918 | |
| 3919 | |
| 3920 | /* This routine will be called by the PortAudio engine when audio is needed. |
| 3921 | ** It may called at interrupt level on some machines so don't do anything |
| 3922 | ** that could mess up the system like calling malloc() or free(). |
| 3923 | */ |
| 3924 | static int BlockingIoPaCallback(const void *inputBuffer , |
| 3925 | void *outputBuffer , |
| 3926 | unsigned long framesPerBuffer, |
| 3927 | const PaStreamCallbackTimeInfo *timeInfo , |
| 3928 | PaStreamCallbackFlags statusFlags , |
| 3929 | void *userData ) |
| 3930 | { |
| 3931 | PaError result = paNoError; /* Initial return value. */ |
| 3932 | PaAsioStream *stream = *(PaAsioStream**)userData; /* The PA ASIO stream. */ |
| 3933 | PaAsioStreamBlockingState *blockingState = stream->blockingState; /* Persume blockingState is valid, otherwise the callback wouldn't be running. */ |
| 3934 | |
| 3935 | /* Get a pointer to the stream's blocking i/o buffer processor. */ |
| 3936 | PaUtilBufferProcessor *pBp = &blockingState->bufferProcessor; |
| 3937 | PaUtilRingBuffer *pRb = NULL; |
| 3938 | |
| 3939 | /* If output data has been requested. */ |
| 3940 | if( stream->outputChannelCount ) |
| 3941 | { |
| 3942 | /* If the callback input argument signalizes a output underflow, |
| 3943 | make sure the WriteStream() function knows about it, too! */ |
| 3944 | if( statusFlags & paOutputUnderflowed ) { |
| 3945 | blockingState->outputUnderflowFlag = TRUE; |
| 3946 | } |
| 3947 | |
| 3948 | /* Access the corresponding ring buffer. */ |
| 3949 | pRb = &blockingState->writeRingBuffer; |
| 3950 | |
| 3951 | /* If the blocking i/o buffer contains enough output data, */ |
| 3952 | if( PaUtil_GetRingBufferReadAvailable(pRb) >= (long) framesPerBuffer ) |
| 3953 | { |
| 3954 | /* Extract the requested data from the ring buffer. */ |
| 3955 | PaUtil_ReadRingBuffer( pRb, outputBuffer, framesPerBuffer ); |
| 3956 | } |
| 3957 | else /* If no output data is available :-( */ |
| 3958 | { |
| 3959 | /* Signalize a write-buffer underflow. */ |
| 3960 | blockingState->outputUnderflowFlag = TRUE; |
| 3961 | |
| 3962 | /* Fill the output buffer with silence. */ |
| 3963 | (*pBp->outputZeroer)( outputBuffer, 1, pBp->outputChannelCount * framesPerBuffer ); |
| 3964 | |
| 3965 | /* If playback is to be stopped */ |
| 3966 | if( blockingState->stopFlag && PaUtil_GetRingBufferReadAvailable(pRb) < (long) framesPerBuffer ) |
| 3967 | { |
| 3968 | /* Extract all the remaining data from the ring buffer, |
| 3969 | whether it is a complete data block or not. */ |
| 3970 | PaUtil_ReadRingBuffer( pRb, outputBuffer, PaUtil_GetRingBufferReadAvailable(pRb) ); |
| 3971 | } |
| 3972 | } |
| 3973 | |
| 3974 | /* Set blocking i/o event? */ |
| 3975 | if( blockingState->writeBuffersRequestedFlag && PaUtil_GetRingBufferWriteAvailable(pRb) >= (long) blockingState->writeBuffersRequested ) |
| 3976 | { |
| 3977 | /* Reset buffer request. */ |
| 3978 | blockingState->writeBuffersRequestedFlag = FALSE; |
| 3979 | blockingState->writeBuffersRequested = 0; |
| 3980 | /* Signalize that requested buffers are ready. */ |
| 3981 | SetEvent( blockingState->writeBuffersReadyEvent ); |
| 3982 | /* What do we do if SetEvent() returns zero, i.e. the event |
| 3983 | could not be set? How to return errors from within the |
| 3984 | callback? - S.Fischer */ |
| 3985 | } |
| 3986 | } |
| 3987 | |
| 3988 | /* If input data has been supplied. */ |
| 3989 | if( stream->inputChannelCount ) |
| 3990 | { |
| 3991 | /* If the callback input argument signalizes a input overflow, |
| 3992 | make sure the ReadStream() function knows about it, too! */ |
| 3993 | if( statusFlags & paInputOverflowed ) { |
| 3994 | blockingState->inputOverflowFlag = TRUE; |
| 3995 | } |
| 3996 | |
| 3997 | /* Access the corresponding ring buffer. */ |
| 3998 | pRb = &blockingState->readRingBuffer; |
| 3999 | |
| 4000 | /* If the blocking i/o buffer contains not enough input buffers */ |
| 4001 | if( PaUtil_GetRingBufferWriteAvailable(pRb) < (long) framesPerBuffer ) |
| 4002 | { |
| 4003 | /* Signalize a read-buffer overflow. */ |
| 4004 | blockingState->inputOverflowFlag = TRUE; |
| 4005 | |
| 4006 | /* Remove some old data frames from the buffer. */ |
| 4007 | PaUtil_AdvanceRingBufferReadIndex( pRb, framesPerBuffer ); |
| 4008 | } |
| 4009 | |
| 4010 | /* Insert the current input data into the ring buffer. */ |
| 4011 | PaUtil_WriteRingBuffer( pRb, inputBuffer, framesPerBuffer ); |
| 4012 | |
| 4013 | /* Set blocking i/o event? */ |
| 4014 | if( blockingState->readFramesRequestedFlag && PaUtil_GetRingBufferReadAvailable(pRb) >= (long) blockingState->readFramesRequested ) |
| 4015 | { |
| 4016 | /* Reset buffer request. */ |
| 4017 | blockingState->readFramesRequestedFlag = FALSE; |
| 4018 | blockingState->readFramesRequested = 0; |
| 4019 | /* Signalize that requested buffers are ready. */ |
| 4020 | SetEvent( blockingState->readFramesReadyEvent ); |
| 4021 | /* What do we do if SetEvent() returns zero, i.e. the event |
| 4022 | could not be set? How to return errors from within the |
| 4023 | callback? - S.Fischer */ |
| 4024 | /** @todo report an error with PA_DEBUG */ |
| 4025 | } |
| 4026 | } |
| 4027 | |
| 4028 | return paContinue; |
| 4029 | } |
| 4030 | |
| 4031 | |
| 4032 | PaError PaAsio_ShowControlPanel( PaDeviceIndex device, void* systemSpecific ) |
| 4033 | { |
| 4034 | PaError result = paNoError; |
| 4035 | PaUtilHostApiRepresentation *hostApi; |
| 4036 | PaDeviceIndex hostApiDevice; |
| 4037 | ASIODriverInfo asioDriverInfo; |
| 4038 | ASIOError asioError; |
| 4039 | int asioIsInitialized = 0; |
| 4040 | PaAsioHostApiRepresentation *asioHostApi; |
| 4041 | PaAsioDeviceInfo *asioDeviceInfo; |
| 4042 | PaWinUtilComInitializationResult comInitializationResult; |
| 4043 | |
| 4044 | /* initialize COM again here, we might be in another thread */ |
| 4045 | result = PaWinUtil_CoInitialize( paASIO, &comInitializationResult ); |
| 4046 | if( result != paNoError ) |
| 4047 | return result; |
| 4048 | |
| 4049 | result = PaUtil_GetHostApiRepresentation( &hostApi, paASIO ); |
| 4050 | if( result != paNoError ) |
| 4051 | goto error; |
| 4052 | |
| 4053 | result = PaUtil_DeviceIndexToHostApiDeviceIndex( &hostApiDevice, device, hostApi ); |
| 4054 | if( result != paNoError ) |
| 4055 | goto error; |
| 4056 | |
| 4057 | /* |
| 4058 | In theory we could proceed if the currently open device was the same |
| 4059 | one for which the control panel was requested, however because the |
| 4060 | window pointer is not available until this function is called we |
| 4061 | currently need to call ASIOInit() again here, which of course can't be |
| 4062 | done safely while a stream is open. |
| 4063 | */ |
| 4064 | |
| 4065 | asioHostApi = (PaAsioHostApiRepresentation*)hostApi; |
| 4066 | if( asioHostApi->openAsioDeviceIndex != paNoDevice ) |
| 4067 | { |
| 4068 | result = paDeviceUnavailable; |
| 4069 | goto error; |
| 4070 | } |
| 4071 | |
| 4072 | asioDeviceInfo = (PaAsioDeviceInfo*)hostApi->deviceInfos[hostApiDevice]; |
| 4073 | |
| 4074 | if( !asioHostApi->asioDrivers->loadDriver( const_cast<char*>(asioDeviceInfo->commonDeviceInfo.name) ) ) |
| 4075 | { |
| 4076 | result = paUnanticipatedHostError; |
| 4077 | goto error; |
| 4078 | } |
| 4079 | |
| 4080 | /* CRUCIAL!!! */ |
| 4081 | memset( &asioDriverInfo, 0, sizeof(ASIODriverInfo) ); |
| 4082 | asioDriverInfo.asioVersion = 2; |
| 4083 | asioDriverInfo.sysRef = systemSpecific; |
| 4084 | asioError = ASIOInit( &asioDriverInfo ); |
| 4085 | if( asioError != ASE_OK ) |
| 4086 | { |
| 4087 | result = paUnanticipatedHostError; |
| 4088 | PA_ASIO_SET_LAST_ASIO_ERROR( asioError ); |
| 4089 | goto error; |
| 4090 | } |
| 4091 | else |
| 4092 | { |
| 4093 | asioIsInitialized = 1; |
| 4094 | } |
| 4095 | |
| 4096 | PA_DEBUG(("PaAsio_ShowControlPanel: ASIOInit(): %s\n", PaAsio_GetAsioErrorText(asioError) )); |
| 4097 | PA_DEBUG(("asioVersion: ASIOInit(): %ld\n", asioDriverInfo.asioVersion )); |
| 4098 | PA_DEBUG(("driverVersion: ASIOInit(): %ld\n", asioDriverInfo.driverVersion )); |
| 4099 | PA_DEBUG(("Name: ASIOInit(): %s\n", asioDriverInfo.name )); |
| 4100 | PA_DEBUG(("ErrorMessage: ASIOInit(): %s\n", asioDriverInfo.errorMessage )); |
| 4101 | |
| 4102 | asioError = ASIOControlPanel(); |
| 4103 | if( asioError != ASE_OK ) |
| 4104 | { |
| 4105 | PA_DEBUG(("PaAsio_ShowControlPanel: ASIOControlPanel(): %s\n", PaAsio_GetAsioErrorText(asioError) )); |
| 4106 | result = paUnanticipatedHostError; |
| 4107 | PA_ASIO_SET_LAST_ASIO_ERROR( asioError ); |
| 4108 | goto error; |
| 4109 | } |
| 4110 | |
| 4111 | PA_DEBUG(("PaAsio_ShowControlPanel: ASIOControlPanel(): %s\n", PaAsio_GetAsioErrorText(asioError) )); |
| 4112 | |
| 4113 | asioError = ASIOExit(); |
| 4114 | if( asioError != ASE_OK ) |
| 4115 | { |
| 4116 | result = paUnanticipatedHostError; |
| 4117 | PA_ASIO_SET_LAST_ASIO_ERROR( asioError ); |
| 4118 | asioIsInitialized = 0; |
| 4119 | goto error; |
| 4120 | } |
| 4121 | |
| 4122 | PA_DEBUG(("PaAsio_ShowControlPanel: ASIOExit(): %s\n", PaAsio_GetAsioErrorText(asioError) )); |
| 4123 | |
| 4124 | return result; |
| 4125 | |
| 4126 | error: |
| 4127 | if( asioIsInitialized ) |
| 4128 | { |
| 4129 | ASIOExit(); |
| 4130 | } |
| 4131 | |
| 4132 | PaWinUtil_CoUninitialize( paASIO, &comInitializationResult ); |
| 4133 | |
| 4134 | return result; |
| 4135 | } |
| 4136 | |
| 4137 | |
| 4138 | PaError PaAsio_GetInputChannelName( PaDeviceIndex device, int channelIndex, |
| 4139 | const char** channelName ) |
| 4140 | { |
| 4141 | PaError result = paNoError; |
| 4142 | PaUtilHostApiRepresentation *hostApi; |
| 4143 | PaDeviceIndex hostApiDevice; |
| 4144 | PaAsioDeviceInfo *asioDeviceInfo; |
| 4145 | |
| 4146 | |
| 4147 | result = PaUtil_GetHostApiRepresentation( &hostApi, paASIO ); |
| 4148 | if( result != paNoError ) |
| 4149 | goto error; |
| 4150 | |
| 4151 | result = PaUtil_DeviceIndexToHostApiDeviceIndex( &hostApiDevice, device, hostApi ); |
| 4152 | if( result != paNoError ) |
| 4153 | goto error; |
| 4154 | |
| 4155 | asioDeviceInfo = (PaAsioDeviceInfo*)hostApi->deviceInfos[hostApiDevice]; |
| 4156 | |
| 4157 | if( channelIndex < 0 || channelIndex >= asioDeviceInfo->commonDeviceInfo.maxInputChannels ){ |
| 4158 | result = paInvalidChannelCount; |
| 4159 | goto error; |
| 4160 | } |
| 4161 | |
| 4162 | *channelName = asioDeviceInfo->asioChannelInfos[channelIndex].name; |
| 4163 | |
| 4164 | return paNoError; |
| 4165 | |
| 4166 | error: |
| 4167 | return result; |
| 4168 | } |
| 4169 | |
| 4170 | |
| 4171 | PaError PaAsio_GetOutputChannelName( PaDeviceIndex device, int channelIndex, |
| 4172 | const char** channelName ) |
| 4173 | { |
| 4174 | PaError result = paNoError; |
| 4175 | PaUtilHostApiRepresentation *hostApi; |
| 4176 | PaDeviceIndex hostApiDevice; |
| 4177 | PaAsioDeviceInfo *asioDeviceInfo; |
| 4178 | |
| 4179 | |
| 4180 | result = PaUtil_GetHostApiRepresentation( &hostApi, paASIO ); |
| 4181 | if( result != paNoError ) |
| 4182 | goto error; |
| 4183 | |
| 4184 | result = PaUtil_DeviceIndexToHostApiDeviceIndex( &hostApiDevice, device, hostApi ); |
| 4185 | if( result != paNoError ) |
| 4186 | goto error; |
| 4187 | |
| 4188 | asioDeviceInfo = (PaAsioDeviceInfo*)hostApi->deviceInfos[hostApiDevice]; |
| 4189 | |
| 4190 | if( channelIndex < 0 || channelIndex >= asioDeviceInfo->commonDeviceInfo.maxOutputChannels ){ |
| 4191 | result = paInvalidChannelCount; |
| 4192 | goto error; |
| 4193 | } |
| 4194 | |
| 4195 | *channelName = asioDeviceInfo->asioChannelInfos[ |
| 4196 | asioDeviceInfo->commonDeviceInfo.maxInputChannels + channelIndex].name; |
| 4197 | |
| 4198 | return paNoError; |
| 4199 | |
| 4200 | error: |
| 4201 | return result; |
| 4202 | } |
| 4203 | |
| 4204 | |
| 4205 | /* NOTE: the following functions are ASIO-stream specific, and are called directly |
| 4206 | by client code. We need to check for many more error conditions here because |
| 4207 | we don't have the benefit of pa_front.c's parameter checking. |
| 4208 | */ |
| 4209 | |
| 4210 | static PaError GetAsioStreamPointer( PaAsioStream **stream, PaStream *s ) |
| 4211 | { |
| 4212 | PaError result; |
| 4213 | PaUtilHostApiRepresentation *hostApi; |
| 4214 | PaAsioHostApiRepresentation *asioHostApi; |
| 4215 | |
| 4216 | result = PaUtil_ValidateStreamPointer( s ); |
| 4217 | if( result != paNoError ) |
| 4218 | return result; |
| 4219 | |
| 4220 | result = PaUtil_GetHostApiRepresentation( &hostApi, paASIO ); |
| 4221 | if( result != paNoError ) |
| 4222 | return result; |
| 4223 | |
| 4224 | asioHostApi = (PaAsioHostApiRepresentation*)hostApi; |
| 4225 | |
| 4226 | if( PA_STREAM_REP( s )->streamInterface == &asioHostApi->callbackStreamInterface |
| 4227 | || PA_STREAM_REP( s )->streamInterface == &asioHostApi->blockingStreamInterface ) |
| 4228 | { |
| 4229 | /* s is an ASIO stream */ |
| 4230 | *stream = (PaAsioStream *)s; |
| 4231 | return paNoError; |
| 4232 | } |
| 4233 | else |
| 4234 | { |
| 4235 | return paIncompatibleStreamHostApi; |
| 4236 | } |
| 4237 | } |
| 4238 | |
| 4239 | |
| 4240 | PaError PaAsio_SetStreamSampleRate( PaStream* s, double sampleRate ) |
| 4241 | { |
| 4242 | PaAsioStream *stream; |
| 4243 | PaError result = GetAsioStreamPointer( &stream, s ); |
| 4244 | if( result != paNoError ) |
| 4245 | return result; |
| 4246 | |
| 4247 | if( stream != theAsioStream ) |
| 4248 | return paBadStreamPtr; |
| 4249 | |
| 4250 | return ValidateAndSetSampleRate( sampleRate ); |
| 4251 | } |
| 4252 | #endif |