From 0935f2fe2864198723356a2fa8ff1b0a57e56876 Mon Sep 17 00:00:00 2001 From: Jules Laplace Date: Tue, 20 Sep 2016 13:31:55 -0400 Subject: parsing scl files --- intonation.js | 51 ++++++++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 50 insertions(+), 1 deletion(-) (limited to 'intonation.js') diff --git a/intonation.js b/intonation.js index 509addd..8c12847 100644 --- a/intonation.js +++ b/intonation.js @@ -11,7 +11,10 @@ var Intonation = (function(){ } Intonation.prototype.generate = function(opt){ opt = Object.assign(this.opt, opt || {}) - if (opt.tet) { + if (opt.scl) { + this.generate_scl() + } + else if (opt.tet) { this.generate_tet() } else if (opt.intervals) { @@ -44,6 +47,52 @@ var Intonation = (function(){ } this.intervals = null } + Intonation.prototype.generate_scl = function(){ + var root = this.opt.root + var scl = this.parse_scl( this.opt.scl ) + this.intervals = scl.notes + this.interval = scl.notes.pop() + this.scale = scl.notes.map(function(v){ + return v * root + }) + } + Intonation.prototype.parse_scl = function(s){ + var scl = {} + scl.comments = [] + scl.notes = [] + s.trim().split("\n").forEach(function(line){ + // Lines beginning with an exclamation mark are regarded as comments + // and are to be ignored. + if ( line.indexOf("!") !== -1 ) { + scl.comments.push(line) + } + // The first (non comment) line contains a short description of the scale. + // If there is no description, there should be an empty line. (nb: which is falsey) + else if ( ! ('description' in scl) ) { + scl.description = line + } + // The second line contains the number of notes. + // The first note of 1/1 or 0.0 cents is implicit and not in the files. + else if ( ! scl.notes.length) { + scl.notes.push(1) + } + else { + // If the value contains a period, it is a cents value, otherwise a ratio. + var note = line.replace(/^[^-\.0-9]+/,"").replace(/[^-\/\.0-9]+$/,"") + if ( note.indexOf(".") !== -1 ) { + note = Math.pow( 2, (parseFloat(note) / 1200) ) + } + else { + console.log("note", line, note) + note = parseInterval(note) + } + if (note) { + scl.notes.push(note) + } + } + }) + return scl + } Intonation.prototype.index = function(i, octave){ octave = octave || this.opt.octave var f = this.scale[ mod(i, this.scale.length)|0 ] -- cgit v1.2.3-70-g09d2