Finish that Song

Another experiment with Claude AI for coding a Python music composition program.

Think of all those times when you come up with a great basic chord progression but can’t seem to figure out where to take it next. You are vaguely aware of the options – shift to another diatonically close key, go relative minor, parallel minor, etc. – but specific pathways seem elusive. Perhaps you are too caught up in the existing chord sequence to subject the idea to the further analysis it needs? Considering the possibility of a program that not only suggests relevant musical options (a chorus to match a verse sequence, a bridge to fit somewhere in the middle of our song, etc) but, more importantly, encourages us to be more analytical ourselves. It aims to clarify options and ways we can think about where to go next rather than simply tell us precisely where to go.

1st Iteration

Here was my initial prompt (I like to be polite, especially as Claude is infinitely more productive than I could ever hope to be): Please make a Python program that takes a sequence of chords and then explains their most likely musical key and a variety of chord sequence options for complementary song parts, as well as explaining a variety of simple and more complex modulation options for creating transitions and bridge parts.

Here was Claude’s response:


I’ll create a comprehensive Python program for chord analysis and musical composition assistance. This will include key detection, chord progression suggestions, and modulation options.

Key Features:

I’ve created a comprehensive musical chord analysis and composition tool! This Python program provides:

🔑 Key Analysis:

  • Analyzes chord sequences to determine the most likely key
  • Provides confidence scores and alternative key candidates
  • Works with both major and minor keys

🎼 Chord Progression Suggestions:

  • Suggests complementary progressions for different song sections (verse, chorus, bridge, pre-chorus)
  • Converts between Roman numeral analysis and actual chords
  • Includes descriptions of each progression’s musical character

🔄 Modulation Options: Three levels of complexity:

  • Simple: Whole step up, relative major/minor
  • Intermediate: Circle of fifths, chromatic mediant
  • Advanced: Tritone substitution, Neapolitan modulation

How to Use:

  1. Run the program
  2. Enter chord sequences like:
    • C Am F G
    • Em, C, G, D
    • Dm7, G7, Cmaj7, Am

Example Analysis:

For the sequence “C Am F G”, it would identify:

  • Key: C major (very high confidence)
  • Verse options: Like vi-IV-I-V progressions
  • Modulation ideas: Up to D major for chorus, to A minor for contrast

