-
Notifications
You must be signed in to change notification settings - Fork 0
How it Works
AudioContext allows us to construct GainNodes and OscillatorNodes. GainNodes store the volume and OscillatorNodes store the frequency and waveforms. They are then connected towards the AudioContext instance destination for the sound to be played.
OscillatorNodes are super efficient and they dispose of themselves when they are done playing. The PlayableNote wrapper class would construct OscillatorNodes and GainNodes to play notes. The PlayableNote wrapper class would eventually contain instruments which can be selected.
The PlayableMusic class is a collection of PlayableNotes, which gets help from the MusicPlayer class to time itself correctly. The MusicPlayer class is probably the class you would use most of the methods from, and is how people would most likely interact with the module.
The MusicPlayer class uses AudioContextManager to ensure that the number of AudioContexts is kept to a maximum of 1. It uses the manager to close the AudioContext instance after it is used to play music. The MusicPlayer instance would store an array of PlayableMusic which it can play.
This structure allows us to deal with volume changes, BPM changes, notes which are less than one beat and polyphonic music easily.
The entire song is placed into several strings, making it easier to input. One string would be very cluttered, so each instrument is given different strings. The melody and bass can be separated into different strings.
The strings store what we call as frames, as they are very similar to animation frames. Frames are points in time which at least one new note is pressed or one is released. Each frame stores the new notes, the notes which would be released, and the notes which are sustained.
Songs exist independently of what is playing them, but need their help. MusicPlayer is thus used as a song iterator. More than one song might be playing at a time, but each MusicPlayer should play only one song. It is safe to run multiple MusicPlayers at a time.