14b65ef9efdf5aba01bea89d8cdd64f500560a28dGlenn Kasten/*
24b65ef9efdf5aba01bea89d8cdd64f500560a28dGlenn Kasten * Copyright (C) 2010 The Android Open Source Project
34b65ef9efdf5aba01bea89d8cdd64f500560a28dGlenn Kasten *
44b65ef9efdf5aba01bea89d8cdd64f500560a28dGlenn Kasten * Licensed under the Apache License, Version 2.0 (the "License");
54b65ef9efdf5aba01bea89d8cdd64f500560a28dGlenn Kasten * you may not use this file except in compliance with the License.
64b65ef9efdf5aba01bea89d8cdd64f500560a28dGlenn Kasten * You may obtain a copy of the License at
74b65ef9efdf5aba01bea89d8cdd64f500560a28dGlenn Kasten *
84b65ef9efdf5aba01bea89d8cdd64f500560a28dGlenn Kasten *      http://www.apache.org/licenses/LICENSE-2.0
94b65ef9efdf5aba01bea89d8cdd64f500560a28dGlenn Kasten *
104b65ef9efdf5aba01bea89d8cdd64f500560a28dGlenn Kasten * Unless required by applicable law or agreed to in writing, software
114b65ef9efdf5aba01bea89d8cdd64f500560a28dGlenn Kasten * distributed under the License is distributed on an "AS IS" BASIS,
124b65ef9efdf5aba01bea89d8cdd64f500560a28dGlenn Kasten * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
134b65ef9efdf5aba01bea89d8cdd64f500560a28dGlenn Kasten * See the License for the specific language governing permissions and
144b65ef9efdf5aba01bea89d8cdd64f500560a28dGlenn Kasten * limitations under the License.
154b65ef9efdf5aba01bea89d8cdd64f500560a28dGlenn Kasten */
164b65ef9efdf5aba01bea89d8cdd64f500560a28dGlenn Kasten
174b65ef9efdf5aba01bea89d8cdd64f500560a28dGlenn Kasten/* interactive buffer queue test program */
184b65ef9efdf5aba01bea89d8cdd64f500560a28dGlenn Kasten
1901e9f5fa4698856f92bcfd88188ee4c8397b22dbGlenn Kasten#ifdef ANDROID
2001e9f5fa4698856f92bcfd88188ee4c8397b22dbGlenn Kasten#define USE_ANDROID_SIMPLE_BUFFER_QUEUE     // change to #undef for compatibility testing
2101e9f5fa4698856f92bcfd88188ee4c8397b22dbGlenn Kasten#endif
2201e9f5fa4698856f92bcfd88188ee4c8397b22dbGlenn Kasten
23f6f5ceb363286d5ebef2c2e70c8a5aa135d5d1eeGlenn Kasten#include <assert.h>
244b65ef9efdf5aba01bea89d8cdd64f500560a28dGlenn Kasten#include <math.h>
254b65ef9efdf5aba01bea89d8cdd64f500560a28dGlenn Kasten#include <stdio.h>
264b65ef9efdf5aba01bea89d8cdd64f500560a28dGlenn Kasten#include <stdlib.h>
27e629abc58c2acadc7487ea71c1e063f8f8989199Glenn Kasten#include <unistd.h>
28c6853892c94800e72c0bd676d5d2136d48cea76eGlenn Kasten#include <SLES/OpenSLES.h>
2901e9f5fa4698856f92bcfd88188ee4c8397b22dbGlenn Kasten#ifdef USE_ANDROID_SIMPLE_BUFFER_QUEUE
30c6853892c94800e72c0bd676d5d2136d48cea76eGlenn Kasten#include <SLES/OpenSLES_Android.h>
3101e9f5fa4698856f92bcfd88188ee4c8397b22dbGlenn Kasten#endif
3258432eb9cea995c69b4f905e68b38c1b8216edebGlenn Kasten#include "getch.h"
3301e9f5fa4698856f92bcfd88188ee4c8397b22dbGlenn Kasten
3401e9f5fa4698856f92bcfd88188ee4c8397b22dbGlenn Kasten#ifdef USE_ANDROID_SIMPLE_BUFFER_QUEUE
3501e9f5fa4698856f92bcfd88188ee4c8397b22dbGlenn Kasten#define DATALOCATOR_BUFFERQUEUE SL_DATALOCATOR_ANDROIDSIMPLEBUFFERQUEUE
3601e9f5fa4698856f92bcfd88188ee4c8397b22dbGlenn Kasten#define IID_BUFFERQUEUE SL_IID_ANDROIDSIMPLEBUFFERQUEUE
3701e9f5fa4698856f92bcfd88188ee4c8397b22dbGlenn Kasten#define BufferQueueItf SLAndroidSimpleBufferQueueItf
3801e9f5fa4698856f92bcfd88188ee4c8397b22dbGlenn Kasten#define BufferQueueState SLAndroidSimpleBufferQueueState
3901e9f5fa4698856f92bcfd88188ee4c8397b22dbGlenn Kasten#define INDEX index
4001e9f5fa4698856f92bcfd88188ee4c8397b22dbGlenn Kasten#else
4101e9f5fa4698856f92bcfd88188ee4c8397b22dbGlenn Kasten#define DATALOCATOR_BUFFERQUEUE SL_DATALOCATOR_BUFFERQUEUE
4201e9f5fa4698856f92bcfd88188ee4c8397b22dbGlenn Kasten#define IID_BUFFERQUEUE SL_IID_BUFFERQUEUE
4301e9f5fa4698856f92bcfd88188ee4c8397b22dbGlenn Kasten#define BufferQueueItf SLBufferQueueItf
4401e9f5fa4698856f92bcfd88188ee4c8397b22dbGlenn Kasten#define BufferQueueState SLBufferQueueState
4501e9f5fa4698856f92bcfd88188ee4c8397b22dbGlenn Kasten#define INDEX playIndex
4601e9f5fa4698856f92bcfd88188ee4c8397b22dbGlenn Kasten#endif
474b65ef9efdf5aba01bea89d8cdd64f500560a28dGlenn Kasten
484b65ef9efdf5aba01bea89d8cdd64f500560a28dGlenn Kasten#define checkResult(r) do { if ((r) != SL_RESULT_SUCCESS) fprintf(stderr, "error %d at %s:%d\n", \
494b65ef9efdf5aba01bea89d8cdd64f500560a28dGlenn Kasten    (int) r, __FILE__, __LINE__); } while (0)
504b65ef9efdf5aba01bea89d8cdd64f500560a28dGlenn Kasten
514b65ef9efdf5aba01bea89d8cdd64f500560a28dGlenn Kastentypedef struct {
524b65ef9efdf5aba01bea89d8cdd64f500560a28dGlenn Kasten    short left;
534b65ef9efdf5aba01bea89d8cdd64f500560a28dGlenn Kasten    short right;
544b65ef9efdf5aba01bea89d8cdd64f500560a28dGlenn Kasten} frame_t;
554b65ef9efdf5aba01bea89d8cdd64f500560a28dGlenn Kasten
564b65ef9efdf5aba01bea89d8cdd64f500560a28dGlenn Kasten#define SINE_FRAMES (44100*5)
574b65ef9efdf5aba01bea89d8cdd64f500560a28dGlenn Kastenframe_t sine[SINE_FRAMES];
584b65ef9efdf5aba01bea89d8cdd64f500560a28dGlenn Kasten
594b65ef9efdf5aba01bea89d8cdd64f500560a28dGlenn Kasten#define SQUARE_FRAMES (44100*5)
604b65ef9efdf5aba01bea89d8cdd64f500560a28dGlenn Kastenframe_t square[SQUARE_FRAMES];
614b65ef9efdf5aba01bea89d8cdd64f500560a28dGlenn Kasten
624b65ef9efdf5aba01bea89d8cdd64f500560a28dGlenn Kasten#define SAWTOOTH_FRAMES (44100*5)
634b65ef9efdf5aba01bea89d8cdd64f500560a28dGlenn Kastenframe_t sawtooth[SAWTOOTH_FRAMES];
644b65ef9efdf5aba01bea89d8cdd64f500560a28dGlenn Kasten
65f6f5ceb363286d5ebef2c2e70c8a5aa135d5d1eeGlenn Kasten#define HALF_FRAMES (44100*5)
66f6f5ceb363286d5ebef2c2e70c8a5aa135d5d1eeGlenn Kastenframe_t half[HALF_FRAMES];
67f6f5ceb363286d5ebef2c2e70c8a5aa135d5d1eeGlenn Kasten
6801e9f5fa4698856f92bcfd88188ee4c8397b22dbGlenn KastenBufferQueueItf expectedCaller = NULL;
694b65ef9efdf5aba01bea89d8cdd64f500560a28dGlenn Kastenvoid *expectedContext = NULL;
704b65ef9efdf5aba01bea89d8cdd64f500560a28dGlenn Kasten
7101e9f5fa4698856f92bcfd88188ee4c8397b22dbGlenn Kastenstatic void callback(BufferQueueItf caller, void *context)
724b65ef9efdf5aba01bea89d8cdd64f500560a28dGlenn Kasten{
734b65ef9efdf5aba01bea89d8cdd64f500560a28dGlenn Kasten    putchar('.');
744b65ef9efdf5aba01bea89d8cdd64f500560a28dGlenn Kasten    if (caller != expectedCaller)
754b65ef9efdf5aba01bea89d8cdd64f500560a28dGlenn Kasten        printf("caller %p expected %p\r\n", caller, expectedCaller);
764b65ef9efdf5aba01bea89d8cdd64f500560a28dGlenn Kasten    if (context != expectedContext)
774b65ef9efdf5aba01bea89d8cdd64f500560a28dGlenn Kasten        printf("context %p expected %p\r\n", context, expectedContext);
784b65ef9efdf5aba01bea89d8cdd64f500560a28dGlenn Kasten    fflush(stdout);
794b65ef9efdf5aba01bea89d8cdd64f500560a28dGlenn Kasten}
804b65ef9efdf5aba01bea89d8cdd64f500560a28dGlenn Kasten
81086a6f51a7b12880ed114962136972f89ed70da2Glenn Kastenint main(int argc __unused, char **argv __unused)
824b65ef9efdf5aba01bea89d8cdd64f500560a28dGlenn Kasten{
834b65ef9efdf5aba01bea89d8cdd64f500560a28dGlenn Kasten    SLresult result;
844b65ef9efdf5aba01bea89d8cdd64f500560a28dGlenn Kasten
854b65ef9efdf5aba01bea89d8cdd64f500560a28dGlenn Kasten    // create engine
864b65ef9efdf5aba01bea89d8cdd64f500560a28dGlenn Kasten    SLObjectItf engineObject;
874b65ef9efdf5aba01bea89d8cdd64f500560a28dGlenn Kasten    result = slCreateEngine(&engineObject, 0, NULL, 0, NULL, NULL);
884b65ef9efdf5aba01bea89d8cdd64f500560a28dGlenn Kasten    checkResult(result);
894b65ef9efdf5aba01bea89d8cdd64f500560a28dGlenn Kasten    result = (*engineObject)->Realize(engineObject, SL_BOOLEAN_FALSE);
904b65ef9efdf5aba01bea89d8cdd64f500560a28dGlenn Kasten    checkResult(result);
914b65ef9efdf5aba01bea89d8cdd64f500560a28dGlenn Kasten    SLEngineItf engineEngine;
924b65ef9efdf5aba01bea89d8cdd64f500560a28dGlenn Kasten    result = (*engineObject)->GetInterface(engineObject, SL_IID_ENGINE, &engineEngine);
934b65ef9efdf5aba01bea89d8cdd64f500560a28dGlenn Kasten    checkResult(result);
944b65ef9efdf5aba01bea89d8cdd64f500560a28dGlenn Kasten
954b65ef9efdf5aba01bea89d8cdd64f500560a28dGlenn Kasten    // create output mix
964b65ef9efdf5aba01bea89d8cdd64f500560a28dGlenn Kasten    SLObjectItf outputmixObject;
974b65ef9efdf5aba01bea89d8cdd64f500560a28dGlenn Kasten    result = (*engineEngine)->CreateOutputMix(engineEngine, &outputmixObject, 0, NULL, NULL);
984b65ef9efdf5aba01bea89d8cdd64f500560a28dGlenn Kasten    checkResult(result);
994b65ef9efdf5aba01bea89d8cdd64f500560a28dGlenn Kasten    result = (*outputmixObject)->Realize(outputmixObject, SL_BOOLEAN_FALSE);
1004b65ef9efdf5aba01bea89d8cdd64f500560a28dGlenn Kasten    checkResult(result);
1014b65ef9efdf5aba01bea89d8cdd64f500560a28dGlenn Kasten
1024b65ef9efdf5aba01bea89d8cdd64f500560a28dGlenn Kasten    // create audio player
1034b65ef9efdf5aba01bea89d8cdd64f500560a28dGlenn Kasten    SLDataSource audiosrc;
1044b65ef9efdf5aba01bea89d8cdd64f500560a28dGlenn Kasten    SLDataSink audiosnk;
1054b65ef9efdf5aba01bea89d8cdd64f500560a28dGlenn Kasten    SLDataFormat_PCM pcm;
1064b65ef9efdf5aba01bea89d8cdd64f500560a28dGlenn Kasten    SLDataLocator_OutputMix locator_outputmix;
1074b65ef9efdf5aba01bea89d8cdd64f500560a28dGlenn Kasten    SLDataLocator_BufferQueue locator_bufferqueue;
10801e9f5fa4698856f92bcfd88188ee4c8397b22dbGlenn Kasten    locator_bufferqueue.locatorType = DATALOCATOR_BUFFERQUEUE;
1094b65ef9efdf5aba01bea89d8cdd64f500560a28dGlenn Kasten    locator_bufferqueue.numBuffers = 255;
1104b65ef9efdf5aba01bea89d8cdd64f500560a28dGlenn Kasten    locator_outputmix.locatorType = SL_DATALOCATOR_OUTPUTMIX;
1114b65ef9efdf5aba01bea89d8cdd64f500560a28dGlenn Kasten    locator_outputmix.outputMix = outputmixObject;
1124b65ef9efdf5aba01bea89d8cdd64f500560a28dGlenn Kasten    pcm.formatType = SL_DATAFORMAT_PCM;
1134b65ef9efdf5aba01bea89d8cdd64f500560a28dGlenn Kasten    pcm.numChannels = 2;
1144b65ef9efdf5aba01bea89d8cdd64f500560a28dGlenn Kasten    pcm.samplesPerSec = SL_SAMPLINGRATE_44_1;
1154b65ef9efdf5aba01bea89d8cdd64f500560a28dGlenn Kasten    pcm.bitsPerSample = SL_PCMSAMPLEFORMAT_FIXED_16;
1164b65ef9efdf5aba01bea89d8cdd64f500560a28dGlenn Kasten    pcm.containerSize = 16;
1174b65ef9efdf5aba01bea89d8cdd64f500560a28dGlenn Kasten    pcm.channelMask = SL_SPEAKER_FRONT_LEFT | SL_SPEAKER_FRONT_RIGHT;
1184b65ef9efdf5aba01bea89d8cdd64f500560a28dGlenn Kasten    pcm.endianness = SL_BYTEORDER_LITTLEENDIAN;
1194b65ef9efdf5aba01bea89d8cdd64f500560a28dGlenn Kasten    audiosrc.pLocator = &locator_bufferqueue;
1204b65ef9efdf5aba01bea89d8cdd64f500560a28dGlenn Kasten    audiosrc.pFormat = &pcm;
1214b65ef9efdf5aba01bea89d8cdd64f500560a28dGlenn Kasten    audiosnk.pLocator = &locator_outputmix;
1224b65ef9efdf5aba01bea89d8cdd64f500560a28dGlenn Kasten    audiosnk.pFormat = NULL;
1234b65ef9efdf5aba01bea89d8cdd64f500560a28dGlenn Kasten    SLObjectItf playerObject;
124f6f5ceb363286d5ebef2c2e70c8a5aa135d5d1eeGlenn Kasten    SLInterfaceID ids[2] = {IID_BUFFERQUEUE, SL_IID_MUTESOLO};
125f6f5ceb363286d5ebef2c2e70c8a5aa135d5d1eeGlenn Kasten    SLboolean flags[2] = {SL_BOOLEAN_TRUE, SL_BOOLEAN_TRUE};
12601e9f5fa4698856f92bcfd88188ee4c8397b22dbGlenn Kasten    result = (*engineEngine)->CreateAudioPlayer(engineEngine, &playerObject, &audiosrc, &audiosnk,
127f6f5ceb363286d5ebef2c2e70c8a5aa135d5d1eeGlenn Kasten            2, ids, flags);
1284b65ef9efdf5aba01bea89d8cdd64f500560a28dGlenn Kasten    checkResult(result);
1294b65ef9efdf5aba01bea89d8cdd64f500560a28dGlenn Kasten    result = (*playerObject)->Realize(playerObject, SL_BOOLEAN_FALSE);
1304b65ef9efdf5aba01bea89d8cdd64f500560a28dGlenn Kasten    checkResult(result);
1314b65ef9efdf5aba01bea89d8cdd64f500560a28dGlenn Kasten    SLPlayItf playerPlay;
1324b65ef9efdf5aba01bea89d8cdd64f500560a28dGlenn Kasten    result = (*playerObject)->GetInterface(playerObject, SL_IID_PLAY, &playerPlay);
1334b65ef9efdf5aba01bea89d8cdd64f500560a28dGlenn Kasten    checkResult(result);
13401e9f5fa4698856f92bcfd88188ee4c8397b22dbGlenn Kasten    BufferQueueItf playerBufferqueue;
13501e9f5fa4698856f92bcfd88188ee4c8397b22dbGlenn Kasten    result = (*playerObject)->GetInterface(playerObject, IID_BUFFERQUEUE, &playerBufferqueue);
1364b65ef9efdf5aba01bea89d8cdd64f500560a28dGlenn Kasten    checkResult(result);
137f6f5ceb363286d5ebef2c2e70c8a5aa135d5d1eeGlenn Kasten    SLMuteSoloItf playerMuteSolo;
138f6f5ceb363286d5ebef2c2e70c8a5aa135d5d1eeGlenn Kasten    result = (*playerObject)->GetInterface(playerObject, SL_IID_MUTESOLO, &playerMuteSolo);
139f6f5ceb363286d5ebef2c2e70c8a5aa135d5d1eeGlenn Kasten    checkResult(result);
140f6f5ceb363286d5ebef2c2e70c8a5aa135d5d1eeGlenn Kasten    SLuint8 numChannels = 123;
141f6f5ceb363286d5ebef2c2e70c8a5aa135d5d1eeGlenn Kasten    result = (*playerMuteSolo)->GetNumChannels(playerMuteSolo, &numChannels);
142f6f5ceb363286d5ebef2c2e70c8a5aa135d5d1eeGlenn Kasten    assert(2 == numChannels);
1434b65ef9efdf5aba01bea89d8cdd64f500560a28dGlenn Kasten    SLuint32 state;
1444b65ef9efdf5aba01bea89d8cdd64f500560a28dGlenn Kasten    state = SL_PLAYSTATE_PLAYING;
1454b65ef9efdf5aba01bea89d8cdd64f500560a28dGlenn Kasten    result = (*playerPlay)->SetPlayState(playerPlay, state);
1464b65ef9efdf5aba01bea89d8cdd64f500560a28dGlenn Kasten    checkResult(result);
1474b65ef9efdf5aba01bea89d8cdd64f500560a28dGlenn Kasten
1484b65ef9efdf5aba01bea89d8cdd64f500560a28dGlenn Kasten    unsigned i;
1494b65ef9efdf5aba01bea89d8cdd64f500560a28dGlenn Kasten    float pi2 = 3.14*2;
1504b65ef9efdf5aba01bea89d8cdd64f500560a28dGlenn Kasten    float hz = 441;
1514b65ef9efdf5aba01bea89d8cdd64f500560a28dGlenn Kasten    float sr = 44100;
1524b65ef9efdf5aba01bea89d8cdd64f500560a28dGlenn Kasten    for (i = 0; i < SINE_FRAMES; ++i) {
1534b65ef9efdf5aba01bea89d8cdd64f500560a28dGlenn Kasten        sine[i].left = sin((float) (i  / (sr / hz)) * pi2 ) * 32000.0;
1544b65ef9efdf5aba01bea89d8cdd64f500560a28dGlenn Kasten        sine[i].right = sine[i].left;
1554b65ef9efdf5aba01bea89d8cdd64f500560a28dGlenn Kasten    }
1564b65ef9efdf5aba01bea89d8cdd64f500560a28dGlenn Kasten    for (i = 0; i < SQUARE_FRAMES; ++i) {
1574b65ef9efdf5aba01bea89d8cdd64f500560a28dGlenn Kasten        square[i].left = (i % (unsigned) (sr / hz)) < 50 ? 32767 : -32768;
1584b65ef9efdf5aba01bea89d8cdd64f500560a28dGlenn Kasten        square[i].right = square[i].left;
1594b65ef9efdf5aba01bea89d8cdd64f500560a28dGlenn Kasten    }
1604b65ef9efdf5aba01bea89d8cdd64f500560a28dGlenn Kasten    for (i = 0; i < SAWTOOTH_FRAMES; ++i) {
1614b65ef9efdf5aba01bea89d8cdd64f500560a28dGlenn Kasten        sawtooth[i].left = ((((int) (i % (unsigned) (sr / hz))) - 50) / 100.0) * 60000.0 - 30000.0;
1624b65ef9efdf5aba01bea89d8cdd64f500560a28dGlenn Kasten        sawtooth[i].right = sawtooth[i].left;
1634b65ef9efdf5aba01bea89d8cdd64f500560a28dGlenn Kasten    }
164f6f5ceb363286d5ebef2c2e70c8a5aa135d5d1eeGlenn Kasten    for (i = 0; i < HALF_FRAMES; ++i) {
165f6f5ceb363286d5ebef2c2e70c8a5aa135d5d1eeGlenn Kasten        half[i].left = sine[i].left;
166f6f5ceb363286d5ebef2c2e70c8a5aa135d5d1eeGlenn Kasten        half[i].right = sawtooth[i].right / 2;
167f6f5ceb363286d5ebef2c2e70c8a5aa135d5d1eeGlenn Kasten    }
1684b65ef9efdf5aba01bea89d8cdd64f500560a28dGlenn Kasten
1694b65ef9efdf5aba01bea89d8cdd64f500560a28dGlenn Kasten    set_conio_terminal_mode();
1704b65ef9efdf5aba01bea89d8cdd64f500560a28dGlenn Kasten    int in_count = 0;
17101f8573bc2a850536b02855d483dfe130c050a2fAshok Bhat    uintptr_t count = 0;
1724b65ef9efdf5aba01bea89d8cdd64f500560a28dGlenn Kasten    for (;;) {
1734b65ef9efdf5aba01bea89d8cdd64f500560a28dGlenn Kasten        usleep(10000);
1744b65ef9efdf5aba01bea89d8cdd64f500560a28dGlenn Kasten        if (kbhit()) {
1754b65ef9efdf5aba01bea89d8cdd64f500560a28dGlenn Kasten            frame_t *buffer;
1764b65ef9efdf5aba01bea89d8cdd64f500560a28dGlenn Kasten            unsigned size;
17701e9f5fa4698856f92bcfd88188ee4c8397b22dbGlenn Kasten            BufferQueueState bufqstate;
1784b65ef9efdf5aba01bea89d8cdd64f500560a28dGlenn Kasten            int ch = getch();
1794b65ef9efdf5aba01bea89d8cdd64f500560a28dGlenn Kasten            switch (ch) {
1804b65ef9efdf5aba01bea89d8cdd64f500560a28dGlenn Kasten            case '0' ... '9':
1814b65ef9efdf5aba01bea89d8cdd64f500560a28dGlenn Kasten                if (in_count) {
1824b65ef9efdf5aba01bea89d8cdd64f500560a28dGlenn Kasten                    count = count * 10 + (ch - '0');
1834b65ef9efdf5aba01bea89d8cdd64f500560a28dGlenn Kasten                } else {
1844b65ef9efdf5aba01bea89d8cdd64f500560a28dGlenn Kasten                    count = ch - '0';
1854b65ef9efdf5aba01bea89d8cdd64f500560a28dGlenn Kasten                    in_count = 1;
1864b65ef9efdf5aba01bea89d8cdd64f500560a28dGlenn Kasten                }
1874b65ef9efdf5aba01bea89d8cdd64f500560a28dGlenn Kasten                continue;
1884b65ef9efdf5aba01bea89d8cdd64f500560a28dGlenn Kasten            case 'i':
1894b65ef9efdf5aba01bea89d8cdd64f500560a28dGlenn Kasten                buffer = sine;
1904b65ef9efdf5aba01bea89d8cdd64f500560a28dGlenn Kasten                size = sizeof(sine);
1914b65ef9efdf5aba01bea89d8cdd64f500560a28dGlenn Kasten                goto enqueue;
1924b65ef9efdf5aba01bea89d8cdd64f500560a28dGlenn Kasten            case 'q':
1934b65ef9efdf5aba01bea89d8cdd64f500560a28dGlenn Kasten                buffer = square;
1944b65ef9efdf5aba01bea89d8cdd64f500560a28dGlenn Kasten                size = sizeof(square);
1954b65ef9efdf5aba01bea89d8cdd64f500560a28dGlenn Kasten                goto enqueue;
196f6f5ceb363286d5ebef2c2e70c8a5aa135d5d1eeGlenn Kasten            case 'h':
197f6f5ceb363286d5ebef2c2e70c8a5aa135d5d1eeGlenn Kasten                buffer = half;
198f6f5ceb363286d5ebef2c2e70c8a5aa135d5d1eeGlenn Kasten                size = sizeof(half);
199f6f5ceb363286d5ebef2c2e70c8a5aa135d5d1eeGlenn Kasten                goto enqueue;
2004b65ef9efdf5aba01bea89d8cdd64f500560a28dGlenn Kasten            case 'r':
2014b65ef9efdf5aba01bea89d8cdd64f500560a28dGlenn Kasten                if (in_count) {
2024b65ef9efdf5aba01bea89d8cdd64f500560a28dGlenn Kasten                    expectedCaller = playerBufferqueue;
2034b65ef9efdf5aba01bea89d8cdd64f500560a28dGlenn Kasten                    expectedContext = (void *) count;
2044b65ef9efdf5aba01bea89d8cdd64f500560a28dGlenn Kasten                } else {
2054b65ef9efdf5aba01bea89d8cdd64f500560a28dGlenn Kasten                    expectedCaller = NULL;
2064b65ef9efdf5aba01bea89d8cdd64f500560a28dGlenn Kasten                    expectedContext = (void *) NULL;
2074b65ef9efdf5aba01bea89d8cdd64f500560a28dGlenn Kasten                }
2084b65ef9efdf5aba01bea89d8cdd64f500560a28dGlenn Kasten                result = (*playerBufferqueue)->RegisterCallback(playerBufferqueue, in_count ?
2094b65ef9efdf5aba01bea89d8cdd64f500560a28dGlenn Kasten                    callback : NULL, expectedContext);
2104b65ef9efdf5aba01bea89d8cdd64f500560a28dGlenn Kasten                checkResult(result);
2114b65ef9efdf5aba01bea89d8cdd64f500560a28dGlenn Kasten                break;
2124b65ef9efdf5aba01bea89d8cdd64f500560a28dGlenn Kasten            case 'a':
2134b65ef9efdf5aba01bea89d8cdd64f500560a28dGlenn Kasten                buffer = sawtooth;
2144b65ef9efdf5aba01bea89d8cdd64f500560a28dGlenn Kasten                size = sizeof(sawtooth);
2154b65ef9efdf5aba01bea89d8cdd64f500560a28dGlenn Kastenenqueue:
2164b65ef9efdf5aba01bea89d8cdd64f500560a28dGlenn Kasten                for (i = 0; i < (in_count ? count : 1); ++i) {
2174b65ef9efdf5aba01bea89d8cdd64f500560a28dGlenn Kasten                    result = (*playerBufferqueue)->Enqueue(playerBufferqueue, buffer, size);
2184b65ef9efdf5aba01bea89d8cdd64f500560a28dGlenn Kasten                    checkResult(result);
2194b65ef9efdf5aba01bea89d8cdd64f500560a28dGlenn Kasten                }
2204b65ef9efdf5aba01bea89d8cdd64f500560a28dGlenn Kasten                break;
2214b65ef9efdf5aba01bea89d8cdd64f500560a28dGlenn Kasten            case 'c':
2224b65ef9efdf5aba01bea89d8cdd64f500560a28dGlenn Kasten                result = (*playerBufferqueue)->Clear(playerBufferqueue);
2234b65ef9efdf5aba01bea89d8cdd64f500560a28dGlenn Kasten                checkResult(result);
2244b65ef9efdf5aba01bea89d8cdd64f500560a28dGlenn Kasten                putchar('\r');
2254b65ef9efdf5aba01bea89d8cdd64f500560a28dGlenn Kasten                result = (*playerBufferqueue)->GetState(playerBufferqueue, &bufqstate);
2264b65ef9efdf5aba01bea89d8cdd64f500560a28dGlenn Kasten                checkResult(result);
2274b65ef9efdf5aba01bea89d8cdd64f500560a28dGlenn Kasten                if (bufqstate.count != 0)
2284b65ef9efdf5aba01bea89d8cdd64f500560a28dGlenn Kasten                    printf("\rcount=%u\r\n", (unsigned) bufqstate.count);
2294b65ef9efdf5aba01bea89d8cdd64f500560a28dGlenn Kasten#if 0
2304b65ef9efdf5aba01bea89d8cdd64f500560a28dGlenn Kasten                putchar('\r');
2314b65ef9efdf5aba01bea89d8cdd64f500560a28dGlenn Kasten                putchar('\n');
2324b65ef9efdf5aba01bea89d8cdd64f500560a28dGlenn Kasten#endif
2334b65ef9efdf5aba01bea89d8cdd64f500560a28dGlenn Kasten                fflush(stdout);
2344b65ef9efdf5aba01bea89d8cdd64f500560a28dGlenn Kasten                break;
2354b65ef9efdf5aba01bea89d8cdd64f500560a28dGlenn Kasten            case 'g':
2364b65ef9efdf5aba01bea89d8cdd64f500560a28dGlenn Kasten                result = (*playerBufferqueue)->GetState(playerBufferqueue, &bufqstate);
2374b65ef9efdf5aba01bea89d8cdd64f500560a28dGlenn Kasten                checkResult(result);
23801e9f5fa4698856f92bcfd88188ee4c8397b22dbGlenn Kasten                printf("\rplayIndex=%u\r\n", (unsigned) bufqstate.INDEX);
2394b65ef9efdf5aba01bea89d8cdd64f500560a28dGlenn Kasten                printf("count=%u\r\n", (unsigned) bufqstate.count);
2404b65ef9efdf5aba01bea89d8cdd64f500560a28dGlenn Kasten                break;
2414b65ef9efdf5aba01bea89d8cdd64f500560a28dGlenn Kasten            case 'p':
2424b65ef9efdf5aba01bea89d8cdd64f500560a28dGlenn Kasten                state = SL_PLAYSTATE_PAUSED;
2434b65ef9efdf5aba01bea89d8cdd64f500560a28dGlenn Kasten                goto setplaystate;
2444b65ef9efdf5aba01bea89d8cdd64f500560a28dGlenn Kasten            case 's':
2454b65ef9efdf5aba01bea89d8cdd64f500560a28dGlenn Kasten                state = SL_PLAYSTATE_STOPPED;
2464b65ef9efdf5aba01bea89d8cdd64f500560a28dGlenn Kasten                goto setplaystate;
2474b65ef9efdf5aba01bea89d8cdd64f500560a28dGlenn Kasten            case 'P':
2484b65ef9efdf5aba01bea89d8cdd64f500560a28dGlenn Kasten                state = SL_PLAYSTATE_PLAYING;
2494b65ef9efdf5aba01bea89d8cdd64f500560a28dGlenn Kastensetplaystate:
2504b65ef9efdf5aba01bea89d8cdd64f500560a28dGlenn Kasten                result = (*playerPlay)->SetPlayState(playerPlay, state);
2514b65ef9efdf5aba01bea89d8cdd64f500560a28dGlenn Kasten                checkResult(result);
2524b65ef9efdf5aba01bea89d8cdd64f500560a28dGlenn Kasten                SLuint32 newstate;
2534b65ef9efdf5aba01bea89d8cdd64f500560a28dGlenn Kasten                result = (*playerPlay)->GetPlayState(playerPlay, &newstate);
2544b65ef9efdf5aba01bea89d8cdd64f500560a28dGlenn Kasten                checkResult(result);
2554b65ef9efdf5aba01bea89d8cdd64f500560a28dGlenn Kasten                if (newstate != state)
2564b65ef9efdf5aba01bea89d8cdd64f500560a28dGlenn Kasten                    printf("\rSetPlayState(%u) -> GetPlayState(%u)\r\n", (unsigned) state,
2574b65ef9efdf5aba01bea89d8cdd64f500560a28dGlenn Kasten                        (unsigned) newstate);
2584b65ef9efdf5aba01bea89d8cdd64f500560a28dGlenn Kasten#if 0
2594b65ef9efdf5aba01bea89d8cdd64f500560a28dGlenn Kasten                putchar('\r');
2604b65ef9efdf5aba01bea89d8cdd64f500560a28dGlenn Kasten                putchar('\n');
2614b65ef9efdf5aba01bea89d8cdd64f500560a28dGlenn Kasten                fflush(stdout);
2624b65ef9efdf5aba01bea89d8cdd64f500560a28dGlenn Kasten#endif
2634b65ef9efdf5aba01bea89d8cdd64f500560a28dGlenn Kasten                checkResult(result);
2644b65ef9efdf5aba01bea89d8cdd64f500560a28dGlenn Kasten                break;
2654b65ef9efdf5aba01bea89d8cdd64f500560a28dGlenn Kasten            case 'x':
2660a058cc3d720cdf3f0f8222472a862258482f34fGlenn Kasten                goto out;
2674b65ef9efdf5aba01bea89d8cdd64f500560a28dGlenn Kasten            default:
2684b65ef9efdf5aba01bea89d8cdd64f500560a28dGlenn Kasten                putchar('?');
2694b65ef9efdf5aba01bea89d8cdd64f500560a28dGlenn Kasten                fflush(stdout);
2704b65ef9efdf5aba01bea89d8cdd64f500560a28dGlenn Kasten                break;
2714b65ef9efdf5aba01bea89d8cdd64f500560a28dGlenn Kasten            }
2724b65ef9efdf5aba01bea89d8cdd64f500560a28dGlenn Kasten            in_count = 0;
2734b65ef9efdf5aba01bea89d8cdd64f500560a28dGlenn Kasten        }
2744b65ef9efdf5aba01bea89d8cdd64f500560a28dGlenn Kasten    }
2754b65ef9efdf5aba01bea89d8cdd64f500560a28dGlenn Kasten
2760a058cc3d720cdf3f0f8222472a862258482f34fGlenn Kastenout:
2770a058cc3d720cdf3f0f8222472a862258482f34fGlenn Kasten    (*playerObject)->Destroy(playerObject);
2780a058cc3d720cdf3f0f8222472a862258482f34fGlenn Kasten    (*outputmixObject)->Destroy(outputmixObject);
2790a058cc3d720cdf3f0f8222472a862258482f34fGlenn Kasten    (*engineObject)->Destroy(engineObject);
2800a058cc3d720cdf3f0f8222472a862258482f34fGlenn Kasten    return EXIT_SUCCESS;
2814b65ef9efdf5aba01bea89d8cdd64f500560a28dGlenn Kasten}
282