Join GitHub today
GitHub is home to over 40 million developers working together to host and review code, manage projects, and build software together.Sign up
Add onended callback to p5.SoundFile.prototype.play #78
See this thread on the Processing forum. It seems like a reasonable suggestion to allow passing of a callback to the play method, that gets called when the sound file finishes playing.
I just tested the feasibility of this locally (simply replacing the separate arguments in p5.SoundFile.prototype.play with an object) and it seems relatively trivial. Of course you may well have deliberately chosen to separate the arguments. I don't know what's considered best practice with argument overloading, but presumably you could just check if the last arg is a function and assume a callback and then call that from this.bufferSourceNode.onended...
Great suggestion, @blindfish3! Thank you for looking into it and bringing the issue here.
I'd lean towards adding a separate method to schedule this callback, like
Let me know if you'd like to work on this, or I can, and we can run it by the Processing forum for feedback
Using objects as arguments seems a fairly standard technique for dealing with large numbers of method arguments in JS - e.g. it makes it easy to override one default without having to provide all those that come before it in the argument list (and also gets around the fact that JS only supports overloading the number of args and not by arg type).
This is one reason I feel a little uneasy with p5js's apparent attempts to duplicate Processing syntax: it means you're missing opportunities to implement good practice afforded by JS's more flexible approach to Object creation. Anyway that's a separate topic for discussion (but one well worth having)...
If I had the time I'd commit to implementing this feature; but in practice I suspect it may happen more quickly if it's done at your end. My 2 year old daughter's demands mean that the time I have for personal dev work is sporadic and very limited :/
ok! I've got it working, and this will be a very useful feature.
I think you're right about the options object, and other opportunities to take introduce p5 users to JS rather than just sticking to the Processing syntax. It would have a lot of benefits.
I'd want to do this as part of a larger effort so that it's consistent with other p5.sound methods, and ideally with p5.js in general.
Let's continue the discussion on the p5 github.