Need help with a recursive function to name musical scales

0
9
Asked By MusicalCoder42 On

I'm working on a function to name musical scales based on binary representations of them. For instance, if I have a scale represented by the binary number 1001011100101 and a root note of F, I expect the output to be F Ab Bb B C Eb F due to musical theory rules.

My function is recursive and tries different spellings of the scale to find the one with the lowest cost. However, I'm facing issues with how costs are assigned, particularly in the calculate cost section. I noticed that the function returns certain costs that don't make sense, like Cb having a cost of 0 while Dbb has a cost of 3. Ideally, I want C to represent B and B to represent C instead, as they should have lower costs than Cb and Dbb.

I've also created some helper functions to assist with enharmonic roots and key signatures to ensure accuracy in music theory. Can any experienced coders help troubleshoot my recursion? I've provided the code I'm using for reference.

4 Answers

Answered By CuriousCoder On

Out of curiosity, how did you come up with this problem? It seems like a fun and complex challenge!

Answered By ScaleSeeker On

By the way, I noticed you have 13 bits in that scale representation, is that correct? I thought it repeated the starting note at the end.

Answered By NoteNerd99 On

Have you considered just using a lookup table instead of recursion? You might find that it simplifies your code and gets you where you want to go without all the computation.

MusicalCoder42 -

That might work, but I think recursion lets me explore all possible enharmonic spellings. A lookup table might limit me to a few options.

Answered By DebuggingDiva On

I couldn’t pinpoint the exact bug in your code, but I have a couple of suggestions. First off, for readability, try using named functions instead of all those ord and chr manipulations. It makes your code easier to understand. Also, have you tried running a debugger? It could help you step through the code and see where things go wrong.

MusicalCoder42 -

Great idea! I’ll definitely look into using a debugger. The manipulation stuff is just to get note letters, but I see how using named functions could help.

Related Questions

LEAVE A REPLY

Please enter your comment!
Please enter your name here

This site uses Akismet to reduce spam. Learn how your comment data is processed.