Live data from Hacker News

Basic Music Theory in ~200 Lines of Python

mvanga.com

31–40 of 192 posts

Re: Basic Music Theory in ~200 Lines of Python

#31
post #24

Earlier quoted context omitted.

Agreed. I whinge about this all the time. The C-based system is convenient for piano players but it's a mess for guitar players, violinists, and other instruments where there are no There have been many attempts at a chromatic music notation, but nothing has caught on so far [1]. Things are a little better with solfege -- there is "chromatic fixed do" solfege, where every note has its own name, rather than only havin…

I find the paino-roll notation on DAWs to be a lot more intuitive. Not much good for perfomers of course, but it helped me understand things better. Each semitone is given the same amount of space.

Here's that one weird tip that you were looking for all your life but didn't realize it: pretend the front part of the piano keyboard isn't there, and just look at the part closest to the fingerboard. Presto: chromatic keyboard.

Re: Basic Music Theory in ~200 Lines of Python

#32
post #20

I've worked on music theory coding for a while. I originally used a dict-lookup style like you have done, but found a simpler way (for me). The problem with that approach is you have to maintain values for enharmonics of note names. It's hardwired. Also what if you gave it something like ♭♭♭♭♭♭44? Why shouldn't it "theoretically" be able to handle that. It is "theory" after all. I use something like this to convert t…

Very nice! I like your way much better than the one in my writeup :-) I'll refactor things over the weekend to use this approach if that's OK with you.

That would be very cool! Maybe just give me a mention if that is cool, you can use the code verbatim (or changed) if you want. My name is Édrihan Lévesque. My book on music theory which isn't out is called Way Of Change.. which is what I refer to these algorithms by.

You might just realise how this approach goes back into keys.. like Ab, C#, F.. it's almost exactly the same, but you have to account for the accidentals being on the right side of the string as opposed the the left, as it is in Jazz.

And ya! - I actually originally wrote almost exactly what you wrote.. but I kept adding enharmonics of things.. like ['3','##2','b4','bbb5'] # and so on..

So I got to a point where it's like.. yeah this should just understand it. I'll give you another hint for the keys.. Use the scale degree to get your root note name. Get rid of the accidentals (do it after). Once you know that Major in dist == [0,2,4,5,7,9,11] you can use the list that contains all 12 notes in one spelling to find it. That's why I'm getting rid of accidentals. That way if you're looking for C# but you wrote as I did with all flat-spellings, it throws away the "#", finds the 'C', counts from there, and finally adds the sharp back if necessary. Just kinda paying attention to adding a flat to a note with a sharp.. they cancel out etc. Usually that's why it makes sense to keep the degree part separate from the accidentals part in some way. At the end you reconcile a difference between distance and degree-distance. Really easy to do double sharps or flats that way cause you know that all valid note names will work.. don't have to worry about giving it a particular format.

Not only can you use any names notes may have, but you can specify an odd rule.. like for example the difference between looking at the scale in "Western" vs. "Indian". Let's say a scale like Mela Vanaspati/Raga Bhanumati/Zaptian (number 1129 on my site). If it's Zaptian, then let's say we're Western. I'd say it's spelled like the first line following this. If it's a Raga or Mela and we're looking at it that way then even in Jazz we can correctly see it how it's originally stated as the second spelling.

1 b2 2 4 5 6 b7

1 b2 bb3 4 5 6 b7

For me in this case the Indian numbers make sense as you are just counting up integers.. albeit with the "ugly" double flat. And yes it's ugly unless you were using a system that doesn't express it as uglily. Here I'm just comparing the first three notes in a few ways. Let's say for a bb3 a system that would express that less ugly than some would be in the key of C#.. as in [1 b2 bb3] == [C# D Eb] == [Db Ebb Fbb]. Of course all these can be described as [S R1 G1]. This is how it's notated in Indian.. but equivilent to Jazz in that there is a part that talks of which nth note of the change and a part that talks about how far from where it usually is. Obviously C# is better for this change than Db. Even if you use the Western Jazz to derive it it's not good unless in C#. Of course the Western jazz statement to me is more ugly because it doesn't count up in degrees sensibly from 1 through 7. The ugliness of the jazz numbers is equal to the ugliness of putting it in the key of C, like I said before. On other changes Jazz wins because Indian won't let you use #4 or b5.

I'm glad you'll use my codes too. Eventually once you have it working you can do a scale in the key of like... let's say Abbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb.. which is actually also known as C or even B#.. ok stay sharp out there in code land. ;) Music theory is an obsession of mine and fun with codes. There are always many options. There's more than one correct answer. And there are ones that make less sense than others.

