views:

417

answers:

3

Hi!

I wrote this post Music and Mathematics, finding the Natural and the Pentatonic scales.

I want to find the best programmatic aproach. A solution could be:

<script>
  function getScaleIntervals(c) {
    var tot = 0;
    var scale = [];

    while(tot <= 12){
      scale.push(Math.round(tot));
      tot += c;
    }
    return scale;
  }
  var natural_scale = getScaleIntervals(12/7);
  document.write(natural_scale + " \n"); // ==> 0, 2, 3, 5, 7, 9, 10, 12

  var pentatonic_scale = getScaleIntervals(12/5);
  document.write(pentatonic_scale + " \n"); // ==> 0, 2, 5, 7, 10, 12
</script>

The resultant intervals starts with D (Re) in 0 so you have D E F G A B C D This is the Dorian Mode

+3  A: 

I don't know if the theory in the blog page is correct, but if for some crazy reason I put 18 notes into the 12 semitones then the upper limit of tot will be 13; also the pentatonic scale won't include the last 12.

Programmatically to translate the stuff on the blog page should use

while (tot <= 12) {

The rest are fine, except I don't know why the if(scale.length == 8) { is needed.


For the validity of the music theory, ask somewhere else.

KennyTM
+1 but what's wrong with asking a question related to both music and programming?
SDX2000
@Alfonso: You can generate a scale as an arithmetic sequence, but a 1.7 Hz difference at 440 Hz is too small for human ear to distinguish easily (note A# -> A has 26 Hz difference), and human perception is logarithmic to frequency, so a geometric sequence is reasonable.
KennyTM
@Alfonso: So you mean, you want to programmatically compute the frequencies of an octave?
KennyTM
@Alfonso: I don't understand what are you talking about. What is a SCALE GENERATOR? Something like http://www.scalerator.com/?
KennyTM
@KennyTM yes, if you run the above code, it returns the intervals of the natural scale starting on D (Re), if you run it with var c = 12/5; it returns the intervals of the pentatonic scale starting on D (Re).
Alfonso de la Osa
Added your tip to the code and autoanswered.
Alfonso de la Osa
+2  A: 

Your question (as written) is ambiguous. If you mean "can your algorithm be used to generate natural scales?" (those containing all natural notes, i.e. notes that are neither sharp nor flat), then yes, but only one (unless you allow for different rounding methods, in which case you get one natural scale per rounding method) and only by cherry picking a tonic. If you mean "does your algorithm, by itself, result in a natural scale?", then the answer is no because it does not, by itself, generate a scale; it generates a mode.

Note: in this answer, all named modes (e.g. Ionian) refer to the modern definitions.

In terms of semitones, your algorithm results in intervals (from the tonic) of 0,2,3,5,7,9,10,12, which corresponds to a semitone sequence (i.e. scale steps) of 2-1-2-2-2-1-2, or Dorian mode. Note that the algorithm doesn't determine the tonic (the first note in a scale), so it doesn't give you a specific scale, which is a sequence of pitches, such as C-major or Dorian mode in D.

In terms of the named intervals from the tonic, the Dorian mode contains the major 2nd, minor 3rd, perfect 4th, perfect 5th, major 6th, minor 7th, and perfect 8th. C major is M2, M3, P4, P5, M6, M7, P8 (all major or perfect intervals).

Generating Other Modes

Your choice of rounding function is arbitrary. If you always round up (⌈i*12/7⌉), you get the intervals 0,2,4,6,7,9,11,12 and semitone sequence 2-2-2-1-2-2-1, which is Lydian mode. Rounding down (⌊i*12/7⌋) gets you intervals 0,1,3,5,6,8,10,12 and steps 1-2-2-1-2-2-2, which is Locrian mode. None of these are the intervals or semitone sequence for the natural scale in C (i.e. Ionian mode in C, or C major), which is 0,2,4,5,7,9,11,12 and 2-2-1-2-2-2-1, respectively.

If you expand the algorithm to use a different rounding function for each term rather than the same rounding function for all, you can generate the other named modes (e.g. (_, ceil, ceil, floor, ceil, ceil, ceil, _), where _ means "don't care", will generate Ionian), but you will also generate many other modes that can't result in a natural scale. You can generate a total of 2n-1 modes this way, where n is the number of tones (I'm covering n=7, or heptatonic modes). The number of heptatonic modes equals the number of compositions of 12 of length 7, which is 11C6=462, so this method won't generate all modes. If we define roundr(x) = floor(x+r), we can generate only the diatonic modes (the 7 named modes, or Heptatonia Prima, which maximally separate the semitone steps) using this rounding function by limiting r to i/7, where i ∈ [0, 7) ⊂ ℕ. For example, Ionian is roundTo5/7(i*12/7) for i ∈ [0, 7) (note: round-to-nearest is roundTo0.5(x)). With this last approach, we can generate modes that can generate all 7 natural scales.

Diatonic(n) = <roundTon/7(x*12/7) for x in ∈ [0, 7)>

where <...> indicates a tuple (i.e. a finite sequence).

Generating Scales

Your algorithm only generates modes, but those can be used to generate scales. Depending on which note you pick to start on (the tonic), you'll get different scales for a given mode. Dorian results in a minor scale because it includes the minor third (the first two semitone steps in Dorian are 2-1, which sum to 3, the minor third) and perfect fifth (the sequence starts with 2-1-2-2, giving 7 semitones above the tonic). Lydian gives you a major scale because it includes the major third (it's semitone sequence starts with 2-2, which sums to 4, the major third) and perfect fifth (2-1-2-2). Locrian doesn't include the perfect fifth, so it's neither major nor minor.

