Join GitHub today
GitHub is home to over 31 million developers working together to host and review code, manage projects, and build software together.Sign up
The once and future audio engine (notes on a proposed re-design) #74
We will probably need to re-design the audio engine. This is mainly to separate the life cycle of a SoundPlayer from that of a clone. In 2.0, a sound started by a clone continues after the clone has been deleted (see LLK/scratch-vm#949), so the re-design is required to support that behavior.
@thisandagain and I discussed a possible new design for the audio engine, which I wanted to document here, because this work is likely to be deferred for another few months.
TL;DR: AudioEngine manages a set of persistent SoundPlayers, one for each sound
A target (sprite or clone) needs to be able to:
The requirements above are currently being met, but this one is not:
Current Audio Engine Design
Here's the current (April 2018) setup of the audio engine:
A sprite and its clone share a single activeSoundPlayers list, which is used to
The idea is to reduce the system down to just an AudioEngine that manages a set of SoundPlayers. Each sound created in Scratch is associated with a single persistent SoundPlayer object.
The Audio Engine creates and deletes these SoundPlayers to stay in sync with the VM.
Each SoundPlayer would know if it is currently playing, and when played, restart itself if it is already playing.
The Audio Engine can update a SoundPlayer's parameters (volume, pitch, pan) when the sound starts or while it plays.
When a sound is started by a target, the VM would use the parameters for that target to update the SoundPlayer.
When the VM updates sound parameters for a target (e.g. using a "set volume" block), the AudioEngine will need a way to update those parameters on all the SoundPlayers currently being played by that target. How should this work? Two ideas:
The other big question is how this will affect other users of the AudioEngine, such as the sound library. Hopefully the changes to keep things working would be small.