The program handles various chord notations including:

  • Basic triads (C, Am, F#)
  • Seventh chords (Cmaj7, Dm7, G7)
  • Slash chords (C/E, Am/C)
  • Extended and altered chords

This tool is perfect for songwriters, composers, and music students who want to understand harmonic relationships and explore creative possibilities with their chord progressions!


And here is some sample program output:

Lots of potential for refinement, but an incredible initial effort to produce just what I was after.

Key Observation

Previously we had to think like computers to program something like this. Now computers have the capacity to comprehend us – to make sufficient sense of our messy, natural language instructions to produce something closely approximating what we have requested. They can mediate between the general and the numerically precise without any significant slippage between aspects of human intention and computational realisation.

2nd Iteration

After writing this, I decided to try to extend the program to deal with more complex chords.

I wrote the prompt: Can you please also include 9th, 11th and 13th chords?

Claude plainly recognised that I was concerned with the full gamut of chord extensions:


Perfect! I’ve enhanced the chord analyzer to fully support 9th, 11th, and 13th chords, plus many other extended harmonies. Here are the key improvements:

🎼 Enhanced Chord Support:

Extended Chords:

  • 9th chords: C9, Dm9, Gmaj9, Am9
  • 11th chords: F11, Cmaj11, Em11
  • 13th chords: G13, Amaj13, Dm13

Additional Extensions:

  • Add chords: Cadd9, Fadd11, Gadd13
  • Suspended: Csus2, Gsus4
  • 6th chords: C6, Am6/9
  • Altered: G7alt (for jazz)
  • Diminished/Augmented: Cdim7, Caug

🎵 Enhanced Progressions:

Added Jazz sections with sophisticated harmony:

  • Jazz verse: Cmaj9 - Am7 - Dm9 - G13
  • Jazz chorus: Dm9 - G7alt - Cmaj13 - C6/9

🔧 Improved Parser:

The chord parser now correctly handles complex notation like:

  • Cmaj13 (major 13th)
  • Am11 (minor 11th)
  • G7alt (altered dominant)
  • Fmaj9 (major 9th)
  • Dm13 (minor 13th)

Example Usage:

Try these extended chord progressions:

  • Neo-soul: Cmaj9 Am11 Fmaj7 G13
  • Jazz standard: Cmaj7 A7alt Dm9 G13
  • Modern R&B: Fmaj9 G13 Em7 Am11

The analyzer will now recognize these extended harmonies and suggest complementary progressions using similar sophisticated chord voicings. This makes it perfect for jazz, neo-soul, R&B, and contemporary music composition!


I tried this out and instantly discovered a problem. I entered the chord sequence: C Major, Dm9, Dm7, Dm6. The program identified this as C Major, when it was more likely D minor.

3rd Iteration

So I wrote another prompt: Are your sure the key detection is working properly? I put in the chord sequence: C7 Dm9 Dm7 Dm6 and the program suggested that the key is C Major. I think it is more likely D minor.

Claude responded:

You’re absolutely right! That’s a great catch. The chord sequence C7 Dm9 Dm7 Dm6 is clearly in D minor, not C major. The key detection algorithm has a significant flaw – it’s not properly considering chord quality (major vs minor) and the harmonic function of the chords.

Let me fix the key detection to be much more sophisticated:

And it rebuilt the program again:


Perfect! I’ve completely overhauled the key detection algorithm to be much more sophisticated. Here’s what the improved system now considers:

🔍 Enhanced Key Detection Features:

1. Chord Quality Analysis:

  • Considers whether chords are major, minor, diminished, etc.
  • Matches chord qualities to expected diatonic functions
  • Example: In D minor, expects Dm (i), F (bIII), Gm (iv), A (V), etc.

2. Functional Harmony Scoring:

  • Tonic bonus: Extra points for the key center
  • Dominant bonus: V chords get priority
  • Quality matching: Dm in D minor gets higher score than D major
  • Extensions: V7 chords get bonus points as dominants

3. Borrowed Chord Recognition:

  • Recognizes common borrowed chords (like V in minor keys)
  • Handles mode mixture intelligently
  • Doesn’t penalize common chromatic chords

4. Contextual Analysis:

  • Your example C7 Dm9 Dm7 Dm6 should now correctly identify as D minor because:
    • Multiple Dm chords (strong tonic presence)
    • C7 functions as bVII7 (common in minor keys)
    • All chords have minor-key relationships

🎯 Why This Fixes Your Example:

For C7 Dm9 Dm7 Dm6:

  • D minor gets high scores for three Dm variations (tonic)
  • C7 fits well as bVII7 in D minor (very common)
  • C major gets low scores because Dm doesn’t fit as ii in C major context

Try the sequence again – it should now correctly identify D minor as the key! The algorithm now thinks like a musician, considering harmonic function and chord relationships rather than just counting note occurrences.


Here is some sample program output from this 3rd iteration:

4th Iteration

Then, of course, I thought of another change. Be good to enable chord inversions.

My prompt: Thanks! Can you provide chord inversions to minimise movement when playing on the piano? Can you also enable inverted chord input?

Claude: Excellent idea! I’ll add chord inversion support for both input parsing and voice leading optimization. This will make the tool much more practical for pianists.


Perfect! I’ve added comprehensive chord inversion support with voice leading optimization! Here are the new features:

🎹 New Inversion Features:

📥 Inversion Input Support:

  • First inversion: C/E, Am/C, Fmaj7/A
  • Second inversion: F/C, G7/D
  • Third inversion: Cmaj7/B, Dm9/C
  • Slash chords: C/G (non-chord tones)

🎼 Voice Leading Optimization:

  • Analyzes your input sequence and suggests optimal inversions
  • Minimizes hand movement between chords
  • Shows actual piano notes with octave numbers (e.g., C4-E4-G4)
  • Calculates movement distance in semitones between chords

🔧 How It Works:

Smart Inversion Detection:

  • Recognizes when slash chords are actual inversions vs. bass notes
  • Identifies first, second, third inversions automatically
  • Handles extended chords (9th, 11th, 13th) in inversions

Voice Leading Algorithm:

  • Starts with comfortable hand position (around middle C)
  • For each subsequent chord, finds the inversion requiring least movement
  • Considers all possible inversions and octave placements
  • Keeps voicings in practical piano range

🎵 Example Output:

For C Am F G, you might see:

🎹 OPTIMIZED VOICE LEADING (Piano):
  1. C - root position
     Notes: C3 - E4 - G4 - C5 (starting position)
  2. Am - first inversion  
     Notes: C3 - E4 - A4 - C5 (2 semitones)
  3. F - root position
     Notes: C3 - F4 - A4 - C5 (1 semitone)
  4. G - second inversion
     Notes: D3 - G4 - B4 - D5 (3 semitones)

Average movement per chord: 2.0 semitones

🎯 Benefits for Pianists:

  • Smoother playing with minimal hand jumps
  • Professional voicings that sound great
  • Practical fingerings in comfortable range
  • Shows both theory and practice – Roman numerals AND actual notes

Try inputting chord progressions now – the program will automatically optimize them for smooth piano playing while maintaining proper harmonic function!


I’ll need to test this version out, but so far looking fantastic!

I’m not posting the actual code output – but you can run the same commands yourself. I’ve given you the prompt ‘code’, which is no longer code as we have known it, which scarcely even resembles anything like a traditional ‘technical specification’.

Reminder: how to run the Python output

  • run the prompts in Claude to produce Python output.
  • copy the code and paste into a text editor (I use Sublime Text with Python formatting support)
  • save the file to a folder on your system. I called it ‘FinishThatSong4.py’
  • make sure you have Python 3 or later installed (check online for instructions)
  • open Terminal/Command line and navigate to the relevant folder via ‘cd’ command
  • run the following command: python3.11 FinishThatSong4.py (note that you can also just write python3)

Improvements

Need to create as an executable desktop app. Assuming that I’d be better to shift from the free to the ‘pro’ subscription tier if I want to make this easy. Claude pro allows Claude to run from the Terminal/Command line so that it has full system access – the capacity to create directories and files on my system, download dependencies and directly execute instructions.

This entry was posted in Uncategorized and tagged , , . Bookmark the permalink.

Leave a Reply

Your email address will not be published. Required fields are marked *