Generating Natural Scales

To see which natural scales your algorithm can generate, let ADLO(n, round, tonic) stand for the scale resulting from the generalized version of your algorithm, where n is the number of pitches per octave, round is the rounding function and tonic is the tonic for a scale. If any is unspecified, the result is a collection of all possible values (thus ADLO(7, nearest) is all scales in Dorian mode). A named mode and tonic will be used for the scale in that mode with that tonic (e.g. Ionian('C') is C-major); a named mode with no tonic (e.g. Ionian()) will stand for the set of all scales in that mode. {} denotes a set, and <> a sequence.

    ADLO(7, nearest) = Dorian() = Diatonic(3)
      {
        <C, D,  D#, F,  G,  A,  A#, C>,
        <D, E,  F,  G,  A,  B,  C,  D>,
        <E, F#, G,  A,  B,  C#, D,  E>,
        <F, G,  G#, A#, C,  D,  D#, F>,
        <G, A,  A#, C,  D,  E,  F,  G>,
        <A, B,  C,  D,  E,  F#, G,  A>,
        <B, C#, D,  E,  F#, G#, A,  B>
      }
    ADLO(7, ceil) = Lydian() = Diatonic(6)
      {
        <C, D,  E,  F#, G,  A,  B,  C>,
        <D, E,  F#, G#, A,  B,  C#, D>,
        <E, F#, G#, A#, B,  C#, D#, E>,
        <F, G,  A,  B,  C,  D,  E,  F>,
        <G, A,  B,  C#, D,  E,  F#, G>,
        <A, B,  C#, D#, E,  F#, G#, A>,
        <B, C#, D#, F,  F#, G#, A#, B>
      }
    ADLO(7, floor) = Locrian() = Diatonic(0)
      {
        <C, C#, D#, F,  F#, G#, A#, C>,
        <D, D#, F,  G,  G#, A#, C,  D>,
        <E, F,  G,  A,  A#, C,  D,  E>,
        <F, F#, G#, A#, B,  C#, D#, F>,
        <G, G#, A#, C,  C#, D#, F,  G>,
        <A, A#, C,  D,  D#, F,  G,  A>,
        <B, C,  D,  E,  F,  G,  A,  B>
      }

Thus we see that

      if you         and pick
* round to nearest     D
* round down           B
* round up             F
* ...
                     as the tonic, you get a natural scale.

Also,

    ADLO(7, roundTo5/7(x)) = Ionian()     = Diatonic(5)
    ADLO(7, roundTo3/7(x)) = Dorian()     = Diatonic(3)
    ADLO(7, roundTo1/7(x)) = Phrygian()   = Diatonic(1)
    ADLO(7, roundTo5/7(x)) = Lydian()     = Diatonic(6)
    ADLO(7, roundTo4/7(x)) = Mixolydian() = Diatonic(4)
    ADLO(7, roundTo2/7(x)) = Aeolian()    = Diatonic(2)
    ADLO(7, roundTo0/7(x)) = Locrian()    = Diatonic(0)

Which note to pick as the tonic is also given as the "white note" in the Wikipedia article on modes in modern music.

Computational Correctness

The reason the code works to generate a mode is that the notes in a diatonic scale are as evenly distributed among the semitones as is possible. i*n/m, for i ∈ [0, m), is an even distribution of m things among n things. Round those values and you get a distribution that's as even as possible among integers from 0 through n. Thus your algorithm results a diatonic mode. This isn't all that significant a result; it's a rather simple consequence of rounding.

