Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Audio delays and performance issues in Safari on iOS/iPadOS #18

Closed
morgant opened this issue Jan 10, 2022 · 4 comments
Closed

Audio delays and performance issues in Safari on iOS/iPadOS #18

morgant opened this issue Jan 10, 2022 · 4 comments
Assignees
Labels

Comments

@morgant
Copy link
Collaborator

morgant commented Jan 10, 2022

When used in Safari on iPadOS (14.x) with the Apple Magic Keyboard, the audio playback stutters and causes general performance issues.

Looking at keyzen.js, this may be due to the fact that the Audio element is created, loaded, and then played every time keyHandler() or level_up() is called (see

(new Audio("click.wav")).play();
,
(new Audio("clack.wav")).play();
, and
(new Audio('ding.wav')).play();
). I believe that Safari on iOS/iPadOS doesn't cache media files, so it may be re-fetching the .wavs on every instantiation of Audio.

I'd suggest starting by instantiating the three Audio elements only once and re-using them. If that doesn't work, migrating to the Web Audio API would probably fix it as they all could be loaded under one AudioContext.

@morgant morgant added the bug label Jan 10, 2022
@morgant morgant self-assigned this Jan 10, 2022
@morgant
Copy link
Collaborator Author

morgant commented Jan 10, 2022

Looks like PR #2 from way back in the day would have addressed this, but was closed. I'll clean up and submit a new PR.

@morgant
Copy link
Collaborator Author

morgant commented Jan 11, 2022

I did a quick test of instantiating the sounds once, but upon testing I realized why it wasn't implemented that way: when typing quickly, the same sound will not be played for every key press as it may still/already be playing. It makes it sound/feel like it's not properly registering key presses.

So, I think that migrating to the Web Audio API is probably the best bet.

morgant added a commit to morgant/keyzen that referenced this issue Jan 11, 2022
@morgant
Copy link
Collaborator Author

morgant commented Jan 11, 2022

I have created an implementation using the Web Audio API and have tested in Chrome on desktop and Safari on iPadOS (14.x) and it completely resolves the stuttering audio & performance issues.

There's some minor fallout, including:

It would be very easy to fall back to the Audio media element playback implementation if the newer browser support is an issue for anyone.

I'll create a PR for review before I merge. Feedback is definitely welcome.

morgant added a commit that referenced this issue Feb 17, 2022
Use Web Audio API to instantiate samples once and play performantly. Issue #18
@morgant
Copy link
Collaborator Author

morgant commented Feb 17, 2022

PR #19 has been merged, so this should be all set.

@morgant morgant closed this as completed Feb 17, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

1 participant