diff options
| author | Jules Laplace <jules@okfoc.us> | 2016-09-20 13:31:55 -0400 |
|---|---|---|
| committer | Jules Laplace <jules@okfoc.us> | 2016-09-20 13:31:55 -0400 |
| commit | 0935f2fe2864198723356a2fa8ff1b0a57e56876 (patch) | |
| tree | 012f54c3224c44b686a9ae039d90bab416fb2475 /intonation.js | |
| parent | 5746c183a1b508e9504a9290df2f701c091edcae (diff) | |
parsing scl files
Diffstat (limited to 'intonation.js')
| -rw-r--r-- | intonation.js | 51 |
1 files changed, 50 insertions, 1 deletions
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 ] |
