SDL_openslES.c: Detect float support in runtime and use it Allow using of the float output type on newer Android devices, but keep PCM16 output on older Closes #5358
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
diff --git a/src/audio/openslES/SDL_openslES.c b/src/audio/openslES/SDL_openslES.c
index e0c732b..39d64b2 100644
--- a/src/audio/openslES/SDL_openslES.c
+++ b/src/audio/openslES/SDL_openslES.c
@@ -407,6 +407,7 @@ openslES_CreatePCMPlayer(_THIS)
{
struct SDL_PrivateAudioData *audiodata = this->hidden;
SLDataFormat_PCM format_pcm;
+ SLAndroidDataFormat_PCM_EX format_pcm_ex;
SLresult result;
int i;
@@ -414,30 +415,30 @@ openslES_CreatePCMPlayer(_THIS)
it can be done as described here:
https://developer.android.com/ndk/guides/audio/opensl/android-extensions.html#floating-point
*/
-#if 1
- /* Just go with signed 16-bit audio as it's the most compatible */
- this->spec.format = AUDIO_S16SYS;
-#else
- SDL_AudioFormat test_format;
- for (test_format = SDL_FirstAudioFormat(this->spec.format); test_format; test_format = SDL_NextAudioFormat()) {
- if (SDL_AUDIO_ISSIGNED(test_format) && SDL_AUDIO_ISINT(test_format)) {
- break;
+ if(SDL_GetAndroidSDKVersion() >= 21) {
+ SDL_AudioFormat test_format;
+ for (test_format = SDL_FirstAudioFormat(this->spec.format); test_format; test_format = SDL_NextAudioFormat()) {
+ if (SDL_AUDIO_ISSIGNED(test_format)) {
+ break;
+ }
}
- }
- if (!test_format) {
- /* Didn't find a compatible format : */
- LOGI( "No compatible audio format, using signed 16-bit audio" );
- test_format = AUDIO_S16SYS;
+ if (!test_format) {
+ /* Didn't find a compatible format : */
+ LOGI( "No compatible audio format, using signed 16-bit audio" );
+ test_format = AUDIO_S16SYS;
+ }
+ this->spec.format = test_format;
+ } else {
+ /* Just go with signed 16-bit audio as it's the most compatible */
+ this->spec.format = AUDIO_S16SYS;
}
- this->spec.format = test_format;
-#endif
/* Update the fragment size as size in bytes */
SDL_CalculateAudioSpec(&this->spec);
- LOGI("Try to open %u hz %u bit chan %u %s samples %u",
- this->spec.freq, SDL_AUDIO_BITSIZE(this->spec.format),
+ LOGI("Try to open %u hz %s %u bit chan %u %s samples %u",
+ this->spec.freq, SDL_AUDIO_ISFLOAT(this->spec.format) ? "float" : "pcm", SDL_AUDIO_BITSIZE(this->spec.format),
this->spec.channels, (this->spec.format & 0x1000) ? "BE" : "LE", this->spec.samples);
/* configure audio source */
@@ -488,7 +489,19 @@ openslES_CreatePCMPlayer(_THIS)
break;
}
- SLDataSource audioSrc = { &loc_bufq, &format_pcm };
+ if(SDL_AUDIO_ISFLOAT(this->spec.format)) {
+ /* Copy all setup into PCM EX structure */
+ format_pcm_ex.formatType = SL_ANDROID_DATAFORMAT_PCM_EX;
+ format_pcm_ex.endianness = format_pcm.endianness;
+ format_pcm_ex.channelMask = format_pcm.channelMask;
+ format_pcm_ex.numChannels = format_pcm.numChannels;
+ format_pcm_ex.sampleRate = format_pcm.samplesPerSec;
+ format_pcm_ex.bitsPerSample = format_pcm.bitsPerSample;
+ format_pcm_ex.containerSize = format_pcm.containerSize;
+ format_pcm_ex.representation = SL_ANDROID_PCM_REPRESENTATION_FLOAT;
+ }
+
+ SLDataSource audioSrc = { &loc_bufq, SDL_AUDIO_ISFLOAT(this->spec.format) ? (void*)&format_pcm_ex : (void*)&format_pcm };
/* configure audio sink */
SLDataLocator_OutputMix loc_outmix = { SL_DATALOCATOR_OUTPUTMIX, outputMixObject };