And P.S. to anyone just dropping in.. we're lazy so we type b instead of ♭, and # instead of ♯. The former is pronounced flat, is equal to the number -1 and is pronounced double-flat if there are two. The latter is pronounced sharp, is equal to +1 and same rule applies about not pronouncing something like "sharp sharp" in music ever. This way I can pronounce C septuple-sharp, which I made up. That particular strange way to describe a note is equivalent to G because music is weird like that. Also if something has six sharps then you could just as easily say it has six flats. So B♭♭♭♭♭♭ is the same note as B♯♯♯♯♯♯. And yes those are the very sexy-sounding sextuple type words ;)

Re: Basic Music Theory in ~200 Lines of Python

#34

Earlier quoted context omitted.

The quoted sentence seems false to me. Most physical objects do not have naturally harmonic vibration spectra. The vibration modes are not integer multiples ofthe fundamental (except for a vibrating string). Only the finely tuned western instruments do. So this is a somewhat backwards argument.

> Most physical objects do not have naturally harmonic vibration spectra. What is your basis for saying this? A large number of physical objects, solids as well as hollow can be approximated as systems of springs, surfaces and tensile elements, which all have some frequency response. It isn't rare at all for a physical system to have a very sharp resonant peak in its frequency response, to the point that you'll often…

what you are describing is a graph. The laplacian spectrum of a graph is arbitrary.

Re: Basic Music Theory in ~200 Lines of Python

#35

Earlier quoted context omitted.

> Most physical objects do not have naturally harmonic vibration spectra. What is your basis for saying this? A large number of physical objects, solids as well as hollow can be approximated as systems of springs, surfaces and tensile elements, which all have some frequency response. It isn't rare at all for a physical system to have a very sharp resonant peak in its frequency response, to the point that you'll often…

what you are describing is a graph. The laplacian spectrum of a graph is arbitrary.

Every rigid object has a fundamental frequency, regardless of whether you put it on a graph.

Re: Basic Music Theory in ~200 Lines of Python

#36
post #28

Anyone wanting to take things back a step further to first principles may enjoy this (shameless plug - I wrote it) Deriving the piano keyboard from biological principles using clustering (Jupyter) https://fiftysevendegreesofrad.github.io/JupyterNotes/piano....

As someone who might use these with kids: I think the problem with both of these is lack of, well, sound. To get things, people need to hear sounds, not just see note names and pictures.

it could be argued that this is music theory, and therefore sound belongs to the realm of music practice

Re: Basic Music Theory in ~200 Lines of Python

#37

Earlier quoted context omitted.

The quoted sentence seems false to me. Most physical objects do not have naturally harmonic vibration spectra. The vibration modes are not integer multiples ofthe fundamental (except for a vibrating string). Only the finely tuned western instruments do. So this is a somewhat backwards argument.

> Most physical objects do not have naturally harmonic vibration spectra. What is your basis for saying this? A large number of physical objects, solids as well as hollow can be approximated as systems of springs, surfaces and tensile elements, which all have some frequency response. It isn't rare at all for a physical system to have a very sharp resonant peak in its frequency response, to the point that you'll often…

Many objects don't have audible vibration modes (infrasonic, ultrasonic, so damped that sounds are too brief and too quiet) but it doesn't mean that they don't vibrate.

Re: Basic Music Theory in ~200 Lines of Python

#39
post #17

There are maybe three aspects to music theory: (1) Theory of how things sound like: Tones, melodies, scales, chords, based on the frequencies of individual sounds. (2) How to name things. (3) How to handle the mess of naming things in Western music theory, where things have 12 different names, depending on which note you choose as the base. This post seems to focus on 3.

most disciplines, music included, have theory and practice. how things sound is an element of the latter, whereas why things sound the former. this article as the title atates is about music theory and does a pretty decent job IMO

I would be delighted to see a follow up article that explores frequencies and harmonics while sticking with the code demonstrations and incorporating a simple tone generator for the practice side of things

Re: Basic Music Theory in ~200 Lines of Python

#40
post #17

There are maybe three aspects to music theory: (1) Theory of how things sound like: Tones, melodies, scales, chords, based on the frequencies of individual sounds. (2) How to name things. (3) How to handle the mess of naming things in Western music theory, where things have 12 different names, depending on which note you choose as the base. This post seems to focus on 3.

I once listened to a podcast where fourier transforms were used to generate sounds that otherwise don't exist.
Post reply on HN