Standard Libary
View the source. Imported by default.
Constants
Note values in Hz, for octaves 0 - 8:
C_0throughC_8C_SHARP_0/D_FLAT_0throughC_SHARP_8/D_FLAT_8D_0throughD_8D_SHARP_0/E_FLAT_0throughD_SHARP_8/E_FLAT_8E_0throughE_8F_0throughF_8F_SHARP_0/G_FLAT_0throughF_SHARP_8/G_FLAT_8G_0throughG_8G_SHARP_0/A_FLAT_0throughG_SHARP_8/A_FLAT_8A_0throughA_8A_SHARP_0/B_FLAT_0throughA_SHARP_8/B_FLAT_8B_0throughB_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
delayseconds. 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
delayseconds. 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()'dto 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()'dto run in the scope ofbuildTrack()sothis.every/this.playetc. 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()'dto 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