Skip to content

Make the player's output sample rate configurable - #146

Open
MathieuDubart wants to merge 1 commit into
dimitris-c:mainfrom
MathieuDubart:feature/configurable-output-sample-rate
Open

MathieuDubart wants to merge 1 commit into
dimitris-c:mainfrom
MathieuDubart:feature/configurable-output-sample-rate

Conversation

@MathieuDubart

Copy link
Copy Markdown

outputAudioFormat was pinned to 44100 Hz, so decoded audio was always converted to that rate before reaching the engine. When the output hardware runs at another rate the system then resamples a second time, and a 48 kHz source makes a full round trip for nothing: down to 44100 by the converter and back up to 48000 by the output unit. On iOS the built-in speaker runs at 48 kHz, so this affects the default route on current devices.

Add outputSampleRate to AudioPlayerConfiguration, defaulting to 44100 so existing behaviour and every existing call site are unchanged. Callers that know their hardware rate — on iOS, AVAudioSession.sharedInstance().sampleRate — can now match it and drop one conversion. The per-entry converter is created from the source format to the output format already, so nothing else needs to change.

normalizeValues() clamps a non-positive rate to the default, which is what makes the AVAudioFormat initialiser in AudioPlayer safe. outputAudioFormat also becomes a let, as it was never reassigned.

`outputAudioFormat` was pinned to 44100 Hz, so decoded audio was always
converted to that rate before reaching the engine. When the output hardware
runs at another rate the system then resamples a second time, and a 48 kHz
source makes a full round trip for nothing: down to 44100 by the converter and
back up to 48000 by the output unit. On iOS the built-in speaker runs at
48 kHz, so this affects the default route on current devices.

Add `outputSampleRate` to `AudioPlayerConfiguration`, defaulting to 44100 so
existing behaviour and every existing call site are unchanged. Callers that
know their hardware rate — on iOS, `AVAudioSession.sharedInstance().sampleRate`
— can now match it and drop one conversion. The per-entry converter is created
from the source format to the output format already, so nothing else needs to
change.

`normalizeValues()` clamps a non-positive rate to the default, which is what
makes the `AVAudioFormat` initialiser in `AudioPlayer` safe. `outputAudioFormat`
also becomes a `let`, as it was never reassigned.
@dimitris-c

Copy link
Copy Markdown
Owner

Hey, thanks for raising this, I need to double check this as it might cause incorrect progress for files that at 44.1k but the output is specified as 48k - most likely there will be more places to include the new output...
I think the play method(s) would also need to get the correct format, will need to check

@MathieuDubart

Copy link
Copy Markdown
Author

hey, thanks for your quick repsonse and for checking this :)
If you see some things to patch/modify during your double check you want me to do, don't hesitate to tell!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants