2 * Copyright (C) 2015 The Android Open Source Project
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 // Test program for audio_utils FIFO library.
18 // This only tests the single-threaded aspects, not the barriers.
24 #include <audio_utils/fifo.h>
25 #include <audio_utils/sndfile.h>
28 #define min(x, y) (((x) < (y)) ? (x) : (y))
31 int main(int argc, char **argv)
33 size_t frameCount = 0;
34 size_t maxFramesPerRead = 0;
35 size_t maxFramesPerWrite = 0;
36 bool readerThrottlesWriter = true;
39 for (i = 1; i < argc; i++) {
44 case 'f': // FIFO frame count
45 frameCount = atoi(&arg[2]);
47 case 'r': // maximum frame count per read from FIFO
48 maxFramesPerRead = atoi(&arg[2]);
50 case 't': // disable throttling of writer by reader
51 readerThrottlesWriter = false;
53 case 'v': // enable verbose logging
56 case 'w': // maximum frame count per write to FIFO
57 maxFramesPerWrite = atoi(&arg[2]);
60 fprintf(stderr, "%s: unknown option %s\n", argv[0], arg);
64 if (frameCount == 0) {
67 if (maxFramesPerRead == 0) {
68 maxFramesPerRead = frameCount;
70 if (maxFramesPerWrite == 0) {
71 maxFramesPerWrite = frameCount;
76 fprintf(stderr, "usage: %s [-f#] [-r#] [-t] [-v] [-w#] in.wav out.wav\n", argv[0]);
79 char *inputFile = argv[i];
80 char *outputFile = argv[i+1];
83 memset(&sfinfoin, 0, sizeof(sfinfoin));
84 SNDFILE *sfin = sf_open(inputFile, SFM_READ, &sfinfoin);
89 switch (sfinfoin.format & (SF_FORMAT_TYPEMASK | SF_FORMAT_SUBMASK)) {
90 case SF_FORMAT_WAV | SF_FORMAT_PCM_16:
91 case SF_FORMAT_WAV | SF_FORMAT_PCM_U8:
94 fprintf(stderr, "%s: unsupported format\n", inputFile);
98 size_t frameSize = sizeof(int16_t) * sfinfoin.channels;
99 int16_t *inputBuffer = new int16_t[sfinfoin.frames * sfinfoin.channels];
100 sf_count_t actualRead = sf_readf_short(sfin, inputBuffer, sfinfoin.frames);
101 if (actualRead != sfinfoin.frames) {
102 fprintf(stderr, "%s: unexpected EOF or error\n", inputFile);
108 int16_t *outputBuffer = new int16_t[sfinfoin.frames * sfinfoin.channels];
109 size_t framesWritten = 0;
110 size_t framesRead = 0;
111 int16_t *fifoBuffer = new int16_t[frameCount * sfinfoin.channels];
112 audio_utils_fifo fifo(frameCount, frameSize, fifoBuffer, readerThrottlesWriter);
113 audio_utils_fifo_writer fifoWriter(fifo);
114 audio_utils_fifo_reader fifoReader(fifo, readerThrottlesWriter);
115 int fifoWriteCount = 0, fifoReadCount = 0;
116 int fifoFillLevel = 0, minFillLevel = INT_MAX, maxFillLevel = INT_MIN;
118 size_t framesToWrite = sfinfoin.frames - framesWritten;
119 size_t framesToRead = sfinfoin.frames - framesRead;
120 if (framesToWrite == 0 && (framesToRead == 0 || !readerThrottlesWriter)) {
124 if (framesToWrite > maxFramesPerWrite) {
125 framesToWrite = maxFramesPerWrite;
127 framesToWrite = rand() % (framesToWrite + 1);
128 ssize_t actualWritten = fifoWriter.write(
129 &inputBuffer[framesWritten * sfinfoin.channels], framesToWrite);
131 printf("wrote %d out of %d\n", (int) actualWritten, (int) framesToWrite);
133 if (actualWritten < 0 || (size_t) actualWritten > framesToWrite) {
134 fprintf(stderr, "write to FIFO failed\n");
137 if (actualWritten < min((int) frameCount - fifoFillLevel, (int) framesToWrite)) {
138 fprintf(stderr, "only wrote %d when should have written min(%d, %d)\n",
139 (int) actualWritten, (int) frameCount - fifoFillLevel, (int) framesToWrite);
141 framesWritten += actualWritten;
142 if (actualWritten > 0) {
145 fifoFillLevel += actualWritten;
147 printf("fill level after write %d\n", fifoFillLevel);
149 if (fifoFillLevel > maxFillLevel) {
150 maxFillLevel = fifoFillLevel;
151 if (maxFillLevel > (int) frameCount) {
152 if (readerThrottlesWriter) {
153 printf("maxFillLevel=%d > frameCount=%d\n", maxFillLevel, (int) frameCount);
159 if (framesToRead > maxFramesPerRead) {
160 framesToRead = maxFramesPerRead;
162 framesToRead = rand() % (framesToRead + 1);
163 ssize_t actualRead = fifoReader.read(
164 &outputBuffer[framesRead * sfinfoin.channels], framesToRead);
166 printf("read %d out of %d\n", (int) actualRead, (int) framesToRead);
168 if (actualRead < 0 || (size_t) actualRead > framesToRead) {
169 switch (actualRead) {
171 fprintf(stderr, "read from FIFO failed: corrupted indices\n");
175 if (readerThrottlesWriter) {
176 fprintf(stderr, "read from FIFO failed: unexpected overflow\n");
179 printf("warning: reader lost frames\n");
183 if (actualRead < 0) {
184 fprintf(stderr, "read from FIFO failed: unexpected error code %d\n",
187 fprintf(stderr, "read from FIFO failed: actualRead=%d > framesToRead=%d\n",
188 (int) actualRead, (int) framesToRead);
193 if (actualRead < min(fifoFillLevel, (int) framesToRead)) {
194 //fprintf(stderr, "only read %d when should have read min(%d, %d)\n",
195 // (int) actualRead, fifoFillLevel, (int) framesToRead);
197 framesRead += actualRead;
198 if (actualRead > 0) {
201 fifoFillLevel -= actualRead;
203 printf("fill level after read %d\n", fifoFillLevel);
205 if (fifoFillLevel < minFillLevel) {
206 minFillLevel = fifoFillLevel;
207 if (minFillLevel < 0) {
208 printf("minFillLevel=%d < 0\n", minFillLevel);
213 delete[] inputBuffer;
218 printf("FIFO non-empty writes: %d, non-empty reads: %d\n", fifoWriteCount, fifoReadCount);
219 printf("fill=%d, min=%d, max=%d\n", fifoFillLevel, minFillLevel, maxFillLevel);
221 printf("writing output\n");
223 memset(&sfinfoout, 0, sizeof(sfinfoout));
224 sfinfoout.samplerate = sfinfoin.samplerate;
225 sfinfoout.channels = sfinfoin.channels;
226 sfinfoout.format = sfinfoin.format;
227 SNDFILE *sfout = sf_open(outputFile, SFM_WRITE, &sfinfoout);
232 sf_count_t actualWritten = sf_writef_short(sfout, outputBuffer, framesRead);
233 delete[] outputBuffer;
236 if (actualWritten != (sf_count_t) framesRead) {
237 fprintf(stderr, "%s: unexpected error\n", outputFile);