Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit e2fbb59e authored by Phil Burk's avatar Phil Burk
Browse files

aaudio: add setPerformanceMode()



The performance mode affects the latency and the implementation of the
data path.

MMAP is still disabled for now.

Bug: 37867485
Test: write_sine.cpp
Change-Id: I9bf5d5d13d1047d5ace69bd5ebdce7b6d65c14e7
Signed-off-by: default avatarPhil Burk <philburk@google.com>
parent 441f2ccb
Loading
Loading
Loading
Loading
+9 −3
Original line number Diff line number Diff line
@@ -23,7 +23,7 @@
#include "SineGenerator.h"

#define SAMPLE_RATE           48000
#define NUM_SECONDS           15
#define NUM_SECONDS           5
#define NANOS_PER_MICROSECOND ((int64_t)1000)
#define NANOS_PER_MILLISECOND (NANOS_PER_MICROSECOND * 1000)
#define NANOS_PER_SECOND      (NANOS_PER_MILLISECOND * 1000)
@@ -104,6 +104,10 @@ int main(int argc, char **argv)
    AAudioStreamBuilder_setFormat(aaudioBuilder, REQUESTED_FORMAT);
    AAudioStreamBuilder_setSharingMode(aaudioBuilder, REQUESTED_SHARING_MODE);

    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) {
@@ -132,7 +136,6 @@ int main(int argc, char **argv)
    // 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);
    printf("Buffer: framesPerBurst = %d\n",framesPerBurst);
    printf("Buffer: bufferSize = %d\n", AAudioStream_getBufferSizeInFrames(aaudioStream));
    bufferCapacity = AAudioStream_getBufferCapacityInFrames(aaudioStream);
    printf("Buffer: bufferCapacity = %d, remainder = %d\n",
@@ -144,12 +147,15 @@ int main(int argc, char **argv)
    while (framesPerWrite < 48) {
        framesPerWrite *= 2;
    }
    printf("DataFormat: framesPerWrite = %d\n",framesPerWrite);
    printf("Buffer: framesPerBurst = %d\n",framesPerBurst);
    printf("Buffer: framesPerWrite = %d\n",framesPerWrite);

    actualDataFormat = AAudioStream_getFormat(aaudioStream);
    printf("DataFormat: requested = %d, actual = %d\n", REQUESTED_FORMAT, actualDataFormat);
    // TODO handle other data formats

    printf("PerformanceMode: %d\n", AAudioStream_getPerformanceMode(aaudioStream));

    // Allocate a buffer for the audio data.
    if (actualDataFormat == AAUDIO_FORMAT_PCM_FLOAT) {
        floatData = new float[framesPerWrite * actualChannelCount];
+7 −3
Original line number Diff line number Diff line
@@ -26,7 +26,7 @@
#include <aaudio/AAudio.h>
#include "SineGenerator.h"

#define NUM_SECONDS              15
#define NUM_SECONDS              5

//#define SHARING_MODE  AAUDIO_SHARING_MODE_EXCLUSIVE
#define SHARING_MODE  AAUDIO_SHARING_MODE_SHARED
@@ -88,6 +88,10 @@ public:
 //       AAudioStreamBuilder_setFramesPerDataCallback(mBuilder, CALLBACK_SIZE_FRAMES);
        AAudioStreamBuilder_setBufferCapacityInFrames(mBuilder, 48 * 8);

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

        // Open an AAudioStream using the Builder.
        result = AAudioStreamBuilder_openStream(mBuilder, &mStream);
        if (result != AAUDIO_OK) goto finish1;
@@ -98,7 +102,6 @@ public:
               AAudioStream_getBufferSizeInFrames(mStream));
        printf("AAudioStream_getBufferCapacityInFrames() = %d\n",
               AAudioStream_getBufferCapacityInFrames(mStream));
        return result;

     finish1:
        AAudioStreamBuilder_delete(mBuilder);
@@ -227,7 +230,7 @@ int main(int argc, char **argv)
    // Make printf print immediately so that debug info is not stuck
    // in a buffer if we hang or crash.
    setvbuf(stdout, nullptr, _IONBF, (size_t) 0);
    printf("%s - Play a sine sweep using an AAudio callback, Z1\n", argv[0]);
    printf("%s - Play a sine sweep using an AAudio callback\n", argv[0]);

    player.setSharingMode(SHARING_MODE);

@@ -278,6 +281,7 @@ int main(int argc, char **argv)
            printf("Stream state is %d %s!\n", state, AAudio_convertStreamStateToText(state));
            break;
        }
        printf("framesWritten = %d\n", (int) AAudioStream_getFramesWritten(player.getStream()));
    }
    printf("Woke up now.\n");

+38 −0
Original line number Diff line number Diff line
@@ -125,6 +125,25 @@ enum {
};
typedef int32_t aaudio_sharing_mode_t;


enum {
    /**
     * No particular performance needs. Default.
     */
            AAUDIO_PERFORMANCE_MODE_NONE = 10,

    /**
     * Extending battery life is most important.
     */
            AAUDIO_PERFORMANCE_MODE_POWER_SAVING,

    /**
     * Reducing latency is most important.
     */
            AAUDIO_PERFORMANCE_MODE_LOW_LATENCY
};
typedef int32_t aaudio_performance_mode_t;

typedef struct AAudioStreamStruct         AAudioStream;
typedef struct AAudioStreamBuilderStruct  AAudioStreamBuilder;

@@ -279,6 +298,18 @@ AAUDIO_API void AAudioStreamBuilder_setDirection(AAudioStreamBuilder* builder,
 */
AAUDIO_API void AAudioStreamBuilder_setBufferCapacityInFrames(AAudioStreamBuilder* builder,
                                                                 int32_t numFrames);

/**
 * Set the requested performance mode.
 *
 * The default, if you do not call this function, is AAUDIO_PERFORMANCE_MODE_NONE.
 *
 * @param builder reference provided by AAudio_createStreamBuilder()
 * @param mode the desired performance mode, eg. AAUDIO_PERFORMANCE_MODE_LOW_LATENCY
 */
AAUDIO_API void AAudioStreamBuilder_setPerformanceMode(AAudioStreamBuilder* builder,
                                                aaudio_performance_mode_t mode);

/**
 * Return one of these values from the data callback function.
 */
@@ -721,6 +752,13 @@ AAUDIO_API aaudio_audio_format_t AAudioStream_getFormat(AAudioStream* stream);
 */
AAUDIO_API aaudio_sharing_mode_t AAudioStream_getSharingMode(AAudioStream* stream);

/**
 * Get the performance mode used by the stream.
 *
 * @param stream reference provided by AAudioStreamBuilder_openStream()
 */
AAUDIO_API aaudio_performance_mode_t AAudioStream_getPerformanceMode(AAudioStream* stream);

/**
 * @param stream reference provided by AAudioStreamBuilder_openStream()
 * @return direction
+2 −0
Original line number Diff line number Diff line
@@ -3,6 +3,7 @@ LIBAAUDIO {
    AAudio_convertResultToText;
    AAudio_convertStreamStateToText;
    AAudio_createStreamBuilder;
    AAudioStreamBuilder_setPerformanceMode;
    AAudioStreamBuilder_setDeviceId;
    AAudioStreamBuilder_setDataCallback;
    AAudioStreamBuilder_setErrorCallback;
@@ -34,6 +35,7 @@ LIBAAUDIO {
    AAudioStream_getSampleRate;
    AAudioStream_getSamplesPerFrame;
    AAudioStream_getChannelCount;
    AAudioStream_getPerformanceMode;
    AAudioStream_getDeviceId;
    AAudioStream_getFormat;
    AAudioStream_getSharingMode;
+5 −0
Original line number Diff line number Diff line
@@ -90,6 +90,11 @@ public:
    // Called internally from 'C'
    void *callbackLoop();


    bool isMMap() override {
        return true;
    }

protected:

    aaudio_result_t processCommands();
Loading