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
28static const char *getSharingModeText(aaudio_sharing_mode_t mode) {
29    const char *modeText = "unknown";
30    switch (mode) {
31    case AAUDIO_SHARING_MODE_EXCLUSIVE:
32        modeText = "EXCLUSIVE";
33        break;
34    case AAUDIO_SHARING_MODE_SHARED:
35        modeText = "SHARED";
36        break;
37    default:
38        break;
39    }
40    return modeText;
41}
42
43// Callback function that fills the audio output buffer.
44aaudio_data_callback_result_t MyDataCallbackProc(
45        AAudioStream *stream,
46        void *userData,
47        void *audioData,
48        int32_t numFrames
49) {
50    (void) stream;
51    (void) userData;
52    (void) audioData;
53    (void) numFrames;
54    return AAUDIO_CALLBACK_RESULT_CONTINUE;
55}
56
57static void testOpenOptions(aaudio_direction_t direction,
58                            int32_t channelCount,
59                            int32_t sampleRate,
60                            aaudio_format_t format) {
61
62    aaudio_result_t result = AAUDIO_OK;
63
64    int32_t bufferCapacity;
65    int32_t framesPerBurst = 0;
66
67    int32_t actualChannelCount = 0;
68    int32_t actualSampleRate = 0;
69    aaudio_format_t actualDataFormat = AAUDIO_FORMAT_UNSPECIFIED;
70    aaudio_sharing_mode_t actualSharingMode = AAUDIO_SHARING_MODE_SHARED;
71    aaudio_direction_t actualDirection;
72
73    AAudioStreamBuilder *aaudioBuilder = nullptr;
74    AAudioStream *aaudioStream = nullptr;
75
76    printf("TestOpen: dir = %d, chans = %3d, rate = %6d format = %d\n",
77           direction, channelCount, sampleRate, format);
78
79    // Use an AAudioStreamBuilder to contain requested parameters.
80    ASSERT_EQ(AAUDIO_OK, AAudio_createStreamBuilder(&aaudioBuilder));
81
82    // Request stream properties.
83    AAudioStreamBuilder_setDirection(aaudioBuilder, direction);
84    AAudioStreamBuilder_setSampleRate(aaudioBuilder, sampleRate);
85    AAudioStreamBuilder_setChannelCount(aaudioBuilder, channelCount);
86    AAudioStreamBuilder_setFormat(aaudioBuilder, format);
87    AAudioStreamBuilder_setDataCallback(aaudioBuilder, MyDataCallbackProc, nullptr);
88
89    //AAudioStreamBuilder_setPerformanceMode(aaudioBuilder, AAUDIO_PERFORMANCE_MODE_NONE);
90    AAudioStreamBuilder_setPerformanceMode(aaudioBuilder, AAUDIO_PERFORMANCE_MODE_LOW_LATENCY);
91    //AAudioStreamBuilder_setPerformanceMode(aaudioBuilder, AAUDIO_PERFORMANCE_MODE_POWER_SAVING);
92
93    // Create an AAudioStream using the Builder.
94    result = AAudioStreamBuilder_openStream(aaudioBuilder, &aaudioStream);
95    if (result != AAUDIO_OK) {
96        printf("Stream not opened! That may be OK.\n");
97        goto finish;
98    }
99
100    // Check to see what kind of stream we actually got.
101    actualSampleRate = AAudioStream_getSampleRate(aaudioStream);
102    actualChannelCount = AAudioStream_getChannelCount(aaudioStream);
103    actualDataFormat = AAudioStream_getFormat(aaudioStream);
104    actualDirection = AAudioStream_getDirection(aaudioStream);
105
106    printf("          dir = %d, chans = %3d, rate = %6d format = %d\n",
107           direction, actualChannelCount, actualSampleRate, actualDataFormat);
108
109    // If we ask for something specific then we should get that.
110    if (channelCount != AAUDIO_UNSPECIFIED) {
111        EXPECT_EQ(channelCount, actualChannelCount);
112    }
113    if (sampleRate != AAUDIO_UNSPECIFIED) {
114        EXPECT_EQ(sampleRate, actualSampleRate);
115    }
116    if (format != AAUDIO_FORMAT_UNSPECIFIED) {
117        EXPECT_EQ(format, actualDataFormat);
118    }
119    EXPECT_EQ(direction, actualDirection);
120
121    // This is the number of frames that are read in one chunk by a DMA controller
122    // or a DSP or a mixer.
123    framesPerBurst = AAudioStream_getFramesPerBurst(aaudioStream);
124    bufferCapacity = AAudioStream_getBufferCapacityInFrames(aaudioStream);
125    printf("          bufferCapacity = %d, remainder = %d\n",
126           bufferCapacity, bufferCapacity % framesPerBurst);
127
128finish:
129    AAudioStream_close(aaudioStream);
130    AAudioStreamBuilder_delete(aaudioBuilder);
131    printf("          result = %d = %s\n", result, AAudio_convertResultToText(result));
132}
133
134//void foo() { // for tricking the Android Studio formatter
135TEST(test_open_params, aaudio_open_all) {
136    aaudio_direction_t directions[] = {AAUDIO_DIRECTION_OUTPUT, AAUDIO_DIRECTION_INPUT};
137    aaudio_format_t formats[] = {AAUDIO_FORMAT_UNSPECIFIED,
138                                 AAUDIO_FORMAT_PCM_I16, AAUDIO_FORMAT_PCM_FLOAT};
139    int32_t rates[] = {AAUDIO_UNSPECIFIED, 22050, 32000, 44100, 48000, 88200, 96000, 37913, 59132};
140
141    // Make printf print immediately so that debug info is not stuck
142    // in a buffer if we hang or crash.
143    setvbuf(stdout, nullptr, _IONBF, (size_t) 0);
144
145    for (uint dirIndex = 0;dirIndex < arraysize(directions); dirIndex++) {
146        aaudio_direction_t direction = directions[dirIndex];
147        for (int32_t channelCount = 0; channelCount <= 8; channelCount++) {
148            testOpenOptions(direction, channelCount,
149                            AAUDIO_UNSPECIFIED, AAUDIO_FORMAT_UNSPECIFIED);
150        }
151        for (uint i = 0; i < arraysize(rates); i++) {
152            testOpenOptions(direction, AAUDIO_UNSPECIFIED, rates[i], AAUDIO_FORMAT_UNSPECIFIED);
153        }
154        for (uint i = 0; i < arraysize(formats); i++) {
155            testOpenOptions(direction, AAUDIO_UNSPECIFIED, AAUDIO_UNSPECIFIED, formats[i]);
156        }
157    }
158}
159