in reply to Chord root

I like being able to run code under warnings (I am a module author), so adding this:

for (@f) { $_ = 0 unless defined }

is handy below the foreach in the code.

Also, I am a fan of Sean Burke's MIDI perl modules, so I prefer "As" and "Af" instead of "A#" and "AB". If you use these, you can talk directly to the MIDI modules without any needless parsing gymnastics. Also, putting #'s in qw() arrays breaks with warnings on.

Anyway, here are some questions:

1. Is there a name for "@f" that might give away what it stands for and holds? Variables named things like "f" are pretty frustrating.

2. What does ($n+$_)%12 represent?

3. What is (0,0,0,2,5,5,8,10)?

Maybe the Terhardt link describes this stuff somewhere, but... comments are really useful.

: )

-Gene Boggs <gene at ology dot net> Software Engineer and Epistemologist at-large _____________________________________________

Replies are listed 'Best First'.
Re: Re: Chord root
by benn (Vicar) on Aug 09, 2003 at 16:58 UTC
    Hi Gene.

    Indeed - when I stick this in Music::Chord, it'll have strict and warnings plastered all over it...but I was playing golf :)

    I too am a fan of Sean's MIDI Modules, but not of 'As' and 'Af' - I've been reading / writing real music for too long now. Again, if/when I 'modulise' this, and once we've sorted out a common standard for note interchange, this will probably sort itself out. The 'parsing gymnastics' though would seems to consist of s/s$/#/ and s/f$/b/ :)

    Anyway, here are some answers.

    1. 'f' stands (in my head) for 'final values'. It's just an array that stores the note counts. Yes, I could have called it '@final_values'.

    2. ($n + $_) %12 represents adding a numerical note value ($n) to an interval ($_) and doing a modulus 12 on the result, to remain within the 0-11 note range.

    3. These are harmonics. Indeed, the link describes the algorithm, and explains what the values meant, but briefly, the idea is to take the 1st 8 *sub*-harmonics (8va,5th,8va,3rd,5th,7th etc., but *downwards*) of each note in the chord, then do a popularity count on each note - the most popular is/are the root. The original algorithm misses out all octave equivalents, (doubled root + 5th), but I found that this gives wrong results for minor chords, so added them back in. This gives us a semitone-interval list of (0, -12, -19,-24...), but I didn't bother with working out all the big negative numbers, as this is just functionally equivalent to (0,0,5,0,8,5,10,0,2), ordered how you like - in my case, sorted numerically cos it looked nicer :)

    Hope this answers the questions.

    Ben.

Re^2: Chord root
by Aristotle (Chancellor) on Aug 10, 2003 at 15:33 UTC
    for (@f) { $_ = 0 unless defined }
    Since the only false values are undef, '', 0, '0', you can safely write this more idiomatically as
    $_ ||= 0 for @f;

    Makeshifts last the longest.