Skip to content

Instantly share code, notes, and snippets.

@yorung
Last active December 12, 2015 07:25
Show Gist options
  • Select an option

  • Save yorung/beb785bc24227f2765f1 to your computer and use it in GitHub Desktop.

Select an option

Save yorung/beb785bc24227f2765f1 to your computer and use it in GitHub Desktop.
struct WaveFormatEx {
uint16_t tag, channels;
uint32_t samplesPerSecond, averageBytesPerSecond;
uint16_t blockAlign, bitsPerSample;
};
struct WaveContext
{
SLObjectItf playerObject;
SLPlayItf playerPlay;
SLAndroidSimpleBufferQueueItf playerBufferQueue;
void *fileImg;
int enqueuedSize;
bool loop;
};
void Voice::Create(const char* fileName)
{
context = new WaveContext;
memset(context, 0, sizeof(*context));
bool result = false;
result = !!(context->fileImg = LoadFile(fileName));
assert(result);
const WaveFormatEx* wfx = (WaveFormatEx*)RiffFindChunk(context->fileImg, "fmt ");
assert(wfx);
SLDataLocator_AndroidSimpleBufferQueue q = {SL_DATALOCATOR_ANDROIDSIMPLEBUFFERQUEUE, 2};
SLDataFormat_PCM f = {SL_DATAFORMAT_PCM, wfx->channels, wfx->samplesPerSecond * 1000, wfx->bitsPerSample, wfx->bitsPerSample, wfx->channels == 2 ? SL_SPEAKER_FRONT_LEFT | SL_SPEAKER_FRONT_RIGHT : SL_SPEAKER_FRONT_CENTER, SL_BYTEORDER_LITTLEENDIAN};
SLDataSource src = {&q, &f};
SLDataLocator_OutputMix m = {SL_DATALOCATOR_OUTPUTMIX, sl.GetOutputMixObject()};
SLDataSink sink = {&m, nullptr};
SLInterfaceID ids = SL_IID_ANDROIDSIMPLEBUFFERQUEUE;
SLboolean req = SL_BOOLEAN_TRUE;
SLCall(sl.GetEngine(), CreateAudioPlayer, &context->playerObject, &src, &sink, 1, &ids, &req);
SLCall(context->playerObject, Realize, SL_BOOLEAN_FALSE);
SLCall(context->playerObject, GetInterface, SL_IID_ANDROIDSIMPLEBUFFERQUEUE, &context->playerBufferQueue);
SLCall(context->playerObject, GetInterface, SL_IID_PLAY, &context->playerPlay);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment