diff options
author | Tom Yan <tom.ty89@gmail.com> | 2018-07-16 01:22:22 +0800 |
---|---|---|
committer | sfan5 <sfan5@live.de> | 2018-08-05 17:51:45 +0200 |
commit | 8baad91e7b4383d4bf5176b2711614f79446b5f6 (patch) | |
tree | 7bc74459fdd2ab9fe1664865b159cc8bbcf9525a | |
parent | 4e91cb72ef926e6a7276c15f6a24549ff62afba8 (diff) |
ao_opensles: allow s32 and float output
OpenSLES (and its AudioTrack backend) in Android can take 32-bit
fixed and floating point input since Android L (API 21).
-rw-r--r-- | audio/out/ao_opensles.c | 42 |
1 files changed, 15 insertions, 27 deletions
diff --git a/audio/out/ao_opensles.c b/audio/out/ao_opensles.c index ea48de892e..f3082a9aa6 100644 --- a/audio/out/ao_opensles.c +++ b/audio/out/ao_opensles.c @@ -43,12 +43,6 @@ struct priv { int cfg_frames_per_buffer; }; -static const int fmtmap[][2] = { - { AF_FORMAT_U8, SL_PCMSAMPLEFORMAT_FIXED_8 }, - { AF_FORMAT_S16, SL_PCMSAMPLEFORMAT_FIXED_16 }, - { 0 } -}; - #define DESTROY(thing) \ if (p->thing) { \ (*p->thing)->Destroy(p->thing); \ @@ -115,7 +109,7 @@ static int init(struct ao *ao) struct priv *p = ao->priv; SLDataLocator_BufferQueue locator_buffer_queue; SLDataLocator_OutputMix locator_output_mix; - SLDataFormat_PCM pcm; + SLAndroidDataFormat_PCM_EX pcm; SLDataSource audio_source; SLDataSink audio_sink; @@ -131,29 +125,23 @@ static int init(struct ao *ao) locator_buffer_queue.locatorType = SL_DATALOCATOR_BUFFERQUEUE; locator_buffer_queue.numBuffers = 1; - pcm.formatType = SL_DATAFORMAT_PCM; - pcm.numChannels = 2; - - int compatible_formats[AF_FORMAT_COUNT + 1]; - af_get_best_sample_formats(ao->format, compatible_formats); - pcm.bitsPerSample = 0; - for (int i = 0; compatible_formats[i] && !pcm.bitsPerSample; ++i) - for (int j = 0; fmtmap[j][0]; ++j) - if (compatible_formats[i] == fmtmap[j][0]) { - ao->format = fmtmap[j][0]; - pcm.bitsPerSample = fmtmap[j][1]; - break; - } - if (!pcm.bitsPerSample) { - MP_ERR(ao, "Cannot find compatible audio format\n"); - goto error; + if (af_fmt_is_int(ao->format)) { + // Be future-proof + if (af_fmt_to_bytes(ao->format) > 2) + ao->format = AF_FORMAT_S32; + else + ao->format = af_fmt_from_planar(ao->format); + pcm.formatType = SL_DATAFORMAT_PCM; + } else { + ao->format = AF_FORMAT_FLOAT; + pcm.formatType = SL_ANDROID_DATAFORMAT_PCM_EX; + pcm.representation = SL_ANDROID_PCM_REPRESENTATION_FLOAT; } - pcm.containerSize = 8 * af_fmt_to_bytes(ao->format); + pcm.numChannels = ao->channels.num; + pcm.containerSize = pcm.bitsPerSample = 8 * af_fmt_to_bytes(ao->format); pcm.channelMask = SL_SPEAKER_FRONT_LEFT | SL_SPEAKER_FRONT_RIGHT; pcm.endianness = SL_BYTEORDER_LITTLEENDIAN; - - // samplesPerSec is misnamed, actually it's samples per ms - pcm.samplesPerSec = ao->samplerate * 1000; + pcm.sampleRate = ao->samplerate * 1000; if (p->cfg_frames_per_buffer) ao->device_buffer = p->cfg_frames_per_buffer; |