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

Commit 20523edb authored by Phil Burk's avatar Phil Burk
Browse files

aaudio: change samplesPerFrame() to channelCount()



Both functions provided for now.
Will remove deprecated function in later CL.

Bug: 37646784
Test: write_sine.cpp and write_sine_callback.cpp
Change-Id: I1241aafa206112c526d6ca250ba5209489e1a46e
Signed-off-by: default avatarPhil Burk <philburk@google.com>
parent 79a89ef4
Loading
Loading
Loading
Loading
+15 −15
Original line number Original line Diff line number Diff line
@@ -63,8 +63,8 @@ int main(int argc, char **argv)


    aaudio_result_t result = AAUDIO_OK;
    aaudio_result_t result = AAUDIO_OK;


    const int requestedSamplesPerFrame = 2;
    const int requestedChannelCount = 2;
    int actualSamplesPerFrame = 0;
    int actualChannelCount = 0;
    const int requestedSampleRate = SAMPLE_RATE;
    const int requestedSampleRate = SAMPLE_RATE;
    int actualSampleRate = 0;
    int actualSampleRate = 0;
    aaudio_audio_format_t actualDataFormat = AAUDIO_FORMAT_UNSPECIFIED;
    aaudio_audio_format_t actualDataFormat = AAUDIO_FORMAT_UNSPECIFIED;
@@ -90,7 +90,7 @@ int main(int argc, char **argv)
    // in a buffer if we hang or crash.
    // in a buffer if we hang or crash.
    setvbuf(stdout, nullptr, _IONBF, (size_t) 0);
    setvbuf(stdout, nullptr, _IONBF, (size_t) 0);


    printf("%s - Play a sine wave using AAudio, Z2\n", argv[0]);
    printf("%s - Play a sine wave using AAudio\n", argv[0]);


    // Use an AAudioStreamBuilder to contain requested parameters.
    // Use an AAudioStreamBuilder to contain requested parameters.
    result = AAudio_createStreamBuilder(&aaudioBuilder);
    result = AAudio_createStreamBuilder(&aaudioBuilder);
@@ -100,7 +100,7 @@ int main(int argc, char **argv)


    // Request stream properties.
    // Request stream properties.
    AAudioStreamBuilder_setSampleRate(aaudioBuilder, requestedSampleRate);
    AAudioStreamBuilder_setSampleRate(aaudioBuilder, requestedSampleRate);
    AAudioStreamBuilder_setSamplesPerFrame(aaudioBuilder, requestedSamplesPerFrame);
    AAudioStreamBuilder_setChannelCount(aaudioBuilder, requestedChannelCount);
    AAudioStreamBuilder_setFormat(aaudioBuilder, REQUESTED_FORMAT);
    AAudioStreamBuilder_setFormat(aaudioBuilder, REQUESTED_FORMAT);
    AAudioStreamBuilder_setSharingMode(aaudioBuilder, REQUESTED_SHARING_MODE);
    AAudioStreamBuilder_setSharingMode(aaudioBuilder, REQUESTED_SHARING_MODE);


