nodesynth
Version:
Library for audio synthesis
116 lines (93 loc) • 4.03 kB
JavaScript
// Generated by CoffeeScript 1.10.0
(function() {
var Common, Note, chromaticInterval, chromaticNotes, i, interval, intervalMap, j, k, l, len, len1, len2, m, n, name, names,
bind = function(fn, me){ return function(){ return fn.apply(me, arguments); }; },
extend = function(child, parent) { for (var key in parent) { if (hasProp.call(parent, key)) child[key] = parent[key]; } function ctor() { this.constructor = child; } ctor.prototype = parent.prototype; child.prototype = new ctor(); child.__super__ = parent.prototype; return child; },
hasProp = {}.hasOwnProperty;
chromaticInterval = Math.pow(2, 1.0 / 12.0);
chromaticNotes = ['C', 'C#', 'D', 'D#', 'E', 'F', 'F#', 'G', 'G#', 'A', 'A#', 'B'];
intervalMap = [['perfect_unison', 'unison', 'P1', 'diminished_second', 'd2'], ['minor_second', 'm2', 'augmented_unison', 'A1', 'semitone', 'S'], ['major_second', 'M2', 'diminished_third', 'd3', 'tone', 'whole_tone', 'T'], ['minor_third', 'm3', 'augmented_second', 'A2'], ['major_third', 'M3', 'diminished_fourth', 'd4'], ['perfect_fourth', 'fourth', 'P4', 'augmented_third', 'A3'], ['tritone', 'diminished_fifth', 'd5', 'augmented_fourth', 'A4', 'TT'], ['perfect_fifth', 'fifth', 'P5', 'diminished_sixth', 'd6'], ['minor_sixth', 'm6', 'augmented_fifth', 'A5'], ['major_sixth', 'M6', 'diminished_seventh', 'd7'], ['minor_seventh', 'm7', 'augmented_sixth', 'A6'], ['major_seventh', 'M7', 'diminished_octave', 'd8'], ['perfect_octave', 'octave', 'P8', 'augmented_seventh', 'A7']];
Common = require('./common');
Note = (function(superClass) {
extend(Note, superClass);
function Note(note, octave) {
this.valueAt = bind(this.valueAt, this);
var rn, ro, steps;
this.note = note.toUpperCase();
this.octave = octave;
if (chromaticNotes.indexOf(this.note) === -1) {
raise("Invalid note value: " + note);
}
if (octave < 0) {
raise("Invalid octave: " + octave + ", must be >= 0");
}
rn = -(chromaticNotes.indexOf('A') - chromaticNotes.indexOf(this.note));
ro = this.octave - 4;
steps = (ro * 12) + rn;
this.freq = 440 * (Math.pow(chromaticInterval, steps));
}
Note.prototype.valueOf = function() {
return this.freq;
};
Note.prototype.valueAt = function(t) {
return this.freq;
};
Note.prototype.transpose = function(semitones) {
var ni, oct;
oct = this.octave;
ni = chromaticNotes.indexOf(this.note);
ni += semitones;
while (ni >= chromaticNotes.length) {
oct += 1;
ni -= chromaticNotes.length;
}
while (ni < 0) {
oct -= 1;
ni += chromaticNotes.length;
}
return this.constructor.retrieve(chromaticNotes[ni], oct);
};
Note.prototype.next = function() {
return this.transpose(1);
};
Note.prototype.previous = function() {
return this.transpose(-1);
};
Note.prototype.nextOctave = function() {
return this.transpose(12);
};
Note.prototype.previousOctave = function() {
return this.transpose(-12);
};
Note.prototype.toString = function() {
return "" + this.note + this.octave;
};
Note.retrieve = function(note, octave) {
var name;
note = note.toUpperCase();
name = note.replace('#', 'Sharp') + octave;
if (global[name] == null) {
global[name] = new Note(note, octave);
}
return global[name];
};
return Note;
})(Common);
for (interval = j = 0, len = intervalMap.length; j < len; interval = ++j) {
names = intervalMap[interval];
for (k = 0, len1 = names.length; k < len1; k++) {
name = names[k];
Note.prototype[name] = function() {
return this.transpose(interval);
};
}
}
for (i = l = 0; l < 9; i = ++l) {
for (m = 0, len2 = chromaticNotes.length; m < len2; m++) {
n = chromaticNotes[m];
Note.retrieve(n, i);
}
}
module.exports = Note;
}).call(this);
//# sourceMappingURL=notes.js.map