ronnie
2022-10-23 d7a691c7a2527f2da145355a40a0402c95c67aac
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
/*
 * Copyright (C) 2010 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.
 */
 
/* interactive buffer queue test program */
 
#ifdef ANDROID
#define USE_ANDROID_SIMPLE_BUFFER_QUEUE     // change to #undef for compatibility testing
#endif
 
#include <assert.h>
#include <math.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <SLES/OpenSLES.h>
#ifdef USE_ANDROID_SIMPLE_BUFFER_QUEUE
#include <SLES/OpenSLES_Android.h>
#endif
#include "getch.h"
 
#ifdef USE_ANDROID_SIMPLE_BUFFER_QUEUE
#define DATALOCATOR_BUFFERQUEUE SL_DATALOCATOR_ANDROIDSIMPLEBUFFERQUEUE
#define IID_BUFFERQUEUE SL_IID_ANDROIDSIMPLEBUFFERQUEUE
#define BufferQueueItf SLAndroidSimpleBufferQueueItf
#define BufferQueueState SLAndroidSimpleBufferQueueState
#define INDEX index
#else
#define DATALOCATOR_BUFFERQUEUE SL_DATALOCATOR_BUFFERQUEUE
#define IID_BUFFERQUEUE SL_IID_BUFFERQUEUE
#define BufferQueueItf SLBufferQueueItf
#define BufferQueueState SLBufferQueueState
#define INDEX playIndex
#endif
 
#define checkResult(r) do { if ((r) != SL_RESULT_SUCCESS) fprintf(stderr, "error %d at %s:%d\n", \
    (int) (r), __FILE__, __LINE__); } while (0)
 
typedef struct {
    short left;
    short right;
} frame_t;
 
#define SINE_FRAMES (44100*5)
frame_t sine[SINE_FRAMES];
 
#define SQUARE_FRAMES (44100*5)
frame_t square[SQUARE_FRAMES];
 
#define SAWTOOTH_FRAMES (44100*5)
frame_t sawtooth[SAWTOOTH_FRAMES];
 
#define HALF_FRAMES (44100*5)
frame_t half[HALF_FRAMES];
 
BufferQueueItf expectedCaller = NULL;
void *expectedContext = NULL;
 
static void callback(BufferQueueItf caller, void *context)
{
    putchar('.');
    if (caller != expectedCaller)
        printf("caller %p expected %p\r\n", caller, expectedCaller);
    if (context != expectedContext)
        printf("context %p expected %p\r\n", context, expectedContext);
    fflush(stdout);
}
 
