play()¶
At the core of litePlay.js we have play(). This can be simply run as
to play a sound, and stop() to stop it.
We can make changes to this sound by setting the default instrument playing it,
the default was set to piano at the start, but we now changed it to organ. You can see all instruments available here.
Now let's pause the organ and select another instrument:
Note
See? We can select a new instrument and play it in a single line!
However, to stop it, now we should do: lead1.stop();.
We can also, of course, tell litePlay what to play. If the sound is coming from a pitched instrument, we can ask it to play a given pitch,
The pitch symbolic names range from Cm1 (C-1) to G8 (G8), in such a way that the middle C of a piano keyboard is set to C4. The sound is played immediately and lasts indefinitely (although some may decay in intensity over time).
Accidentals are represented by lowercase s for sharp and b for flat notes:
But the system also supports microtonal tunings set from midi values. To get them, just add fractional parts to it. For example:
See more details in here.
Events¶
In litePlay.js we can think of the resulting action of play() in this form is
a musical event. We can define it with five attributes:
-
what: the thing that we play, which may vary, but in the case shown earlier, is the pitch of the sound.
-
howLoud: the level of the sound, which can be set by a numeric value in a scale from 0 to 1.
-
when: the event time, when it is supposed to be happening, in the present case, set in seconds.
-
howLong: how long the sound is to last for, in seconds.
-
onSomething: the thing that will be making the sound, the instrument, such as
organ,violinetc. There are several of these to choose from. Depending on the type of instrument, what can be played may vary. For example, in the case ofdrums, we do not have pitch, but different percussion sounds likesnare,kick, etc.
Event attributes (or parameters) are optional, as we have seen. If we do pass them, defaults are used. It is possible to pass only a few parameters, for example just what; what and howLoud; what, howLoud, and when; as well as what, howLoud,when, and howLong.
Events are passed using a JS list (or array) with attributes in the order listed earlier:
For example,
The top-level play() action can take several events as arguments, such
as
Now we learned that can we work with lists of events, instead of only sending
individual events to play(). There is a particular aspect of this that we
should note, the when attributes of each event will be interpreted in a
certain way.
A simple list of what¶
In this case, the events will be separated by the default howLong for the onSomething being played (set to 1 sec). So we hear the sounds in sequence.
A list of incomplete events¶
In this case, the default for when is 0, immediately, for all events in the list. We hear the sounds starting at the same time, mixed up.
A list of events with when attributes explicitly defined¶
In this case, the timing of events is relative to the time we asked for the event list to be played, with perhaps a very short delay. All events are precisely timed in relation to that. We can decide when these should come in with an exact time.
Silent events¶
For any instrument, we get a silent event by passing a capital O to the what parameter:
Note
Silent events could also be generated by setting the amplitude at zero, however, the intention would not be so clear in the code, as you're asking an instrument to play something.
What if a list of events become an object?