outis
@Alfonso: your blog post mentions major and minor notes along the semitone sequence, while I'm talking about major and minor scales. Your marking of major and minor notes, by the way, doesn't make sense to me as the notes in a semitone sequence are the major notes for that scale by definition (as I've heard "major note" and "minor note" defined). That is, the major notes are the ones that are an integer number of scale steps from the tonic, while minor notes are a half-integer number of scale steps ({1/2, 3/2, 7/2, 9/2, 11/2} for C-major).
outis
+1 I now understand you, yes, it looks like the Dorian Mode is more natural than the natural scale.
Alfonso de la Osa
@outis Sorry about the confusion, take a look to my comment in Debilski's answer where I explain it better, I resume with "minor note", a note that has a minor third and is a minor third.
Alfonso de la Osa
@outis more in Debilski's answer
Alfonso de la Osa
A: 

From reading your article, it seems to me that you are seriously confusing minor with flat and major with sharp, respectively.

So, from “2 rounded to ceil, minor note” you conclude that the second note in the scale must be the flat version of the second white key on the piano starting with D. That is, E♭. You basically start with a Dorian mode (i.e. all the white keys starting at D) and then augment or diminish the keys depending on the result of your rounding.

However, you should note that the Dorian scale is already a scale; therefore the most natural scale you’d get from starting with a Dorian would be the very same Dorian scale. And you’d achieve this without any flats or sharps (or minor and major notes as you name it).

Starting this whole theory on the Dorian scale is already such a strong assumption that it would need some hard work to put this in a reasonable way. And this hard work would eventually lead to a real theory of scales. And not the number casting you do afterwards.

As you write, you “used to believe that the half-tones between some notes where a kind of adjustment” – it really looks to me that this is still the case, otherwise you wouldn’t be ignoring them by starting with a non-equally tempered scale. If you want to get any further, this scale should be your starting point:

c – c♯ – d – d♯ – e – f – f♯ – g – g♯ – a – a♯ – h

(And to avoid further confusion, you should drop their names and refer to them by 1, 2, 3, … 12.)

Or in frequency fractions:

1 – 2^(1/12) – 2^(2/12) – 2^(3/12) – … – 2^(11/12) – 2^(12/12) = 2
1 – 1.059    – 1.122    – 1.189    – … – 1.888     – 2

You can do your number games on the equal tempered scale and see what comes out then. Some theory regarding the finding of major chords is done by looking at the overtone series in musical instruments. You’ll find some notes in this series which naturally lead to some sort of major chords and basic scale, if you accept some compromises. One of the compromises being that what you’ll find is actually not exactly equal tempered…

Debilski
But what is your explanation for the Dorian mode being the “natural-natural scale” then?
Debilski
@Alfonso: your definition of "minor note" still doesn't make sense as every note is a minor third above another note and (by the same token) has a note that's a minor third above it, though some of the notes in the intervals may not be included in whatever set of pitches (e.g. scales) you're considering. In your article, you mark the note that's a minor third from the tonic as a "major note", which makes no sense. Also, by "rounding method", are you referring to "round to nearest"? What do you mean by "trustable?"
outis
@outis take 7/7 = 12/12 = 1 Octave and the D note as 0 for the experiment (Dorian Mode, C tonality): D's third is 2/7 and more near to the 3/12 than to the 4/12 so it needs to be "minorized to fit" so is a major key. And in C tonality F Key is major, and is the minor third of D (so D is minor). Next note: the 3/7 is more near to 5/12, so G key is major too and is the minor third of E, so E has to be minor. Next note 4/7 is nearer to 7/12 than to the 6/12 so it has to be "majorized to fit", so A is minor key and is the major third of F, so F has to be a major key, and so on.
Alfonso de la Osa
@outis i also changed stuff in the article to explain it clearly, thanks!
Alfonso de la Osa
outis
@Alfonso: though if by "Dorian mode, C tonality" you mean you fix the pitches in your scale to those in C major and rotate them to change the tonic and mode, it begins to make a little more sense. In D, you'd have Dorian mode. In E, Phrygian the real reason is the third interval in the scale is minor. The scales thus include the minor triad of the tonic, thus the keys are also minor. In any case, your system involves making arbitrary choices.
outis
@Alfonso: Also, you haven't supported your statement that Dorian mode is more "natural" than the Diatonic scale (i.e. a scale in the Ionian mode), nor that "D" is the "natural" tonic for the Dorian mode, in part because you haven't defined "natural".
outis
@Alfonso: in the end, your question of the correctness of your algorithm has nothing to do with computational correctness (by most any defn of "correct" for your problem, it's computationally correct: it generates a sequence of `m` integers as nearly evenly distributed among 12 as is possible; it generates a sequence of integers which, ignoring that they are off-by-one, are suitable for scale degrees) and everything to do with musical theory. As for that question, it's entirely dependent on which note you pick as the tonic.
outis
@outis Ok, lot of concepts of music that I don't have and this is the reason of my problems to explain this and I'm adding more semantic to the article too. But back in the programmatic. What this technique does is find the intervals of the keys of Dorian Mode in D tonality, right? Is easy to move the intervals and get any other scale in the white keys.
Alfonso de la Osa
@outis also, if you want to try to explain the reason why this works, you can do it in the post, half of it is a try to explain / understand it.
Alfonso de la Osa
@Alfonso: my answer already describes what your algorithm does and why, but I'll make it clearer.
outis
@outis: if you publish code probably get the answer.
Alfonso de la Osa