int main(int argc __unused, char **argv __unused)
{
    SLresult result;
 
    // create engine
    SLObjectItf engineObject;
    result = slCreateEngine(&engineObject, 0, NULL, 0, NULL, NULL);
    checkResult(result);
    result = (*engineObject)->Realize(engineObject, SL_BOOLEAN_FALSE);
    checkResult(result);
    SLEngineItf engineEngine;
    result = (*engineObject)->GetInterface(engineObject, SL_IID_ENGINE, &engineEngine);
    checkResult(result);
 
    // create output mix
    SLObjectItf outputmixObject;
    result = (*engineEngine)->CreateOutputMix(engineEngine, &outputmixObject, 0, NULL, NULL);
    checkResult(result);
    result = (*outputmixObject)->Realize(outputmixObject, SL_BOOLEAN_FALSE);
    checkResult(result);
 
    // create audio player
    SLDataSource audiosrc;
    SLDataSink audiosnk;
    SLDataFormat_PCM pcm;
    SLDataLocator_OutputMix locator_outputmix;
    SLDataLocator_BufferQueue locator_bufferqueue;
    locator_bufferqueue.locatorType = DATALOCATOR_BUFFERQUEUE;
    locator_bufferqueue.numBuffers = 255;
    locator_outputmix.locatorType = SL_DATALOCATOR_OUTPUTMIX;
    locator_outputmix.outputMix = outputmixObject;
    pcm.formatType = SL_DATAFORMAT_PCM;
    pcm.numChannels = 2;
    pcm.samplesPerSec = SL_SAMPLINGRATE_44_1;
    pcm.bitsPerSample = SL_PCMSAMPLEFORMAT_FIXED_16;
    pcm.containerSize = 16;
    pcm.channelMask = SL_SPEAKER_FRONT_LEFT | SL_SPEAKER_FRONT_RIGHT;
    pcm.endianness = SL_BYTEORDER_LITTLEENDIAN;
    audiosrc.pLocator = &locator_bufferqueue;
    audiosrc.pFormat = &pcm;
    audiosnk.pLocator = &locator_outputmix;
    audiosnk.pFormat = NULL;
    SLObjectItf playerObject;
    SLInterfaceID ids[2] = {IID_BUFFERQUEUE, SL_IID_MUTESOLO};
    SLboolean flags[2] = {SL_BOOLEAN_TRUE, SL_BOOLEAN_TRUE};
    result = (*engineEngine)->CreateAudioPlayer(engineEngine, &playerObject, &audiosrc, &audiosnk,
            2, ids, flags);
    checkResult(result);
    result = (*playerObject)->Realize(playerObject, SL_BOOLEAN_FALSE);
    checkResult(result);
    SLPlayItf playerPlay;
    result = (*playerObject)->GetInterface(playerObject, SL_IID_PLAY, &playerPlay);
    checkResult(result);
    BufferQueueItf playerBufferqueue;
    result = (*playerObject)->GetInterface(playerObject, IID_BUFFERQUEUE, &playerBufferqueue);
    checkResult(result);
    SLMuteSoloItf playerMuteSolo;
    result = (*playerObject)->GetInterface(playerObject, SL_IID_MUTESOLO, &playerMuteSolo);
    checkResult(result);
    SLuint8 numChannels = 123;
    result = (*playerMuteSolo)->GetNumChannels(playerMuteSolo, &numChannels);
    assert(2 == numChannels);
    SLuint32 state;
    state = SL_PLAYSTATE_PLAYING;
    result = (*playerPlay)->SetPlayState(playerPlay, state);
    checkResult(result);
 
    unsigned i;
    float pi2 = 3.14*2;
    float hz = 441;
    float sr = 44100;
    for (i = 0; i < SINE_FRAMES; ++i) {
        sine[i].left = sin((float) (i  / (sr / hz)) * pi2 ) * 32000.0;
        sine[i].right = sine[i].left;
    }
    for (i = 0; i < SQUARE_FRAMES; ++i) {
        square[i].left = (i % (unsigned) (sr / hz)) < 50 ? 32767 : -32768;
        square[i].right = square[i].left;
    }
    for (i = 0; i < SAWTOOTH_FRAMES; ++i) {
        sawtooth[i].left = ((((int) (i % (unsigned) (sr / hz))) - 50) / 100.0) * 60000.0 - 30000.0;
        sawtooth[i].right = sawtooth[i].left;
    }
    for (i = 0; i < HALF_FRAMES; ++i) {
        half[i].left = sine[i].left;
        half[i].right = sawtooth[i].right / 2;
    }
 
    set_conio_terminal_mode();
    int in_count = 0;
    uintptr_t count = 0;
    for (;;) {
        usleep(10000);
        if (kbhit()) {
            frame_t *buffer;
            unsigned size;
            BufferQueueState bufqstate;
            int ch = getch();
            switch (ch) {
            case '0' ... '9':
                if (in_count) {
                    count = count * 10 + (ch - '0');
                } else {
                    count = ch - '0';
                    in_count = 1;
                }
                continue;
            case 'i':
                buffer = sine;
                size = sizeof(sine);
                goto enqueue;
            case 'q':
                buffer = square;
                size = sizeof(square);
                goto enqueue;
            case 'h':
                buffer = half;
                size = sizeof(half);
                goto enqueue;
            case 'r':
                if (in_count) {
                    expectedCaller = playerBufferqueue;
                    expectedContext = (void *) count;
                } else {
                    expectedCaller = NULL;
                    expectedContext = (void *) NULL;
                }
                result = (*playerBufferqueue)->RegisterCallback(playerBufferqueue, in_count ?
                    callback : NULL, expectedContext);
                checkResult(result);
                break;
            case 'a':
                buffer = sawtooth;
                size = sizeof(sawtooth);
enqueue:
                for (i = 0; i < (in_count ? count : 1); ++i) {
                    result = (*playerBufferqueue)->Enqueue(playerBufferqueue, buffer, size);
                    checkResult(result);
                }
                break;
            case 'c':
                result = (*playerBufferqueue)->Clear(playerBufferqueue);
                checkResult(result);
                putchar('\r');
                result = (*playerBufferqueue)->GetState(playerBufferqueue, &bufqstate);
                checkResult(result);
                if (bufqstate.count != 0)
                    printf("\rcount=%u\r\n", (unsigned) bufqstate.count);
#if 0
                putchar('\r');
                putchar('\n');
#endif
                fflush(stdout);
                break;
            case 'g':
                result = (*playerBufferqueue)->GetState(playerBufferqueue, &bufqstate);
                checkResult(result);
                printf("\rplayIndex=%u\r\n", (unsigned) bufqstate.INDEX);
                printf("count=%u\r\n", (unsigned) bufqstate.count);
                break;
            case 'p':
                state = SL_PLAYSTATE_PAUSED;
                goto setplaystate;
            case 's':
                state = SL_PLAYSTATE_STOPPED;
                goto setplaystate;
            case 'P':
                state = SL_PLAYSTATE_PLAYING;
setplaystate:
                result = (*playerPlay)->SetPlayState(playerPlay, state);
                checkResult(result);
                SLuint32 newstate;
                result = (*playerPlay)->GetPlayState(playerPlay, &newstate);
                checkResult(result);
                if (newstate != state)
                    printf("\rSetPlayState(%u) -> GetPlayState(%u)\r\n", (unsigned) state,
                        (unsigned) newstate);
#if 0
                putchar('\r');
                putchar('\n');
                fflush(stdout);
#endif
                checkResult(result);
                break;
            case 'x':
                goto out;
            default:
                putchar('?');
                fflush(stdout);
                break;
            }
            in_count = 0;
        }
    }
 
out:
    (*playerObject)->Destroy(playerObject);
    (*outputmixObject)->Destroy(outputmixObject);
    (*engineObject)->Destroy(engineObject);
    return EXIT_SUCCESS;
}