Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.javascript > #40631

sidetrack

Newsgroups comp.lang.javascript
Date 2021-12-07 18:42 -0800
Message-ID <06038aa0-e554-4cc7-b3cc-77fa0d3c143fn@googlegroups.com> (permalink)
Subject sidetrack
From luserdroog <luser.droog@gmail.com>

Show all headers | View raw


I'm making some progress at reconceiving my pianoroll app
to be a more coherent object based design. But also kinda
not making progress on it. I doodled up a sketch of the overall
structure of the MVC example but instead of working on that,
here's a "robust" function for decoding note names from letters
and producing a MIDI note number. 

It uses two minor tetrachords to construct the intervals of an 
aeolian scale then takes a running sum to get "fretstops" for 
each of the scale degrees. Then you index the resulting array
with 0 for 'a', 1 for 'b' etc, a little more twiddling and poof kazow
presto! The desired number comes out the other end.

Is there a better way to do the running_sum()? I was trying to
do an APL "scan" and doing a reduce() while building the result
backwards was the best I could come up with.

const major_tetrachord = [ 2, 2, 1 ];
const mixolydian_mode = [ ...major_tetrachord,
                          ...major_tetrachord,
                          2 ];
const minor_tetrachord = [ 2, 1, 2 ];
const aeolian_mode = [ ...minor_tetrachord,
                       ...minor_tetrachord ];
const scale = [ ...mixolydian_mode,
                ...mixolydian_mode,
                ...major_tetrachord ];

function running_sum( a ){
  return  a.reduce( (pre,cur)=>[ cur+pre[0], ...pre ], [0] ).reverse();
}
console.log(running_sum(aeolian_mode));
console.log(running_sum(scale));

function note( str, octave=0 ){
  var num = str.toLowerCase().charCodeAt(0) - 'a'.charCodeAt(0);
  var fromA = running_sum(aeolian_mode)[ num ] + 
              (str.length > 1  ? (str[1]=='#' ? 1 : -1)
                               : 0);
  var fromC = fromA + 9;
  return  octave*12 + (fromC >= 12 ? fromC - 12 : fromC);
}

console.log(note("a",0));
console.log(note("c#",0));
console.log(note("eb",0));

Back to comp.lang.javascript | Previous | Next — Next in thread | Find similar | Unroll thread


Thread

sidetrack luserdroog <luser.droog@gmail.com> - 2021-12-07 18:42 -0800
  Re: sidetrack "Michael Haufe (TNO)" <tno@thenewobjective.com> - 2021-12-08 13:03 -0800
    Re: sidetrack luserdroog <luser.droog@gmail.com> - 2021-12-08 16:17 -0800
      Re: sidetrack luserdroog <luser.droog@gmail.com> - 2021-12-08 16:23 -0800
        Re: sidetrack "Michael Haufe (TNO)" <tno@thenewobjective.com> - 2021-12-08 21:38 -0800
          Re: sidetrack luserdroog <luser.droog@gmail.com> - 2021-12-09 17:39 -0800
            Re: sidetrack luserdroog <luser.droog@gmail.com> - 2021-12-09 17:44 -0800
            Re: sidetrack Scott Sauyet <scott@sauyet.com> - 2021-12-10 07:26 -0800
        Re: sidetrack Elhwen Dico <elhwen.dicote@gmail.com> - 2021-12-10 16:30 +0100
          Re: sidetrack Elhwen Dico <elhwen.dicote@gmail.com> - 2021-12-10 16:37 +0100
          Re: sidetrack Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2021-12-11 01:56 +0100
            Re: sidetrack Julio Di Egidio <julio@diegidio.name> - 2021-12-11 10:12 +0100
  Re: sidetrack luserdroog <luser.droog@gmail.com> - 2021-12-09 19:22 -0800

csiph-web