@@ -120,9 +120,9 @@ int main(int argc, char **argv)
    sineOsc1.setup(440.0, actualSampleRate);
    sineOsc1.setup(440.0, actualSampleRate);
    sineOsc2.setup(660.0, actualSampleRate);
    sineOsc2.setup(660.0, actualSampleRate);


    actualSamplesPerFrame = AAudioStream_getSamplesPerFrame(aaudioStream);
    actualChannelCount = AAudioStream_getChannelCount(aaudioStream);
    printf("SamplesPerFrame: requested = %d, actual = %d\n",
    printf("ChannelCount: requested = %d, actual = %d\n",
            requestedSamplesPerFrame, actualSamplesPerFrame);
            requestedChannelCount, actualChannelCount);


    actualSharingMode = AAudioStream_getSharingMode(aaudioStream);
    actualSharingMode = AAudioStream_getSharingMode(aaudioStream);
    printf("SharingMode: requested = %s, actual = %s\n",
    printf("SharingMode: requested = %s, actual = %s\n",
@@ -152,9 +152,9 @@ int main(int argc, char **argv)


    // Allocate a buffer for the audio data.
    // Allocate a buffer for the audio data.
    if (actualDataFormat == AAUDIO_FORMAT_PCM_FLOAT) {
    if (actualDataFormat == AAUDIO_FORMAT_PCM_FLOAT) {
        floatData = new float[framesPerWrite * actualSamplesPerFrame];
        floatData = new float[framesPerWrite * actualChannelCount];
    } else if (actualDataFormat == AAUDIO_FORMAT_PCM_I16) {
    } else if (actualDataFormat == AAUDIO_FORMAT_PCM_I16) {
        shortData = new int16_t[framesPerWrite * actualSamplesPerFrame];
        shortData = new int16_t[framesPerWrite * actualChannelCount];
    } else {
    } else {
        printf("ERROR Unsupported data format!\n");
        printf("ERROR Unsupported data format!\n");
        goto finish;
        goto finish;
@@ -178,15 +178,15 @@ int main(int argc, char **argv)


        if (actualDataFormat == AAUDIO_FORMAT_PCM_FLOAT) {
        if (actualDataFormat == AAUDIO_FORMAT_PCM_FLOAT) {
            // Render sine waves to left and right channels.
            // Render sine waves to left and right channels.
            sineOsc1.render(&floatData[0], actualSamplesPerFrame, framesPerWrite);
            sineOsc1.render(&floatData[0], actualChannelCount, framesPerWrite);
            if (actualSamplesPerFrame > 1) {
            if (actualChannelCount > 1) {
                sineOsc2.render(&floatData[1], actualSamplesPerFrame, framesPerWrite);
                sineOsc2.render(&floatData[1], actualChannelCount, framesPerWrite);
            }
            }
        } else if (actualDataFormat == AAUDIO_FORMAT_PCM_I16) {
        } else if (actualDataFormat == AAUDIO_FORMAT_PCM_I16) {
            // Render sine waves to left and right channels.
            // Render sine waves to left and right channels.
            sineOsc1.render(&shortData[0], actualSamplesPerFrame, framesPerWrite);
            sineOsc1.render(&shortData[0], actualChannelCount, framesPerWrite);
            if (actualSamplesPerFrame > 1) {
            if (actualChannelCount > 1) {
                sineOsc2.render(&shortData[1], actualSamplesPerFrame, framesPerWrite);
                sineOsc2.render(&shortData[1], actualChannelCount, framesPerWrite);
            }
            }
        }
        }


+5 −5
Original line number Original line Diff line number Diff line
@@ -65,11 +65,11 @@ public:
    /**
    /**
     * Only call this after open() has been called.
     * Only call this after open() has been called.
     */
     */
    int32_t getSamplesPerFrame() {
    int32_t getChannelCount() {
        if (mStream == nullptr) {
        if (mStream == nullptr) {
            return AAUDIO_ERROR_INVALID_STATE;
            return AAUDIO_ERROR_INVALID_STATE;
        }
        }
        return AAudioStream_getSamplesPerFrame(mStream);;
        return AAudioStream_getChannelCount(mStream);;
    }
    }


    /**
    /**
@@ -119,7 +119,7 @@ public:


    // Write zero data to fill up the buffer and prevent underruns.
    // Write zero data to fill up the buffer and prevent underruns.
    aaudio_result_t prime() {
    aaudio_result_t prime() {
        int32_t samplesPerFrame = AAudioStream_getSamplesPerFrame(mStream);
        int32_t samplesPerFrame = AAudioStream_getChannelCount(mStream);
        const int numFrames = 32;
        const int numFrames = 32;
        float zeros[numFrames * samplesPerFrame];
        float zeros[numFrames * samplesPerFrame];
        memset(zeros, 0, sizeof(zeros));
        memset(zeros, 0, sizeof(zeros));
@@ -186,7 +186,7 @@ aaudio_data_callback_result_t MyDataCallbackProc(
        sineData->schedulerChecked = true;
        sineData->schedulerChecked = true;
    }
    }


    int32_t samplesPerFrame = AAudioStream_getSamplesPerFrame(stream);
    int32_t samplesPerFrame = AAudioStream_getChannelCount(stream);
    // This code only plays on the first one or two channels.
    // This code only plays on the first one or two channels.
    // TODO Support arbitrary number of channels.
    // TODO Support arbitrary number of channels.
    switch (AAudioStream_getFormat(stream)) {
    switch (AAudioStream_getFormat(stream)) {
@@ -239,7 +239,7 @@ int main(int argc, char **argv)
        goto error;
        goto error;
    }
    }
    printf("player.getFramesPerSecond() = %d\n", player.getFramesPerSecond());
    printf("player.getFramesPerSecond() = %d\n", player.getFramesPerSecond());
    printf("player.getSamplesPerFrame() = %d\n", player.getSamplesPerFrame());
    printf("player.getChannelCount() = %d\n", player.getChannelCount());
    myData.sineOsc1.setup(440.0, 48000);
    myData.sineOsc1.setup(440.0, 48000);
    myData.sineOsc1.setSweep(300.0, 600.0, 5.0);
    myData.sineOsc1.setSweep(300.0, 600.0, 5.0);
    myData.sineOsc2.setup(660.0, 48000);
    myData.sineOsc2.setup(660.0, 48000);
+22 −4
Original line number Original line Diff line number Diff line
@@ -206,18 +206,26 @@ AAUDIO_API void AAudioStreamBuilder_setSampleRate(AAudioStreamBuilder* builder,
                                                       int32_t sampleRate);
                                                       int32_t sampleRate);


/**
/**
 * Request a number of samples per frame.
 * Request a number of channels for the stream.
 *
 *
 * The stream may be opened with a different value.
 * The stream may be opened with a different value.
 * So the application should query for the actual value after the stream is opened.
 * So the application should query for the actual value after the stream is opened.
 *
 *
 * The default, if you do not call this function, is AAUDIO_UNSPECIFIED.
 * The default, if you do not call this function, is AAUDIO_UNSPECIFIED.
 *
 *
 * Note, this quantity is sometimes referred to as "channel count".
 * Note, this quantity is sometimes referred to as "samples per frame".
 *
 *
 * @param builder reference provided by AAudio_createStreamBuilder()
 * @param builder reference provided by AAudio_createStreamBuilder()
 * @param samplesPerFrame Number of samples in one frame, ie. numChannels.
 * @param channelCount Number of channels desired.
 */
 */
AAUDIO_API void AAudioStreamBuilder_setChannelCount(AAudioStreamBuilder* builder,
                                                   int32_t channelCount);

/**
 *
 * @deprecated use AAudioStreamBuilder_setChannelCount()
 */
// TODO remove as soon as the NDK and OS are in sync, before RC1
AAUDIO_API void AAudioStreamBuilder_setSamplesPerFrame(AAudioStreamBuilder* builder,
AAUDIO_API void AAudioStreamBuilder_setSamplesPerFrame(AAudioStreamBuilder* builder,
                                                       int32_t samplesPerFrame);
                                                       int32_t samplesPerFrame);


@@ -676,9 +684,19 @@ AAUDIO_API int32_t AAudioStream_getXRunCount(AAudioStream* stream);
 */
 */
AAUDIO_API int32_t AAudioStream_getSampleRate(AAudioStream* stream);
AAUDIO_API int32_t AAudioStream_getSampleRate(AAudioStream* stream);


/**
 * A stream has one or more channels of data.
 * A frame will contain one sample for each channel.
 *
 * @param stream reference provided by AAudioStreamBuilder_openStream()
 * @return actual number of channels
 */
AAUDIO_API int32_t AAudioStream_getChannelCount(AAudioStream* stream);

/**
/**
 * The samplesPerFrame is also known as channelCount.
 * The samplesPerFrame is also known as channelCount.
 *
 *
 * @deprecated use AAudioStream_getChannelCount()
 * @param stream reference provided by AAudioStreamBuilder_openStream()
 * @param stream reference provided by AAudioStreamBuilder_openStream()
 * @return actual samples per frame
 * @return actual samples per frame
 */
 */
+2 −0
Original line number Original line Diff line number Diff line
@@ -9,6 +9,7 @@ LIBAAUDIO {
    AAudioStreamBuilder_setFramesPerDataCallback;
    AAudioStreamBuilder_setFramesPerDataCallback;
    AAudioStreamBuilder_setSampleRate;
    AAudioStreamBuilder_setSampleRate;
    AAudioStreamBuilder_setSamplesPerFrame;
    AAudioStreamBuilder_setSamplesPerFrame;
    AAudioStreamBuilder_setChannelCount;
    AAudioStreamBuilder_setFormat;
    AAudioStreamBuilder_setFormat;
    AAudioStreamBuilder_setSharingMode;
    AAudioStreamBuilder_setSharingMode;
    AAudioStreamBuilder_setDirection;
    AAudioStreamBuilder_setDirection;
@@ -32,6 +33,7 @@ LIBAAUDIO {
    AAudioStream_getXRunCount;
    AAudioStream_getXRunCount;
    AAudioStream_getSampleRate;
    AAudioStream_getSampleRate;
    AAudioStream_getSamplesPerFrame;
    AAudioStream_getSamplesPerFrame;
    AAudioStream_getChannelCount;
    AAudioStream_getDeviceId;
    AAudioStream_getDeviceId;
    AAudioStream_getFormat;
    AAudioStream_getFormat;
    AAudioStream_getSharingMode;
    AAudioStream_getSharingMode;
+14 −1
Original line number Original line Diff line number Diff line
@@ -128,6 +128,13 @@ AAUDIO_API void AAudioStreamBuilder_setSampleRate(AAudioStreamBuilder* builder,
    streamBuilder->setSampleRate(sampleRate);
    streamBuilder->setSampleRate(sampleRate);
}
}


AAUDIO_API void AAudioStreamBuilder_setChannelCount(AAudioStreamBuilder* builder,
                                                       int32_t channelCount)
{
    AudioStreamBuilder *streamBuilder = convertAAudioBuilderToStreamBuilder(builder);
    streamBuilder->setSamplesPerFrame(channelCount);
}

AAUDIO_API void AAudioStreamBuilder_setSamplesPerFrame(AAudioStreamBuilder* builder,
AAUDIO_API void AAudioStreamBuilder_setSamplesPerFrame(AAudioStreamBuilder* builder,
                                                       int32_t samplesPerFrame)
                                                       int32_t samplesPerFrame)
{
{
@@ -334,6 +341,12 @@ AAUDIO_API int32_t AAudioStream_getSampleRate(AAudioStream* stream)
    return audioStream->getSampleRate();
    return audioStream->getSampleRate();
}
}


AAUDIO_API int32_t AAudioStream_getChannelCount(AAudioStream* stream)
{
    AudioStream *audioStream = convertAAudioStreamToAudioStream(stream);
    return audioStream->getSamplesPerFrame();
}

AAUDIO_API int32_t AAudioStream_getSamplesPerFrame(AAudioStream* stream)
AAUDIO_API int32_t AAudioStream_getSamplesPerFrame(AAudioStream* stream)
{
{
    AudioStream *audioStream = convertAAudioStreamToAudioStream(stream);
    AudioStream *audioStream = convertAAudioStreamToAudioStream(stream);