Here's a quick, hideous demo.
You can also see (a slightly modified version of) TinyMusic in action in this js13k entry.
Available on npm via
npm install tinymusic.
Version 1.0.0 removes some weirdness around module format.
If you put
dist/TinyMusic.js directly into your page with no
RequireJS/browserify/etc, you'll get a global object called
Otherwise, you should be able to
require('tinymusic') with browserify,
require('./node_modules/tinymusic/dist/TinyMusic.js') with RequireJS.
Getting started is pretty easy. Creating and playing a melody only takes a few lines of code.
// create a new Web Audio API contextvar ac = ;// set the playback tempo (120 beats per minute)var tempo = 120;// create some notes ('<Note Name> <Beat Length>')// q = quarter note, h = half note (more on that later)var note1 = 'G3 q';var note2 = 'E4 q';var note3 = 'C4 h';// create a new sequencevar sequence = ac tempo ;// add the notessequence;// disable loopingsequenceloop = false;// play itsequence;
To eliminate a bit of boilerplate, you can also pass an array of strings to the
Sequence constructor. Here's the last example, tweaked to take advantage of that:
// create a new Web Audio API contextvar ac = ;// set the playback tempo (120 beats per minute)var tempo = 120;// create a new sequencevar sequence = ac tempo'G3 q''E4 q''C4 h';// disable loopingsequenceloop = false;// play itsequence;
Note constructor only acceps one argument: a string that dictates note frequency and duration. An example input string might look like
In that example,
'A4' refers to the frequency 440Hz (more on that here), and
'q' means "quarter note".
To create a rest, you can pass in any "note name" that's not valid. I usually use
'- q' would be a quarter note rest (or one beat of silence for you non-music-types).
The duration component of the string can follow a few different forms:
'w'is a whole note
'h'is a half note
'q'is a quarter note
'e'is an eighth note
's'is a sixteenth note
'es'is a dotted eighth note (eighth plus sixteenth). This works for any combination of the letters above.
'0.0125'is a 32nd note, but any numeric value will work here. So
'2'would be a half note,
'0.5'would be an eighth note, etc.
After the constructor is run,
Note instances have two properties:
frequencyis a number representing the note's frequency in Hz
durationis the ratio of beat length (always 1) to the note's time value.
1would be a quarter note,
0.5a half note, etc.
Sequence constructor accepts 3 arguments, but all are optional:
ac is an
AudioContext instance. If not provided, one will be created -- but you really should provide one.
tempo is a tempo in beats per minute. If not provided, a default tempo of 120 will be set.
arr is an array of
Note instances or strings (like the ones passed to the
Sequence class has a bunch of methods and properties, but only a few are really intended to be "public":
loop property is a boolean value that controls whether or not the sequence will play repeatedly. By default, its value is set to
smoothing property controls portamento (smoothily sliding from one frequency to the next). The
smoothing value should be a number between
0 (no smoothing) to
1 (lots of smoothing). The default value is
staccato property controls "choppiness". With a value of
0, notes will play out for their entire duration. With a value of
1, they'll be harshly truncated. By default, the
staccato value is
gain property is a Web Audio
There's too much to fully cover it here, but basically:
var sequence = ;// the double "gain" isn't a typo. the GainNode instance has a property// called "gain", which has a "value" that you can setsequencegaingainvalue = 05; // half volume
bass property is a Web Audio
If that sounds scary, just know this: it's basically just an EQ slider that controls low-end frequencies (centered around 100Hz by default)
var sequence = ;// give the sequence some sweet, sweet bass (positive values add bass, negative values reduce it)sequencebassgainvalue = 10;
Sequence.bass, but focuses on midrange frequencies (1000Hz by default)
Sequence.bass, but focuses on higher frequencies (2500Hz by default)
waveType property defines what kind of oscillator the sequence will use. By default, oscillators use
'square' waves because they sound totally fucking awesome. You can also use
Creates a custom waveType to use with
Sequence.waveType = 'custom'. This will create and set a periodicWaveForm on the sequencer's OscillatorNode.
imaginary should both be Arrays (they will be converted to Float32Array) and they must be the same length, or the AudioContext will generate an error. If no value is supplied for
real will be duplicated.
var sequence = ac tempo harmony;sequence;// An electric piano/organy (The Doors-ish) soundsequence;// A soft, sine-like wave (a little grittier than plain "sine")sequence;// A bit grittier, sounds a little more like a hornsequence;// Trumpetysequence;
push() method accepts any number of
Note instances (or "note strings" that can be passed to the
Note constructor) and adds them to the sequence.
play() method begins playback of your sequence. It optionally accepts a Web Audio timestamp to determine when playback should begin.
If no timestamp is supplied (or the timestamp is in the past) playback will begin immediately.
stop() method stops all playback immediately.
Here's a more elaborate demo that shows how to play multiple sequences in sync, as well as use gain and EQ:
// create the audio contextvar ac =// get the current Web Audio timestamp (this is when playback should begin)when = accurrentTime// set the tempotempo = 132// initialize some varssequence1sequence2sequence3// create an array of "note strings" that can be passed to a sequencelead ='- e''Bb3 e''A3 e''Bb3 e''G3 e''A3 e''F3 e''G3 e''E3 e''F3 e''G3 e''F3 e''E3 e''F3 e''D3 q''- e''Bb3 s''A3 s''Bb3 e''G3 e''A3 e''G3 e''F3 e''G3 e''E3 e''F3 e''G3 e''F3 e''E3 s''F3 s''E3 e''D3 q'harmony ='- e''D4 e''C4 e''D4 e''Bb3 e''C4 e''A3 e''Bb3 e''G3 e''A3 e''Bb3 e''A3 e''G3 e''A3 e''F3 q''- e''D4 s''C4 s''D4 e''Bb3 e''C4 e''Bb3 e''A3 e''Bb3 e''G3 e''A3 e''Bb3 e''A3 e''G3 s''A3 s''G3 e''F3 q'bass ='D3 q''- h''D3 q''A2 q''- h''A2 q''Bb2 q''- h''Bb2 q''F2 h''A2 h';// create 3 new sequences (one for lead, one for harmony, one for bass)sequence1 = ac tempo lead ;sequence2 = ac tempo harmony ;sequence3 = ac tempo bass ;// set staccato and smoothing values for maximum coolnesssequence1staccato = 055;sequence2staccato = 055;sequence3staccato = 005;sequence3smoothing = 04;// adjust the levels so the bass and harmony aren't too loudsequence1gaingainvalue = 10;sequence2gaingainvalue = 08;sequence3gaingainvalue = 065;// apply EQ settingssequence1midfrequencyvalue = 800;sequence1midgainvalue = 3;sequence2midfrequencyvalue = 1200;sequence3midgainvalue = 3;sequence3bassgainvalue = 6;sequence3bassfrequencyvalue = 80;sequence3midgainvalue = -6;sequence3midfrequencyvalue = 500;sequence3treblegainvalue = -2;sequence3treblefrequencyvalue = 1400;//start the lead part immediatelysequence1;// delay the harmony by 16 beatssequence2;// start the bass part immediatelysequence3;
- Install Node.js
I also have
grunt watch set up to automatically build when you change a source file.
Go for it. I wrote this in an afternoon, it could probably be improved. Pull requests are completely welcome, but please make sure that the test suite passes and that you conform to the Idiomatic.js style guide.
File 'em. I'm happy to fix bugs or answer questions.
I'm probably not gonna look to add many more features though, since this is really intended to be a very small library (people are using it for js13k, so, I mean...).