SDL
SDL copied to clipboard
"flat audio" when using AAudio low-latency option
Okay, let's go ahead and add this patch for SDL 2.30 and I'll figure out what device was getting flat audio and see if I can improve that hint logic.
Originally posted by @slouken in https://github.com/libsdl-org/SDL/issues/8888#issuecomment-1904938459
Just adding this issue so we remember to look at this later.
Here's the log from the affected system:
10-10 17:52:30.682 19827 19827 V SDL : nativeSetupJNI()
10-10 17:52:30.684 19827 19827 V SDL : AUDIO nativeSetupJNI()
10-10 17:52:30.684 19827 19827 V SDL : CONTROLLER nativeSetupJNI()
10-10 17:52:31.168 19827 19913 I SDL/APP : AAUDIO_Init
10-10 17:52:31.169 19827 19913 I SDL/APP : SDL AAUDIO_Init OK
10-10 17:52:31.169 19827 19913 I SDL/APP : AAUDIO_OpenDevice
10-10 17:52:31.170 19827 19913 I SDL/APP : AAudio Try to open 48000 hz 16 bit chan 2 LE samples 1024
10-10 17:52:31.170 19827 19913 I AAudio : AAudioStreamBuilder_openStream() called ----------------------------------------
10-10 17:52:31.170 19827 19913 I AudioStreamBuilder: rate = 0, channels = 0, channelMask = 0, format = 0, sharing = SH, dir = OUTPUT
10-10 17:52:31.170 19827 19913 I AudioStreamBuilder: device = 0, sessionId = -1, perfMode = 10, callback: ON with frames = 0
10-10 17:52:31.170 19827 19913 I AudioStreamBuilder: usage = 0, contentType = 0, inputPreset = 0, allowedCapturePolicy = 0
10-10 17:52:31.170 19827 19913 I AudioStreamBuilder: privacy sensitive = false, opPackageName = (null), attributionTag = (null)
10-10 17:52:31.177 19827 19913 D AudioStreamBuilder: build() MMAP not used because AAUDIO_PERFORMANCE_MODE_LOW_LATENCY not requested.
10-10 17:52:31.178 19827 19913 D AudioStreamTrack: open(), request notificationFrames = 0, frameCount = 0
10-10 17:52:31.187 5096 18689 V APM_AudioPolicyManager: getOutputForAttrInt() attributes={ Content type: AUDIO_CONTENT_TYPE_MUSIC Usage: AUDIO_USAGE_MEDIA Source: AUDIO_SOURCE_DEFAULT Flags: 0x0 Tags: } stream=AUDIO_STREAM_MUSIC session 100337 selectedDeviceId 0
10-10 17:52:31.188 5096 18689 I APM_AudioPolicyManager: selectOutputFlags: remove ULL flag for dolby
10-10 17:52:31.188 5096 18689 V APM_AudioPolicyManager: getOutputForAttrInt() device {AUDIO_DEVICE_OUT_SPEAKER, @:}, sampling rate 0, format 0x5, channel mask 0x3, flags 0x8 stream AUDIO_STREAM_MUSIC
10-10 17:52:31.188 5096 18689 V APM_AudioPolicyManager: selectOutput new bestOutput 13 criteria 0 100 0 0 0 1 2 2
10-10 17:52:31.188 5096 18689 V APM_AudioPolicyManager: getOutputForAttrInt returns output 13 selectedDeviceId 2
10-10 17:52:31.188 5096 18689 V APM_AudioPolicyManager: getOutputForAttr() returns output 13 requestedPortId 0 selectedDeviceId 2 for port ID 14120
10-10 17:52:31.189 5096 18689 I AudioFlinger: Find game Uid 10764, change usage
10-10 17:52:31.189 5096 18689 I AudioFlinger: nonSecFlag 0, secFlag 0, outputFlags a
10-10 17:52:31.189 5096 18689 D AudioFlinger: Client defaulted notificationFrames to 1282 for frameCount 3848
10-10 17:52:31.192 5096 18689 D AF::TrackHandle: OpPlayAudio: track:10711 usage:14 not muted
10-10 17:52:31.197 19827 19913 D AudioTrack: setVolume(1.000000, 1.000000) pid : 19827
10-10 17:52:31.197 19827 19913 D AAudioStream: setState(s#1) from 0 to 2
10-10 17:52:31.200 19827 19913 I AAudio : AAudioStreamBuilder_openStream() returns 0 = AAUDIO_OK for s#1 ----------------
10-10 17:52:31.200 19827 19913 I SDL/APP : AAudio got AAUDIO_FORMAT_PCM_FLOAT
10-10 17:52:31.200 19827 19913 I SDL/APP : AAudio Actually opened 48000 hz 32 bit chan 2 LE samples 1924, buffers 2
10-10 17:52:31.200 19827 19913 D AAudio : AAudioStream_requestStart(s#1) called --------------
10-10 17:52:31.200 19827 19913 D AAudioStream: setState(s#1) from 2 to 3
10-10 17:52:31.201 5096 18689 I AudioPolicyEffectException: MySpaceHandler::checkDetachCondition media 1, ring 0, mp 0
10-10 17:52:31.201 5096 18689 I AudioPolicyEffectException: MySpace detach value 1
10-10 17:52:31.201 5096 18689 V APM_AudioPolicyManager: startOutput portId 14120
10-10 17:52:31.202 5096 18689 V APM_AudioPolicyManager: startOutput() output 13, stream 3, session 100337
10-10 17:52:31.202 5096 18689 V APM_AudioPolicyManager: setBeaconMute(1) mBeaconMuteRefCount=9 mBeaconPlayingRefCount=0
10-10 17:52:31.202 5096 18689 V APM_AudioPolicyManager: selectOutputForMusicEffects activeOnly 1 output 13 flags 0x0000000a
10-10 17:52:31.202 5096 18689 V APM_AudioPolicyManager: selectOutputForMusicEffects selected output 13
10-10 17:52:31.203 5096 18689 V APM_AudioPolicyManager: getNewOutputDevices selected devices {AUDIO_DEVICE_OUT_SPEAKER, @:}
10-10 17:52:31.203 5096 18689 V APM_AudioPolicyManager: setOutputDevices device {AUDIO_DEVICE_OUT_SPEAKER, @:} delayMs 0
10-10 17:52:31.203 5096 18689 V APM_AudioPolicyManager: setOutputDevices() prevDevice {AUDIO_DEVICE_OUT_SPEAKER, @:}
10-10 17:52:31.203 5096 18689 V APM_AudioPolicyManager: setOutputDevices: suppressing checkDeviceMuteStrategies
10-10 17:52:31.203 5096 18689 V APM_AudioPolicyManager: setOutputDevices setting same device {AUDIO_DEVICE_OUT_SPEAKER, @:} or null device, force=0, patch handle=14080
10-10 17:52:31.203 5096 18689 V APM_AudioPolicyManager: computeVolume: stream 3, index 12, volume 0.316228, deviceTypes 0x2
10-10 17:52:31.204 5096 18689 V APM_AudioPolicyManager: computeVolume: stream 3, index 13, volume 0.446684, deviceTypes 0x2
10-10 17:52:31.207 5096 18689 I AudioFlinger: updateStageIndex new -1309
10-10 17:52:31.208 19827 19913 D AAudio : AAudioStream_requestStart(s#1) returned 0 ---------
10-10 17:52:31.208 19827 19855 D AudioStreamLegacy: onAudioDeviceUpdate(deviceId = 2)
10-10 17:52:31.208 19827 19913 I SDL/APP : SDL AAudioStream_requestStart OK
10-10 17:52:31.209 5096 5330 I AudioSAEffectSecPlus: command EFFECT_CMD_SET_VOLUME left = 0.316228, right =0.316228
10-10 17:52:31.210 5008 4428 D audio_hw_primary: primary_out-out_update_source_metadata: called, but not implemented yet
10-10 17:52:31.210 5008 4428 D audio_hw_primary: primary_out-out_update_source_metadata: This stream has 1 tracks
10-10 17:52:31.210 5008 4428 D audio_hw_primary: 1 Track has Usage(14), Content Type(2), Gain(0.000000)
10-10 17:52:31.211 5354 5354 D AudioPlayerStateMonitor: Found a new active media playback. AudioPlaybackConfiguration piid:77687 deviceId:2 type:AAudio u/pid:10764/19827 state:started attr:AudioAttributes: usage=USAGE_MEDIA content=CONTENT_TYPE_UNKNOWN flags=0x0 tags= bundle=null sessionId:-1
I'm not sure how to detect this case though?
Hi, I ran into this issue with SDL2 release-2.30.0 which don't seems to have the hint patch. My device is a Samsung Galaxy S29+ which I believe could handle low latency audio. I read AAudio documentation and I think we might use it incorrectly:
In the current version of AAudio, in order to achieve the lowest possible latency you must use the AAUDIO_PERFORMANCE_MODE_LOW_LATENCY performance mode along with a high-priority callback.
In SDL2 we use AAudioStream_write which is fine for AAUDIO_PERFORMANCE_MODE_NONE
but maybe not for AAUDIO_PERFORMANCE_MODE_LOW_LATENCY
.
In this case I'm pretty sure we should use an high priority callback using AAudioStreamBuilder_setDataCallback.
I would be happy to contribute and fix it myself but my knowledge of SDL2 backend is very limited.
SDL3 uses a high performance callback and has the same issue.
If you have a device that can reproduce this and you can record the sound in some way, I'd like to understand what "flat audio" sounds like exactly.
The volume is normalized, be warned that it could sound very loudly! Android_good_vs_bad_audio_sine440.zip I recorded it using a microphone, first part is how it should sound (sine wave at 440Hz) and the second is how it actually sound. I tried to increase and decrease chunk size in Mix_OpenAudio call but it didn't changed much.
This problem exists on Huawei Matepad 11, after revert https://github.com/libsdl-org/SDL/commit/8367b00e8c816a7878060ef9479e2e1482ec9693 and manually remove AAUDIO_PERFORMANCE_MODE_LOW_LATENCY
, this "flat audio" is reported has fixed.
diff --git a/src/audio/aaudio/SDL_aaudio.c b/src/audio/aaudio/SDL_aaudio.c
index 401fef6a8..3c599db8a 100644
--- a/src/audio/aaudio/SDL_aaudio.c
+++ b/src/audio/aaudio/SDL_aaudio.c
@@ -239,7 +239,6 @@ static int RebuildAAudioStream(SDL_AudioDevice *device)
}
ctx.AAudioStreamBuilder_setErrorCallback(ctx.builder, aaudio_errorCallback, hidden);
- ctx.AAudioStreamBuilder_setPerformanceMode(ctx.builder, AAUDIO_PERFORMANCE_MODE_LOW_LATENCY);
LOGI("AAudio Try to reopen %u hz %u bit chan %u %s samples %u",
device->spec.freq, SDL_AUDIO_BITSIZE(device->spec.format),