| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
|
| 45 |
|
| 46 |
|
| 47 |
|
| 48 |
|
| 49 |
|
| 50 |
|
| 51 |
|
| 52 |
|
| 53 |
|
| 54 |
|
| 55 |
|
| 56 |
|
| 57 |
|
| 58 |
|
| 59 |
|
| 60 |
|
| 61 |
|
| 62 |
|
| 63 |
|
| 64 |
|
| 65 |
|
| 66 |
|
| 67 |
|
| 68 |
|
| 69 |
|
| 70 |
|
| 71 |
|
| 72 |
|
| 73 |
|
| 74 |
|
| 75 |
|
| 76 |
|
| 77 |
|
| 78 |
|
| 79 |
|
| 80 |
|
| 81 |
|
| 82 |
|
| 83 |
|
| 84 |
|
| 85 |
|
| 86 |
|
| 87 |
|
| 88 |
|
| 89 |
|
| 90 |
|
| 91 |
|
| 92 |
|
| 93 |
|
| 94 |
|
| 95 |
|
| 96 |
|
| 97 |
|
| 98 |
|
| 99 |
|
| 100 |
|
| 101 |
|
| 102 |
|
| 103 |
|
| 104 |
|
| 105 |
|
| 106 |
|
| 107 |
|
| 108 |
|
| 109 |
|
| 110 |
|
| 111 |
|
| 112 |
|
| 113 |
|
| 114 |
|
| 115 |
|
| 116 |
|
| 117 |
|
| 118 |
|
| 119 |
|
| 120 |
|
| 121 |
|
| 122 |
|
| 123 |
|
| 124 |
|
| 125 |
|
| 126 |
|
| 127 |
|
| 128 |
|
| 129 |
|
| 130 |
|
| 131 |
|
| 132 |
|
| 133 |
|
| 134 |
|
| 135 |
|
| 136 |
|
| 137 |
|
| 138 |
|
| 139 |
|
| 140 |
|
| 141 |
|
| 142 |
|
| 143 |
#include <unistd.h> |
| 144 |
#include <stdio.h> |
| 145 |
#include <stdlib.h> |
| 146 |
#include <string.h> |
| 147 |
#include <pthread.h> |
| 148 |
#include <assert.h> |
| 149 |
#include <math.h> |
| 150 |
|
| 151 |
#include <asihpi/hpi.h> |
| 152 |
|
| 153 |
#include "portaudio.h" |
| 154 |
#include "pa_util.h" |
| 155 |
#include "pa_unix_util.h" |
| 156 |
#include "pa_allocation.h" |
| 157 |
#include "pa_hostapi.h" |
| 158 |
#include "pa_stream.h" |
| 159 |
#include "pa_cpuload.h" |
| 160 |
#include "pa_process.h" |
| 161 |
#include "pa_converters.h" |
| 162 |
#include "pa_debugprint.h" |
| 163 |
|
| 164 |
|
| 165 |
|
| 166 |
|
| 167 |
|
| 168 |
|
| 169 |
|
| 170 |
|
| 171 |
|
| 172 |
|
| 173 |
#define PA_ENSURE_(expr) \ |
| 174 |
do { \ |
| 175 |
PaError paError = (expr); \ |
| 176 |
if( UNLIKELY( paError < paNoError ) ) \ |
| 177 |
{ \ |
| 178 |
PA_DEBUG(( "Expression '" #expr "' failed in '" __FILE__ "', line: " STRINGIZE( __LINE__ ) "\n" )); \ |
| 179 |
result = paError; \ |
| 180 |
goto error; \ |
| 181 |
} \ |
| 182 |
} while (0); |
| 183 |
|
| 184 |
|
| 185 |
#define PA_UNLESS_(expr, paError) \ |
| 186 |
do { \ |
| 187 |
if( UNLIKELY( (expr) == 0 ) ) \ |
| 188 |
{ \ |
| 189 |
PA_DEBUG(( "Expression '" #expr "' failed in '" __FILE__ "', line: " STRINGIZE( __LINE__ ) "\n" )); \ |
| 190 |
result = (paError); \ |
| 191 |
goto error; \ |
| 192 |
} \ |
| 193 |
} while( 0 ); |
| 194 |
|
| 195 |
|
| 196 |
#define PA_ASIHPI_UNLESS_(expr, paError) \ |
| 197 |
do { \ |
| 198 |
hpi_err_t hpiError = (expr); \ |
| 199 |
\ |
| 200 |
if( UNLIKELY( hpiError ) ) \ |
| 201 |
{ \ |
| 202 |
char szError[256]; \ |
| 203 |
HPI_GetErrorText( hpiError, szError ); \ |
| 204 |
PA_DEBUG(( "HPI error %d occurred: %s\n", hpiError, szError )); \ |
| 205 |
\ |
| 206 |
PA_DEBUG(( "Expression '" #expr "' failed in '" __FILE__ "', line: " STRINGIZE( __LINE__ ) "\n" )); \ |
| 207 |
if( (paError) == paUnanticipatedHostError ) \ |
| 208 |
{ \ |
| 209 |
PA_DEBUG(( "Host error description: %s\n", szError )); \ |
| 210 |
\ |
| 211 |
if( pthread_equal( pthread_self(), paUnixMainThread ) ) \ |
| 212 |
{ \ |
| 213 |
PaUtil_SetLastHostErrorInfo( paInDevelopment, hpiError, szError ); \ |
| 214 |
} \ |
| 215 |
} \ |
| 216 |
\ |
| 217 |
\ |
| 218 |
if( (paError) < 0 ) \ |
| 219 |
{ \ |
| 220 |
result = (paError); \ |
| 221 |
goto error; \ |
| 222 |
} \ |
| 223 |
} \ |
| 224 |
} while( 0 ); |
| 225 |
|
| 226 |
|
| 227 |
#define PA_ASIHPI_REPORT_ERROR_(hpiErrorCode) \ |
| 228 |
do { \ |
| 229 |
char szError[256]; \ |
| 230 |
HPI_GetErrorText( hpiError, szError ); \ |
| 231 |
PA_DEBUG(( "HPI error %d occurred: %s\n", hpiError, szError )); \ |
| 232 |
\ |
| 233 |
if( pthread_equal( pthread_self(), paUnixMainThread ) ) \ |
| 234 |
{ \ |
| 235 |
PaUtil_SetLastHostErrorInfo( paInDevelopment, (hpiErrorCode), szError ); \ |
| 236 |
} \ |
| 237 |
} while( 0 ); |
| 238 |
|
| 239 |
|
| 240 |
|
| 241 |
|
| 242 |
#define PA_ASIHPI_AVAILABLE_FORMATS_ (paFloat32 | paInt32 | paInt24 | paInt16 | paUInt8) |
| 243 |
|
| 244 |
#define PA_ASIHPI_USE_BBM_ 1 |
| 245 |
|
| 246 |
|
| 247 |
#define PA_ASIHPI_MIN_FRAMES_ 1152 |
| 248 |
|
| 249 |
#define PA_ASIHPI_MIN_POLLING_INTERVAL_ 10 |
| 250 |
|
| 251 |
|
| 252 |
|
| 253 |
|
| 254 |
|
| 255 |
|
| 256 |
|
| 257 |
|
| 258 |
typedef struct PaAsiHpiHostApiRepresentation |
| 259 |
{ |
| 260 |
|
| 261 |
PaUtilHostApiRepresentation baseHostApiRep; |
| 262 |
PaUtilStreamInterface callbackStreamInterface; |
| 263 |
PaUtilStreamInterface blockingStreamInterface; |
| 264 |
|
| 265 |
PaUtilAllocationGroup *allocations; |
| 266 |
|
| 267 |
|
| 268 |
|
| 269 |
PaHostApiIndex hostApiIndex; |
| 270 |
} |
| 271 |
PaAsiHpiHostApiRepresentation; |
| 272 |
|
| 273 |
|
| 274 |
|
| 275 |
typedef struct PaAsiHpiDeviceInfo |
| 276 |
{ |
| 277 |
|
| 278 |
|
| 279 |
PaDeviceInfo baseDeviceInfo; |
| 280 |
|
| 281 |
|
| 282 |
|
| 283 |
|
| 284 |
uint16_t adapterIndex; |
| 285 |
|
| 286 |
uint16_t adapterType; |
| 287 |
|
| 288 |
uint16_t adapterVersion; |
| 289 |
|
| 290 |
uint32_t adapterSerialNumber; |
| 291 |
|
| 292 |
uint16_t streamIndex; |
| 293 |
|
| 294 |
uint16_t streamIsOutput; |
| 295 |
} |
| 296 |
PaAsiHpiDeviceInfo; |
| 297 |
|
| 298 |
|
| 299 |
|
| 300 |
|
| 301 |
|
| 302 |
|
| 303 |
|
| 304 |
|
| 305 |
|
| 306 |
|
| 307 |
|
| 308 |
|
| 309 |
|
| 310 |
|
| 311 |
|
| 312 |
|
| 313 |
|
| 314 |
|
| 315 |
typedef enum PaAsiHpiStreamState |
| 316 |
{ |
| 317 |
paAsiHpiStoppedState=0, |
| 318 |
paAsiHpiActiveState=1, |
| 319 |
paAsiHpiCallbackFinishedState=2 |
| 320 |
} |
| 321 |
PaAsiHpiStreamState; |
| 322 |
|
| 323 |
|
| 324 |
|
| 325 |
typedef struct PaAsiHpiStreamComponent |
| 326 |
{ |
| 327 |
|
| 328 |
PaAsiHpiDeviceInfo *hpiDevice; |
| 329 |
|
| 330 |
hpi_handle_t hpiStream; |
| 331 |
|
| 332 |
struct hpi_format hpiFormat; |
| 333 |
|
| 334 |
uint32_t bytesPerFrame; |
| 335 |
|
| 336 |
uint32_t hardwareBufferSize; |
| 337 |
|
| 338 |
uint32_t hostBufferSize; |
| 339 |
|
| 340 |
|
| 341 |
|
| 342 |
|
| 343 |
uint32_t outputBufferCap; |
| 344 |
|
| 345 |
uint8_t *tempBuffer; |
| 346 |
|
| 347 |
uint32_t tempBufferSize; |
| 348 |
} |
| 349 |
PaAsiHpiStreamComponent; |
| 350 |
|
| 351 |
|
| 352 |
|
| 353 |
typedef struct PaAsiHpiStream |
| 354 |
{ |
| 355 |
|
| 356 |
PaUtilStreamRepresentation baseStreamRep; |
| 357 |
PaUtilCpuLoadMeasurer cpuLoadMeasurer; |
| 358 |
PaUtilBufferProcessor bufferProcessor; |
| 359 |
|
| 360 |
PaUtilAllocationGroup *allocations; |
| 361 |
|
| 362 |
|
| 363 |
|
| 364 |
|
| 365 |
PaAsiHpiStreamComponent *input, *output; |
| 366 |
|
| 367 |
|
| 368 |
uint32_t pollingInterval; |
| 369 |
|
| 370 |
int callbackMode; |
| 371 |
|
| 372 |
unsigned long maxFramesPerHostBuffer; |
| 373 |
|
| 374 |
int neverDropInput; |
| 375 |
|
| 376 |
|
| 377 |
|
| 378 |
|
| 379 |
void **blockingUserBufferCopy; |
| 380 |
|
| 381 |
|
| 382 |
|
| 383 |
|
| 384 |
PaUnixThread thread; |
| 385 |
|
| 386 |
volatile sig_atomic_t state; |
| 387 |
|
| 388 |
volatile sig_atomic_t callbackAbort; |
| 389 |
|
| 390 |
|
| 391 |
volatile sig_atomic_t callbackFinished; |
| 392 |
} |
| 393 |
PaAsiHpiStream; |
| 394 |
|
| 395 |
|
| 396 |
|
| 397 |
typedef struct PaAsiHpiStreamInfo |
| 398 |
{ |
| 399 |
|
| 400 |
uint16_t state; |
| 401 |
|
| 402 |
uint32_t bufferSize; |
| 403 |
|
| 404 |
uint32_t dataSize; |
| 405 |
|
| 406 |
uint32_t frameCounter; |
| 407 |
|
| 408 |
|
| 409 |
|
| 410 |
uint32_t auxDataSize; |
| 411 |
|
| 412 |
uint32_t totalBufferedData; |
| 413 |
|
| 414 |
|
| 415 |
|
| 416 |
uint32_t availableFrames; |
| 417 |
|
| 418 |
int overflow; |
| 419 |
|
| 420 |
int underflow; |
| 421 |
} |
| 422 |
PaAsiHpiStreamInfo; |
| 423 |
|
| 424 |
|
| 425 |
|
| 426 |
|
| 427 |
|
| 428 |
|
| 429 |
|
| 430 |
#ifdef __cplusplus |
| 431 |
extern "C" |
| 432 |
{ |
| 433 |
#endif |
| 434 |
|
| 435 |
|
| 436 |
PaError PaAsiHpi_Initialize( PaUtilHostApiRepresentation **hostApi, PaHostApiIndex index ); |
| 437 |
|
| 438 |
#ifdef __cplusplus |
| 439 |
} |
| 440 |
#endif |
| 441 |
|
| 442 |
static void Terminate( struct PaUtilHostApiRepresentation *hostApi ); |
| 443 |
static PaError IsFormatSupported( struct PaUtilHostApiRepresentation *hostApi, |
| 444 |
const PaStreamParameters *inputParameters, |
| 445 |
const PaStreamParameters *outputParameters, |
| 446 |
double sampleRate ); |
| 447 |
|
| 448 |
|
| 449 |
static PaError OpenStream( struct PaUtilHostApiRepresentation *hostApi, |
| 450 |
PaStream **s, |
| 451 |
const PaStreamParameters *inputParameters, |
| 452 |
const PaStreamParameters *outputParameters, |
| 453 |
double sampleRate, |
| 454 |
unsigned long framesPerBuffer, |
| 455 |
PaStreamFlags streamFlags, |
| 456 |
PaStreamCallback *streamCallback, |
| 457 |
void *userData ); |
| 458 |
static PaError CloseStream( PaStream *s ); |
| 459 |
static PaError StartStream( PaStream *s ); |
| 460 |
static PaError StopStream( PaStream *s ); |
| 461 |
static PaError AbortStream( PaStream *s ); |
| 462 |
static PaError IsStreamStopped( PaStream *s ); |
| 463 |
static PaError IsStreamActive( PaStream *s ); |
| 464 |
static PaTime GetStreamTime( PaStream *s ); |
| 465 |
static double GetStreamCpuLoad( PaStream *s ); |
| 466 |
|
| 467 |
|
| 468 |
static PaError ReadStream( PaStream *s, void *buffer, unsigned long frames ); |
| 469 |
static PaError WriteStream( PaStream *s, const void *buffer, unsigned long frames ); |
| 470 |
static signed long GetStreamReadAvailable( PaStream *s ); |
| 471 |
static signed long GetStreamWriteAvailable( PaStream *s ); |
| 472 |
|
| 473 |
|
| 474 |
static void *CallbackThreadFunc( void *userData ); |
| 475 |
|
| 476 |
|
| 477 |
static PaError PaAsiHpi_BuildDeviceList( PaAsiHpiHostApiRepresentation *hpiHostApi ); |
| 478 |
static uint16_t PaAsiHpi_PaToHpiFormat( PaSampleFormat paFormat ); |
| 479 |
static PaSampleFormat PaAsiHpi_HpiToPaFormat( uint16_t hpiFormat ); |
| 480 |
static PaError PaAsiHpi_CreateFormat( struct PaUtilHostApiRepresentation *hostApi, |
| 481 |
const PaStreamParameters *parameters, double sampleRate, |
| 482 |
PaAsiHpiDeviceInfo **hpiDevice, struct hpi_format *hpiFormat ); |
| 483 |
static PaError PaAsiHpi_OpenInput( struct PaUtilHostApiRepresentation *hostApi, |
| 484 |
const PaAsiHpiDeviceInfo *hpiDevice, const struct hpi_format *hpiFormat, |
| 485 |
hpi_handle_t *hpiStream ); |
| 486 |
static PaError PaAsiHpi_OpenOutput( struct PaUtilHostApiRepresentation *hostApi, |
| 487 |
const PaAsiHpiDeviceInfo *hpiDevice, const struct hpi_format *hpiFormat, |
| 488 |
hpi_handle_t *hpiStream ); |
| 489 |
static PaError PaAsiHpi_GetStreamInfo( PaAsiHpiStreamComponent *streamComp, PaAsiHpiStreamInfo *info ); |
| 490 |
static void PaAsiHpi_StreamComponentDump( PaAsiHpiStreamComponent *streamComp, PaAsiHpiStream *stream ); |
| 491 |
static void PaAsiHpi_StreamDump( PaAsiHpiStream *stream ); |
| 492 |
static PaError PaAsiHpi_SetupBuffers( PaAsiHpiStreamComponent *streamComp, uint32_t pollingInterval, |
| 493 |
unsigned long framesPerPaHostBuffer, PaTime suggestedLatency ); |
| 494 |
static PaError PaAsiHpi_PrimeOutputWithSilence( PaAsiHpiStream *stream ); |
| 495 |
static PaError PaAsiHpi_StartStream( PaAsiHpiStream *stream, int outputPrimed ); |
| 496 |
static PaError PaAsiHpi_StopStream( PaAsiHpiStream *stream, int abort ); |
| 497 |
static PaError PaAsiHpi_ExplicitStop( PaAsiHpiStream *stream, int abort ); |
| 498 |
static void PaAsiHpi_OnThreadExit( void *userData ); |
| 499 |
static PaError PaAsiHpi_WaitForFrames( PaAsiHpiStream *stream, unsigned long *framesAvail, |
| 500 |
PaStreamCallbackFlags *cbFlags ); |
| 501 |
static void PaAsiHpi_CalculateTimeInfo( PaAsiHpiStream *stream, PaStreamCallbackTimeInfo *timeInfo ); |
| 502 |
static PaError PaAsiHpi_BeginProcessing( PaAsiHpiStream* stream, unsigned long* numFrames, |
| 503 |
PaStreamCallbackFlags *cbFlags ); |
| 504 |
static PaError PaAsiHpi_EndProcessing( PaAsiHpiStream *stream, unsigned long numFrames, |
| 505 |
PaStreamCallbackFlags *cbFlags ); |
| 506 |
|
| 507 |
|
| 508 |
|
| 509 |
|
| 510 |
|
| 511 |
|
| 512 |
|
| 513 |
|
| 514 |
|
| 515 |
|
| 516 |
|
| 517 |
|
| 518 |
|
| 519 |
|
| 520 |
|
| 521 |
|
| 522 |
static PaError PaAsiHpi_BuildDeviceList( PaAsiHpiHostApiRepresentation *hpiHostApi ) |
| 523 |
{ |
| 524 |
PaError result = paNoError; |
| 525 |
PaUtilHostApiRepresentation *hostApi = &hpiHostApi->baseHostApiRep; |
| 526 |
PaHostApiInfo *baseApiInfo = &hostApi->info; |
| 527 |
PaAsiHpiDeviceInfo *hpiDeviceList; |
| 528 |
int numAdapters; |
| 529 |
hpi_err_t hpiError = 0; |
| 530 |
int i, j, deviceCount = 0, deviceIndex = 0; |
| 531 |
|
| 532 |
assert( hpiHostApi ); |
| 533 |
|
| 534 |
|
| 535 |
|
| 536 |
PA_ASIHPI_UNLESS_( HPI_SubSysGetNumAdapters( NULL, &numAdapters), paNoError ); |
| 537 |
|
| 538 |
for( i=0; i < numAdapters; ++i ) |
| 539 |
{ |
| 540 |
uint16_t inStreams, outStreams; |
| 541 |
uint16_t version; |
| 542 |
uint32_t serial; |
| 543 |
uint16_t type; |
| 544 |
uint32_t idx; |
| 545 |
|
| 546 |
hpiError = HPI_SubSysGetAdapter(NULL, i, &idx, &type); |
| 547 |
if (hpiError) |
| 548 |
continue; |
| 549 |
|
| 550 |
|
| 551 |
hpiError = HPI_AdapterOpen( NULL, idx ); |
| 552 |
|
| 553 |
if( hpiError ) |
| 554 |
{ |
| 555 |
PA_ASIHPI_REPORT_ERROR_( hpiError ); |
| 556 |
continue; |
| 557 |
} |
| 558 |
hpiError = HPI_AdapterGetInfo( NULL, idx, &outStreams, &inStreams, |
| 559 |
&version, &serial, &type ); |
| 560 |
|
| 561 |
if( hpiError ) |
| 562 |
{ |
| 563 |
PA_ASIHPI_REPORT_ERROR_( hpiError ); |
| 564 |
continue; |
| 565 |
} |
| 566 |
else |
| 567 |
{ |
| 568 |
|
| 569 |
if( (baseApiInfo->defaultInputDevice == paNoDevice) && (inStreams > 0) ) |
| 570 |
baseApiInfo->defaultInputDevice = deviceCount; |
| 571 |
deviceCount += inStreams; |
| 572 |
if( (baseApiInfo->defaultOutputDevice == paNoDevice) && (outStreams > 0) ) |
| 573 |
baseApiInfo->defaultOutputDevice = deviceCount; |
| 574 |
deviceCount += outStreams; |
| 575 |
} |
| 576 |
} |
| 577 |
|
| 578 |
|
| 579 |
if( deviceCount > 0 ) |
| 580 |
{ |
| 581 |
|
| 582 |
PA_UNLESS_( hostApi->deviceInfos = (PaDeviceInfo**) PaUtil_GroupAllocateMemory( |
| 583 |
hpiHostApi->allocations, sizeof(PaDeviceInfo*) * deviceCount ), |
| 584 |
paInsufficientMemory ); |
| 585 |
|
| 586 |
PA_UNLESS_( hpiDeviceList = (PaAsiHpiDeviceInfo*) PaUtil_GroupAllocateMemory( |
| 587 |
hpiHostApi->allocations, sizeof(PaAsiHpiDeviceInfo) * deviceCount ), |
| 588 |
paInsufficientMemory ); |
| 589 |
|
| 590 |
|
| 591 |
for( i=0; i < numAdapters; ++i ) |
| 592 |
{ |
| 593 |
uint16_t inStreams, outStreams; |
| 594 |
uint16_t version; |
| 595 |
uint32_t serial; |
| 596 |
uint16_t type; |
| 597 |
uint32_t idx; |
| 598 |
|
| 599 |
hpiError = HPI_SubSysGetAdapter( NULL, i, &idx, &type ); |
| 600 |
if (hpiError) |
| 601 |
continue; |
| 602 |
|
| 603 |
|
| 604 |
hpiError = HPI_AdapterGetInfo( NULL, idx, |
| 605 |
&outStreams, &inStreams, &version, &serial, &type ); |
| 606 |
|
| 607 |
if( hpiError ) |
| 608 |
{ |
| 609 |
PA_ASIHPI_REPORT_ERROR_( hpiError ); |
| 610 |
continue; |
| 611 |
} |
| 612 |
else |
| 613 |
{ |
| 614 |
PA_DEBUG(( "Found HPI Adapter ID=%4X Idx=%d #In=%d #Out=%d S/N=%d HWver=%c%d DSPver=%03d\n", |
| 615 |
type, idx, inStreams, outStreams, serial, |
| 616 |
((version>>3)&0xf)+'A', |
| 617 |
version&0x7, |
| 618 |
((version>>13)*100)+((version>>7)&0x3f) |
| 619 |
)); |
| 620 |
} |
| 621 |
|
| 622 |
|
| 623 |
for( j=0; j < inStreams; ++j ) |
| 624 |
{ |
| 625 |
PaAsiHpiDeviceInfo *hpiDevice = &hpiDeviceList[deviceIndex]; |
| 626 |
PaDeviceInfo *baseDeviceInfo = &hpiDevice->baseDeviceInfo; |
| 627 |
char srcName[72]; |
| 628 |
char *deviceName; |
| 629 |
|
| 630 |
memset( hpiDevice, 0, sizeof(PaAsiHpiDeviceInfo) ); |
| 631 |
|
| 632 |
hpiDevice->adapterIndex = idx; |
| 633 |
hpiDevice->adapterType = type; |
| 634 |
hpiDevice->adapterVersion = version; |
| 635 |
hpiDevice->adapterSerialNumber = serial; |
| 636 |
hpiDevice->streamIndex = j; |
| 637 |
hpiDevice->streamIsOutput = 0; |
| 638 |
|
| 639 |
baseDeviceInfo->structVersion = 2; |
| 640 |
|
| 641 |
sprintf( srcName, |
| 642 |
"Adapter %d (%4X) - Input Stream %d", i+1, type, j+1 ); |
| 643 |
PA_UNLESS_( deviceName = (char *) PaUtil_GroupAllocateMemory( |
| 644 |
hpiHostApi->allocations, strlen(srcName) + 1 ), paInsufficientMemory ); |
| 645 |
strcpy( deviceName, srcName ); |
| 646 |
baseDeviceInfo->name = deviceName; |
| 647 |
baseDeviceInfo->hostApi = hpiHostApi->hostApiIndex; |
| 648 |
baseDeviceInfo->maxInputChannels = HPI_MAX_CHANNELS; |
| 649 |
baseDeviceInfo->maxOutputChannels = 0; |
| 650 |
|
| 651 |
baseDeviceInfo->defaultLowInputLatency = 0.01; |
| 652 |
baseDeviceInfo->defaultLowOutputLatency = -1.0; |
| 653 |
|
| 654 |
baseDeviceInfo->defaultHighInputLatency = 0.2; |
| 655 |
baseDeviceInfo->defaultHighOutputLatency = -1.0; |
| 656 |
|
| 657 |
|
| 658 |
baseDeviceInfo->defaultSampleRate = 44100; |
| 659 |
|
| 660 |
|
| 661 |
hostApi->deviceInfos[deviceIndex++] = (PaDeviceInfo *) hpiDevice; |
| 662 |
} |
| 663 |
|
| 664 |
|
| 665 |
for( j=0; j < outStreams; ++j ) |
| 666 |
{ |
| 667 |
PaAsiHpiDeviceInfo *hpiDevice = &hpiDeviceList[deviceIndex]; |
| 668 |
PaDeviceInfo *baseDeviceInfo = &hpiDevice->baseDeviceInfo; |
| 669 |
char srcName[72]; |
| 670 |
char *deviceName; |
| 671 |
|
| 672 |
memset( hpiDevice, 0, sizeof(PaAsiHpiDeviceInfo) ); |
| 673 |
|
| 674 |
hpiDevice->adapterIndex = idx; |
| 675 |
hpiDevice->adapterType = type; |
| 676 |
hpiDevice->adapterVersion = version; |
| 677 |
hpiDevice->adapterSerialNumber = serial; |
| 678 |
hpiDevice->streamIndex = j; |
| 679 |
hpiDevice->streamIsOutput = 1; |
| 680 |
|
| 681 |
baseDeviceInfo->structVersion = 2; |
| 682 |
|
| 683 |
sprintf( srcName, |
| 684 |
"Adapter %d (%4X) - Output Stream %d", i+1, type, j+1 ); |
| 685 |
PA_UNLESS_( deviceName = (char *) PaUtil_GroupAllocateMemory( |
| 686 |
hpiHostApi->allocations, strlen(srcName) + 1 ), paInsufficientMemory ); |
| 687 |
strcpy( deviceName, srcName ); |
| 688 |
baseDeviceInfo->name = deviceName; |
| 689 |
baseDeviceInfo->hostApi = hpiHostApi->hostApiIndex; |
| 690 |
baseDeviceInfo->maxInputChannels = 0; |
| 691 |
baseDeviceInfo->maxOutputChannels = HPI_MAX_CHANNELS; |
| 692 |
|
| 693 |
baseDeviceInfo->defaultLowInputLatency = -1.0; |
| 694 |
baseDeviceInfo->defaultLowOutputLatency = 0.01; |
| 695 |
|
| 696 |
baseDeviceInfo->defaultHighInputLatency = -1.0; |
| 697 |
baseDeviceInfo->defaultHighOutputLatency = 0.2; |
| 698 |
|
| 699 |
|
| 700 |
baseDeviceInfo->defaultSampleRate = 44100; |
| 701 |
|
| 702 |
|
| 703 |
hostApi->deviceInfos[deviceIndex++] = (PaDeviceInfo *) hpiDevice; |
| 704 |
} |
| 705 |
} |
| 706 |
} |
| 707 |
|
| 708 |
|
| 709 |
baseApiInfo->deviceCount = deviceIndex; |
| 710 |
|
| 711 |
error: |
| 712 |
return result; |
| 713 |
} |
| 714 |
|
| 715 |
|
| 716 |
|
| 717 |
|
| 718 |
|
| 719 |
|
| 720 |
|
| 721 |
|
| 722 |
|
| 723 |
|
| 724 |
|
| 725 |
|
| 726 |
|
| 727 |
PaError PaAsiHpi_Initialize( PaUtilHostApiRepresentation **hostApi, PaHostApiIndex hostApiIndex ) |
| 728 |
{ |
| 729 |
PaError result = paNoError; |
| 730 |
PaAsiHpiHostApiRepresentation *hpiHostApi = NULL; |
| 731 |
PaHostApiInfo *baseApiInfo; |
| 732 |
|
| 733 |
|
| 734 |
if (!HPI_SubSysCreate()) |
| 735 |
{ |
| 736 |
|
| 737 |
|
| 738 |
|
| 739 |
PA_DEBUG(( "Could not open HPI interface\n" )); |
| 740 |
|
| 741 |
*hostApi = NULL; |
| 742 |
return paNoError; |
| 743 |
} |
| 744 |
else |
| 745 |
{ |
| 746 |
uint32_t hpiVersion; |
| 747 |
PA_ASIHPI_UNLESS_( HPI_SubSysGetVersionEx( NULL, &hpiVersion ), paUnanticipatedHostError ); |
| 748 |
PA_DEBUG(( "HPI interface v%d.%02d.%02d\n", |
| 749 |
hpiVersion >> 16, (hpiVersion >> 8) & 0x0F, (hpiVersion & 0x0F) )); |
| 750 |
} |
| 751 |
|
| 752 |
|
| 753 |
PA_UNLESS_( hpiHostApi = (PaAsiHpiHostApiRepresentation*) PaUtil_AllocateMemory( |
| 754 |
sizeof(PaAsiHpiHostApiRepresentation) ), paInsufficientMemory ); |
| 755 |
PA_UNLESS_( hpiHostApi->allocations = PaUtil_CreateAllocationGroup(), paInsufficientMemory ); |
| 756 |
|
| 757 |
hpiHostApi->hostApiIndex = hostApiIndex; |
| 758 |
|
| 759 |
*hostApi = &hpiHostApi->baseHostApiRep; |
| 760 |
baseApiInfo = &((*hostApi)->info); |
| 761 |
|
| 762 |
baseApiInfo->structVersion = 1; |
| 763 |
baseApiInfo->type = paAudioScienceHPI; |
| 764 |
baseApiInfo->name = "AudioScience HPI"; |
| 765 |
baseApiInfo->deviceCount = 0; |
| 766 |
baseApiInfo->defaultInputDevice = paNoDevice; |
| 767 |
baseApiInfo->defaultOutputDevice = paNoDevice; |
| 768 |
|
| 769 |
PA_ENSURE_( PaAsiHpi_BuildDeviceList( hpiHostApi ) ); |
| 770 |
|
| 771 |
(*hostApi)->Terminate = Terminate; |
| 772 |
(*hostApi)->OpenStream = OpenStream; |
| 773 |
(*hostApi)->IsFormatSupported = IsFormatSupported; |
| 774 |
|
| 775 |
PaUtil_InitializeStreamInterface( &hpiHostApi->callbackStreamInterface, CloseStream, StartStream, |
| 776 |
StopStream, AbortStream, IsStreamStopped, IsStreamActive, |
| 777 |
GetStreamTime, GetStreamCpuLoad, |
| 778 |
PaUtil_DummyRead, PaUtil_DummyWrite, |
| 779 |
PaUtil_DummyGetReadAvailable, PaUtil_DummyGetWriteAvailable ); |
| 780 |
|
| 781 |
PaUtil_InitializeStreamInterface( &hpiHostApi->blockingStreamInterface, CloseStream, StartStream, |
| 782 |
StopStream, AbortStream, IsStreamStopped, IsStreamActive, |
| 783 |
GetStreamTime, PaUtil_DummyGetCpuLoad, |
| 784 |
ReadStream, WriteStream, GetStreamReadAvailable, GetStreamWriteAvailable ); |
| 785 |
|
| 786 |
|
| 787 |
PA_ENSURE_( PaUnixThreading_Initialize() ); |
| 788 |
|
| 789 |
return result; |
| 790 |
error: |
| 791 |
if (hpiHostApi) |
| 792 |
PaUtil_FreeMemory( hpiHostApi ); |
| 793 |
return result; |
| 794 |
} |
| 795 |
|
| 796 |
|
| 797 |
|
| 798 |
|
| 799 |
|
| 800 |
|
| 801 |
|
| 802 |
|
| 803 |
static void Terminate( struct PaUtilHostApiRepresentation *hostApi ) |
| 804 |
{ |
| 805 |
PaAsiHpiHostApiRepresentation *hpiHostApi = (PaAsiHpiHostApiRepresentation*)hostApi; |
| 806 |
int i; |
| 807 |
PaError result = paNoError; |
| 808 |
|
| 809 |
if( hpiHostApi ) |
| 810 |
{ |
| 811 |
|
| 812 |
uint16_t lastAdapterIndex = HPI_MAX_ADAPTERS; |
| 813 |
|
| 814 |
for( i=0; i < hostApi->info.deviceCount; ++i ) |
| 815 |
{ |
| 816 |
PaAsiHpiDeviceInfo *hpiDevice = (PaAsiHpiDeviceInfo *) hostApi->deviceInfos[ i ]; |
| 817 |
|
| 818 |
if( hpiDevice->adapterIndex != lastAdapterIndex ) |
| 819 |
{ |
| 820 |
|
| 821 |
PA_ASIHPI_UNLESS_( HPI_AdapterClose( NULL, |
| 822 |
hpiDevice->adapterIndex ), paNoError ); |
| 823 |
lastAdapterIndex = hpiDevice->adapterIndex; |
| 824 |
} |
| 825 |
} |
| 826 |
|
| 827 |
HPI_SubSysFree( NULL ); |
| 828 |
|
| 829 |
if( hpiHostApi->allocations ) |
| 830 |
{ |
| 831 |
PaUtil_FreeAllAllocations( hpiHostApi->allocations ); |
| 832 |
PaUtil_DestroyAllocationGroup( hpiHostApi->allocations ); |
| 833 |
} |
| 834 |
|
| 835 |
PaUtil_FreeMemory( hpiHostApi ); |
| 836 |
} |
| 837 |
error: |
| 838 |
return; |
| 839 |
} |
| 840 |
|
| 841 |
|
| 842 |
|
| 843 |
|
| 844 |
|
| 845 |
|
| 846 |
|
| 847 |
|
| 848 |
static uint16_t PaAsiHpi_PaToHpiFormat( PaSampleFormat paFormat ) |
| 849 |
{ |
| 850 |
|
| 851 |
switch( paFormat & ~paNonInterleaved ) |
| 852 |
{ |
| 853 |
case paFloat32: |
| 854 |
return HPI_FORMAT_PCM32_FLOAT; |
| 855 |
|
| 856 |
case paInt32: |
| 857 |
return HPI_FORMAT_PCM32_SIGNED; |
| 858 |
|
| 859 |
case paInt24: |
| 860 |
return HPI_FORMAT_PCM24_SIGNED; |
| 861 |
|
| 862 |
case paInt16: |
| 863 |
return HPI_FORMAT_PCM16_SIGNED; |
| 864 |
|
| 865 |
case paUInt8: |
| 866 |
return HPI_FORMAT_PCM8_UNSIGNED; |
| 867 |
|
| 868 |
|
| 869 |
case paInt8: |
| 870 |
default: |
| 871 |
return HPI_FORMAT_PCM16_SIGNED; |
| 872 |
} |
| 873 |
} |
| 874 |
|
| 875 |
|
| 876 |
|
| 877 |
|
| 878 |
|
| 879 |
|
| 880 |
|
| 881 |
|
| 882 |
static PaSampleFormat PaAsiHpi_HpiToPaFormat( uint16_t hpiFormat ) |
| 883 |
{ |
| 884 |
switch( hpiFormat ) |
| 885 |
{ |
| 886 |
case HPI_FORMAT_PCM32_FLOAT: |
| 887 |
return paFloat32; |
| 888 |
|
| 889 |
case HPI_FORMAT_PCM32_SIGNED: |
| 890 |
return paInt32; |
| 891 |
|
| 892 |
case HPI_FORMAT_PCM24_SIGNED: |
| 893 |
return paInt24; |
| 894 |
|
| 895 |
case HPI_FORMAT_PCM16_SIGNED: |
| 896 |
return paInt16; |
| 897 |
|
| 898 |
case HPI_FORMAT_PCM8_UNSIGNED: |
| 899 |
return paUInt8; |
| 900 |
|
| 901 |
|
| 902 |
default: |
| 903 |
return paCustomFormat; |
| 904 |
} |
| 905 |
} |
| 906 |
|
| 907 |
|
| 908 |
|
| 909 |
|
| 910 |
|
| 911 |
|
| 912 |
|
| 913 |
|
| 914 |
|
| 915 |
|
| 916 |
|
| 917 |
|
| 918 |
|
| 919 |
|
| 920 |
|
| 921 |
|
| 922 |
|
| 923 |
|
| 924 |
|
| 925 |
static PaError PaAsiHpi_CreateFormat( struct PaUtilHostApiRepresentation *hostApi, |
| 926 |
const PaStreamParameters *parameters, double sampleRate, |
| 927 |
PaAsiHpiDeviceInfo **hpiDevice, struct hpi_format *hpiFormat ) |
| 928 |
{ |
| 929 |
int maxChannelCount = 0; |
| 930 |
PaSampleFormat hostSampleFormat = 0; |
| 931 |
hpi_err_t hpiError = 0; |
| 932 |
|
| 933 |
|
| 934 |
|
| 935 |
if( parameters->device == paUseHostApiSpecificDeviceSpecification ) |
| 936 |
return paInvalidDevice; |
| 937 |
else |
| 938 |
{ |
| 939 |
assert( parameters->device < hostApi->info.deviceCount ); |
| 940 |
*hpiDevice = (PaAsiHpiDeviceInfo*) hostApi->deviceInfos[ parameters->device ]; |
| 941 |
} |
| 942 |
|
| 943 |
|
| 944 |
if( parameters->hostApiSpecificStreamInfo ) |
| 945 |
return paIncompatibleHostApiSpecificStreamInfo; |
| 946 |
|
| 947 |
|
| 948 |
if( (*hpiDevice)->streamIsOutput ) |
| 949 |
{ |
| 950 |
maxChannelCount = (*hpiDevice)->baseDeviceInfo.maxOutputChannels; |
| 951 |
} |
| 952 |
else |
| 953 |
{ |
| 954 |
maxChannelCount = (*hpiDevice)->baseDeviceInfo.maxInputChannels; |
| 955 |
} |
| 956 |
if( (maxChannelCount == 0) || (parameters->channelCount > maxChannelCount) ) |
| 957 |
return paInvalidChannelCount; |
| 958 |
|
| 959 |
|
| 960 |
|
| 961 |
if( parameters->sampleFormat & paCustomFormat ) |
| 962 |
return paSampleFormatNotSupported; |
| 963 |
|
| 964 |
|
| 965 |
hostSampleFormat = PaUtil_SelectClosestAvailableFormat(PA_ASIHPI_AVAILABLE_FORMATS_, |
| 966 |
parameters->sampleFormat ); |
| 967 |
|
| 968 |
hpiError = HPI_FormatCreate( hpiFormat, (uint16_t)parameters->channelCount, |
| 969 |
PaAsiHpi_PaToHpiFormat( hostSampleFormat ), |
| 970 |
(uint32_t)sampleRate, 0, 0 ); |
| 971 |
if( hpiError ) |
| 972 |
{ |
| 973 |
PA_ASIHPI_REPORT_ERROR_( hpiError ); |
| 974 |
switch( hpiError ) |
| 975 |
{ |
| 976 |
case HPI_ERROR_INVALID_FORMAT: |
| 977 |
return paSampleFormatNotSupported; |
| 978 |
|
| 979 |
case HPI_ERROR_INVALID_SAMPLERATE: |
| 980 |
case HPI_ERROR_INCOMPATIBLE_SAMPLERATE: |
| 981 |
return paInvalidSampleRate; |
| 982 |
|
| 983 |
case HPI_ERROR_INVALID_CHANNELS: |
| 984 |
return paInvalidChannelCount; |
| 985 |
} |
| 986 |
} |
| 987 |
|
| 988 |
return paNoError; |
| 989 |
} |
| 990 |
|
| 991 |
|
| 992 |
|
| 993 |
|
| 994 |
|
| 995 |
|
| 996 |
|
| 997 |
|
| 998 |
|
| 999 |
|
| 1000 |
|
| 1001 |
|
| 1002 |
|
| 1003 |
|
| 1004 |
static PaError PaAsiHpi_OpenInput( struct PaUtilHostApiRepresentation *hostApi, |
| 1005 |
const PaAsiHpiDeviceInfo *hpiDevice, const struct hpi_format *hpiFormat, |
| 1006 |
hpi_handle_t *hpiStream ) |
| 1007 |
{ |
| 1008 |
PaAsiHpiHostApiRepresentation *hpiHostApi = (PaAsiHpiHostApiRepresentation*)hostApi; |
| 1009 |
PaError result = paNoError; |
| 1010 |
hpi_err_t hpiError = 0; |
| 1011 |
|
| 1012 |
|
| 1013 |
PA_UNLESS_( !hpiDevice->streamIsOutput, paInvalidChannelCount ); |
| 1014 |
|
| 1015 |
PA_ASIHPI_UNLESS_( HPI_InStreamOpen( NULL, hpiDevice->adapterIndex, |
| 1016 |
hpiDevice->streamIndex, hpiStream ), paDeviceUnavailable ); |
| 1017 |
|
| 1018 |
|
| 1019 |
hpiError = HPI_InStreamSetFormat( NULL, *hpiStream, (struct hpi_format*)hpiFormat ); |
| 1020 |
if( hpiError ) |
| 1021 |
{ |
| 1022 |
PA_ASIHPI_REPORT_ERROR_( hpiError ); |
| 1023 |
PA_ASIHPI_UNLESS_( HPI_InStreamClose( NULL, *hpiStream ), paNoError ); |
| 1024 |
switch( hpiError ) |
| 1025 |
{ |
| 1026 |
case HPI_ERROR_INVALID_FORMAT: |
| 1027 |
return paSampleFormatNotSupported; |
| 1028 |
|
| 1029 |
case HPI_ERROR_INVALID_SAMPLERATE: |
| 1030 |
case HPI_ERROR_INCOMPATIBLE_SAMPLERATE: |
| 1031 |
return paInvalidSampleRate; |
| 1032 |
|
| 1033 |
case HPI_ERROR_INVALID_CHANNELS: |
| 1034 |
return paInvalidChannelCount; |
| 1035 |
|
| 1036 |
default: |
| 1037 |
|
| 1038 |
return paInvalidDevice; |
| 1039 |
} |
| 1040 |
} |
| 1041 |
|
| 1042 |
error: |
| 1043 |
return result; |
| 1044 |
} |
| 1045 |
|
| 1046 |
|
| 1047 |
|
| 1048 |
|
| 1049 |
|
| 1050 |
|
| 1051 |
|
| 1052 |
|
| 1053 |
|
| 1054 |
|
| 1055 |
|
| 1056 |
|
| 1057 |
|
| 1058 |
|
| 1059 |
static PaError PaAsiHpi_OpenOutput( struct PaUtilHostApiRepresentation *hostApi, |
| 1060 |
const PaAsiHpiDeviceInfo *hpiDevice, const struct hpi_format *hpiFormat, |
| 1061 |
hpi_handle_t *hpiStream ) |
| 1062 |
{ |
| 1063 |
PaAsiHpiHostApiRepresentation *hpiHostApi = (PaAsiHpiHostApiRepresentation*)hostApi; |
| 1064 |
PaError result = paNoError; |
| 1065 |
hpi_err_t hpiError = 0; |
| 1066 |
|
| 1067 |
|
| 1068 |
PA_UNLESS_( hpiDevice->streamIsOutput, paInvalidChannelCount ); |
| 1069 |
|
| 1070 |
PA_ASIHPI_UNLESS_( HPI_OutStreamOpen( NULL, hpiDevice->adapterIndex, |
| 1071 |
hpiDevice->streamIndex, hpiStream ), paDeviceUnavailable ); |
| 1072 |
|
| 1073 |
|
| 1074 |
hpiError = HPI_OutStreamQueryFormat( NULL, *hpiStream, (struct hpi_format*)hpiFormat ); |
| 1075 |
if( hpiError ) |
| 1076 |
{ |
| 1077 |
PA_ASIHPI_REPORT_ERROR_( hpiError ); |
| 1078 |
PA_ASIHPI_UNLESS_( HPI_OutStreamClose( NULL, *hpiStream ), paNoError ); |
| 1079 |
switch( hpiError ) |
| 1080 |
{ |
| 1081 |
case HPI_ERROR_INVALID_FORMAT: |
| 1082 |
return paSampleFormatNotSupported; |
| 1083 |
|
| 1084 |
case HPI_ERROR_INVALID_SAMPLERATE: |
| 1085 |
case HPI_ERROR_INCOMPATIBLE_SAMPLERATE: |
| 1086 |
return paInvalidSampleRate; |
| 1087 |
|
| 1088 |
case HPI_ERROR_INVALID_CHANNELS: |
| 1089 |
return paInvalidChannelCount; |
| 1090 |
|
| 1091 |
default: |
| 1092 |
|
| 1093 |
return paInvalidDevice; |
| 1094 |
} |
| 1095 |
} |
| 1096 |
|
| 1097 |
error: |
| 1098 |
return result; |
| 1099 |
} |
| 1100 |
|
| 1101 |
|
| 1102 |
|
| 1103 |
|
| 1104 |
|
| 1105 |
|
| 1106 |
|
| 1107 |
|
| 1108 |
|
| 1109 |
|
| 1110 |
|
| 1111 |
|
| 1112 |
|
| 1113 |
|
| 1114 |
|
| 1115 |
|
| 1116 |
static PaError IsFormatSupported( struct PaUtilHostApiRepresentation *hostApi, |
| 1117 |
const PaStreamParameters *inputParameters, |
| 1118 |
const PaStreamParameters *outputParameters, |
| 1119 |
double sampleRate ) |
| 1120 |
{ |
| 1121 |
PaError result = paFormatIsSupported; |
| 1122 |
PaAsiHpiHostApiRepresentation *hpiHostApi = (PaAsiHpiHostApiRepresentation*)hostApi; |
| 1123 |
PaAsiHpiDeviceInfo *hpiDevice = NULL; |
| 1124 |
struct hpi_format hpiFormat; |
| 1125 |
|
| 1126 |
|
| 1127 |
if( inputParameters ) |
| 1128 |
{ |
| 1129 |
hpi_handle_t hpiStream; |
| 1130 |
PA_DEBUG(( "%s: Checking input params: dev=%d, sr=%d, chans=%d, fmt=%d\n", |
| 1131 |
__FUNCTION__, inputParameters->device, (int)sampleRate, |
| 1132 |
inputParameters->channelCount, inputParameters->sampleFormat )); |
| 1133 |
|
| 1134 |
PA_ENSURE_( PaAsiHpi_CreateFormat( hostApi, inputParameters, sampleRate, |
| 1135 |
&hpiDevice, &hpiFormat ) ); |
| 1136 |
|
| 1137 |
PA_ENSURE_( PaAsiHpi_OpenInput( hostApi, hpiDevice, &hpiFormat, &hpiStream ) ); |
| 1138 |
|
| 1139 |
PA_ASIHPI_UNLESS_( HPI_InStreamClose( NULL, hpiStream ), paNoError ); |
| 1140 |
} |
| 1141 |
|
| 1142 |
|
| 1143 |
if( outputParameters ) |
| 1144 |
{ |
| 1145 |
hpi_handle_t hpiStream; |
| 1146 |
PA_DEBUG(( "%s: Checking output params: dev=%d, sr=%d, chans=%d, fmt=%d\n", |
| 1147 |
__FUNCTION__, outputParameters->device, (int)sampleRate, |
| 1148 |
outputParameters->channelCount, outputParameters->sampleFormat )); |
| 1149 |
|
| 1150 |
PA_ENSURE_( PaAsiHpi_CreateFormat( hostApi, outputParameters, sampleRate, |
| 1151 |
&hpiDevice, &hpiFormat ) ); |
| 1152 |
|
| 1153 |
PA_ENSURE_( PaAsiHpi_OpenOutput( hostApi, hpiDevice, &hpiFormat, &hpiStream ) ); |
| 1154 |
|
| 1155 |
PA_ASIHPI_UNLESS_( HPI_OutStreamClose( NULL, hpiStream ), paNoError ); |
| 1156 |
} |
| 1157 |
|
| 1158 |
error: |
| 1159 |
return result; |
| 1160 |
} |
| 1161 |
|
| 1162 |
|
| 1163 |
|
| 1164 |
|
| 1165 |
|
| 1166 |
|
| 1167 |
|
| 1168 |
|
| 1169 |
|
| 1170 |
|
| 1171 |
|
| 1172 |
|
| 1173 |
|
| 1174 |
static PaError PaAsiHpi_GetStreamInfo( PaAsiHpiStreamComponent *streamComp, PaAsiHpiStreamInfo *info ) |
| 1175 |
{ |
| 1176 |
PaError result = paDeviceUnavailable; |
| 1177 |
uint16_t state; |
| 1178 |
uint32_t bufferSize, dataSize, frameCounter, auxDataSize, threshold; |
| 1179 |
uint32_t hwBufferSize, hwDataSize; |
| 1180 |
|
| 1181 |
assert( streamComp ); |
| 1182 |
assert( info ); |
| 1183 |
|
| 1184 |
|
| 1185 |
|
| 1186 |
info->state = 0; |
| 1187 |
info->bufferSize = 0; |
| 1188 |
info->dataSize = 0; |
| 1189 |
info->frameCounter = 0; |
| 1190 |
info->auxDataSize = 0; |
| 1191 |
info->totalBufferedData = 0; |
| 1192 |
info->availableFrames = 0; |
| 1193 |
info->underflow = 0; |
| 1194 |
info->overflow = 0; |
| 1195 |
|
| 1196 |
if( streamComp->hpiDevice && streamComp->hpiStream ) |
| 1197 |
{ |
| 1198 |
|
| 1199 |
if( streamComp->hpiDevice->streamIsOutput ) |
| 1200 |
{ |
| 1201 |
PA_ASIHPI_UNLESS_( HPI_OutStreamGetInfoEx( NULL, |
| 1202 |
streamComp->hpiStream, |
| 1203 |
&state, &bufferSize, &dataSize, &frameCounter, |
| 1204 |
&auxDataSize ), paUnanticipatedHostError ); |
| 1205 |
} |
| 1206 |
else |
| 1207 |
{ |
| 1208 |
PA_ASIHPI_UNLESS_( HPI_InStreamGetInfoEx( NULL, |
| 1209 |
streamComp->hpiStream, |
| 1210 |
&state, &bufferSize, &dataSize, &frameCounter, |
| 1211 |
&auxDataSize ), paUnanticipatedHostError ); |
| 1212 |
} |
| 1213 |
|
| 1214 |
info->state = state; |
| 1215 |
info->bufferSize = bufferSize; |
| 1216 |
info->dataSize = dataSize; |
| 1217 |
info->frameCounter = frameCounter; |
| 1218 |
info->auxDataSize = auxDataSize; |
| 1219 |
|
| 1220 |
info->totalBufferedData = dataSize; |
| 1221 |
if( streamComp->hostBufferSize > 0 ) |
| 1222 |
info->totalBufferedData += auxDataSize; |
| 1223 |
info->totalBufferedData /= streamComp->bytesPerFrame; |
| 1224 |
|
| 1225 |
info->availableFrames = streamComp->hpiDevice->streamIsOutput ? |
| 1226 |
bufferSize - dataSize : dataSize; |
| 1227 |
info->availableFrames /= streamComp->bytesPerFrame; |
| 1228 |
|
| 1229 |
threshold = PA_MIN( streamComp->tempBufferSize, |
| 1230 |
streamComp->bytesPerFrame * PA_ASIHPI_MIN_FRAMES_ ); |
| 1231 |
|
| 1232 |
hwBufferSize = streamComp->hardwareBufferSize; |
| 1233 |
hwDataSize = streamComp->hostBufferSize > 0 ? auxDataSize : dataSize; |
| 1234 |
|
| 1235 |
info->underflow = streamComp->hpiDevice->streamIsOutput ? |
| 1236 |
|
| 1237 |
(frameCounter > 0) && ( (state == HPI_STATE_DRAINED) || (hwDataSize == 0) ) : |
| 1238 |
|
| 1239 |
(state != HPI_STATE_STOPPED) && (dataSize < threshold); |
| 1240 |
|
| 1241 |
info->overflow = (state != HPI_STATE_STOPPED) && (hwBufferSize - hwDataSize < threshold); |
| 1242 |
|
| 1243 |
return paNoError; |
| 1244 |
} |
| 1245 |
|
| 1246 |
error: |
| 1247 |
return result; |
| 1248 |
} |
| 1249 |
|
| 1250 |
|
| 1251 |
|
| 1252 |
|
| 1253 |
|
| 1254 |
|
| 1255 |
|
| 1256 |
|
| 1257 |
static void PaAsiHpi_StreamComponentDump( PaAsiHpiStreamComponent *streamComp, |
| 1258 |
PaAsiHpiStream *stream ) |
| 1259 |
{ |
| 1260 |
PaAsiHpiStreamInfo streamInfo; |
| 1261 |
|
| 1262 |
assert( streamComp ); |
| 1263 |
assert( stream ); |
| 1264 |
|
| 1265 |
|
| 1266 |
PA_DEBUG(( "device: %s\n", streamComp->hpiDevice->baseDeviceInfo.name )); |
| 1267 |
|
| 1268 |
if( streamComp->hpiDevice->streamIsOutput ) |
| 1269 |
{ |
| 1270 |
|
| 1271 |
PA_DEBUG(( "user: %d-bit, %d ", |
| 1272 |
8*stream->bufferProcessor.bytesPerUserOutputSample, |
| 1273 |
stream->bufferProcessor.outputChannelCount)); |
| 1274 |
if( stream->bufferProcessor.userOutputIsInterleaved ) |
| 1275 |
{ |
| 1276 |
PA_DEBUG(( "interleaved channels, " )); |
| 1277 |
} |
| 1278 |
else |
| 1279 |
{ |
| 1280 |
PA_DEBUG(( "non-interleaved channels, " )); |
| 1281 |
} |
| 1282 |
PA_DEBUG(( "%d frames/buffer, latency = %5.1f ms\n", |
| 1283 |
stream->bufferProcessor.framesPerUserBuffer, |
| 1284 |
1000*stream->baseStreamRep.streamInfo.outputLatency )); |
| 1285 |
|
| 1286 |
PA_DEBUG(( "host: %d-bit, %d interleaved channels, %d frames/buffer ", |
| 1287 |
8*stream->bufferProcessor.bytesPerHostOutputSample, |
| 1288 |
stream->bufferProcessor.outputChannelCount, |
| 1289 |
stream->bufferProcessor.framesPerHostBuffer )); |
| 1290 |
} |
| 1291 |
else |
| 1292 |
{ |
| 1293 |
|
| 1294 |
PA_DEBUG(( "user: %d-bit, %d ", |
| 1295 |
8*stream->bufferProcessor.bytesPerUserInputSample, |
| 1296 |
stream->bufferProcessor.inputChannelCount)); |
| 1297 |
if( stream->bufferProcessor.userInputIsInterleaved ) |
| 1298 |
{ |
| 1299 |
PA_DEBUG(( "interleaved channels, " )); |
| 1300 |
} |
| 1301 |
else |
| 1302 |
{ |
| 1303 |
PA_DEBUG(( "non-interleaved channels, " )); |
| 1304 |
} |
| 1305 |
PA_DEBUG(( "%d frames/buffer, latency = %5.1f ms\n", |
| 1306 |
stream->bufferProcessor.framesPerUserBuffer, |
| 1307 |
1000*stream->baseStreamRep.streamInfo.inputLatency )); |
| 1308 |
|
| 1309 |
PA_DEBUG(( "host: %d-bit, %d interleaved channels, %d frames/buffer ", |
| 1310 |
8*stream->bufferProcessor.bytesPerHostInputSample, |
| 1311 |
stream->bufferProcessor.inputChannelCount, |
| 1312 |
stream->bufferProcessor.framesPerHostBuffer )); |
| 1313 |
} |
| 1314 |
switch( stream->bufferProcessor.hostBufferSizeMode ) |
| 1315 |
{ |
| 1316 |
case paUtilFixedHostBufferSize: |
| 1317 |
PA_DEBUG(( "[fixed] " )); |
| 1318 |
break; |
| 1319 |
case paUtilBoundedHostBufferSize: |
| 1320 |
PA_DEBUG(( "[bounded] " )); |
| 1321 |
break; |
| 1322 |
case paUtilUnknownHostBufferSize: |
| 1323 |
PA_DEBUG(( "[unknown] " )); |
| 1324 |
break; |
| 1325 |
case paUtilVariableHostBufferSizePartialUsageAllowed: |
| 1326 |
PA_DEBUG(( "[variable] " )); |
| 1327 |
break; |
| 1328 |
} |
| 1329 |
PA_DEBUG(( "(%d max)\n", streamComp->tempBufferSize / streamComp->bytesPerFrame )); |
| 1330 |
|
| 1331 |
PA_DEBUG(( "HPI: adapter %d stream %d, %d-bit, %d-channel, %d Hz\n", |
| 1332 |
streamComp->hpiDevice->adapterIndex, streamComp->hpiDevice->streamIndex, |
| 1333 |
8 * streamComp->bytesPerFrame / streamComp->hpiFormat.wChannels, |
| 1334 |
streamComp->hpiFormat.wChannels, |
| 1335 |
streamComp->hpiFormat.dwSampleRate )); |
| 1336 |
|
| 1337 |
PA_DEBUG(( "HPI: " )); |
| 1338 |
PaAsiHpi_GetStreamInfo( streamComp, &streamInfo ); |
| 1339 |
switch( streamInfo.state ) |
| 1340 |
{ |
| 1341 |
case HPI_STATE_STOPPED: |
| 1342 |
PA_DEBUG(( "[STOPPED] " )); |
| 1343 |
break; |
| 1344 |
case HPI_STATE_PLAYING: |
| 1345 |
PA_DEBUG(( "[PLAYING] " )); |
| 1346 |
break; |
| 1347 |
case HPI_STATE_RECORDING: |
| 1348 |
PA_DEBUG(( "[RECORDING] " )); |
| 1349 |
break; |
| 1350 |
case HPI_STATE_DRAINED: |
| 1351 |
PA_DEBUG(( "[DRAINED] " )); |
| 1352 |
break; |
| 1353 |
default: |
| 1354 |
PA_DEBUG(( "[unknown state] " )); |
| 1355 |
break; |
| 1356 |
} |
| 1357 |
if( streamComp->hostBufferSize ) |
| 1358 |
{ |
| 1359 |
PA_DEBUG(( "host = %d/%d B, ", streamInfo.dataSize, streamComp->hostBufferSize )); |
| 1360 |
PA_DEBUG(( "hw = %d/%d (%d) B, ", streamInfo.auxDataSize, |
| 1361 |
streamComp->hardwareBufferSize, streamComp->outputBufferCap )); |
| 1362 |
} |
| 1363 |
else |
| 1364 |
{ |
| 1365 |
PA_DEBUG(( "hw = %d/%d B, ", streamInfo.dataSize, streamComp->hardwareBufferSize )); |
| 1366 |
} |
| 1367 |
PA_DEBUG(( "count = %d", streamInfo.frameCounter )); |
| 1368 |
if( streamInfo.overflow ) |
| 1369 |
{ |
| 1370 |
PA_DEBUG(( " [overflow]" )); |
| 1371 |
} |
| 1372 |
else if( streamInfo.underflow ) |
| 1373 |
{ |
| 1374 |
PA_DEBUG(( " [underflow]" )); |
| 1375 |
} |
| 1376 |
PA_DEBUG(( "\n" )); |
| 1377 |
} |
| 1378 |
|
| 1379 |
|
| 1380 |
|
| 1381 |
|
| 1382 |
|
| 1383 |
|
| 1384 |
static void PaAsiHpi_StreamDump( PaAsiHpiStream *stream ) |
| 1385 |
{ |
| 1386 |
assert( stream ); |
| 1387 |
|
| 1388 |
PA_DEBUG(( "\n------------------------- STREAM INFO FOR %p ---------------------------\n", stream )); |
| 1389 |
|
| 1390 |
if( stream->baseStreamRep.streamCallback ) |
| 1391 |
{ |
| 1392 |
PA_DEBUG(( "[callback] " )); |
| 1393 |
} |
| 1394 |
else |
| 1395 |
{ |
| 1396 |
PA_DEBUG(( "[blocking] " )); |
| 1397 |
} |
| 1398 |
PA_DEBUG(( "sr=%d Hz, poll=%d ms, max %d frames/buf ", |
| 1399 |
(int)stream->baseStreamRep.streamInfo.sampleRate, |
| 1400 |
stream->pollingInterval, stream->maxFramesPerHostBuffer )); |
| 1401 |
switch( stream->state ) |
| 1402 |
{ |
| 1403 |
case paAsiHpiStoppedState: |
| 1404 |
PA_DEBUG(( "[stopped]\n" )); |
| 1405 |
break; |
| 1406 |
case paAsiHpiActiveState: |
| 1407 |
PA_DEBUG(( "[active]\n" )); |
| 1408 |
break; |
| 1409 |
case paAsiHpiCallbackFinishedState: |
| 1410 |
PA_DEBUG(( "[cb fin]\n" )); |
| 1411 |
break; |
| 1412 |
default: |
| 1413 |
PA_DEBUG(( "[unknown state]\n" )); |
| 1414 |
break; |
| 1415 |
} |
| 1416 |
if( stream->callbackMode ) |
| 1417 |
{ |
| 1418 |
PA_DEBUG(( "cb info: thread=%p, cbAbort=%d, cbFinished=%d\n", |
| 1419 |
stream->thread.thread, stream->callbackAbort, stream->callbackFinished )); |
| 1420 |
} |
| 1421 |
|
| 1422 |
PA_DEBUG(( "----------------------------------- Input ------------------------------------\n" )); |
| 1423 |
if( stream->input ) |
| 1424 |
{ |
| 1425 |
PaAsiHpi_StreamComponentDump( stream->input, stream ); |
| 1426 |
} |
| 1427 |
else |
| 1428 |
{ |
| 1429 |
PA_DEBUG(( "*none*\n" )); |
| 1430 |
} |
| 1431 |
|
| 1432 |
PA_DEBUG(( "----------------------------------- Output ------------------------------------\n" )); |
| 1433 |
if( stream->output ) |
| 1434 |
{ |
| 1435 |
PaAsiHpi_StreamComponentDump( stream->output, stream ); |
| 1436 |
} |
| 1437 |
else |
| 1438 |
{ |
| 1439 |
PA_DEBUG(( "*none*\n" )); |
| 1440 |
} |
| 1441 |
PA_DEBUG(( "-------------------------------------------------------------------------------\n\n" )); |
| 1442 |
|
| 1443 |
} |
| 1444 |
|
| 1445 |
|
| 1446 |
|
| 1447 |
|
| 1448 |
|
| 1449 |
|
| 1450 |
|
| 1451 |
|
| 1452 |
|
| 1453 |
|
| 1454 |
|
| 1455 |
|
| 1456 |
|
| 1457 |
|
| 1458 |
|
| 1459 |
|
| 1460 |
|
| 1461 |
|
| 1462 |
|
| 1463 |
|
| 1464 |
|
| 1465 |
|
| 1466 |
|
| 1467 |
|
| 1468 |
static PaError PaAsiHpi_SetupBuffers( PaAsiHpiStreamComponent *streamComp, uint32_t pollingInterval, |
| 1469 |
unsigned long framesPerPaHostBuffer, PaTime suggestedLatency ) |
| 1470 |
{ |
| 1471 |
PaError result = paNoError; |
| 1472 |
PaAsiHpiStreamInfo streamInfo; |
| 1473 |
unsigned long hpiBufferSize = 0, paHostBufferSize = 0; |
| 1474 |
|
| 1475 |
assert( streamComp ); |
| 1476 |
assert( streamComp->hpiDevice ); |
| 1477 |
|
| 1478 |
|
| 1479 |
|
| 1480 |
|
| 1481 |
PA_ENSURE_( PaAsiHpi_GetStreamInfo( streamComp, &streamInfo ) ); |
| 1482 |
streamComp->hardwareBufferSize = streamInfo.bufferSize; |
| 1483 |
hpiBufferSize = streamInfo.bufferSize; |
| 1484 |
|
| 1485 |
|
| 1486 |
if( PA_ASIHPI_USE_BBM_ ) |
| 1487 |
{ |
| 1488 |
uint32_t bbmBufferSize = 0, preLatencyBufferSize = 0; |
| 1489 |
hpi_err_t hpiError = 0; |
| 1490 |
PaTime pollingOverhead; |
| 1491 |
|
| 1492 |
|
| 1493 |
pollingOverhead = PaUtil_GetTime(); |
| 1494 |
Pa_Sleep( 0 ); |
| 1495 |
pollingOverhead = 1000*(PaUtil_GetTime() - pollingOverhead); |
| 1496 |
PA_DEBUG(( "polling overhead = %f ms (length of 0-second sleep)\n", pollingOverhead )); |
| 1497 |
|
| 1498 |
PA_ASIHPI_UNLESS_( HPI_StreamEstimateBufferSize( &streamComp->hpiFormat, |
| 1499 |
pollingInterval + (uint32_t)ceil( pollingOverhead ), |
| 1500 |
&bbmBufferSize ), paUnanticipatedHostError ); |
| 1501 |
|
| 1502 |
|
| 1503 |
bbmBufferSize *= 3; |
| 1504 |
|
| 1505 |
if( bbmBufferSize < 3 * streamComp->bytesPerFrame * framesPerPaHostBuffer ) |
| 1506 |
bbmBufferSize = 3 * streamComp->bytesPerFrame * framesPerPaHostBuffer; |
| 1507 |
|
| 1508 |
if( suggestedLatency > 0.0 ) |
| 1509 |
{ |
| 1510 |
PaTime bufferDuration = ((PaTime)bbmBufferSize) / streamComp->bytesPerFrame |
| 1511 |
/ streamComp->hpiFormat.dwSampleRate; |
| 1512 |
|
| 1513 |
if( bufferDuration < suggestedLatency ) |
| 1514 |
{ |
| 1515 |
|
| 1516 |
preLatencyBufferSize = bbmBufferSize; |
| 1517 |
bbmBufferSize = (uint32_t)ceil( suggestedLatency * streamComp->bytesPerFrame |
| 1518 |
* streamComp->hpiFormat.dwSampleRate ); |
| 1519 |
} |
| 1520 |
} |
| 1521 |
|
| 1522 |
|
| 1523 |
|
| 1524 |
bbmBufferSize = ((uint32_t)ceil((bbmBufferSize + 20)/4096.0))*4096 - 20; |
| 1525 |
streamComp->hostBufferSize = bbmBufferSize; |
| 1526 |
|
| 1527 |
if( streamComp->hpiDevice->streamIsOutput ) |
| 1528 |
hpiError = HPI_OutStreamHostBufferAllocate( NULL, |
| 1529 |
streamComp->hpiStream, |
| 1530 |
bbmBufferSize ); |
| 1531 |
else |
| 1532 |
hpiError = HPI_InStreamHostBufferAllocate( NULL, |
| 1533 |
streamComp->hpiStream, |
| 1534 |
bbmBufferSize ); |
| 1535 |
if( hpiError ) |
| 1536 |
{ |
| 1537 |
|
| 1538 |
streamComp->hostBufferSize = 0; |
| 1539 |
|
| 1540 |
if( hpiError == HPI_ERROR_INVALID_DATASIZE ) |
| 1541 |
{ |
| 1542 |
|
| 1543 |
if( preLatencyBufferSize > 0 ) |
| 1544 |
{ |
| 1545 |
PA_DEBUG(( "Retrying BBM allocation with smaller size (%d vs. %d bytes)\n", |
| 1546 |
preLatencyBufferSize, bbmBufferSize )); |
| 1547 |
bbmBufferSize = preLatencyBufferSize; |
| 1548 |
if( streamComp->hpiDevice->streamIsOutput ) |
| 1549 |
hpiError = HPI_OutStreamHostBufferAllocate( NULL, |
| 1550 |
streamComp->hpiStream, |
| 1551 |
bbmBufferSize ); |
| 1552 |
else |
| 1553 |
hpiError = HPI_InStreamHostBufferAllocate( NULL, |
| 1554 |
streamComp->hpiStream, |
| 1555 |
bbmBufferSize ); |
| 1556 |
|
| 1557 |
if( hpiError ) |
| 1558 |
{ |
| 1559 |
PA_ASIHPI_REPORT_ERROR_( hpiError ); |
| 1560 |
|
| 1561 |
if( hpiError == HPI_ERROR_INVALID_DATASIZE ) |
| 1562 |
{ |
| 1563 |
result = paBufferTooBig; |
| 1564 |
goto error; |
| 1565 |
} |
| 1566 |
else if( hpiError != HPI_ERROR_INVALID_OPERATION ) |
| 1567 |
{ |
| 1568 |
result = paUnanticipatedHostError; |
| 1569 |
goto error; |
| 1570 |
} |
| 1571 |
} |
| 1572 |
else |
| 1573 |
{ |
| 1574 |
streamComp->hostBufferSize = bbmBufferSize; |
| 1575 |
hpiBufferSize = bbmBufferSize; |
| 1576 |
} |
| 1577 |
} |
| 1578 |
else |
| 1579 |
{ |
| 1580 |
result = paBufferTooBig; |
| 1581 |
goto error; |
| 1582 |
} |
| 1583 |
} |
| 1584 |
|
| 1585 |
|
| 1586 |
else if (( hpiError != HPI_ERROR_INVALID_OPERATION ) && |
| 1587 |
( hpiError != HPI_ERROR_INVALID_FUNC )) |
| 1588 |
{ |
| 1589 |
PA_ASIHPI_REPORT_ERROR_( hpiError ); |
| 1590 |
result = paUnanticipatedHostError; |
| 1591 |
goto error; |
| 1592 |
} |
| 1593 |
} |
| 1594 |
else |
| 1595 |
{ |
| 1596 |
hpiBufferSize = bbmBufferSize; |
| 1597 |
} |
| 1598 |
} |
| 1599 |
|
| 1600 |
|
| 1601 |
paHostBufferSize = streamComp->bytesPerFrame * framesPerPaHostBuffer; |
| 1602 |
if( hpiBufferSize < 3*paHostBufferSize ) |
| 1603 |
{ |
| 1604 |
result = paBufferTooBig; |
| 1605 |
goto error; |
| 1606 |
} |
| 1607 |
|
| 1608 |
if( streamComp->hpiDevice->streamIsOutput ) |
| 1609 |
{ |
| 1610 |
PaTime latency = suggestedLatency > 0.0 ? suggestedLatency : |
| 1611 |
streamComp->hpiDevice->baseDeviceInfo.defaultHighOutputLatency; |
| 1612 |
streamComp->outputBufferCap = |
| 1613 |
(uint32_t)ceil( latency * streamComp->bytesPerFrame * streamComp->hpiFormat.dwSampleRate ); |
| 1614 |
|
| 1615 |
if( streamComp->outputBufferCap < 4*paHostBufferSize ) |
| 1616 |
streamComp->outputBufferCap = 4*paHostBufferSize; |
| 1617 |
} |
| 1618 |
else |
| 1619 |
{ |
| 1620 |
streamComp->outputBufferCap = 0; |
| 1621 |
} |
| 1622 |
|
| 1623 |
streamComp->tempBufferSize = paHostBufferSize; |
| 1624 |
|
| 1625 |
PA_UNLESS_( streamComp->tempBuffer = (uint8_t *)PaUtil_AllocateMemory( streamComp->tempBufferSize ), |
| 1626 |
paInsufficientMemory ); |
| 1627 |
error: |
| 1628 |
return result; |
| 1629 |
} |
| 1630 |
|
| 1631 |
|
| 1632 |
|
| 1633 |
|
| 1634 |
|
| 1635 |
|
| 1636 |
|
| 1637 |
|
| 1638 |
|
| 1639 |
|
| 1640 |
|
| 1641 |
|
| 1642 |
|
| 1643 |
|
| 1644 |
|
| 1645 |
|
| 1646 |
|
| 1647 |
|
| 1648 |
|
| 1649 |
|
| 1650 |
|
| 1651 |
|
| 1652 |
|
| 1653 |
|
| 1654 |
|
| 1655 |
|
| 1656 |
|
| 1657 |
|
| 1658 |
|
| 1659 |
|
| 1660 |
|
| 1661 |
static PaError OpenStream( struct PaUtilHostApiRepresentation *hostApi, |
| 1662 |
PaStream **s, |
| 1663 |
const PaStreamParameters *inputParameters, |
| 1664 |
const PaStreamParameters *outputParameters, |
| 1665 |
double sampleRate, |
| 1666 |
unsigned long framesPerBuffer, |
| 1667 |
PaStreamFlags streamFlags, |
| 1668 |
PaStreamCallback *streamCallback, |
| 1669 |
void *userData ) |
| 1670 |
{ |
| 1671 |
PaError result = paNoError; |
| 1672 |
PaAsiHpiHostApiRepresentation *hpiHostApi = (PaAsiHpiHostApiRepresentation*)hostApi; |
| 1673 |
PaAsiHpiStream *stream = NULL; |
| 1674 |
unsigned long framesPerHostBuffer = framesPerBuffer; |
| 1675 |
int inputChannelCount = 0, outputChannelCount = 0; |
| 1676 |
PaSampleFormat inputSampleFormat = 0, outputSampleFormat = 0; |
| 1677 |
PaSampleFormat hostInputSampleFormat = 0, hostOutputSampleFormat = 0; |
| 1678 |
PaTime maxSuggestedLatency = 0.0; |
| 1679 |
|
| 1680 |
|
| 1681 |
if( (streamFlags & paPlatformSpecificFlags) != 0 ) |
| 1682 |
return paInvalidFlag; |
| 1683 |
|
| 1684 |
|
| 1685 |
PA_UNLESS_( stream = (PaAsiHpiStream *)PaUtil_AllocateMemory( sizeof(PaAsiHpiStream) ), |
| 1686 |
paInsufficientMemory ); |
| 1687 |
memset( stream, 0, sizeof(PaAsiHpiStream) ); |
| 1688 |
|
| 1689 |
|
| 1690 |
if( framesPerHostBuffer == paFramesPerBufferUnspecified ) |
| 1691 |
{ |
| 1692 |
if( inputParameters ) |
| 1693 |
maxSuggestedLatency = inputParameters->suggestedLatency; |
| 1694 |
if( outputParameters && (outputParameters->suggestedLatency > maxSuggestedLatency) ) |
| 1695 |
maxSuggestedLatency = outputParameters->suggestedLatency; |
| 1696 |
|
| 1697 |
if( maxSuggestedLatency > 0.0 ) |
| 1698 |
framesPerHostBuffer = (unsigned long)ceil( maxSuggestedLatency * sampleRate ); |
| 1699 |
else |
| 1700 |
|
| 1701 |
framesPerHostBuffer = 4096; |
| 1702 |
} |
| 1703 |
|
| 1704 |
if( 1000.0*framesPerHostBuffer/sampleRate < PA_ASIHPI_MIN_POLLING_INTERVAL_ ) |
| 1705 |
framesPerHostBuffer = (unsigned long)ceil( sampleRate * PA_ASIHPI_MIN_POLLING_INTERVAL_ / 1000.0 ); |
| 1706 |
|
| 1707 |
|
| 1708 |
|
| 1709 |
if( framesPerBuffer > 0 ) |
| 1710 |
framesPerHostBuffer = (unsigned long)ceil( (double)framesPerHostBuffer / framesPerBuffer ) * framesPerBuffer; |
| 1711 |
|
| 1712 |
|
| 1713 |
framesPerHostBuffer = (framesPerHostBuffer / 4) * 4; |
| 1714 |
|
| 1715 |
stream->pollingInterval = (uint32_t)ceil( 1000.0*framesPerHostBuffer/sampleRate ); |
| 1716 |
assert( framesPerHostBuffer > 0 ); |
| 1717 |
|
| 1718 |
|
| 1719 |
if( inputParameters ) |
| 1720 |
{ |
| 1721 |
|
| 1722 |
PA_UNLESS_( stream->input = (PaAsiHpiStreamComponent *)PaUtil_AllocateMemory( sizeof(PaAsiHpiStreamComponent) ), |
| 1723 |
paInsufficientMemory ); |
| 1724 |
memset( stream->input, 0, sizeof(PaAsiHpiStreamComponent) ); |
| 1725 |
|
| 1726 |
PA_ENSURE_( PaAsiHpi_CreateFormat( hostApi, inputParameters, sampleRate, |
| 1727 |
&stream->input->hpiDevice, &stream->input->hpiFormat ) ); |
| 1728 |
|
| 1729 |
PA_ENSURE_( PaAsiHpi_OpenInput( hostApi, stream->input->hpiDevice, &stream->input->hpiFormat, |
| 1730 |
&stream->input->hpiStream ) ); |
| 1731 |
inputChannelCount = inputParameters->channelCount; |
| 1732 |
inputSampleFormat = inputParameters->sampleFormat; |
| 1733 |
hostInputSampleFormat = PaAsiHpi_HpiToPaFormat( stream->input->hpiFormat.wFormat ); |
| 1734 |
stream->input->bytesPerFrame = inputChannelCount * Pa_GetSampleSize( hostInputSampleFormat ); |
| 1735 |
assert( stream->input->bytesPerFrame > 0 ); |
| 1736 |
|
| 1737 |
PA_ENSURE_( PaAsiHpi_SetupBuffers( stream->input, stream->pollingInterval, |
| 1738 |
framesPerHostBuffer, inputParameters->suggestedLatency ) ); |
| 1739 |
} |
| 1740 |
if( outputParameters ) |
| 1741 |
{ |
| 1742 |
|
| 1743 |
PA_UNLESS_( stream->output = (PaAsiHpiStreamComponent *)PaUtil_AllocateMemory( sizeof(PaAsiHpiStreamComponent) ), |
| 1744 |
paInsufficientMemory ); |
| 1745 |
memset( stream->output, 0, sizeof(PaAsiHpiStreamComponent) ); |
| 1746 |
|
| 1747 |
PA_ENSURE_( PaAsiHpi_CreateFormat( hostApi, outputParameters, sampleRate, |
| 1748 |
&stream->output->hpiDevice, &stream->output->hpiFormat ) ); |
| 1749 |
|
| 1750 |
PA_ENSURE_( PaAsiHpi_OpenOutput( hostApi, stream->output->hpiDevice, |
| 1751 |
&stream->output->hpiFormat, |
| 1752 |
&stream->output->hpiStream ) ); |
| 1753 |
outputChannelCount = outputParameters->channelCount; |
| 1754 |
outputSampleFormat = outputParameters->sampleFormat; |
| 1755 |
hostOutputSampleFormat = PaAsiHpi_HpiToPaFormat( stream->output->hpiFormat.wFormat ); |
| 1756 |
stream->output->bytesPerFrame = outputChannelCount * Pa_GetSampleSize( hostOutputSampleFormat ); |
| 1757 |
|
| 1758 |
PA_ENSURE_( PaAsiHpi_SetupBuffers( stream->output, stream->pollingInterval, |
| 1759 |
framesPerHostBuffer, outputParameters->suggestedLatency ) ); |
| 1760 |
} |
| 1761 |
|
| 1762 |
|
| 1763 |
if( inputParameters && outputParameters ) |
| 1764 |
{ |
| 1765 |
stream->maxFramesPerHostBuffer = PA_MIN( stream->input->tempBufferSize / stream->input->bytesPerFrame, |
| 1766 |
stream->output->tempBufferSize / stream->output->bytesPerFrame ); |
| 1767 |
} |
| 1768 |
else |
| 1769 |
{ |
| 1770 |
stream->maxFramesPerHostBuffer = inputParameters ? stream->input->tempBufferSize / stream->input->bytesPerFrame |
| 1771 |
: stream->output->tempBufferSize / stream->output->bytesPerFrame; |
| 1772 |
} |
| 1773 |
assert( stream->maxFramesPerHostBuffer > 0 ); |
| 1774 |
|
| 1775 |
stream->neverDropInput = streamFlags & paNeverDropInput; |
| 1776 |
stream->state = paAsiHpiStoppedState; |
| 1777 |
|
| 1778 |
|
| 1779 |
if( streamCallback ) |
| 1780 |
{ |
| 1781 |
PaUtil_InitializeStreamRepresentation( &stream->baseStreamRep, |
| 1782 |
&hpiHostApi->callbackStreamInterface, |
| 1783 |
streamCallback, userData ); |
| 1784 |
stream->callbackMode = 1; |
| 1785 |
} |
| 1786 |
else |
| 1787 |
{ |
| 1788 |
PaUtil_InitializeStreamRepresentation( &stream->baseStreamRep, |
| 1789 |
&hpiHostApi->blockingStreamInterface, |
| 1790 |
streamCallback, userData ); |
| 1791 |
|
| 1792 |
PA_UNLESS_( stream->blockingUserBufferCopy = |
| 1793 |
PaUtil_AllocateMemory( sizeof(void *) * PA_MAX( inputChannelCount, outputChannelCount ) ), |
| 1794 |
paInsufficientMemory ); |
| 1795 |
stream->callbackMode = 0; |
| 1796 |
} |
| 1797 |
PaUtil_InitializeCpuLoadMeasurer( &stream->cpuLoadMeasurer, sampleRate ); |
| 1798 |
|
| 1799 |
|
| 1800 |
|
| 1801 |
PA_ENSURE_( PaUtil_InitializeBufferProcessor( &stream->bufferProcessor, |
| 1802 |
inputChannelCount, inputSampleFormat, hostInputSampleFormat, |
| 1803 |
outputChannelCount, outputSampleFormat, hostOutputSampleFormat, |
| 1804 |
sampleRate, streamFlags, |
| 1805 |
framesPerBuffer, framesPerHostBuffer, paUtilFixedHostBufferSize, |
| 1806 |
streamCallback, userData ) ); |
| 1807 |
|
| 1808 |
stream->baseStreamRep.streamInfo.structVersion = 1; |
| 1809 |
stream->baseStreamRep.streamInfo.sampleRate = sampleRate; |
| 1810 |
|
| 1811 |
if( stream->input ) |
| 1812 |
{ |
| 1813 |
PaTime bufferDuration = ( stream->input->hostBufferSize + stream->input->hardwareBufferSize ) |
| 1814 |
/ sampleRate / stream->input->bytesPerFrame; |
| 1815 |
stream->baseStreamRep.streamInfo.inputLatency = |
| 1816 |
bufferDuration + |
| 1817 |
((PaTime)PaUtil_GetBufferProcessorInputLatencyFrames( &stream->bufferProcessor ) - |
| 1818 |
stream->maxFramesPerHostBuffer) / sampleRate; |
| 1819 |
assert( stream->baseStreamRep.streamInfo.inputLatency > 0.0 ); |
| 1820 |
} |
| 1821 |
|
| 1822 |
if( stream->output ) |
| 1823 |
{ |
| 1824 |
PaTime bufferDuration = ( stream->output->hostBufferSize + stream->output->hardwareBufferSize ) |
| 1825 |
/ sampleRate / stream->output->bytesPerFrame; |
| 1826 |
|
| 1827 |
if( !stream->input && (stream->output->outputBufferCap > 0) ) |
| 1828 |
{ |
| 1829 |
bufferDuration = PA_MIN( bufferDuration, |
| 1830 |
stream->output->outputBufferCap / sampleRate / stream->output->bytesPerFrame ); |
| 1831 |
} |
| 1832 |
stream->baseStreamRep.streamInfo.outputLatency = |
| 1833 |
bufferDuration + |
| 1834 |
((PaTime)PaUtil_GetBufferProcessorOutputLatencyFrames( &stream->bufferProcessor ) - |
| 1835 |
stream->maxFramesPerHostBuffer) / sampleRate; |
| 1836 |
assert( stream->baseStreamRep.streamInfo.outputLatency > 0.0 ); |
| 1837 |
} |
| 1838 |
|
| 1839 |
|
| 1840 |
PaAsiHpi_StreamDump( stream ); |
| 1841 |
|
| 1842 |
|
| 1843 |
*s = (PaStream*)stream; |
| 1844 |
return result; |
| 1845 |
|
| 1846 |
error: |
| 1847 |
CloseStream( (PaStream*)stream ); |
| 1848 |
return result; |
| 1849 |
} |
| 1850 |
|
| 1851 |
|
| 1852 |
|
| 1853 |
|
| 1854 |
|
| 1855 |
|
| 1856 |
|
| 1857 |
|
| 1858 |
|
| 1859 |
|
| 1860 |
|
| 1861 |
static PaError CloseStream( PaStream *s ) |
| 1862 |
{ |
| 1863 |
PaError result = paNoError; |
| 1864 |
PaAsiHpiStream *stream = (PaAsiHpiStream*)s; |
| 1865 |
|
| 1866 |
|
| 1867 |
if( stream == NULL ) |
| 1868 |
return paNoError; |
| 1869 |
|
| 1870 |
|
| 1871 |
PaUtil_TerminateBufferProcessor( &stream->bufferProcessor ); |
| 1872 |
PaUtil_TerminateStreamRepresentation( &stream->baseStreamRep ); |
| 1873 |
|
| 1874 |
|
| 1875 |
if( stream->input ) |
| 1876 |
{ |
| 1877 |
|
| 1878 |
if( stream->input->hpiStream ) |
| 1879 |
{ |
| 1880 |
PA_ASIHPI_UNLESS_( HPI_InStreamClose( NULL, |
| 1881 |
stream->input->hpiStream ), paUnanticipatedHostError ); |
| 1882 |
} |
| 1883 |
|
| 1884 |
PaUtil_FreeMemory( stream->input->tempBuffer ); |
| 1885 |
PaUtil_FreeMemory( stream->input ); |
| 1886 |
} |
| 1887 |
if( stream->output ) |
| 1888 |
{ |
| 1889 |
|
| 1890 |
if( stream->output->hpiStream ) |
| 1891 |
{ |
| 1892 |
PA_ASIHPI_UNLESS_( HPI_OutStreamClose( NULL, |
| 1893 |
stream->output->hpiStream ), paUnanticipatedHostError ); |
| 1894 |
} |
| 1895 |
|
| 1896 |
PaUtil_FreeMemory( stream->output->tempBuffer ); |
| 1897 |
PaUtil_FreeMemory( stream->output ); |
| 1898 |
} |
| 1899 |
|
| 1900 |
PaUtil_FreeMemory( stream->blockingUserBufferCopy ); |
| 1901 |
PaUtil_FreeMemory( stream ); |
| 1902 |
|
| 1903 |
error: |
| 1904 |
return result; |
| 1905 |
} |
| 1906 |
|
| 1907 |
|
| 1908 |
|
| 1909 |
|
| 1910 |
|
| 1911 |
|
| 1912 |
|
| 1913 |
|
| 1914 |
|
| 1915 |
|
| 1916 |
|
| 1917 |
static PaError PaAsiHpi_PrimeOutputWithSilence( PaAsiHpiStream *stream ) |
| 1918 |
{ |
| 1919 |
PaError result = paNoError; |
| 1920 |
PaAsiHpiStreamComponent *out; |
| 1921 |
PaUtilZeroer *zeroer; |
| 1922 |
PaSampleFormat outputFormat; |
| 1923 |
assert( stream ); |
| 1924 |
out = stream->output; |
| 1925 |
|
| 1926 |
if( !out ) |
| 1927 |
return result; |
| 1928 |
assert( out->tempBuffer ); |
| 1929 |
|
| 1930 |
|
| 1931 |
PA_ASIHPI_UNLESS_( HPI_OutStreamReset( NULL, |
| 1932 |
out->hpiStream ), paUnanticipatedHostError ); |
| 1933 |
|
| 1934 |
outputFormat = PaAsiHpi_HpiToPaFormat( out->hpiFormat.wFormat ); |
| 1935 |
zeroer = PaUtil_SelectZeroer( outputFormat ); |
| 1936 |
zeroer(out->tempBuffer, 1, out->tempBufferSize / Pa_GetSampleSize(outputFormat) ); |
| 1937 |
|
| 1938 |
PA_ASIHPI_UNLESS_( HPI_OutStreamWriteBuf( NULL, out->hpiStream, |
| 1939 |
out->tempBuffer, out->tempBufferSize, &out->hpiFormat), |
| 1940 |
paUnanticipatedHostError ); |
| 1941 |
PA_ASIHPI_UNLESS_( HPI_OutStreamWriteBuf( NULL, out->hpiStream, |
| 1942 |
out->tempBuffer, out->tempBufferSize, &out->hpiFormat), |
| 1943 |
paUnanticipatedHostError ); |
| 1944 |
error: |
| 1945 |
return result; |
| 1946 |
} |
| 1947 |
|
| 1948 |
|
| 1949 |
|
| 1950 |
|
| 1951 |
|
| 1952 |
|
| 1953 |
|
| 1954 |
|
| 1955 |
|
| 1956 |
|
| 1957 |
|
| 1958 |
|
| 1959 |
|
| 1960 |
|
| 1961 |
static PaError PaAsiHpi_StartStream( PaAsiHpiStream *stream, int outputPrimed ) |
| 1962 |
{ |
| 1963 |
PaError result = paNoError; |
| 1964 |
|
| 1965 |
if( stream->input ) |
| 1966 |
{ |
| 1967 |
PA_ASIHPI_UNLESS_( HPI_InStreamStart( NULL, |
| 1968 |
stream->input->hpiStream ), paUnanticipatedHostError ); |
| 1969 |
} |
| 1970 |
if( stream->output ) |
| 1971 |
{ |
| 1972 |
if( !outputPrimed ) |
| 1973 |
{ |
| 1974 |
|
| 1975 |
PA_ENSURE_( PaAsiHpi_PrimeOutputWithSilence( stream ) ); |
| 1976 |
} |
| 1977 |
PA_ASIHPI_UNLESS_( HPI_OutStreamStart( NULL, |
| 1978 |
stream->output->hpiStream ), paUnanticipatedHostError ); |
| 1979 |
} |
| 1980 |
stream->state = paAsiHpiActiveState; |
| 1981 |
stream->callbackFinished = 0; |
| 1982 |
|
| 1983 |
|
| 1984 |
|
| 1985 |
|
| 1986 |
error: |
| 1987 |
return result; |
| 1988 |
} |
| 1989 |
|
| 1990 |
|
| 1991 |
|
| 1992 |
|
| 1993 |
|
| 1994 |
|
| 1995 |
|
| 1996 |
|
| 1997 |
|
| 1998 |
|
| 1999 |
|
| 2000 |
|
| 2001 |
static PaError StartStream( PaStream *s ) |
| 2002 |
{ |
| 2003 |
PaError result = paNoError; |
| 2004 |
PaAsiHpiStream *stream = (PaAsiHpiStream*)s; |
| 2005 |
|
| 2006 |
assert( stream ); |
| 2007 |
|
| 2008 |
|
| 2009 |
PaUtil_ResetBufferProcessor( &stream->bufferProcessor ); |
| 2010 |
|
| 2011 |
if( stream->callbackMode ) |
| 2012 |
{ |
| 2013 |
|
| 2014 |
|
| 2015 |
PA_ENSURE_( PaUnixThread_New( &stream->thread, &CallbackThreadFunc, stream, 1., 0 ) ); |
| 2016 |
} |
| 2017 |
else |
| 2018 |
{ |
| 2019 |
PA_ENSURE_( PaAsiHpi_StartStream( stream, 0 ) ); |
| 2020 |
} |
| 2021 |
|
| 2022 |
error: |
| 2023 |
return result; |
| 2024 |
} |
| 2025 |
|
| 2026 |
|
| 2027 |
|
| 2028 |
|
| 2029 |
|
| 2030 |
|
| 2031 |
|
| 2032 |
|
| 2033 |
|
| 2034 |
|
| 2035 |
|
| 2036 |
|
| 2037 |
|
| 2038 |
|
| 2039 |
|
| 2040 |
static PaError PaAsiHpi_StopStream( PaAsiHpiStream *stream, int abort ) |
| 2041 |
{ |
| 2042 |
PaError result = paNoError; |
| 2043 |
|
| 2044 |
assert( stream ); |
| 2045 |
|
| 2046 |
|
| 2047 |
if( stream->input ) |
| 2048 |
{ |
| 2049 |
PA_ASIHPI_UNLESS_( HPI_InStreamReset( NULL, |
| 2050 |
stream->input->hpiStream ), paUnanticipatedHostError ); |
| 2051 |
} |
| 2052 |
|
| 2053 |
if( stream->output ) |
| 2054 |
{ |
| 2055 |
if( !abort ) |
| 2056 |
{ |
| 2057 |
|
| 2058 |
while( 1 ) |
| 2059 |
{ |
| 2060 |
PaAsiHpiStreamInfo streamInfo; |
| 2061 |
PaTime timeLeft; |
| 2062 |
|
| 2063 |
|
| 2064 |
PA_ENSURE_( PaAsiHpi_GetStreamInfo( stream->output, &streamInfo ) ); |
| 2065 |
|
| 2066 |
if( (streamInfo.state != HPI_STATE_PLAYING) && |
| 2067 |
(streamInfo.dataSize < stream->output->bytesPerFrame * PA_ASIHPI_MIN_FRAMES_) ) |
| 2068 |
break; |
| 2069 |
|
| 2070 |
timeLeft = 1000.0 * streamInfo.dataSize / stream->output->bytesPerFrame |
| 2071 |
/ stream->baseStreamRep.streamInfo.sampleRate; |
| 2072 |
Pa_Sleep( (long)ceil( timeLeft ) ); |
| 2073 |
} |
| 2074 |
} |
| 2075 |
PA_ASIHPI_UNLESS_( HPI_OutStreamReset( NULL, |
| 2076 |
stream->output->hpiStream ), paUnanticipatedHostError ); |
| 2077 |
} |
| 2078 |
|
| 2079 |
|
| 2080 |
|
| 2081 |
|
| 2082 |
error: |
| 2083 |
return result; |
| 2084 |
} |
| 2085 |
|
| 2086 |
|
| 2087 |
|
| 2088 |
|
| 2089 |
|
| 2090 |
|
| 2091 |
|
| 2092 |
|
| 2093 |
|
| 2094 |
|
| 2095 |
|
| 2096 |
|
| 2097 |
|
| 2098 |
|
| 2099 |
|
| 2100 |
|
| 2101 |
|
| 2102 |
|
| 2103 |
|
| 2104 |
static PaError PaAsiHpi_ExplicitStop( PaAsiHpiStream *stream, int abort ) |
| 2105 |
{ |
| 2106 |
PaError result = paNoError; |
| 2107 |
|
| 2108 |
|
| 2109 |
if( stream->callbackMode ) |
| 2110 |
{ |
| 2111 |
PaError threadRes; |
| 2112 |
stream->callbackAbort = abort; |
| 2113 |
if( abort ) |
| 2114 |
{ |
| 2115 |
PA_DEBUG(( "Aborting callback\n" )); |
| 2116 |
} |
| 2117 |
else |
| 2118 |
{ |
| 2119 |
PA_DEBUG(( "Stopping callback\n" )); |
| 2120 |
} |
| 2121 |
PA_ENSURE_( PaUnixThread_Terminate( &stream->thread, !abort, &threadRes ) ); |
| 2122 |
if( threadRes != paNoError ) |
| 2123 |
{ |
| 2124 |
PA_DEBUG(( "Callback thread returned: %d\n", threadRes )); |
| 2125 |
} |
| 2126 |
} |
| 2127 |
else |
| 2128 |
{ |
| 2129 |
PA_ENSURE_( PaAsiHpi_StopStream( stream, abort ) ); |
| 2130 |
} |
| 2131 |
|
| 2132 |
stream->state = paAsiHpiStoppedState; |
| 2133 |
|
| 2134 |
error: |
| 2135 |
return result; |
| 2136 |
} |
| 2137 |
|
| 2138 |
|
| 2139 |
|
| 2140 |
|
| 2141 |
|
| 2142 |
|
| 2143 |
|
| 2144 |
|
| 2145 |
|
| 2146 |
static PaError StopStream( PaStream *s ) |
| 2147 |
{ |
| 2148 |
return PaAsiHpi_ExplicitStop( (PaAsiHpiStream *) s, 0 ); |
| 2149 |
} |
| 2150 |
|
| 2151 |
|
| 2152 |
|
| 2153 |
|
| 2154 |
|
| 2155 |
|
| 2156 |
|
| 2157 |
|
| 2158 |
|
| 2159 |
static PaError AbortStream( PaStream *s ) |
| 2160 |
{ |
| 2161 |
return PaAsiHpi_ExplicitStop( (PaAsiHpiStream * ) s, 1 ); |
| 2162 |
} |
| 2163 |
|
| 2164 |
|
| 2165 |
|
| 2166 |
|
| 2167 |
|
| 2168 |
|
| 2169 |
|
| 2170 |
|
| 2171 |
|
| 2172 |
|
| 2173 |
|
| 2174 |
|
| 2175 |
|
| 2176 |
static PaError IsStreamStopped( PaStream *s ) |
| 2177 |
{ |
| 2178 |
PaAsiHpiStream *stream = (PaAsiHpiStream*)s; |
| 2179 |
|
| 2180 |
assert( stream ); |
| 2181 |
return stream->state == paAsiHpiStoppedState ? 1 : 0; |
| 2182 |
} |
| 2183 |
|
| 2184 |
|
| 2185 |
|
| 2186 |
|
| 2187 |
|
| 2188 |
|
| 2189 |
|
| 2190 |
|
| 2191 |
|
| 2192 |
|
| 2193 |
|
| 2194 |
|
| 2195 |
|
| 2196 |
|
| 2197 |
static PaError IsStreamActive( PaStream *s ) |
| 2198 |
{ |
| 2199 |
PaAsiHpiStream *stream = (PaAsiHpiStream*)s; |
| 2200 |
|
| 2201 |
assert( stream ); |
| 2202 |
return stream->state == paAsiHpiActiveState ? 1 : 0; |
| 2203 |
} |
| 2204 |
|
| 2205 |
|
| 2206 |
|
| 2207 |
|
| 2208 |
|
| 2209 |
|
| 2210 |
|
| 2211 |
|
| 2212 |
|
| 2213 |
|
| 2214 |
|
| 2215 |
static PaTime GetStreamTime( PaStream *s ) |
| 2216 |
{ |
| 2217 |
return PaUtil_GetTime(); |
| 2218 |
} |
| 2219 |
|
| 2220 |
|
| 2221 |
|
| 2222 |
|
| 2223 |
|
| 2224 |
|
| 2225 |
|
| 2226 |
|
| 2227 |
static double GetStreamCpuLoad( PaStream *s ) |
| 2228 |
{ |
| 2229 |
PaAsiHpiStream *stream = (PaAsiHpiStream*)s; |
| 2230 |
|
| 2231 |
return stream->callbackMode ? PaUtil_GetCpuLoad( &stream->cpuLoadMeasurer ) : 0.0; |
| 2232 |
} |
| 2233 |
|
| 2234 |
|
| 2235 |
|
| 2236 |
|
| 2237 |
|
| 2238 |
|
| 2239 |
|
| 2240 |
|
| 2241 |
|
| 2242 |
|
| 2243 |
|
| 2244 |
static void PaAsiHpi_OnThreadExit( void *userData ) |
| 2245 |
{ |
| 2246 |
PaAsiHpiStream *stream = (PaAsiHpiStream *) userData; |
| 2247 |
|
| 2248 |
assert( stream ); |
| 2249 |
|
| 2250 |
PaUtil_ResetCpuLoadMeasurer( &stream->cpuLoadMeasurer ); |
| 2251 |
|
| 2252 |
PA_DEBUG(( "%s: Stopping HPI streams\n", __FUNCTION__ )); |
| 2253 |
PaAsiHpi_StopStream( stream, stream->callbackAbort ); |
| 2254 |
PA_DEBUG(( "%s: Stoppage\n", __FUNCTION__ )); |
| 2255 |
|
| 2256 |
|
| 2257 |
if( stream->baseStreamRep.streamFinishedCallback ) |
| 2258 |
{ |
| 2259 |
stream->baseStreamRep.streamFinishedCallback( stream->baseStreamRep.userData ); |
| 2260 |
} |
| 2261 |
|
| 2262 |
|
| 2263 |
|
| 2264 |
|
| 2265 |
if( stream->callbackFinished ) |
| 2266 |
stream->state = paAsiHpiCallbackFinishedState; |
| 2267 |
} |
| 2268 |
|
| 2269 |
|
| 2270 |
|
| 2271 |
|
| 2272 |
|
| 2273 |
|
| 2274 |
|
| 2275 |
|
| 2276 |
|
| 2277 |
|
| 2278 |
|
| 2279 |
|
| 2280 |
|
| 2281 |
|
| 2282 |
|
| 2283 |
|
| 2284 |
|
| 2285 |
|
| 2286 |
|
| 2287 |
static PaError PaAsiHpi_WaitForFrames( PaAsiHpiStream *stream, unsigned long *framesAvail, |
| 2288 |
PaStreamCallbackFlags *cbFlags ) |
| 2289 |
{ |
| 2290 |
PaError result = paNoError; |
| 2291 |
double sampleRate; |
| 2292 |
unsigned long framesTarget; |
| 2293 |
uint32_t outputData = 0, outputSpace = 0, inputData = 0, framesLeft = 0; |
| 2294 |
|
| 2295 |
assert( stream ); |
| 2296 |
assert( stream->input || stream->output ); |
| 2297 |
|
| 2298 |
sampleRate = stream->baseStreamRep.streamInfo.sampleRate; |
| 2299 |
|
| 2300 |
framesTarget = stream->bufferProcessor.framesPerHostBuffer; |
| 2301 |
assert( framesTarget > 0 ); |
| 2302 |
|
| 2303 |
while( 1 ) |
| 2304 |
{ |
| 2305 |
PaAsiHpiStreamInfo info; |
| 2306 |
|
| 2307 |
if( stream->output ) |
| 2308 |
{ |
| 2309 |
PA_ENSURE_( PaAsiHpi_GetStreamInfo( stream->output, &info ) ); |
| 2310 |
|
| 2311 |
if( info.availableFrames < framesTarget ) |
| 2312 |
{ |
| 2313 |
framesLeft = framesTarget - info.availableFrames; |
| 2314 |
Pa_Sleep( (long)ceil( 1000 * framesLeft / sampleRate ) ); |
| 2315 |
continue; |
| 2316 |
} |
| 2317 |
|
| 2318 |
|
| 2319 |
|
| 2320 |
|
| 2321 |
|
| 2322 |
|
| 2323 |
if( !stream->input && (stream->output->outputBufferCap > 0) && |
| 2324 |
( info.totalBufferedData > stream->output->outputBufferCap / stream->output->bytesPerFrame ) ) |
| 2325 |
{ |
| 2326 |
framesLeft = info.totalBufferedData - stream->output->outputBufferCap / stream->output->bytesPerFrame; |
| 2327 |
Pa_Sleep( (long)ceil( 1000 * framesLeft / sampleRate ) ); |
| 2328 |
continue; |
| 2329 |
} |
| 2330 |
outputData = info.totalBufferedData; |
| 2331 |
outputSpace = info.availableFrames; |
| 2332 |
|
| 2333 |
if( info.underflow ) |
| 2334 |
{ |
| 2335 |
*cbFlags |= paOutputUnderflow; |
| 2336 |
} |
| 2337 |
} |
| 2338 |
|
| 2339 |
|
| 2340 |
if( stream->input ) |
| 2341 |
{ |
| 2342 |
PA_ENSURE_( PaAsiHpi_GetStreamInfo( stream->input, &info ) ); |
| 2343 |
|
| 2344 |
if( info.availableFrames < framesTarget ) |
| 2345 |
{ |
| 2346 |
framesLeft = framesTarget - info.availableFrames; |
| 2347 |
|
| 2348 |
|
| 2349 |
if( !stream->output || (outputData > framesLeft) ) |
| 2350 |
{ |
| 2351 |
Pa_Sleep( (long)ceil( 1000 * framesLeft / sampleRate ) ); |
| 2352 |
continue; |
| 2353 |
} |
| 2354 |
} |
| 2355 |
inputData = info.availableFrames; |
| 2356 |
|
| 2357 |
|
| 2358 |
|
| 2359 |
|
| 2360 |
|
| 2361 |
|
| 2362 |
|
| 2363 |
|
| 2364 |
if( info.overflow ) |
| 2365 |
{ |
| 2366 |
*cbFlags |= paInputOverflow; |
| 2367 |
} |
| 2368 |
} |
| 2369 |
break; |
| 2370 |
} |
| 2371 |
|
| 2372 |
if( stream->input && stream->output ) |
| 2373 |
{ |
| 2374 |
if( outputSpace >= framesTarget ) |
| 2375 |
*framesAvail = outputSpace; |
| 2376 |
|
| 2377 |
if( (inputData >= framesTarget) && (inputData < outputSpace) ) |
| 2378 |
*framesAvail = inputData; |
| 2379 |
} |
| 2380 |
else |
| 2381 |
{ |
| 2382 |
*framesAvail = stream->input ? inputData : outputSpace; |
| 2383 |
} |
| 2384 |
|
| 2385 |
error: |
| 2386 |
return result; |
| 2387 |
} |
| 2388 |
|
| 2389 |
|
| 2390 |
|
| 2391 |
|
| 2392 |
|
| 2393 |
|
| 2394 |
|
| 2395 |
|
| 2396 |
|
| 2397 |
|
| 2398 |
|
| 2399 |
|
| 2400 |
|
| 2401 |
|
| 2402 |
|
| 2403 |
|
| 2404 |
|
| 2405 |
|
| 2406 |
static void PaAsiHpi_CalculateTimeInfo( PaAsiHpiStream *stream, PaStreamCallbackTimeInfo *timeInfo ) |
| 2407 |
{ |
| 2408 |
PaAsiHpiStreamInfo streamInfo; |
| 2409 |
double sampleRate; |
| 2410 |
|
| 2411 |
assert( stream ); |
| 2412 |
assert( timeInfo ); |
| 2413 |
sampleRate = stream->baseStreamRep.streamInfo.sampleRate; |
| 2414 |
|
| 2415 |
|
| 2416 |
timeInfo->currentTime = GetStreamTime( (PaStream *)stream ); |
| 2417 |
|
| 2418 |
|
| 2419 |
timeInfo->inputBufferAdcTime = timeInfo->currentTime; |
| 2420 |
if( stream->input ) |
| 2421 |
{ |
| 2422 |
PaAsiHpi_GetStreamInfo( stream->input, &streamInfo ); |
| 2423 |
timeInfo->inputBufferAdcTime -= streamInfo.totalBufferedData / sampleRate; |
| 2424 |
} |
| 2425 |
|
| 2426 |
|
| 2427 |
timeInfo->outputBufferDacTime = timeInfo->currentTime; |
| 2428 |
if( stream->output ) |
| 2429 |
{ |
| 2430 |
PaAsiHpi_GetStreamInfo( stream->output, &streamInfo ); |
| 2431 |
timeInfo->outputBufferDacTime += streamInfo.totalBufferedData / sampleRate; |
| 2432 |
} |
| 2433 |
} |
| 2434 |
|
| 2435 |
|
| 2436 |
|
| 2437 |
|
| 2438 |
|
| 2439 |
|
| 2440 |
|
| 2441 |
|
| 2442 |
|
| 2443 |
|
| 2444 |
|
| 2445 |
|
| 2446 |
|
| 2447 |
|
| 2448 |
|
| 2449 |
|
| 2450 |
static PaError PaAsiHpi_BeginProcessing( PaAsiHpiStream *stream, unsigned long *numFrames, |
| 2451 |
PaStreamCallbackFlags *cbFlags ) |
| 2452 |
{ |
| 2453 |
PaError result = paNoError; |
| 2454 |
|
| 2455 |
assert( stream ); |
| 2456 |
if( *numFrames > stream->maxFramesPerHostBuffer ) |
| 2457 |
*numFrames = stream->maxFramesPerHostBuffer; |
| 2458 |
|
| 2459 |
if( stream->input ) |
| 2460 |
{ |
| 2461 |
PaAsiHpiStreamInfo info; |
| 2462 |
|
| 2463 |
uint32_t framesToGet = *numFrames; |
| 2464 |
|
| 2465 |
|
| 2466 |
PA_ENSURE_( PaAsiHpi_GetStreamInfo( stream->input, &info ) ); |
| 2467 |
if( info.overflow ) |
| 2468 |
{ |
| 2469 |
*cbFlags |= paInputOverflow; |
| 2470 |
} |
| 2471 |
|
| 2472 |
if( framesToGet > info.availableFrames ) |
| 2473 |
{ |
| 2474 |
PaUtilZeroer *zeroer; |
| 2475 |
PaSampleFormat inputFormat; |
| 2476 |
|
| 2477 |
|
| 2478 |
if( stream->output ) |
| 2479 |
*cbFlags |= paInputUnderflow; |
| 2480 |
framesToGet = info.availableFrames; |
| 2481 |
|
| 2482 |
inputFormat = PaAsiHpi_HpiToPaFormat( stream->input->hpiFormat.wFormat ); |
| 2483 |
zeroer = PaUtil_SelectZeroer( inputFormat ); |
| 2484 |
zeroer(stream->input->tempBuffer, 1, |
| 2485 |
stream->input->tempBufferSize / Pa_GetSampleSize(inputFormat) ); |
| 2486 |
} |
| 2487 |
|
| 2488 |
|
| 2489 |
PA_ASIHPI_UNLESS_( HPI_InStreamReadBuf( NULL, |
| 2490 |
stream->input->hpiStream, |
| 2491 |
stream->input->tempBuffer, |
| 2492 |
framesToGet * stream->input->bytesPerFrame), |
| 2493 |
paUnanticipatedHostError ); |
| 2494 |
|
| 2495 |
PaUtil_SetInputFrameCount( &stream->bufferProcessor, *numFrames ); |
| 2496 |
|
| 2497 |
PaUtil_SetInterleavedInputChannels( &stream->bufferProcessor, |
| 2498 |
0, stream->input->tempBuffer, |
| 2499 |
stream->input->hpiFormat.wChannels ); |
| 2500 |
} |
| 2501 |
if( stream->output ) |
| 2502 |
{ |
| 2503 |
|
| 2504 |
PaUtil_SetOutputFrameCount( &stream->bufferProcessor, *numFrames ); |
| 2505 |
|
| 2506 |
PaUtil_SetInterleavedOutputChannels( &stream->bufferProcessor, |
| 2507 |
0, stream->output->tempBuffer, |
| 2508 |
stream->output->hpiFormat.wChannels ); |
| 2509 |
} |
| 2510 |
|
| 2511 |
error: |
| 2512 |
return result; |
| 2513 |
} |
| 2514 |
|
| 2515 |
|
| 2516 |
|
| 2517 |
|
| 2518 |
|
| 2519 |
|
| 2520 |
|
| 2521 |
|
| 2522 |
|
| 2523 |
|
| 2524 |
|
| 2525 |
|
| 2526 |
|
| 2527 |
static PaError PaAsiHpi_EndProcessing( PaAsiHpiStream *stream, unsigned long numFrames, |
| 2528 |
PaStreamCallbackFlags *cbFlags ) |
| 2529 |
{ |
| 2530 |
PaError result = paNoError; |
| 2531 |
|
| 2532 |
assert( stream ); |
| 2533 |
|
| 2534 |
if( stream->output ) |
| 2535 |
{ |
| 2536 |
PaAsiHpiStreamInfo info; |
| 2537 |
|
| 2538 |
PA_ENSURE_( PaAsiHpi_GetStreamInfo( stream->output, &info ) ); |
| 2539 |
if( info.underflow ) |
| 2540 |
{ |
| 2541 |
*cbFlags |= paOutputUnderflow; |
| 2542 |
} |
| 2543 |
|
| 2544 |
|
| 2545 |
PA_ASIHPI_UNLESS_( HPI_OutStreamWriteBuf( NULL, |
| 2546 |
stream->output->hpiStream, |
| 2547 |
stream->output->tempBuffer, |
| 2548 |
numFrames * stream->output->bytesPerFrame, |
| 2549 |
&stream->output->hpiFormat), |
| 2550 |
paUnanticipatedHostError ); |
| 2551 |
} |
| 2552 |
|
| 2553 |
error: |
| 2554 |
return result; |
| 2555 |
} |
| 2556 |
|
| 2557 |
|
| 2558 |
|
| 2559 |
|
| 2560 |
|
| 2561 |
|
| 2562 |
|
| 2563 |
|
| 2564 |
|
| 2565 |
|
| 2566 |
|
| 2567 |
static void *CallbackThreadFunc( void *userData ) |
| 2568 |
{ |
| 2569 |
PaError result = paNoError; |
| 2570 |
PaAsiHpiStream *stream = (PaAsiHpiStream *) userData; |
| 2571 |
int callbackResult = paContinue; |
| 2572 |
|
| 2573 |
assert( stream ); |
| 2574 |
|
| 2575 |
|
| 2576 |
pthread_cleanup_push( &PaAsiHpi_OnThreadExit, stream ); |
| 2577 |
|
| 2578 |
|
| 2579 |
PA_ENSURE_( PaUnixThread_PrepareNotify( &stream->thread ) ); |
| 2580 |
|
| 2581 |
PA_ENSURE_( PaAsiHpi_StartStream( stream, 0 ) ); |
| 2582 |
PA_ENSURE_( PaUnixThread_NotifyParent( &stream->thread ) ); |
| 2583 |
|
| 2584 |
|
| 2585 |
while( 1 ) |
| 2586 |
{ |
| 2587 |
PaStreamCallbackFlags cbFlags = 0; |
| 2588 |
unsigned long framesAvail, framesGot; |
| 2589 |
|
| 2590 |
pthread_testcancel(); |
| 2591 |
|
| 2592 |
|
| 2593 |
|
| 2594 |
|
| 2595 |
if( PaUnixThread_StopRequested( &stream->thread ) && (callbackResult == paContinue) ) |
| 2596 |
{ |
| 2597 |
PA_DEBUG(( "Setting callbackResult to paComplete\n" )); |
| 2598 |
callbackResult = paComplete; |
| 2599 |
} |
| 2600 |
|
| 2601 |
|
| 2602 |
if( callbackResult != paContinue ) |
| 2603 |
{ |
| 2604 |
stream->callbackAbort = (callbackResult == paAbort); |
| 2605 |
if( stream->callbackAbort || |
| 2606 |
|
| 2607 |
PaUtil_IsBufferProcessorOutputEmpty( &stream->bufferProcessor ) ) |
| 2608 |
{ |
| 2609 |
goto end; |
| 2610 |
} |
| 2611 |
PA_DEBUG(( "%s: Flushing buffer processor\n", __FUNCTION__ )); |
| 2612 |
|
| 2613 |
} |
| 2614 |
|
| 2615 |
|
| 2616 |
|
| 2617 |
|
| 2618 |
PA_ENSURE_( PaAsiHpi_WaitForFrames( stream, &framesAvail, &cbFlags ) ); |
| 2619 |
|
| 2620 |
|
| 2621 |
|
| 2622 |
|
| 2623 |
while( framesAvail > 0 ) |
| 2624 |
{ |
| 2625 |
PaStreamCallbackTimeInfo timeInfo = {0, 0, 0}; |
| 2626 |
|
| 2627 |
pthread_testcancel(); |
| 2628 |
|
| 2629 |
framesGot = framesAvail; |
| 2630 |
if( stream->bufferProcessor.hostBufferSizeMode == paUtilFixedHostBufferSize ) |
| 2631 |
{ |
| 2632 |
|
| 2633 |
framesGot = framesGot >= stream->maxFramesPerHostBuffer ? stream->maxFramesPerHostBuffer : 0; |
| 2634 |
} |
| 2635 |
else |
| 2636 |
{ |
| 2637 |
|
| 2638 |
assert( stream->bufferProcessor.hostBufferSizeMode == paUtilBoundedHostBufferSize ); |
| 2639 |
framesGot = PA_MIN( framesGot, stream->maxFramesPerHostBuffer ); |
| 2640 |
} |
| 2641 |
|
| 2642 |
|
| 2643 |
PaAsiHpi_CalculateTimeInfo( stream, &timeInfo ); |
| 2644 |
PaUtil_BeginBufferProcessing( &stream->bufferProcessor, &timeInfo, cbFlags ); |
| 2645 |
|
| 2646 |
PaUtil_BeginCpuLoadMeasurement( &stream->cpuLoadMeasurer ); |
| 2647 |
if( framesGot > 0 ) |
| 2648 |
{ |
| 2649 |
|
| 2650 |
PA_ENSURE_( PaAsiHpi_BeginProcessing( stream, &framesGot, &cbFlags ) ); |
| 2651 |
|
| 2652 |
if( stream->input && stream->output && (cbFlags & paInputOverflow) ) |
| 2653 |
{ |
| 2654 |
|
| 2655 |
if( stream->neverDropInput ) |
| 2656 |
{ |
| 2657 |
PaUtil_SetNoOutput( &stream->bufferProcessor ); |
| 2658 |
cbFlags |= paOutputOverflow; |
| 2659 |
} |
| 2660 |
} |
| 2661 |
|
| 2662 |
PaUtil_EndBufferProcessing( &stream->bufferProcessor, &callbackResult ); |
| 2663 |
|
| 2664 |
|
| 2665 |
cbFlags = 0; |
| 2666 |
|
| 2667 |
PA_ENSURE_( PaAsiHpi_EndProcessing( stream, framesGot, &cbFlags ) ); |
| 2668 |
|
| 2669 |
framesAvail -= framesGot; |
| 2670 |
} |
| 2671 |
PaUtil_EndCpuLoadMeasurement( &stream->cpuLoadMeasurer, framesGot ); |
| 2672 |
|
| 2673 |
if( framesGot == 0 ) |
| 2674 |
{ |
| 2675 |
|
| 2676 |
break; |
| 2677 |
|
| 2678 |
} |
| 2679 |
if( callbackResult != paContinue ) |
| 2680 |
break; |
| 2681 |
} |
| 2682 |
} |
| 2683 |
|
| 2684 |
|
| 2685 |
|
| 2686 |
|
| 2687 |
|
| 2688 |
pthread_cleanup_pop( 1 ); |
| 2689 |
|
| 2690 |
end: |
| 2691 |
|
| 2692 |
stream->callbackFinished = 1; |
| 2693 |
PA_DEBUG(( "%s: Thread %d exiting (callbackResult = %d)\n ", |
| 2694 |
__FUNCTION__, pthread_self(), callbackResult )); |
| 2695 |
|
| 2696 |
PaUnixThreading_EXIT( result ); |
| 2697 |
error: |
| 2698 |
goto end; |
| 2699 |
} |
| 2700 |
|
| 2701 |
|
| 2702 |
|
| 2703 |
|
| 2704 |
|
| 2705 |
|
| 2706 |
|
| 2707 |
|
| 2708 |
|
| 2709 |
|
| 2710 |
|
| 2711 |
|
| 2712 |
|
| 2713 |
|
| 2714 |
|
| 2715 |
|
| 2716 |
|
| 2717 |
|
| 2718 |
|
| 2719 |
static PaError ReadStream( PaStream *s, |
| 2720 |
void *buffer, |
| 2721 |
unsigned long frames ) |
| 2722 |
{ |
| 2723 |
PaError result = paNoError; |
| 2724 |
PaAsiHpiStream *stream = (PaAsiHpiStream*)s; |
| 2725 |
PaAsiHpiStreamInfo info; |
| 2726 |
void *userBuffer; |
| 2727 |
|
| 2728 |
assert( stream ); |
| 2729 |
PA_UNLESS_( stream->input, paCanNotReadFromAnOutputOnlyStream ); |
| 2730 |
|
| 2731 |
|
| 2732 |
PA_ENSURE_( PaAsiHpi_GetStreamInfo( stream->input, &info ) ); |
| 2733 |
if( info.overflow ) |
| 2734 |
{ |
| 2735 |
result = paInputOverflowed; |
| 2736 |
} |
| 2737 |
|
| 2738 |
|
| 2739 |
if( stream->bufferProcessor.userInputIsInterleaved ) |
| 2740 |
{ |
| 2741 |
userBuffer = buffer; |
| 2742 |
} |
| 2743 |
else |
| 2744 |
{ |
| 2745 |
|
| 2746 |
userBuffer = stream->blockingUserBufferCopy; |
| 2747 |
memcpy( userBuffer, buffer, sizeof (void *) * stream->input->hpiFormat.wChannels ); |
| 2748 |
} |
| 2749 |
|
| 2750 |
while( frames > 0 ) |
| 2751 |
{ |
| 2752 |
unsigned long framesGot, framesAvail; |
| 2753 |
PaStreamCallbackFlags cbFlags = 0; |
| 2754 |
|
| 2755 |
PA_ENSURE_( PaAsiHpi_WaitForFrames( stream, &framesAvail, &cbFlags ) ); |
| 2756 |
framesGot = PA_MIN( framesAvail, frames ); |
| 2757 |
PA_ENSURE_( PaAsiHpi_BeginProcessing( stream, &framesGot, &cbFlags ) ); |
| 2758 |
|
| 2759 |
if( framesGot > 0 ) |
| 2760 |
{ |
| 2761 |
framesGot = PaUtil_CopyInput( &stream->bufferProcessor, &userBuffer, framesGot ); |
| 2762 |
PA_ENSURE_( PaAsiHpi_EndProcessing( stream, framesGot, &cbFlags ) ); |
| 2763 |
|
| 2764 |
frames -= framesGot; |
| 2765 |
} |
| 2766 |
} |
| 2767 |
|
| 2768 |
error: |
| 2769 |
return result; |
| 2770 |
} |
| 2771 |
|
| 2772 |
|
| 2773 |
|
| 2774 |
|
| 2775 |
|
| 2776 |
|
| 2777 |
|
| 2778 |
|
| 2779 |
|
| 2780 |
|
| 2781 |
|
| 2782 |
|
| 2783 |
|
| 2784 |
|
| 2785 |
|
| 2786 |
static PaError WriteStream( PaStream *s, |
| 2787 |
const void *buffer, |
| 2788 |
unsigned long frames ) |
| 2789 |
{ |
| 2790 |
PaError result = paNoError; |
| 2791 |
PaAsiHpiStream *stream = (PaAsiHpiStream*)s; |
| 2792 |
PaAsiHpiStreamInfo info; |
| 2793 |
const void *userBuffer; |
| 2794 |
|
| 2795 |
assert( stream ); |
| 2796 |
PA_UNLESS_( stream->output, paCanNotWriteToAnInputOnlyStream ); |
| 2797 |
|
| 2798 |
|
| 2799 |
PA_ENSURE_( PaAsiHpi_GetStreamInfo( stream->output, &info ) ); |
| 2800 |
if( info.underflow ) |
| 2801 |
{ |
| 2802 |
result = paOutputUnderflowed; |
| 2803 |
} |
| 2804 |
|
| 2805 |
|
| 2806 |
if( stream->bufferProcessor.userOutputIsInterleaved ) |
| 2807 |
{ |
| 2808 |
userBuffer = buffer; |
| 2809 |
} |
| 2810 |
else |
| 2811 |
{ |
| 2812 |
|
| 2813 |
userBuffer = stream->blockingUserBufferCopy; |
| 2814 |
memcpy( (void *)userBuffer, buffer, sizeof (void *) * stream->output->hpiFormat.wChannels ); |
| 2815 |
} |
| 2816 |
|
| 2817 |
while( frames > 0 ) |
| 2818 |
{ |
| 2819 |
unsigned long framesGot, framesAvail; |
| 2820 |
PaStreamCallbackFlags cbFlags = 0; |
| 2821 |
|
| 2822 |
PA_ENSURE_( PaAsiHpi_WaitForFrames( stream, &framesAvail, &cbFlags ) ); |
| 2823 |
framesGot = PA_MIN( framesAvail, frames ); |
| 2824 |
PA_ENSURE_( PaAsiHpi_BeginProcessing( stream, &framesGot, &cbFlags ) ); |
| 2825 |
|
| 2826 |
if( framesGot > 0 ) |
| 2827 |
{ |
| 2828 |
framesGot = PaUtil_CopyOutput( &stream->bufferProcessor, &userBuffer, framesGot ); |
| 2829 |
PA_ENSURE_( PaAsiHpi_EndProcessing( stream, framesGot, &cbFlags ) ); |
| 2830 |
|
| 2831 |
frames -= framesGot; |
| 2832 |
} |
| 2833 |
} |
| 2834 |
|
| 2835 |
error: |
| 2836 |
return result; |
| 2837 |
} |
| 2838 |
|
| 2839 |
|
| 2840 |
|
| 2841 |
|
| 2842 |
|
| 2843 |
|
| 2844 |
|
| 2845 |
|
| 2846 |
static signed long GetStreamReadAvailable( PaStream *s ) |
| 2847 |
{ |
| 2848 |
PaError result = paNoError; |
| 2849 |
PaAsiHpiStream *stream = (PaAsiHpiStream*)s; |
| 2850 |
PaAsiHpiStreamInfo info; |
| 2851 |
|
| 2852 |
assert( stream ); |
| 2853 |
PA_UNLESS_( stream->input, paCanNotReadFromAnOutputOnlyStream ); |
| 2854 |
|
| 2855 |
PA_ENSURE_( PaAsiHpi_GetStreamInfo( stream->input, &info ) ); |
| 2856 |
|
| 2857 |
result = (info.availableFrames / stream->maxFramesPerHostBuffer) * stream->maxFramesPerHostBuffer; |
| 2858 |
if( info.overflow ) |
| 2859 |
{ |
| 2860 |
result = paInputOverflowed; |
| 2861 |
} |
| 2862 |
|
| 2863 |
error: |
| 2864 |
return result; |
| 2865 |
} |
| 2866 |
|
| 2867 |
|
| 2868 |
|
| 2869 |
|
| 2870 |
|
| 2871 |
|
| 2872 |
|
| 2873 |
|
| 2874 |
static signed long GetStreamWriteAvailable( PaStream *s ) |
| 2875 |
{ |
| 2876 |
PaError result = paNoError; |
| 2877 |
PaAsiHpiStream *stream = (PaAsiHpiStream*)s; |
| 2878 |
PaAsiHpiStreamInfo info; |
| 2879 |
|
| 2880 |
assert( stream ); |
| 2881 |
PA_UNLESS_( stream->output, paCanNotWriteToAnInputOnlyStream ); |
| 2882 |
|
| 2883 |
PA_ENSURE_( PaAsiHpi_GetStreamInfo( stream->output, &info ) ); |
| 2884 |
|
| 2885 |
result = (info.availableFrames / stream->maxFramesPerHostBuffer) * stream->maxFramesPerHostBuffer; |
| 2886 |
if( info.underflow ) |
| 2887 |
{ |
| 2888 |
result = paOutputUnderflowed; |
| 2889 |
} |
| 2890 |
|
| 2891 |
error: |
| 2892 |
return result; |
| 2893 |
} |