1 /* 2 * Copyright (C) 2017 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17 // Play sine waves using AAudio. 18 19 #include <stdio.h> 20 //#include <stdlib.h> 21 //#include <math.h> 22 23 #include <android-base/macros.h> 24 #include <aaudio/AAudio.h> 25 26 #include <gtest/gtest.h> 27 28 // Callback function that fills the audio output buffer. 29 aaudio_data_callback_result_t MyDataCallbackProc( 30 AAudioStream *stream, 31 void *userData, 32 void *audioData, 33 int32_t numFrames 34 ) { 35 (void) stream; 36 (void) userData; 37 (void) audioData; 38 (void) numFrames; 39 return AAUDIO_CALLBACK_RESULT_CONTINUE; 40 } 41 42 static void testOpenOptions(aaudio_direction_t direction, 43 int32_t channelCount, 44 int32_t sampleRate, 45 aaudio_format_t format) { 46 47 aaudio_result_t result = AAUDIO_OK; 48 49 int32_t bufferCapacity; 50 int32_t framesPerBurst = 0; 51 52 int32_t actualChannelCount = 0; 53 int32_t actualSampleRate = 0; 54 aaudio_format_t actualDataFormat = AAUDIO_FORMAT_UNSPECIFIED; 55 aaudio_direction_t actualDirection; 56 57 AAudioStreamBuilder *aaudioBuilder = nullptr; 58 AAudioStream *aaudioStream = nullptr; 59 60 printf("TestOpen: dir = %d, chans = %3d, rate = %6d format = %d\n", 61 direction, channelCount, sampleRate, format); 62 63 // Use an AAudioStreamBuilder to contain requested parameters. 64 ASSERT_EQ(AAUDIO_OK, AAudio_createStreamBuilder(&aaudioBuilder)); 65 66 // Request stream properties. 67 AAudioStreamBuilder_setDirection(aaudioBuilder, direction); 68 AAudioStreamBuilder_setSampleRate(aaudioBuilder, sampleRate); 69 AAudioStreamBuilder_setChannelCount(aaudioBuilder, channelCount); 70 AAudioStreamBuilder_setFormat(aaudioBuilder, format); 71 AAudioStreamBuilder_setDataCallback(aaudioBuilder, MyDataCallbackProc, nullptr); 72 73 //AAudioStreamBuilder_setPerformanceMode(aaudioBuilder, AAUDIO_PERFORMANCE_MODE_NONE); 74 AAudioStreamBuilder_setPerformanceMode(aaudioBuilder, AAUDIO_PERFORMANCE_MODE_LOW_LATENCY); 75 //AAudioStreamBuilder_setPerformanceMode(aaudioBuilder, AAUDIO_PERFORMANCE_MODE_POWER_SAVING); 76 77 // Create an AAudioStream using the Builder. 78 result = AAudioStreamBuilder_openStream(aaudioBuilder, &aaudioStream); 79 if (result != AAUDIO_OK) { 80 printf("Stream not opened! That may be OK.\n"); 81 goto finish; 82 } 83 84 // Check to see what kind of stream we actually got. 85 actualSampleRate = AAudioStream_getSampleRate(aaudioStream); 86 actualChannelCount = AAudioStream_getChannelCount(aaudioStream); 87 actualDataFormat = AAudioStream_getFormat(aaudioStream); 88 actualDirection = AAudioStream_getDirection(aaudioStream); 89 90 printf(" dir = %d, chans = %3d, rate = %6d format = %d\n", 91 direction, actualChannelCount, actualSampleRate, actualDataFormat); 92 93 // If we ask for something specific then we should get that. 94 if (channelCount != AAUDIO_UNSPECIFIED) { 95 EXPECT_EQ(channelCount, actualChannelCount); 96 } 97 if (sampleRate != AAUDIO_UNSPECIFIED) { 98 EXPECT_EQ(sampleRate, actualSampleRate); 99 } 100 if (format != AAUDIO_FORMAT_UNSPECIFIED) { 101 EXPECT_EQ(format, actualDataFormat); 102 } 103 EXPECT_EQ(direction, actualDirection); 104 105 // This is the number of frames that are read in one chunk by a DMA controller 106 // or a DSP or a mixer. 107 framesPerBurst = AAudioStream_getFramesPerBurst(aaudioStream); 108 bufferCapacity = AAudioStream_getBufferCapacityInFrames(aaudioStream); 109 printf(" bufferCapacity = %d, remainder = %d\n", 110 bufferCapacity, bufferCapacity % framesPerBurst); 111 112 finish: 113 AAudioStream_close(aaudioStream); 114 AAudioStreamBuilder_delete(aaudioBuilder); 115 printf(" result = %d = %s\n", result, AAudio_convertResultToText(result)); 116 } 117 118 //void foo() { // for tricking the Android Studio formatter 119 TEST(test_open_params, aaudio_open_all) { 120 aaudio_direction_t directions[] = {AAUDIO_DIRECTION_OUTPUT, AAUDIO_DIRECTION_INPUT}; 121 aaudio_format_t formats[] = {AAUDIO_FORMAT_UNSPECIFIED, 122 AAUDIO_FORMAT_PCM_I16, AAUDIO_FORMAT_PCM_FLOAT}; 123 int32_t rates[] = {AAUDIO_UNSPECIFIED, 22050, 32000, 44100, 48000, 88200, 96000, 37913, 59132}; 124 125 // Make printf print immediately so that debug info is not stuck 126 // in a buffer if we hang or crash. 127 setvbuf(stdout, nullptr, _IONBF, (size_t) 0); 128 129 for (uint dirIndex = 0;dirIndex < arraysize(directions); dirIndex++) { 130 aaudio_direction_t direction = directions[dirIndex]; 131 for (int32_t channelCount = 0; channelCount <= 8; channelCount++) { 132 testOpenOptions(direction, channelCount, 133 AAUDIO_UNSPECIFIED, AAUDIO_FORMAT_UNSPECIFIED); 134 } 135 for (uint i = 0; i < arraysize(rates); i++) { 136 testOpenOptions(direction, AAUDIO_UNSPECIFIED, rates[i], AAUDIO_FORMAT_UNSPECIFIED); 137 } 138 for (uint i = 0; i < arraysize(formats); i++) { 139 testOpenOptions(direction, AAUDIO_UNSPECIFIED, AAUDIO_UNSPECIFIED, formats[i]); 140 } 141 } 142 } 143