-
-
Notifications
You must be signed in to change notification settings - Fork 682
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
resume audio context [WIP] #283
Conversation
Hey @therewasaguy , thanks for doing this! Seems like a reasonable approach, but I'm slightly hesitant about that code example you gave. It would be quite bothersome for any developer (will every single one of our examples, even those in the documentation, need to have this?), and quite a high barrier in situations where we might want to introduce p5-sound to someone new to programming. Imagine having to explain (1) The AudioContext, (2) How to check if it's already running, (3) How to check for a touch event, all before they get their hands on anything fun. I wonder what the alternative would be like, maybe if we tried to encapsulate the whole context-starting issue into a function setup() {
userInitSound(); // This is a blocking statement, waits until the user has touched the screen to start
} Which could place a click-to-start menu over the entire canvas, blocking execution of the sketch until after the user has clicked in. I feel like this is quite an acceptable UI convention, for example: Of course, we would need to handle This would save the amount of boilerplate necessary to get started, and as far as I can see the only downside (compared to the exposed alternative) is that the default touch-to-start menu might not be in the style that the developer wants. In which case the exposed version would still be an option for users who wish to customize the interaction. What do you think? |
On one hand, I think it’s important to keep exposed two aspects fo the issue:
and that is what @therewasaguy 's proposal does with function touchStarted() {
startAudioContext();
} I like this because it really does seem to me that resuming audio contexts is a part of web audio life starting now, and that it’s the dev’s responsibility to structure sketches exactly like the codepen embed example with the blocking statement, and new programmers ought to learn about this newly imposed convention. That being said, I'm also in favor for abstracting away multiple lines of code that I would have to write out for every single sketch, so... Something like Yotam Mann's startAudioContext https://github.com/tambien/StartAudioContext seems like a nice compromise between both of your proposals. Plus I really like that it removes event listeners when resumed. I can easily see this sort of thing implemented in p5 and achieved using one function such as startAudioContext() or indeed userInitSound(), with optional arguments: StartAudioContext(AudioContext, (Elements), (Callback)) => Promise I personally think it’s more in the spirit of p5 to have devs make their own visual block or alert, rather than relying on a pre-built one. with startAudioContext you can define a callback to easily do that, while also eliminating the need of checking the audiocontext state. |
good points all! Some good news: The changes have been rolled back in Chrome 66, and are now postponed until Chrome 70 (October 2018). Definitely want to get this resolved soon, though! @JunShern I like the idea of a library default, but many users will include p5.sound without actually using it, so it might get in the way. The CodePen example is good inspiration for our documentation, though—maybe we can put something like this in a shared library that we use in all example sketches without distracting from the primary goal of each sketch.
This is sort of possible with async/await, as long as I do like Yotam Mann's StartAudioContext, @datramt. Maybe we should just add that as a dependency, perhaps with a wrapper so that users don't need to think about the audio context. I like how it returns a Promise and offers a callback. With the callback, I think this could be used in p5's |
I'm going to change this PR to just be the refactor-y stuff, and will resume the "resume AudioContext" stuff in another PR. It'll help to have Chrome Canary version 70 (they're currently at 69). Or, if someone happens to have a copy of Chrome 66 with the sneak peak at their upcoming autoplay policy, lmk! |
var audioContext = require('audiocontext');
Add an example of how it would work
...but
if (getAudioContext.state !== 'running')
is a bit clunky. It might make more sense to expose a wrapper for the audio context that returns a boolean forrunning
, rather than adding methods likegetAudioContext
startAudioContext
etc