/**
 * Creates an audio stream for playback. The audio device used will depend on playbackDeviceId_.
 */
void PlayAudioEngine::createPlaybackStream(){

  AAudioStreamBuilder* builder = createStreamBuilder();

  if (builder != nullptr){

    setupPlaybackStreamParameters(builder);

    aaudio_result_t result = AAudioStreamBuilder_openStream(builder, &playStream_);

    if (result == AAUDIO_OK && playStream_ != nullptr){

      // check that we got PCM_FLOAT format
      if (sampleFormat_ != AAudioStream_getFormat(playStream_)) {
        LOGW("Sample format is not PCM_FLOAT");
      }

      sampleRate_ = AAudioStream_getSampleRate(playStream_);
      framesPerBurst_ = AAudioStream_getFramesPerBurst(playStream_);

      // Set the buffer size to the burst size - this will give us the minimum possible latency
      AAudioStream_setBufferSizeInFrames(playStream_, framesPerBurst_);
      bufSizeInFrames_ = framesPerBurst_;

      PrintAudioStreamInfo(playStream_);
      prepareOscillators();

      // Start the stream - the dataCallback function will start being called
      result = AAudioStream_requestStart(playStream_);
      if (result != AAUDIO_OK) {
        LOGE("Error starting stream. %s", AAudio_convertResultToText(result));
      }

      // Store the underrun count so we can tune the latency in the dataCallback
      playStreamUnderrunCount_ = AAudioStream_getXRunCount(playStream_);

    } else {
      LOGE("Failed to create stream. Error: %s", AAudio_convertResultToText(result));
    }

  AAudioStreamBuilder_delete(builder);

  } else {
    LOGE("Unable to obtain an AAudioStreamBuilder object");
  }
}
Ejemplo n.º 2
0
static void testOpenOptions(aaudio_direction_t direction,
                            int32_t channelCount,
                            int32_t sampleRate,
                            aaudio_format_t format) {

    aaudio_result_t result = AAUDIO_OK;

    int32_t bufferCapacity;
    int32_t framesPerBurst = 0;

    int32_t actualChannelCount = 0;
    int32_t actualSampleRate = 0;
    aaudio_format_t actualDataFormat = AAUDIO_FORMAT_UNSPECIFIED;
    aaudio_sharing_mode_t actualSharingMode = AAUDIO_SHARING_MODE_SHARED;
    aaudio_direction_t actualDirection;

    AAudioStreamBuilder *aaudioBuilder = nullptr;
    AAudioStream *aaudioStream = nullptr;

    printf("TestOpen: dir = %d, chans = %3d, rate = %6d format = %d\n",
           direction, channelCount, sampleRate, format);

    // Use an AAudioStreamBuilder to contain requested parameters.
    ASSERT_EQ(AAUDIO_OK, AAudio_createStreamBuilder(&aaudioBuilder));

    // Request stream properties.
    AAudioStreamBuilder_setDirection(aaudioBuilder, direction);
    AAudioStreamBuilder_setSampleRate(aaudioBuilder, sampleRate);
    AAudioStreamBuilder_setChannelCount(aaudioBuilder, channelCount);
    AAudioStreamBuilder_setFormat(aaudioBuilder, format);
    AAudioStreamBuilder_setDataCallback(aaudioBuilder, MyDataCallbackProc, nullptr);

    //AAudioStreamBuilder_setPerformanceMode(aaudioBuilder, AAUDIO_PERFORMANCE_MODE_NONE);
    AAudioStreamBuilder_setPerformanceMode(aaudioBuilder, AAUDIO_PERFORMANCE_MODE_LOW_LATENCY);
    //AAudioStreamBuilder_setPerformanceMode(aaudioBuilder, AAUDIO_PERFORMANCE_MODE_POWER_SAVING);

    // Create an AAudioStream using the Builder.
    result = AAudioStreamBuilder_openStream(aaudioBuilder, &aaudioStream);
    if (result != AAUDIO_OK) {
        printf("Stream not opened! That may be OK.\n");
        goto finish;
    }

    // Check to see what kind of stream we actually got.
    actualSampleRate = AAudioStream_getSampleRate(aaudioStream);
    actualChannelCount = AAudioStream_getChannelCount(aaudioStream);
    actualDataFormat = AAudioStream_getFormat(aaudioStream);
    actualDirection = AAudioStream_getDirection(aaudioStream);

    printf("          dir = %d, chans = %3d, rate = %6d format = %d\n",
           direction, actualChannelCount, actualSampleRate, actualDataFormat);

    // If we ask for something specific then we should get that.
    if (channelCount != AAUDIO_UNSPECIFIED) {
        EXPECT_EQ(channelCount, actualChannelCount);
    }
    if (sampleRate != AAUDIO_UNSPECIFIED) {
        EXPECT_EQ(sampleRate, actualSampleRate);
    }
    if (format != AAUDIO_FORMAT_UNSPECIFIED) {
        EXPECT_EQ(format, actualDataFormat);
    }
    EXPECT_EQ(direction, actualDirection);

    // This is the number of frames that are read in one chunk by a DMA controller
    // or a DSP or a mixer.
    framesPerBurst = AAudioStream_getFramesPerBurst(aaudioStream);
    bufferCapacity = AAudioStream_getBufferCapacityInFrames(aaudioStream);
    printf("          bufferCapacity = %d, remainder = %d\n",
           bufferCapacity, bufferCapacity % framesPerBurst);

finish:
    AAudioStream_close(aaudioStream);
    AAudioStreamBuilder_delete(aaudioBuilder);
    printf("          result = %d = %s\n", result, AAudio_convertResultToText(result));
}