News for hackers. Stuff that matters.
Re:The Circle of Fifths, Part One by tinco on Sunday November 25, 2012 @02:36AM (#4827592)
The code he wrote for showing how to generate is pretty ugly and left me confused. I think this shows much better the relation between the 12 notes and a scale:

  NOTES = ['c', 'c#', 'd', 'd#', 'e', 'f', 'f#', 'g', 'g#', 'a', 'a#', 'b']
  # 2 is a whole step, 1 is a half step
  MAJOR_STEPS = [2,2,1,2,2,2,1]

  # returns the scale in the given key with the given steps
  def scale(key, steps)
    note = NOTES.index(key)
    step = 0
    notes = []
    while step < steps.length
        notes << NOTES[note % NOTES.length]
        note += steps[step]
        step += 1
    end
    notes
  end

  scale('d', MAJOR_STEPS) #=> ["d", "e", "f#", "g", "a", "b", "c#"]
I think this shows how a scale is just a list of steps applied to the notes from a certain key :)
Re:The Circle of Fifths, Part One by yayitswei on Sunday November 25, 2012 @03:42AM (#4827698)
Chris Ford gave an elegant explanation of scales at a Clojure talk in London: http://skillsmatter.com/podcast/home/functional-composition.

He starts with a basic sine wave and builds up abstractions until he has a Bach canon.

Re:The Circle of Fifths, Part One by agumonkey on Sunday November 25, 2012 @10:48AM (#4828354)
Only at the first third and already delighted. He's moving between music, physics, programming in such a simple and pragmatic way, thanks for the bottom-up dynamic evaluation demonstration. Many many thanks.

links aside, he uses (and is a project contributor) http://overtone.github.com/

ps: they also provide a nice emacs setup https://github.com/overtone/emacs-live

Re:The Circle of Fifths, Part One by tinco on Sunday November 25, 2012 @01:11PM (#4828758)
This is an excellent talk, thank you very much for linking it. Clojure is such a pretty language!
Re:The Circle of Fifths, Part One by dizzystar on Sunday November 25, 2012 @02:52AM (#4827615)
Is that Ruby? :o

I agree. That is 1 million times better. Sometimes I wonder why I don't think of these things. I'll probably fix the code and use something like that... except in Python, obv.

Thank you;

Re:The Circle of Fifths, Part One by tinco on Sunday November 25, 2012 @03:37AM (#4827694)
Hey, yeah Ruby is a bit easier for me :) Your code is correct so it's not terrible. As to why you didn't think of it.. maybe it's because you practice less at making code look as pretty as possible?

It is something I practice a lot, maybe because it's in Ruby's culture, or just because I enjoy making code look pretty.

When I look at your two for loops that twice make a range, and have a bunch of magic numbers, all kinds of alarm bells go off. It should be one loop if possible, if there are magic numbers they should be in a constant at the top of the file.

Actually, the way you construct the seedList is interesting. A Haskell programmer might have done the same thing, because keeping indexes is ugly in Haskell, and doing operations on infinite lists (which you emulate by appending a copy) are pretty. Perhaps you are just a functional programmer, using a non-functional language :D

Re:The Circle of Fifths, Part One by dizzystar on Sunday November 25, 2012 @04:00AM (#4827725)
Yeah, I am more of a functional programmer and yeah, I sometimes let some ugliness slip in especially in looping in Python. At least there is none of this crap in production somewhere. :) n00b.

It would look better if it was in Scheme or Clojure, but that stuff's too scary for most readers. At least that's the story I'll maintain here.

Re:The Circle of Fifths, Part One by tinco on Sunday November 25, 2012 @11:19AM (#4828443)
I would love to see a Scheme or Clojure version of the code. I posted an attempt at it in Haskell, the only functional programming language I know, but it isn't particularily beautiful.
Re:The Circle of Fifths, Part One by jacquesm on Sunday November 25, 2012 @03:07AM (#4827630)
Thanks a ton for the great explanation, I dropped you a line about some typos and other off-topic stuff.

Of all the bits and pieces I've looked at over the years explaining the circle of fifths this is the one that stands out for me as the easiest to remember. Being able to re-derive from basic principles is so much better than memorization.

Thank you again!

Re:The Circle of Fifths, Part One by kroger on Sunday November 25, 2012 @01:43PM (#4828828)
The following solution defines a function to transpose each scale taking advantage of the mathematical properties of notes:

    def notes_name(numbers):
        notes = "C C# D D# E F F# G G# A A# B".split()
        return [notes[(number % 12)] for number in numbers]
    
    def transposition(notes, start):
        index = start - notes[0]
        return [(n + index) % 12 for n in notes]
    
    def gen_scales():
        c_scale = [0, 2, 4, 5, 7, 9, 11]
        return [notes_name(transposition(c_scale, note)) for note in range(0, 12)]
    
    for scale in gen_scales():
        print " ".join(scale)
EDIT: make the code prettier
Re:The Circle of Fifths, Part One by kroger on Sunday November 25, 2012 @01:46PM (#4828841)
A trick to generate a cycle of fourths or fifths is to multiply a chromatic scale by 5 or 7, respectively:

    notes_name([n*7 for n in range(0, 12)])
Re:The Circle of Fifths, Part One by scrvy on Sunday November 25, 2012 @04:51PM (#4829408)
This is rad, thanks for that.
Re:The Circle of Fifths, Part One by Tycho on Sunday November 25, 2012 @07:58AM (#4828049)
i'd like to see it written without a loop, using map or filter...
Re:The Circle of Fifths, Part One by tinco on Sunday November 25, 2012 @11:14AM (#4828425)
Alright, time to fire up ghci then.. it doesn't work with map or filter, but you get the idea:

  notes = concat $ repeat ["c", "c#", "d", "d#", "e", "f", "f#", "g", "g#", "a", "a#", "b"]
  majorSteps = [1,1,0,1,1,1,0]

  scale key steps =
      key : skip notesFromKey majorSteps
    where
      (_:notesFromKey) = dropWhile ((/=) key) notes

  skip list skips =
      fst $ foldl next ([], list) skips
    where
      next (m, xs) s = (m ++ [xs !! s], drop (s + 1) xs)
edit: cleaned it up a lot. This is Haskell by the way. The loop is replaced by a 'fold' in the skip function that reduces the list by skipping through it taking the skip distances from the skips argument.
Re:The Circle of Fifths, Part One by mifrai on Wednesday November 28, 2012 @05:48PM (#4845228)
Similar concept but a little simpler and avoids manually written recursion.

  notes = cycle ["c", "c#", "d", "d#", "e", "f", "f#", "g", "g#", "a", "a#", "b"]
  majorSteps = [2, 2, 1, 2, 2, 2] :: [Int]

  scale note = map (dropWhile (/= note) notes !!) . scanl (+) 0
I think it's less clear, but if you want to avoid the rescan via (!!), then

  scale note = map head . scanl (flip drop) (dropWhile (/= note) notes)
Both result in

  λ> scale "d" majorSteps
  ["d","e","f#","g","a","b","c#"]
Re:The Circle of Fifths, Part One by kroger on Sunday November 25, 2012 @01:52PM (#4828862)
You can se a solution in Python here: http://news.ycombinator.com/item?id=4828828