musical.js
v0.0.1
Published
An ABC music sequencer and synthesizer for WebAudio.
Downloads
16
Maintainers
Readme
musical.js
musical.js: a tiny library with a sequencing WebAudio synthesizer that supports ABC notation.
Listen to a little demo here. musical.js can be used as a standalone script, a require.js AMD module, or as a node.js module. This code has no dependencies other than the HTML5 WebAudio API, and it minifies down to about 17K.
Originally designed as part of jQuery-turtle.
Three main functions in the API:
instrument = new Instrument([timbre])
makes an instrument. Timbre is optional and defaults to a boring square wave sound. Timbre may be a WebAudio oscillator wave type ("square", "sine", "sawtooth", "triangle"), or a "piano" wave shape that is coded in this libarary. It may also specify (as object properties) gain (generally 0-1), attack (seconds for initial attack), decay (seconds for 1/e decay), sustain (amplitude of sustain), release (seconds for silence after release), cutoff (frequency of a lowapss filter), cutfollow (multiple of main frequency to add to lowpass cutoff), and detune (relative frequency of a second detuned oscillator); these allow basic subtractive analog synthesis. Timbre can be changed later using instrument.setTimbre. See an example below.instrument.tone(frequency [,volume, duration, delay, timbre])
plays a single tone for a little while. Frequency may be specified as a positive number (in Hz) or a negative integer (a midi note number), or a pitch string like '^C,' (ABC notation for a pitch). Other arguments are optional: volume defaults to 1, duration defaults to 10 seconds, delay defaults to zero (play right now), and timbre defaults to null, which applies the instrument's default timbre.instrument.play(abcnotation)
plays a song as expressed in ABC notation, as can be found on the web. See examples below.There is also a
silence()
method andgetTimbre()
andsetTimbre()
for changing an instrument's sound. To listen to sequenced notes as they occur in realtime, listen to events withon('noteon', cb)
andon('noteoff', cb)
. Theinstrument.off
method unregisters a listener.
If used as a require.js or node module, then Instrument will be
a member of the package. For example, after you do
musical = require('musical');
then you can
var ins = new musical.Instrument();
.