Loading media/libaaudio/examples/input_monitor/src/input_monitor.cpp +18 −35 Original line number Original line Diff line number Diff line Loading @@ -26,30 +26,18 @@ #include "AAudioExampleUtils.h" #include "AAudioExampleUtils.h" #include "AAudioSimpleRecorder.h" #include "AAudioSimpleRecorder.h" #define SAMPLE_RATE 48000 // TODO support FLOAT #define REQUIRED_FORMAT AAUDIO_FORMAT_PCM_I16 #define NUM_SECONDS 10 #define MIN_FRAMES_TO_READ 48 /* arbitrary, 1 msec at 48000 Hz */ #define MIN_FRAMES_TO_READ 48 /* arbitrary, 1 msec at 48000 Hz */ int main(int argc, char **argv) int main(int argc, const char **argv) { { (void)argc; // unused AAudioArgsParser argParser; aaudio_result_t result; aaudio_result_t result; AAudioSimpleRecorder recorder; AAudioSimpleRecorder recorder; int actualSamplesPerFrame; int actualSamplesPerFrame; int actualSampleRate; int actualSampleRate; const aaudio_format_t requestedDataFormat = AAUDIO_FORMAT_PCM_I16; aaudio_format_t actualDataFormat; aaudio_format_t actualDataFormat; const int requestedInputChannelCount = 2; // Can affect whether we get a FAST path. //aaudio_performance_mode_t requestedPerformanceMode = AAUDIO_PERFORMANCE_MODE_NONE; const aaudio_performance_mode_t requestedPerformanceMode = AAUDIO_PERFORMANCE_MODE_LOW_LATENCY; //aaudio_performance_mode_t requestedPerformanceMode = AAUDIO_PERFORMANCE_MODE_POWER_SAVING; //const aaudio_sharing_mode_t requestedSharingMode = AAUDIO_SHARING_MODE_SHARED; const aaudio_sharing_mode_t requestedSharingMode = AAUDIO_SHARING_MODE_EXCLUSIVE; aaudio_sharing_mode_t actualSharingMode; aaudio_sharing_mode_t actualSharingMode; AAudioStream *aaudioStream = nullptr; AAudioStream *aaudioStream = nullptr; Loading @@ -70,18 +58,18 @@ int main(int argc, char **argv) printf("%s - Monitor input level using AAudio\n", argv[0]); printf("%s - Monitor input level using AAudio\n", argv[0]); AAudio_setMMapPolicy(AAUDIO_POLICY_ALWAYS); argParser.setFormat(REQUIRED_FORMAT); if (argParser.parseArgs(argc, argv)) { recorder.setPerformanceMode(requestedPerformanceMode); return EXIT_FAILURE; recorder.setSharingMode(requestedSharingMode); } result = recorder.open(requestedInputChannelCount, 48000, requestedDataFormat, result = recorder.open(argParser); nullptr, nullptr, nullptr); if (result != AAUDIO_OK) { if (result != AAUDIO_OK) { fprintf(stderr, "ERROR - recorder.open() returned %d\n", result); fprintf(stderr, "ERROR - recorder.open() returned %d\n", result); goto finish; goto finish; } } aaudioStream = recorder.getStream(); aaudioStream = recorder.getStream(); argParser.compareWithStream(aaudioStream); deviceId = AAudioStream_getDeviceId(aaudioStream); deviceId = AAudioStream_getDeviceId(aaudioStream); printf("deviceId = %d\n", deviceId); printf("deviceId = %d\n", deviceId); Loading @@ -91,11 +79,6 @@ int main(int argc, char **argv) actualSampleRate = AAudioStream_getSampleRate(aaudioStream); actualSampleRate = AAudioStream_getSampleRate(aaudioStream); printf("SamplesPerFrame = %d\n", actualSampleRate); printf("SamplesPerFrame = %d\n", actualSampleRate); actualSharingMode = AAudioStream_getSharingMode(aaudioStream); printf("SharingMode: requested = %s, actual = %s\n", getSharingModeText(requestedSharingMode), getSharingModeText(actualSharingMode)); // This is the number of frames that are written in one chunk by a DMA controller // This is the number of frames that are written in one chunk by a DMA controller // or a DSP. // or a DSP. framesPerBurst = AAudioStream_getFramesPerBurst(aaudioStream); framesPerBurst = AAudioStream_getFramesPerBurst(aaudioStream); Loading @@ -110,14 +93,12 @@ int main(int argc, char **argv) printf("DataFormat: framesPerRead = %d\n",framesPerRead); printf("DataFormat: framesPerRead = %d\n",framesPerRead); actualDataFormat = AAudioStream_getFormat(aaudioStream); actualDataFormat = AAudioStream_getFormat(aaudioStream); printf("DataFormat: requested = %d, actual = %d\n", requestedDataFormat, actualDataFormat); printf("DataFormat: requested = %d, actual = %d\n", REQUIRED_FORMAT, actualDataFormat); // TODO handle other data formats // TODO handle other data formats assert(actualDataFormat == AAUDIO_FORMAT_PCM_I16); assert(actualDataFormat == REQUIRED_FORMAT); printf("PerformanceMode: requested = %d, actual = %d\n", requestedPerformanceMode, // Allocate a buffer for the PCM_16 audio data. AAudioStream_getPerformanceMode(aaudioStream)); // Allocate a buffer for the audio data. data = new(std::nothrow) int16_t[framesPerRead * actualSamplesPerFrame]; data = new(std::nothrow) int16_t[framesPerRead * actualSamplesPerFrame]; if (data == nullptr) { if (data == nullptr) { fprintf(stderr, "ERROR - could not allocate data buffer\n"); fprintf(stderr, "ERROR - could not allocate data buffer\n"); Loading @@ -136,7 +117,7 @@ int main(int argc, char **argv) printf("after start, state = %s\n", AAudio_convertStreamStateToText(state)); printf("after start, state = %s\n", AAudio_convertStreamStateToText(state)); // Record for a while. // Record for a while. framesToRecord = actualSampleRate * NUM_SECONDS; framesToRecord = actualSampleRate * argParser.getDurationSeconds(); framesLeft = framesToRecord; framesLeft = framesToRecord; while (framesLeft > 0) { while (framesLeft > 0) { // Read audio data from the stream. // Read audio data from the stream. Loading Loading @@ -176,6 +157,8 @@ int main(int argc, char **argv) goto finish; goto finish; } } argParser.compareWithStream(aaudioStream); finish: finish: recorder.close(); recorder.close(); delete[] data; delete[] data; Loading media/libaaudio/examples/loopback/src/loopback.cpp +0 −1 Original line number Original line Diff line number Diff line Loading @@ -20,7 +20,6 @@ #include <assert.h> #include <assert.h> #include <cctype> #include <cctype> #include <math.h> #include <math.h> #include <stdlib.h> #include <stdio.h> #include <stdio.h> #include <stdlib.h> #include <stdlib.h> #include <unistd.h> #include <unistd.h> Loading media/libaaudio/examples/utils/AAudioArgsParser.h 0 → 100644 +288 −0 Original line number Original line Diff line number Diff line /* * Copyright (C) 2017 The Android Open Source Project * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ #ifndef AAUDIO_EXAMPLE_ARGS_PARSER_H #define AAUDIO_EXAMPLE_ARGS_PARSER_H #include <cctype> #include <unistd.h> #include <stdio.h> #include <stdlib.h> #include <aaudio/AAudio.h> #include <aaudio/AAudioTesting.h> #include <AAudioExampleUtils.h> // TODO use this as a base class within AAudio class AAudioParameters { public: /** * This is also known as samplesPerFrame. */ int32_t getChannelCount() const { return mChannelCount; } void setChannelCount(int32_t channelCount) { mChannelCount = channelCount; } int32_t getSampleRate() const { return mSampleRate; } void setSampleRate(int32_t sampleRate) { mSampleRate = sampleRate; } aaudio_format_t getFormat() const { return mFormat; } void setFormat(aaudio_format_t format) { mFormat = format; } aaudio_sharing_mode_t getSharingMode() const { return mSharingMode; } void setSharingMode(aaudio_sharing_mode_t sharingMode) { mSharingMode = sharingMode; } int32_t getBufferCapacity() const { return mBufferCapacity; } void setBufferCapacity(int32_t frames) { mBufferCapacity = frames; } int32_t getPerformanceMode() const { return mPerformanceMode; } void setPerformanceMode(aaudio_performance_mode_t performanceMode) { mPerformanceMode = performanceMode; } int32_t getDeviceId() const { return mDeviceId; } void setDeviceId(int32_t deviceId) { mDeviceId = deviceId; } int32_t getNumberOfBursts() const { return mNumberOfBursts; } void setNumberOfBursts(int32_t numBursts) { mNumberOfBursts = numBursts; } /** * Apply these parameters to a stream builder. * @param builder */ void applyParameters(AAudioStreamBuilder *builder) const { AAudioStreamBuilder_setChannelCount(builder, mChannelCount); AAudioStreamBuilder_setFormat(builder, mFormat); AAudioStreamBuilder_setSampleRate(builder, mSampleRate); AAudioStreamBuilder_setBufferCapacityInFrames(builder, mBufferCapacity); AAudioStreamBuilder_setDeviceId(builder, mDeviceId); AAudioStreamBuilder_setSharingMode(builder, mSharingMode); AAudioStreamBuilder_setPerformanceMode(builder, mPerformanceMode); } private: int32_t mChannelCount = AAUDIO_UNSPECIFIED; aaudio_format_t mFormat = AAUDIO_FORMAT_UNSPECIFIED; int32_t mSampleRate = AAUDIO_UNSPECIFIED; int32_t mBufferCapacity = AAUDIO_UNSPECIFIED; int32_t mDeviceId = AAUDIO_UNSPECIFIED; aaudio_sharing_mode_t mSharingMode = AAUDIO_SHARING_MODE_SHARED; aaudio_performance_mode_t mPerformanceMode = AAUDIO_PERFORMANCE_MODE_NONE; int32_t mNumberOfBursts = AAUDIO_UNSPECIFIED; }; class AAudioArgsParser : public AAudioParameters { public: AAudioArgsParser() = default; ~AAudioArgsParser() = default; enum { DEFAULT_DURATION_SECONDS = 5 }; /** * @param arg * @return true if the argument was not handled */ bool parseArg(const char *arg) { bool unrecognized = false; if (arg[0] == '-') { char option = arg[1]; switch (option) { case 'b': setBufferCapacity(atoi(&arg[2])); break; case 'c': setChannelCount(atoi(&arg[2])); break; case 'd': mDurationSeconds = atoi(&arg[2]); break; case 'm': AAudio_setMMapPolicy(AAUDIO_POLICY_AUTO); break; case 'n': setNumberOfBursts(atoi(&arg[2])); break; case 'p': setPerformanceMode(parsePerformanceMode(arg[2])); break; case 'r': setSampleRate(atoi(&arg[2])); break; case 'x': setSharingMode(AAUDIO_SHARING_MODE_EXCLUSIVE); break; default: unrecognized = true; break; } } return unrecognized; } /** * * @param argc * @param argv * @return true if an unrecognized argument was passed */ bool parseArgs(int argc, const char **argv) { for (int i = 1; i < argc; i++) { const char *arg = argv[i]; if (parseArg(arg)) { usage(); return true; } } return false; } static void usage() { printf("-c{channels} -d{duration} -m -n{burstsPerBuffer} -p{perfMode} -r{rate} -x\n"); printf(" Default values are UNSPECIFIED unless otherwise stated.\n"); printf(" -b{bufferCapacity} frames\n"); printf(" -c{channels} for example 2 for stereo\n"); printf(" -d{duration} in seconds, default is %d\n", DEFAULT_DURATION_SECONDS); printf(" -m enable MMAP\n"); printf(" -n{numberOfBursts} for setBufferSize\n"); printf(" -p{performanceMode} set output AAUDIO_PERFORMANCE_MODE*, default NONE\n"); printf(" n for _NONE\n"); printf(" l for _LATENCY\n"); printf(" p for _POWER_SAVING;\n"); printf(" -r{sampleRate} for example 44100\n"); printf(" -x to use EXCLUSIVE mode\n"); } static aaudio_performance_mode_t parsePerformanceMode(char c) { aaudio_performance_mode_t mode = AAUDIO_PERFORMANCE_MODE_NONE; switch (c) { case 'n': mode = AAUDIO_PERFORMANCE_MODE_NONE; break; case 'l': mode = AAUDIO_PERFORMANCE_MODE_LOW_LATENCY; break; case 'p': mode = AAUDIO_PERFORMANCE_MODE_POWER_SAVING; break; default: printf("ERROR invalid performance mode %c\n", c); break; } return mode; } /** * Print stream parameters in comparison with requested values. * @param stream */ void compareWithStream(AAudioStream *stream) { printf(" DeviceId: requested = %d, actual = %d\n", getDeviceId(), AAudioStream_getDeviceId(stream)); aaudio_stream_state_t state = AAudioStream_getState(stream); printf(" State: %s\n", AAudio_convertStreamStateToText(state)); // Check to see what kind of stream we actually got. printf(" SampleRate: requested = %d, actual = %d\n", getSampleRate(), AAudioStream_getSampleRate(stream)); printf(" ChannelCount: requested = %d, actual = %d\n", getChannelCount(), AAudioStream_getChannelCount(stream)); printf(" DataFormat: requested = %d, actual = %d\n", getFormat(), AAudioStream_getFormat(stream)); int32_t framesPerBurst = AAudioStream_getFramesPerBurst(stream); int32_t sizeFrames = AAudioStream_getBufferSizeInFrames(stream); printf(" Buffer: burst = %d\n", framesPerBurst); if (framesPerBurst > 0) { printf(" Buffer: size = %d = (%d * %d) + %d\n", sizeFrames, (sizeFrames / framesPerBurst), framesPerBurst, (sizeFrames % framesPerBurst)); } printf(" Capacity: requested = %d, actual = %d\n", getBufferCapacity(), AAudioStream_getBufferCapacityInFrames(stream)); printf(" SharingMode: requested = %s, actual = %s\n", getSharingModeText(getSharingMode()), getSharingModeText(AAudioStream_getSharingMode(stream))); printf(" PerformanceMode: requested = %d, actual = %d\n", getPerformanceMode(), AAudioStream_getPerformanceMode(stream)); printf(" Is MMAP used? %s\n", AAudioStream_isMMapUsed(stream) ? "yes" : "no"); } int32_t getDurationSeconds() const { return mDurationSeconds; } void setDurationSeconds(int32_t seconds) { mDurationSeconds = seconds; } private: int32_t mDurationSeconds = DEFAULT_DURATION_SECONDS; }; #endif // AAUDIO_EXAMPLE_ARGS_PARSER_H media/libaaudio/examples/utils/AAudioSimplePlayer.h +70 −38 Original line number Original line Diff line number Diff line Loading @@ -23,6 +23,7 @@ #include <sched.h> #include <sched.h> #include <aaudio/AAudio.h> #include <aaudio/AAudio.h> #include "AAudioArgsParser.h" #include "SineGenerator.h" #include "SineGenerator.h" //#define SHARING_MODE AAUDIO_SHARING_MODE_EXCLUSIVE //#define SHARING_MODE AAUDIO_SHARING_MODE_EXCLUSIVE Loading Loading @@ -55,15 +56,23 @@ public: mRequestedPerformanceMode = requestedPerformanceMode; mRequestedPerformanceMode = requestedPerformanceMode; } } // TODO Extract a common base class for record and playback. /** /** * Also known as "sample rate" * Also known as "sample rate" * Only call this after open() has been called. * Only call this after open() has been called. */ */ int32_t getFramesPerSecond() { int32_t getFramesPerSecond() const { return getSampleRate(); // alias } /** * Only call this after open() has been called. */ int32_t getSampleRate() const { if (mStream == nullptr) { if (mStream == nullptr) { return AAUDIO_ERROR_INVALID_STATE; return AAUDIO_ERROR_INVALID_STATE; } } return AAudioStream_getSampleRate(mStream);; return AAudioStream_getSampleRate(mStream); } } /** /** Loading @@ -73,57 +82,83 @@ public: if (mStream == nullptr) { if (mStream == nullptr) { return AAUDIO_ERROR_INVALID_STATE; return AAUDIO_ERROR_INVALID_STATE; } } return AAudioStream_getChannelCount(mStream);; return AAudioStream_getChannelCount(mStream); } } /** /** * Open a stream * Open a stream */ */ aaudio_result_t open(const AAudioParameters ¶meters, AAudioStream_dataCallback dataCallback = nullptr, AAudioStream_errorCallback errorCallback = nullptr, void *userContext = nullptr) { aaudio_result_t result = AAUDIO_OK; // Use an AAudioStreamBuilder to contain requested parameters. AAudioStreamBuilder *builder = nullptr; result = AAudio_createStreamBuilder(&builder); if (result != AAUDIO_OK) return result; parameters.applyParameters(builder); // apply args AAudioStreamBuilder_setDirection(builder, AAUDIO_DIRECTION_OUTPUT); if (dataCallback != nullptr) { AAudioStreamBuilder_setDataCallback(builder, dataCallback, userContext); } if (errorCallback != nullptr) { AAudioStreamBuilder_setErrorCallback(builder, errorCallback, userContext); } //AAudioStreamBuilder_setFramesPerDataCallback(builder, CALLBACK_SIZE_FRAMES); //AAudioStreamBuilder_setBufferCapacityInFrames(builder, 48 * 8); // Open an AAudioStream using the Builder. result = AAudioStreamBuilder_openStream(builder, &mStream); if (result == AAUDIO_OK) { int32_t sizeInBursts = parameters.getNumberOfBursts(); if (sizeInBursts > 0) { int32_t framesPerBurst = AAudioStream_getFramesPerBurst(mStream); AAudioStream_setBufferSizeInFrames(mStream, sizeInBursts * framesPerBurst); } } AAudioStreamBuilder_delete(builder); return result; } aaudio_result_t open(int channelCount, int sampSampleRate, aaudio_format_t format, aaudio_result_t open(int channelCount, int sampSampleRate, aaudio_format_t format, AAudioStream_dataCallback dataProc, AAudioStream_errorCallback errorProc, AAudioStream_dataCallback dataProc, AAudioStream_errorCallback errorProc, void *userContext) { void *userContext) { aaudio_result_t result = AAUDIO_OK; aaudio_result_t result = AAUDIO_OK; // Use an AAudioStreamBuilder to contain requested parameters. // Use an AAudioStreamBuilder to contain requested parameters. result = AAudio_createStreamBuilder(&mBuilder); AAudioStreamBuilder *builder = nullptr; result = AAudio_createStreamBuilder(&builder); if (result != AAUDIO_OK) return result; if (result != AAUDIO_OK) return result; //AAudioStreamBuilder_setSampleRate(mBuilder, 44100); AAudioStreamBuilder_setDirection(builder, AAUDIO_DIRECTION_OUTPUT); AAudioStreamBuilder_setPerformanceMode(mBuilder, mRequestedPerformanceMode); AAudioStreamBuilder_setPerformanceMode(builder, mRequestedPerformanceMode); AAudioStreamBuilder_setSharingMode(mBuilder, mRequestedSharingMode); AAudioStreamBuilder_setSharingMode(builder, mRequestedSharingMode); AAudioStreamBuilder_setChannelCount(builder, channelCount); AAudioStreamBuilder_setSampleRate(builder, sampSampleRate); AAudioStreamBuilder_setFormat(builder, format); if (dataProc != nullptr) { if (dataProc != nullptr) { AAudioStreamBuilder_setDataCallback(mBuilder, dataProc, userContext); AAudioStreamBuilder_setDataCallback(builder, dataProc, userContext); } } if (errorProc != nullptr) { if (errorProc != nullptr) { AAudioStreamBuilder_setErrorCallback(mBuilder, errorProc, userContext); AAudioStreamBuilder_setErrorCallback(builder, errorProc, userContext); } } AAudioStreamBuilder_setChannelCount(mBuilder, channelCount); //AAudioStreamBuilder_setFramesPerDataCallback(builder, CALLBACK_SIZE_FRAMES); AAudioStreamBuilder_setSampleRate(mBuilder, sampSampleRate); //AAudioStreamBuilder_setBufferCapacityInFrames(builder, 48 * 8); AAudioStreamBuilder_setFormat(mBuilder, format); //AAudioStreamBuilder_setFramesPerDataCallback(mBuilder, CALLBACK_SIZE_FRAMES); AAudioStreamBuilder_setBufferCapacityInFrames(mBuilder, 48 * 8); //aaudio_performance_mode_t perfMode = AAUDIO_PERFORMANCE_MODE_NONE; aaudio_performance_mode_t perfMode = AAUDIO_PERFORMANCE_MODE_LOW_LATENCY; //aaudio_performance_mode_t perfMode = AAUDIO_PERFORMANCE_MODE_POWER_SAVING; AAudioStreamBuilder_setPerformanceMode(mBuilder, perfMode); // Open an AAudioStream using the Builder. // Open an AAudioStream using the Builder. result = AAudioStreamBuilder_openStream(mBuilder, &mStream); result = AAudioStreamBuilder_openStream(builder, &mStream); if (result != AAUDIO_OK) goto finish1; AAudioStreamBuilder_delete(builder); printf("AAudioStream_getFramesPerBurst() = %d\n", AAudioStream_getFramesPerBurst(mStream)); printf("AAudioStream_getBufferSizeInFrames() = %d\n", AAudioStream_getBufferSizeInFrames(mStream)); printf("AAudioStream_getBufferCapacityInFrames() = %d\n", AAudioStream_getBufferCapacityInFrames(mStream)); printf("AAudioStream_getPerformanceMode() = %d, requested %d\n", AAudioStream_getPerformanceMode(mStream), perfMode); finish1: AAudioStreamBuilder_delete(mBuilder); mBuilder = nullptr; return result; return result; } } Loading @@ -132,8 +167,6 @@ public: printf("call AAudioStream_close(%p)\n", mStream); fflush(stdout); printf("call AAudioStream_close(%p)\n", mStream); fflush(stdout); AAudioStream_close(mStream); AAudioStream_close(mStream); mStream = nullptr; mStream = nullptr; AAudioStreamBuilder_delete(mBuilder); mBuilder = nullptr; } } return AAUDIO_OK; return AAUDIO_OK; } } Loading Loading @@ -178,7 +211,6 @@ public: } } private: private: AAudioStreamBuilder *mBuilder = nullptr; AAudioStream *mStream = nullptr; AAudioStream *mStream = nullptr; aaudio_sharing_mode_t mRequestedSharingMode = SHARING_MODE; aaudio_sharing_mode_t mRequestedSharingMode = SHARING_MODE; aaudio_performance_mode_t mRequestedPerformanceMode = PERFORMANCE_MODE; aaudio_performance_mode_t mRequestedPerformanceMode = PERFORMANCE_MODE; Loading media/libaaudio/examples/utils/AAudioSimpleRecorder.h +68 −30 File changed.Preview size limit exceeded, changes collapsed. Show changes Loading
media/libaaudio/examples/input_monitor/src/input_monitor.cpp +18 −35 Original line number Original line Diff line number Diff line Loading @@ -26,30 +26,18 @@ #include "AAudioExampleUtils.h" #include "AAudioExampleUtils.h" #include "AAudioSimpleRecorder.h" #include "AAudioSimpleRecorder.h" #define SAMPLE_RATE 48000 // TODO support FLOAT #define REQUIRED_FORMAT AAUDIO_FORMAT_PCM_I16 #define NUM_SECONDS 10 #define MIN_FRAMES_TO_READ 48 /* arbitrary, 1 msec at 48000 Hz */ #define MIN_FRAMES_TO_READ 48 /* arbitrary, 1 msec at 48000 Hz */ int main(int argc, char **argv) int main(int argc, const char **argv) { { (void)argc; // unused AAudioArgsParser argParser; aaudio_result_t result; aaudio_result_t result; AAudioSimpleRecorder recorder; AAudioSimpleRecorder recorder; int actualSamplesPerFrame; int actualSamplesPerFrame; int actualSampleRate; int actualSampleRate; const aaudio_format_t requestedDataFormat = AAUDIO_FORMAT_PCM_I16; aaudio_format_t actualDataFormat; aaudio_format_t actualDataFormat; const int requestedInputChannelCount = 2; // Can affect whether we get a FAST path. //aaudio_performance_mode_t requestedPerformanceMode = AAUDIO_PERFORMANCE_MODE_NONE; const aaudio_performance_mode_t requestedPerformanceMode = AAUDIO_PERFORMANCE_MODE_LOW_LATENCY; //aaudio_performance_mode_t requestedPerformanceMode = AAUDIO_PERFORMANCE_MODE_POWER_SAVING; //const aaudio_sharing_mode_t requestedSharingMode = AAUDIO_SHARING_MODE_SHARED; const aaudio_sharing_mode_t requestedSharingMode = AAUDIO_SHARING_MODE_EXCLUSIVE; aaudio_sharing_mode_t actualSharingMode; aaudio_sharing_mode_t actualSharingMode; AAudioStream *aaudioStream = nullptr; AAudioStream *aaudioStream = nullptr; Loading @@ -70,18 +58,18 @@ int main(int argc, char **argv) printf("%s - Monitor input level using AAudio\n", argv[0]); printf("%s - Monitor input level using AAudio\n", argv[0]); AAudio_setMMapPolicy(AAUDIO_POLICY_ALWAYS); argParser.setFormat(REQUIRED_FORMAT); if (argParser.parseArgs(argc, argv)) { recorder.setPerformanceMode(requestedPerformanceMode); return EXIT_FAILURE; recorder.setSharingMode(requestedSharingMode); } result = recorder.open(requestedInputChannelCount, 48000, requestedDataFormat, result = recorder.open(argParser); nullptr, nullptr, nullptr); if (result != AAUDIO_OK) { if (result != AAUDIO_OK) { fprintf(stderr, "ERROR - recorder.open() returned %d\n", result); fprintf(stderr, "ERROR - recorder.open() returned %d\n", result); goto finish; goto finish; } } aaudioStream = recorder.getStream(); aaudioStream = recorder.getStream(); argParser.compareWithStream(aaudioStream); deviceId = AAudioStream_getDeviceId(aaudioStream); deviceId = AAudioStream_getDeviceId(aaudioStream); printf("deviceId = %d\n", deviceId); printf("deviceId = %d\n", deviceId); Loading @@ -91,11 +79,6 @@ int main(int argc, char **argv) actualSampleRate = AAudioStream_getSampleRate(aaudioStream); actualSampleRate = AAudioStream_getSampleRate(aaudioStream); printf("SamplesPerFrame = %d\n", actualSampleRate); printf("SamplesPerFrame = %d\n", actualSampleRate); actualSharingMode = AAudioStream_getSharingMode(aaudioStream); printf("SharingMode: requested = %s, actual = %s\n", getSharingModeText(requestedSharingMode), getSharingModeText(actualSharingMode)); // This is the number of frames that are written in one chunk by a DMA controller // This is the number of frames that are written in one chunk by a DMA controller // or a DSP. // or a DSP. framesPerBurst = AAudioStream_getFramesPerBurst(aaudioStream); framesPerBurst = AAudioStream_getFramesPerBurst(aaudioStream); Loading @@ -110,14 +93,12 @@ int main(int argc, char **argv) printf("DataFormat: framesPerRead = %d\n",framesPerRead); printf("DataFormat: framesPerRead = %d\n",framesPerRead); actualDataFormat = AAudioStream_getFormat(aaudioStream); actualDataFormat = AAudioStream_getFormat(aaudioStream); printf("DataFormat: requested = %d, actual = %d\n", requestedDataFormat, actualDataFormat); printf("DataFormat: requested = %d, actual = %d\n", REQUIRED_FORMAT, actualDataFormat); // TODO handle other data formats // TODO handle other data formats assert(actualDataFormat == AAUDIO_FORMAT_PCM_I16); assert(actualDataFormat == REQUIRED_FORMAT); printf("PerformanceMode: requested = %d, actual = %d\n", requestedPerformanceMode, // Allocate a buffer for the PCM_16 audio data. AAudioStream_getPerformanceMode(aaudioStream)); // Allocate a buffer for the audio data. data = new(std::nothrow) int16_t[framesPerRead * actualSamplesPerFrame]; data = new(std::nothrow) int16_t[framesPerRead * actualSamplesPerFrame]; if (data == nullptr) { if (data == nullptr) { fprintf(stderr, "ERROR - could not allocate data buffer\n"); fprintf(stderr, "ERROR - could not allocate data buffer\n"); Loading @@ -136,7 +117,7 @@ int main(int argc, char **argv) printf("after start, state = %s\n", AAudio_convertStreamStateToText(state)); printf("after start, state = %s\n", AAudio_convertStreamStateToText(state)); // Record for a while. // Record for a while. framesToRecord = actualSampleRate * NUM_SECONDS; framesToRecord = actualSampleRate * argParser.getDurationSeconds(); framesLeft = framesToRecord; framesLeft = framesToRecord; while (framesLeft > 0) { while (framesLeft > 0) { // Read audio data from the stream. // Read audio data from the stream. Loading Loading @@ -176,6 +157,8 @@ int main(int argc, char **argv) goto finish; goto finish; } } argParser.compareWithStream(aaudioStream); finish: finish: recorder.close(); recorder.close(); delete[] data; delete[] data; Loading
media/libaaudio/examples/loopback/src/loopback.cpp +0 −1 Original line number Original line Diff line number Diff line Loading @@ -20,7 +20,6 @@ #include <assert.h> #include <assert.h> #include <cctype> #include <cctype> #include <math.h> #include <math.h> #include <stdlib.h> #include <stdio.h> #include <stdio.h> #include <stdlib.h> #include <stdlib.h> #include <unistd.h> #include <unistd.h> Loading
media/libaaudio/examples/utils/AAudioArgsParser.h 0 → 100644 +288 −0 Original line number Original line Diff line number Diff line /* * Copyright (C) 2017 The Android Open Source Project * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ #ifndef AAUDIO_EXAMPLE_ARGS_PARSER_H #define AAUDIO_EXAMPLE_ARGS_PARSER_H #include <cctype> #include <unistd.h> #include <stdio.h> #include <stdlib.h> #include <aaudio/AAudio.h> #include <aaudio/AAudioTesting.h> #include <AAudioExampleUtils.h> // TODO use this as a base class within AAudio class AAudioParameters { public: /** * This is also known as samplesPerFrame. */ int32_t getChannelCount() const { return mChannelCount; } void setChannelCount(int32_t channelCount) { mChannelCount = channelCount; } int32_t getSampleRate() const { return mSampleRate; } void setSampleRate(int32_t sampleRate) { mSampleRate = sampleRate; } aaudio_format_t getFormat() const { return mFormat; } void setFormat(aaudio_format_t format) { mFormat = format; } aaudio_sharing_mode_t getSharingMode() const { return mSharingMode; } void setSharingMode(aaudio_sharing_mode_t sharingMode) { mSharingMode = sharingMode; } int32_t getBufferCapacity() const { return mBufferCapacity; } void setBufferCapacity(int32_t frames) { mBufferCapacity = frames; } int32_t getPerformanceMode() const { return mPerformanceMode; } void setPerformanceMode(aaudio_performance_mode_t performanceMode) { mPerformanceMode = performanceMode; } int32_t getDeviceId() const { return mDeviceId; } void setDeviceId(int32_t deviceId) { mDeviceId = deviceId; } int32_t getNumberOfBursts() const { return mNumberOfBursts; } void setNumberOfBursts(int32_t numBursts) { mNumberOfBursts = numBursts; } /** * Apply these parameters to a stream builder. * @param builder */ void applyParameters(AAudioStreamBuilder *builder) const { AAudioStreamBuilder_setChannelCount(builder, mChannelCount); AAudioStreamBuilder_setFormat(builder, mFormat); AAudioStreamBuilder_setSampleRate(builder, mSampleRate); AAudioStreamBuilder_setBufferCapacityInFrames(builder, mBufferCapacity); AAudioStreamBuilder_setDeviceId(builder, mDeviceId); AAudioStreamBuilder_setSharingMode(builder, mSharingMode); AAudioStreamBuilder_setPerformanceMode(builder, mPerformanceMode); } private: int32_t mChannelCount = AAUDIO_UNSPECIFIED; aaudio_format_t mFormat = AAUDIO_FORMAT_UNSPECIFIED; int32_t mSampleRate = AAUDIO_UNSPECIFIED; int32_t mBufferCapacity = AAUDIO_UNSPECIFIED; int32_t mDeviceId = AAUDIO_UNSPECIFIED; aaudio_sharing_mode_t mSharingMode = AAUDIO_SHARING_MODE_SHARED; aaudio_performance_mode_t mPerformanceMode = AAUDIO_PERFORMANCE_MODE_NONE; int32_t mNumberOfBursts = AAUDIO_UNSPECIFIED; }; class AAudioArgsParser : public AAudioParameters { public: AAudioArgsParser() = default; ~AAudioArgsParser() = default; enum { DEFAULT_DURATION_SECONDS = 5 }; /** * @param arg * @return true if the argument was not handled */ bool parseArg(const char *arg) { bool unrecognized = false; if (arg[0] == '-') { char option = arg[1]; switch (option) { case 'b': setBufferCapacity(atoi(&arg[2])); break; case 'c': setChannelCount(atoi(&arg[2])); break; case 'd': mDurationSeconds = atoi(&arg[2]); break; case 'm': AAudio_setMMapPolicy(AAUDIO_POLICY_AUTO); break; case 'n': setNumberOfBursts(atoi(&arg[2])); break; case 'p': setPerformanceMode(parsePerformanceMode(arg[2])); break; case 'r': setSampleRate(atoi(&arg[2])); break; case 'x': setSharingMode(AAUDIO_SHARING_MODE_EXCLUSIVE); break; default: unrecognized = true; break; } } return unrecognized; } /** * * @param argc * @param argv * @return true if an unrecognized argument was passed */ bool parseArgs(int argc, const char **argv) { for (int i = 1; i < argc; i++) { const char *arg = argv[i]; if (parseArg(arg)) { usage(); return true; } } return false; } static void usage() { printf("-c{channels} -d{duration} -m -n{burstsPerBuffer} -p{perfMode} -r{rate} -x\n"); printf(" Default values are UNSPECIFIED unless otherwise stated.\n"); printf(" -b{bufferCapacity} frames\n"); printf(" -c{channels} for example 2 for stereo\n"); printf(" -d{duration} in seconds, default is %d\n", DEFAULT_DURATION_SECONDS); printf(" -m enable MMAP\n"); printf(" -n{numberOfBursts} for setBufferSize\n"); printf(" -p{performanceMode} set output AAUDIO_PERFORMANCE_MODE*, default NONE\n"); printf(" n for _NONE\n"); printf(" l for _LATENCY\n"); printf(" p for _POWER_SAVING;\n"); printf(" -r{sampleRate} for example 44100\n"); printf(" -x to use EXCLUSIVE mode\n"); } static aaudio_performance_mode_t parsePerformanceMode(char c) { aaudio_performance_mode_t mode = AAUDIO_PERFORMANCE_MODE_NONE; switch (c) { case 'n': mode = AAUDIO_PERFORMANCE_MODE_NONE; break; case 'l': mode = AAUDIO_PERFORMANCE_MODE_LOW_LATENCY; break; case 'p': mode = AAUDIO_PERFORMANCE_MODE_POWER_SAVING; break; default: printf("ERROR invalid performance mode %c\n", c); break; } return mode; } /** * Print stream parameters in comparison with requested values. * @param stream */ void compareWithStream(AAudioStream *stream) { printf(" DeviceId: requested = %d, actual = %d\n", getDeviceId(), AAudioStream_getDeviceId(stream)); aaudio_stream_state_t state = AAudioStream_getState(stream); printf(" State: %s\n", AAudio_convertStreamStateToText(state)); // Check to see what kind of stream we actually got. printf(" SampleRate: requested = %d, actual = %d\n", getSampleRate(), AAudioStream_getSampleRate(stream)); printf(" ChannelCount: requested = %d, actual = %d\n", getChannelCount(), AAudioStream_getChannelCount(stream)); printf(" DataFormat: requested = %d, actual = %d\n", getFormat(), AAudioStream_getFormat(stream)); int32_t framesPerBurst = AAudioStream_getFramesPerBurst(stream); int32_t sizeFrames = AAudioStream_getBufferSizeInFrames(stream); printf(" Buffer: burst = %d\n", framesPerBurst); if (framesPerBurst > 0) { printf(" Buffer: size = %d = (%d * %d) + %d\n", sizeFrames, (sizeFrames / framesPerBurst), framesPerBurst, (sizeFrames % framesPerBurst)); } printf(" Capacity: requested = %d, actual = %d\n", getBufferCapacity(), AAudioStream_getBufferCapacityInFrames(stream)); printf(" SharingMode: requested = %s, actual = %s\n", getSharingModeText(getSharingMode()), getSharingModeText(AAudioStream_getSharingMode(stream))); printf(" PerformanceMode: requested = %d, actual = %d\n", getPerformanceMode(), AAudioStream_getPerformanceMode(stream)); printf(" Is MMAP used? %s\n", AAudioStream_isMMapUsed(stream) ? "yes" : "no"); } int32_t getDurationSeconds() const { return mDurationSeconds; } void setDurationSeconds(int32_t seconds) { mDurationSeconds = seconds; } private: int32_t mDurationSeconds = DEFAULT_DURATION_SECONDS; }; #endif // AAUDIO_EXAMPLE_ARGS_PARSER_H
media/libaaudio/examples/utils/AAudioSimplePlayer.h +70 −38 Original line number Original line Diff line number Diff line Loading @@ -23,6 +23,7 @@ #include <sched.h> #include <sched.h> #include <aaudio/AAudio.h> #include <aaudio/AAudio.h> #include "AAudioArgsParser.h" #include "SineGenerator.h" #include "SineGenerator.h" //#define SHARING_MODE AAUDIO_SHARING_MODE_EXCLUSIVE //#define SHARING_MODE AAUDIO_SHARING_MODE_EXCLUSIVE Loading Loading @@ -55,15 +56,23 @@ public: mRequestedPerformanceMode = requestedPerformanceMode; mRequestedPerformanceMode = requestedPerformanceMode; } } // TODO Extract a common base class for record and playback. /** /** * Also known as "sample rate" * Also known as "sample rate" * Only call this after open() has been called. * Only call this after open() has been called. */ */ int32_t getFramesPerSecond() { int32_t getFramesPerSecond() const { return getSampleRate(); // alias } /** * Only call this after open() has been called. */ int32_t getSampleRate() const { if (mStream == nullptr) { if (mStream == nullptr) { return AAUDIO_ERROR_INVALID_STATE; return AAUDIO_ERROR_INVALID_STATE; } } return AAudioStream_getSampleRate(mStream);; return AAudioStream_getSampleRate(mStream); } } /** /** Loading @@ -73,57 +82,83 @@ public: if (mStream == nullptr) { if (mStream == nullptr) { return AAUDIO_ERROR_INVALID_STATE; return AAUDIO_ERROR_INVALID_STATE; } } return AAudioStream_getChannelCount(mStream);; return AAudioStream_getChannelCount(mStream); } } /** /** * Open a stream * Open a stream */ */ aaudio_result_t open(const AAudioParameters ¶meters, AAudioStream_dataCallback dataCallback = nullptr, AAudioStream_errorCallback errorCallback = nullptr, void *userContext = nullptr) { aaudio_result_t result = AAUDIO_OK; // Use an AAudioStreamBuilder to contain requested parameters. AAudioStreamBuilder *builder = nullptr; result = AAudio_createStreamBuilder(&builder); if (result != AAUDIO_OK) return result; parameters.applyParameters(builder); // apply args AAudioStreamBuilder_setDirection(builder, AAUDIO_DIRECTION_OUTPUT); if (dataCallback != nullptr) { AAudioStreamBuilder_setDataCallback(builder, dataCallback, userContext); } if (errorCallback != nullptr) { AAudioStreamBuilder_setErrorCallback(builder, errorCallback, userContext); } //AAudioStreamBuilder_setFramesPerDataCallback(builder, CALLBACK_SIZE_FRAMES); //AAudioStreamBuilder_setBufferCapacityInFrames(builder, 48 * 8); // Open an AAudioStream using the Builder. result = AAudioStreamBuilder_openStream(builder, &mStream); if (result == AAUDIO_OK) { int32_t sizeInBursts = parameters.getNumberOfBursts(); if (sizeInBursts > 0) { int32_t framesPerBurst = AAudioStream_getFramesPerBurst(mStream); AAudioStream_setBufferSizeInFrames(mStream, sizeInBursts * framesPerBurst); } } AAudioStreamBuilder_delete(builder); return result; } aaudio_result_t open(int channelCount, int sampSampleRate, aaudio_format_t format, aaudio_result_t open(int channelCount, int sampSampleRate, aaudio_format_t format, AAudioStream_dataCallback dataProc, AAudioStream_errorCallback errorProc, AAudioStream_dataCallback dataProc, AAudioStream_errorCallback errorProc, void *userContext) { void *userContext) { aaudio_result_t result = AAUDIO_OK; aaudio_result_t result = AAUDIO_OK; // Use an AAudioStreamBuilder to contain requested parameters. // Use an AAudioStreamBuilder to contain requested parameters. result = AAudio_createStreamBuilder(&mBuilder); AAudioStreamBuilder *builder = nullptr; result = AAudio_createStreamBuilder(&builder); if (result != AAUDIO_OK) return result; if (result != AAUDIO_OK) return result; //AAudioStreamBuilder_setSampleRate(mBuilder, 44100); AAudioStreamBuilder_setDirection(builder, AAUDIO_DIRECTION_OUTPUT); AAudioStreamBuilder_setPerformanceMode(mBuilder, mRequestedPerformanceMode); AAudioStreamBuilder_setPerformanceMode(builder, mRequestedPerformanceMode); AAudioStreamBuilder_setSharingMode(mBuilder, mRequestedSharingMode); AAudioStreamBuilder_setSharingMode(builder, mRequestedSharingMode); AAudioStreamBuilder_setChannelCount(builder, channelCount); AAudioStreamBuilder_setSampleRate(builder, sampSampleRate); AAudioStreamBuilder_setFormat(builder, format); if (dataProc != nullptr) { if (dataProc != nullptr) { AAudioStreamBuilder_setDataCallback(mBuilder, dataProc, userContext); AAudioStreamBuilder_setDataCallback(builder, dataProc, userContext); } } if (errorProc != nullptr) { if (errorProc != nullptr) { AAudioStreamBuilder_setErrorCallback(mBuilder, errorProc, userContext); AAudioStreamBuilder_setErrorCallback(builder, errorProc, userContext); } } AAudioStreamBuilder_setChannelCount(mBuilder, channelCount); //AAudioStreamBuilder_setFramesPerDataCallback(builder, CALLBACK_SIZE_FRAMES); AAudioStreamBuilder_setSampleRate(mBuilder, sampSampleRate); //AAudioStreamBuilder_setBufferCapacityInFrames(builder, 48 * 8); AAudioStreamBuilder_setFormat(mBuilder, format); //AAudioStreamBuilder_setFramesPerDataCallback(mBuilder, CALLBACK_SIZE_FRAMES); AAudioStreamBuilder_setBufferCapacityInFrames(mBuilder, 48 * 8); //aaudio_performance_mode_t perfMode = AAUDIO_PERFORMANCE_MODE_NONE; aaudio_performance_mode_t perfMode = AAUDIO_PERFORMANCE_MODE_LOW_LATENCY; //aaudio_performance_mode_t perfMode = AAUDIO_PERFORMANCE_MODE_POWER_SAVING; AAudioStreamBuilder_setPerformanceMode(mBuilder, perfMode); // Open an AAudioStream using the Builder. // Open an AAudioStream using the Builder. result = AAudioStreamBuilder_openStream(mBuilder, &mStream); result = AAudioStreamBuilder_openStream(builder, &mStream); if (result != AAUDIO_OK) goto finish1; AAudioStreamBuilder_delete(builder); printf("AAudioStream_getFramesPerBurst() = %d\n", AAudioStream_getFramesPerBurst(mStream)); printf("AAudioStream_getBufferSizeInFrames() = %d\n", AAudioStream_getBufferSizeInFrames(mStream)); printf("AAudioStream_getBufferCapacityInFrames() = %d\n", AAudioStream_getBufferCapacityInFrames(mStream)); printf("AAudioStream_getPerformanceMode() = %d, requested %d\n", AAudioStream_getPerformanceMode(mStream), perfMode); finish1: AAudioStreamBuilder_delete(mBuilder); mBuilder = nullptr; return result; return result; } } Loading @@ -132,8 +167,6 @@ public: printf("call AAudioStream_close(%p)\n", mStream); fflush(stdout); printf("call AAudioStream_close(%p)\n", mStream); fflush(stdout); AAudioStream_close(mStream); AAudioStream_close(mStream); mStream = nullptr; mStream = nullptr; AAudioStreamBuilder_delete(mBuilder); mBuilder = nullptr; } } return AAUDIO_OK; return AAUDIO_OK; } } Loading Loading @@ -178,7 +211,6 @@ public: } } private: private: AAudioStreamBuilder *mBuilder = nullptr; AAudioStream *mStream = nullptr; AAudioStream *mStream = nullptr; aaudio_sharing_mode_t mRequestedSharingMode = SHARING_MODE; aaudio_sharing_mode_t mRequestedSharingMode = SHARING_MODE; aaudio_performance_mode_t mRequestedPerformanceMode = PERFORMANCE_MODE; aaudio_performance_mode_t mRequestedPerformanceMode = PERFORMANCE_MODE; Loading
media/libaaudio/examples/utils/AAudioSimpleRecorder.h +68 −30 File changed.Preview size limit exceeded, changes collapsed. Show changes