open the android audio device for input and/or output
| 480 | |
| 481 | // open the android audio device for input and/or output |
| 482 | opensl_stream2 * cOpenslesSource::android_OpenAudioDevice(int sr, |
| 483 | int inchannels, int outchannels, int bufferSize, |
| 484 | bool openPlay, bool openRecord) { |
| 485 | opensl_stream2 *p; |
| 486 | p = (opensl_stream2 *) calloc(sizeof(opensl_stream2), 1); |
| 487 | p->hasNewInputData_ = 0; |
| 488 | p->hasNewInternalData_ = 0; |
| 489 | if (bufferSize <= 0) { |
| 490 | SMILE_IERR(1, "bufferSize must be > 0 in android_OpenAudioDevice()"); |
| 491 | android_CloseAudioDevice(p); |
| 492 | return NULL; |
| 493 | } |
| 494 | smileCondCreate(p->recorderCondition_); |
| 495 | smileCondCreate(p->playerCondition_); |
| 496 | smileMutexCreate(p->newDataMutex_); |
| 497 | p->sr = sr; // sampling rate assignment |
| 498 | if (openRecord) { |
| 499 | p->inchannels = inchannels; |
| 500 | p->inBufSamples = bufferSize * inchannels; |
| 501 | p->inBufMSeconds = (int)floor(audioBuffersize_sec_ * 1000.0); // TODO: compute from bufferSize and sample period! |
| 502 | SMILE_IMSG(1, "audioBuffersize_sec_ %i",p->inBufMSeconds); |
| 503 | if (p->inBufSamples != 0) { |
| 504 | for (int i = 0; i < NUM_RECORDING_BUFFERS; ++i) { |
| 505 | p->deviceInputBuffers[i] = (int16_t *)calloc(p->inBufSamples, sizeof(int16_t)); |
| 506 | if (p->deviceInputBuffers[i] == NULL) { |
| 507 | SMILE_IERR(1, "Failed to allocate device audio recording buffer memory # %i.", i); |
| 508 | android_CloseAudioDevice(p); |
| 509 | return NULL; |
| 510 | } |
| 511 | } |
| 512 | for (int i = 0; i < NUM_RECORDING_DBL_BUFFERS; ++i) { |
| 513 | p->internalInputBuffers[i] = (int16_t *)calloc(p->inBufSamples, sizeof(int16_t)); |
| 514 | if (p->internalInputBuffers[i] == NULL) { |
| 515 | SMILE_IERR(1, "Failed to allocate internal audio recording buffer memory # %i.", i); |
| 516 | android_CloseAudioDevice(p); |
| 517 | return NULL; |
| 518 | } |
| 519 | } |
| 520 | } |
| 521 | } |
| 522 | if (openPlay) { |
| 523 | p->outchannels = outchannels; |
| 524 | p->outBufSamples = bufferSize * outchannels; |
| 525 | if (p->outBufSamples != 0) { |
| 526 | for (int i = 0; i < NUM_PLAYBACK_BUFFERS; ++i) { |
| 527 | p->deviceOutputBuffers[i] = (int16_t *)calloc(p->outBufSamples, sizeof(int16_t)); |
| 528 | if (p->deviceOutputBuffers[i] == NULL) { |
| 529 | SMILE_IERR(1, "Failed to allocate audio playback buffer memory # %i.", i); |
| 530 | android_CloseAudioDevice(p); |
| 531 | return NULL; |
| 532 | } |
| 533 | } |
| 534 | } |
| 535 | p->currentDeviceOutputBuffer = 0; |
| 536 | } |
| 537 | |
| 538 | if (openSLCreateEngine(p) != SL_RESULT_SUCCESS) { |
| 539 | SMILE_IERR(2, "Failed to create openSL engine."); |
nothing calls this directly
no test coverage detected