Standard Libary
View the source. Imported by default.
Constants
Note values in Hz, for octaves 0 - 8:
C_0
throughC_8
C_SHARP_0
/D_FLAT_0
throughC_SHARP_8
/D_FLAT_8
D_0
throughD_8
D_SHARP_0
/E_FLAT_0
throughD_SHARP_8
/E_FLAT_8
E_0
throughE_8
F_0
throughF_8
F_SHARP_0
/G_FLAT_0
throughF_SHARP_8
/G_FLAT_8
G_0
throughG_8
G_SHARP_0
/A_FLAT_0
throughG_SHARP_8
/A_FLAT_8
A_0
throughA_8
A_SHARP_0
/B_FLAT_0
throughA_SHARP_8
/B_FLAT_8
B_0
throughB_8
There are more in the source. You may be smarter than me and already know this, but flats and sharps are the same note.
Usage
A_4 // Returns 440.0
Frequency
Frequency.noteNumber(number)
Arguments
A MIDI note number, typically from 0 - 127.
Returns
A frequency value in Hz.
Usage
Frequency.makeNote(69) // Returns 440.0
Global Functions
setTimeout(callback, delay)
Call function after delay
seconds (not ms). Callbacks preempt audio rendering, allowing you to update your environment with sample-level resolution.
Arguments
- The callback function to call after
delay
seconds. Takes no arguments, and any returned value will be ignored. - A delay value in seconds (not ms).
Returns
A number identifying this timeout, which can be passed to clearInterval().
setInterval(callback, delay)
Call function every delay
seconds (not ms). Callbacks preempt audio rendering, allowing you to update your environment with sample-level resolution.
Arguments
- The callback function to call every
delay
seconds. Takes no arguments, and any returned value will be ignored. - A delay value in seconds (not ms).
Returns
A number identifying this interval, which can be passed to clearInterval()
.
clearInterval(id)
Arguments
An ID returned by a previous call to setInterval
or setTimeout
.
Returns
Nothing
buildTrack Scope
Methods available to the body of your buildTrack
function.
this.play(mixable)
Arguments
Any mixable function.
Returns
Nothing.
this.until(duration, callback)
You can internalize this as "do until." Run callback
immediately, but clear any nested timers after duration
seconds.
Arguments
- Duration in seconds, or a number converted to beats.
- Callback function without arguments. Any return value will be ignored. The function is
apply()'d
to run in the scope ofbuildTrack()
sothis.every()
/this.play()
etc. still resolve.
Returns
Chained until
calls will run sequentially.
Usage
// JavaScript
var buildTrack = function(){
this.until((3).beats(), function(){
this.every((1).beat(), function(){
this.play( function(t){ return Math.sin(TWO_PI * A_4 * t); } );
});
// Note the chained until() call:
}).until((2).beats(), function(){
this.every((1).beat(), function(){
this.play( function(t){ return Math.sin(TWO_PI * A_5 * t); } );
});
});
}
// CoffeeScript
buildTrack = ->
@until 3.beats(), ->
@every 1.beat(), ->
@play (t) -> Math.sin(TWO_PI * A_4 * t)
# Note the chained until() call:
.until 2.beats(), ->
@every 1.beat(), ->
@play (t) -> Math.sin(TWO_PI * A_5 * t)
this.every(interval, callback)
Run callback immediately, and every interval
seconds.
Arguments
- Interval in seconds, or a number converted to beats.
- Callback function without arguments. Any return value will be ignored. The function is
apply()'d
to run in the scope ofbuildTrack()
sothis.every
/this.play
etc. still resolve.
Returns
Nothing.
Usage
// JavaScript
var buildTrack = function(){
this.until((3).beats(), function(){
this.every((1).beat(), function(){
this.play( function(t){ return Math.sin(TWO_PI * A_4 * t); } );
});
}).until((2).beats(), function(){
this.every((1).beat(), function(){
this.play( function(t){ return Math.sin(TWO_PI * A_5 * t); } );
});
});
}
// CoffeeScript
buildTrack = ->
@until 3.beats(), ->
@every 1.beat(), ->
@play (t) -> Math.sin(TWO_PI * A_4 * t)
.until 2.beats(), ->
@every 1.beat(), ->
@play (t) -> Math.sin(TWO_PI * A_5 * t)
this.after(delay, callback)
You can internalize this as "do after." Run callback after delay
seconds.
Arguments
- Delay before running callback, in seconds.
- Callback function without arguments. Any return value will be ignored. The function is
apply()'d
to run in the scope ofbuildTrack()
sothis.every()
/this.play()
etc. still resolve.
Returns
Nothing.
Usage
// JavaScript
var buildTrack = function(){
this.until((1).beat(), function(){
var o = new Oscillator;
var e = new Envelope;
this.play( e.process(o) );
});
this.after((3).beats(), function(){
this.every((1).beat(), function(){
var o = new Oscillator;
var e = new Envelope;
this.play( e.process(o) );
});
});
}
// CoffeeScript
buildTrack = ->
@until 1.beat(), ->
o = new Oscillator
e = new Envelope
@play e.process(o)
@after 3.beats(), ->
@every 1.beat(), ->
o = new Oscillator
e = new Envelope
@play e.process(o)
this.setBPM(bpm)
Set the master BPM. When you adjust BPM, any timers created using relative beat values (e.g. 1.beat()
) will be automatically updated to use the new BPM.
Arguments
BPM value to use for calls to Number.beat()
/ Number.beats()
.
Returns
Nothing.
Usage
// JavaScript
require('v1/instruments');
var buildTrack = function(){
var bpm = 100;
this.setBPM(bpm);
this.every((1).beat(), function(){
bpm += 5;
this.setBPM(bpm);
});
this.every((2).beats(), function(){
var o = new Oscillator;
var e = new Envelope;
this.play(e.process(o));
});
});
# CoffeeScript
require 'v1/instruments'
buildTrack = ->
bpm = 100
@setBPM bpm
@every 1.beat(), ->
bpm += 5
@setBPM(bpm)
@every 2.beats(), ->
o = new Oscillator
e = new Envelope
@play e.process(o)
this.setMasterGain(gain)
Arguments
- A gain value in dB. Default -7.0.
Returns
Nothing.
Core Extensions
Number.beat() / Number.beats()
Returns
A value in seconds, which can be "refreshed" if BPM changes.
Usage
TinyRave.setBPM(100)
var length = 100.beats(); // Returns 1
TinyRave.setBPM(200)
length = length.beats(); // Returns 0.5