-
Maybe a tad misleading in the title; I'll do my best to explain...
I'm writing code that takes a MIDI file and can tell you what key the music is in. This is straight-forward enough when the music has 1) no accidentals, and 2) no key changes. Lots of music uses accidentals/has key changes of course, and I'll need to find a logical way of explaining that to the computer. I won't go into the techy details - I just wanted to start a discussion about how to determine the key you are in more uncommon musical contexts, such as jazz which may deviate from the key with every flipping chord. Here are some open ended questions that I'm wondering...
What sort of rules do you all follow for deciding which key the music is in?
How much chromaticism does there have to be before a song no longer has a key?
What if the whole piece of music contained less than 7 notes, and shared notes equally with several keys?
How is the melodic minor likely to wreck my day when trying to code this?
Does jazz technically change key ever, or are radical changes simply considered radical deviations from the key?
What the hell is a key?
Answering any of these questions should help me on my way. Thanks
-
07-30-2015 04:15 PM
-
It's not always easy to tell.
In common practice music (Mozart and all that kind of ting) pieces usually start and end in a particular tonality - C major or E minor say - with various key changing hijinx in between. The forms of the classical era have strict does and don'ts - but the more progressive composers (including Mozart) were interested in stretching the rules.
Things loosened up during the 19th century, with Wagner being arguably the first to kick out the old system (by writing interminable long music dramas where he could structure the music thematically based on what was going on with his various Teutonic dunderheads.) Schoenberg washed his hands of the whole debacle by 'emancipating the dissonance.'
Where does that leave us jazzers? Well, for example, we have the AABA Tin Pan Alley song form which is like a Mozart sonata in miniature. A song like The Way You Look Tonight has an A section in F major an then changes key to Ab in the B section, and back again. Because of this we say that The Way You Look is in F (or whatever the A section is) even though the song has a different tonality halfway through.
This often causes confusion - songs like After You've Gone and Just Friends, for example, start on the IV of the key, which often gets confused for I. Same for Autumn Leaves - if it starts on Cm7, the key is Gm. So I often say 'Just Friends - G major, first chord C' just to make sure there's no confusion. Often the last chord of the tune (excepting the turnaround) is a good indicator of key, but that's not universal. Moment's Notice and Giant Steps are in Eb, for example, by that logic.
In terms of functional harmony, it's very common for there to be progressions that might be sort of key changes. Take a I-VI-II-V for example. This progression is commonly played:
Imaj7 - IV7 - IIm7 - V7
Now, the VI7-IIm progression is term a temporary tonicisation (or secondary dominant.) In effect we have said - 'and now, IIm7 is a key!' by putting a VI7 in front of it as opposed to a VIm. So in Bb, we would have a brief key change to Cm.
This is common in common practice music and is understood to be momentary modulation from the main key to a closely connected key (subdominant minor function.) The overall key is still understood to be Bb, and is the 'boss key' that lords it over the hierarchy of related keys - Gm, Eb, Cm, Dm for example, that can be briefly alluded to over the course of the piece.
More remote modulations (i.e. keys with fewer notes in common) would be common in the B section of a standard (or the 'development' section of a Sonata.)
Some tunes use more remote modulations in the A section - Early Autumn and What's New spring to mind....
With Giant Steps etc we see rapid temporary modulations to much more remote keys to the point where the Eb key centre never feels 'in charge' until the end of the tune.
Of course this is no longer the 1950's and we have a lot of different types of harmony to deal with. Obviously there are the modes - but that's relatively easy compared to the non functional harmony of composers like Wayne Shorter. Discussing key with respect to many contemporary jazz compositions may be irrelevant - sometimes its easiest if you just write everything out with null key signature (no sharps or flats) and are done with it.
Hope that answers your question!Last edited by christianm77; 07-30-2015 at 06:03 PM.
-
good answer ....
what is a Key is actually a pretty deep question !
-
You listen. The key is what sounds like key. (Just having no accidentals and no key changes doesn't mean the key is C major, if that's what you're thinking. That's the most likely key, but not the only possibility.)
Originally Posted by Ollie
As you say, of course, some music moves through several apparent key centres (although most jazz tunes have an overall key they start and return to).
Still, can your code listen to music? If it can't, it can't identify key - a few intelligent guesses might be possible. But what's the point of that? Hearing a key is one of the easiest things in music - for a human. I can't imagine that I'd want any software to give me a few possible answers when the obvious one is in my ears.
It's not a question of quantity, but of how the chromaticism is used. Music with no key will usually have all 12 notes used with equal weight (atonal).
Originally Posted by Ollie
You'd still be able to hear what the key was.
Originally Posted by Ollie
It won't be the only thing. (see all the things christianm77 lists.)
Originally Posted by Ollie
Both. A key change is a "modulation", and most jazz tunes will have one modulation, sometimes more. But they will usually have a lot of secondary chords producing temporary "tonicisation" effects.
Originally Posted by Ollie
The sense that one note (and chord) has an aural governing role in the music. It can't be determined by simply looking at the music and identifying notes or chords - although that should help narrow down the possibilities.
Originally Posted by Ollie
Of course, a lot depends on why you want to write code that will do this. What purpose do you think it will serve? Who do you think will find it useful? (Maybe people who have the wrong idea about what a key is?)
Maybe it's the key signature you want the code to identify? That should be easier (especially from a MIDI file), but is still going to be difficult unless it's obvious enough for no one to need it.
-
You could try a statistical analysis. Count the occurences of each pitch class in a piece. Assign key accordingly.
Would be interested to know if this would actually work.
-
Originally Posted by christianm77
I've already written code that reads all the notes in a MIDI file, compares those notes to all the MIDI notes that could possibly occur in each major key, and returns confirmation that the song is in a particular key if the total number of notes are found to belong to one of the twelve keys only. In other words, no accidentals; no key changes = it does work under those circumstances. if it doesn't work, it would then see if the seven most used notes total number belongs to a key, which would account for accidentals in some cases. Also wrote a Max/MSP patch that does exactly what you said as my initial concept. We're thinking along similar lines!
@JonR
No, my code can't listen to music, but I don't see why that means the listening process can't be broken down into logic. Just because you can sense the key of a song as a human unconsciously doesn't mean that process can't be made conscious. I grant you, it be tedious logic, but it's possible. Just imagine that every single true or false fact you could say about any particular piece of music is a programming jigsaw.
The purpose of this code isn't simply to identify song keys. This is only one sub-function of a data model for analysing music in its entirety. The model will have creative applications and hopefully be capable of some meaningful data differentials for comparing one MIDI file's musical contents with another's. It's all part of my degree project. I'm essentially trying to give my capacity to think about music to a computer.
**EDIT: Also, I can't rely on every MIDI file I want to use to have a key signature meta message, although the code will check for that sort of thing where relevant too.Last edited by Ollie; 07-30-2015 at 07:31 PM.
-
try 'fee fi fo fum' by wayne shorter !
(first chord Eb7)
beats me as to the key .... maybe Bb
maybe ....
-
There's an old thread on TPG with 585 replies debating the key of "Sweet Home Alabama," a tune that has three chords.
It shows that people don't always hear or analyze things the same way.
Danny W.
-
The point is, it's subjective.
Originally Posted by Ollie
Most of the time, it's true, everyone will agree on what the key of a tune is. And in those cases, the objective details of the music (things easily examined by code) would almost certainly point in the same direction.
But there are significant numbers of tunes where that's not the case. The key centre can be ambiguous, even in very simple tunes (often especially in simple tunes).
Key depends on one chord appearing to be the prime chord. But sometimes two chords can vie for that role, and which one dominates depends on our listening experience. Some people naturally favour an opening chord (because the music they listen to tends to start with the key chord) and give that an extra weight against which all following chords will be measured. Others (particularly those used to the fact that music doesn't have to start on its key chord) will wait and listen to a whole sequence before making a judgement.
Neither one is "wrong". The ambiguity of key is actually an important factor in such tunes. It really doesn't matter what the key is. It can be anything you like. (Some people will swear the way they hear it is "correct" and admit no other possibility; others will admit the ambiguity and can make themselves hear it either way.)
IOW, the most useful thing your software could do in those cases would be to offer both choices as equally valid, or to simply return a "don't know".
From that angle, I suspect you're right, and code could handle all the variables necessary. There would be a lot, but - being no coding expert! - I bet it's within the bounds of possibility.
However, that still supposes that you're dealing with music that adheres to the concept of "key" in any case. There's a lot of modal jazz that doesn't do that, and where the question of key is irrelevant. It would be like asking a mechanic to examine a boat and tell you what kind of car it was.
Sounds fascinating, a great project. All I'm saying is that there are human flies in the ointment here (if that's not too weird an image...
Originally Posted by Ollie
). It's true there's all kinds of details in music that software could analyse, usefully. But we each bring our individual background and experience to listening to music, and will each hear in different ways. Some of those subjectivities are unimportant, or even problematic - and an objective analytical tool could serve as a useful corrective there. But others will over-ride any objective analysis. (This is just for the kind of musical details that can be preserved in MIDI; let alone aspects of music that can't be reduced to digital information - not without using enormous amounts of processing power anyhow.)
Key is certainly one of those things that is mostly easy to identify, via a few common signals, but ambiguous exceptions need to be allowed for. (And modal tunes may need a different viewpoint.)
As I'm sure you know, there are a lot of programs out there claiming to identify certain aspects of music - either by analysing audio, or MIDI, or just by user entry of details - and pretty much all of them are laughably incompetent, at least when it comes to cases where one could really use some help. They usually know less than a music student in his first week at college - and a deaf one at that! In a sense, they're like robots programmed to do things you could do much more easily (and quickly) yourself, given a little musical experience. (And if you don't have that experience, then you're not capable of judging how accurate the program is; if you trust it, then it's worse than useless.)
Hopefully yours will be way better than that!
Last edited by JonR; 07-31-2015 at 05:43 AM.
-
The problem is that all the references and terminologies in arts are highly conventional...
they do not represent any truth - they only help to communicate, or to express initial personal perception of piece of art in certain cultural enviroment
It's not physics where terminolgy is put in objextive reality a priori.. so we consider it (also conventionally by the way) to be 'true' from any pov... (though I think even physics today moves beyond this concept)
anyway in physics you can have an event terminologically described as a reference point... you do not need to have any personal experience with it.
But in art it is just impossible...
you have to live the music thourgh personally, you and only you...
after all and all and all it does not matter if you know what key changes are there,
it is important if you can hear these relations...
(that's why there can be 'uneducated' listneres who understand musica perfectly well - and often better than educated)
excuse me for such a general speculation.
As per programm - this option could be more or less functional for the music where functional tonality system was at the highst point of development (high baroque and early classical period).
Because at these period key relations are clearly defined and you can more or less systemize it, and the results that your programm will show will more or less represent the realtions within music.
As per later music or jazz... I just do not see if it makes sence or will work... even the understnding of what key is becomes too vague..
-
It's often the tasks that humans find easy - like using language - that are the biggest AI problems. This probably falls into that category. If you have a solution that works most of the time and is able to be continuously refined and improved, that may be the best you can hope for.
On the subject of chromatics, you probably need to consider the bar/beat structure - pay more attention to notes falling on the downbeat.
I should add that I'm more knowledgeable about AI than about music!
-
I suppose these are the types of music I'd love to discuss in more detail. As you say, the key can be subjective, and sometimes unimportant to know. It can be useful for the program to decide on one though. For e.g. if there were two equally valid choices for a key, it could pick it's favourite of the two as the primary key and the other as its secondary, just so the program has a way of saying "song A's data behaves like this". Thankfully my aim is not to write a program that could debate the key of a song with humans =P So, imagine a song that is really all-over the place - the program could simply say "this is all over the place", and lump it in a category with all the other examples of wild chromaticism. However, it'd be better to say more wherever possible. Even if whether it has a key as a true or false, rather than trying to define one under those circumstances may be useful.
Originally Posted by JonR
The coding is always possible. That doesn't mean I'm definitely going to be the one to figure it out, but I know enough to know that. We can extract data such as which chords are played at any given time (both within tracks and across tracks) how many minor thirds were used in the piece over all; how many of those thirds belonged to m7th chords, how many chords were played in-between those chords, and much much more trivial info. This may be useful in the end though. For e.g. if we know two potential keys from the code, but the reality is that there is a key change half-way through the song, we may be able to say "it might be in C or it might be in G; find locations of tonic chord of C and G, then do the same for II chord, and so on. So it's like saying "We'll assume the key is C; if it were C, certain chords would likely be found here; scan for those; show scan results"
The modal jazz you speak of interests me, if you could discuss this in any more detail? I should add that I intend to throuroughly test the code with all sorts of music. I don't want anything to be able to trip it up (even utter nonsense, black MIDI, files with incorrect key sig meta messages etc) If you have any good examples of this music, as well as guidelines for how to think of key/mode, I might be able to get MIDI versions from a guitar tab file or something. It might be apparent that I can come up with my own system of music theory if I want to here, but I thought it would be better to stick to widely understood theory. I'd be able to say whether a given piece of music adhered to the rules of counterpoint and such things, which may have uses I haven't considered yet.
-
If my MIDI contained information detailing the location of bars, there would be no need for this discussion, as everything could be implied from a combination of the context of the individual bar, and it's surrounding bars. That said, I might end up going down the route of adding bar messages to the MIDI files I'm using, but for the time being, I'm trying to write something that can read barebones MIDI; note ons, delta time, and durations are pretty much the only data from the MIDI I'm working with. Not bothering with velocity because most MIDI files I can easily acquire have a static velocity value and don't represent the composition.
Originally Posted by ohlcv
That's interesting. I know nothing about AI! You're most welcome to weigh in from that perspective.
-
Yeah - I would imagine that for a piece in the key of A major for example, you would find more C#'s than C's, say.
Originally Posted by Ollie
But something tells me it won't be that simple every single time.
-
I'm intrigued, and I'm wondering what the ultimate purpose of the program is. Is it for humans to use? (and in what contexts?) Or for computers to write music in some way?
Originally Posted by Ollie
OK, and the point being?
Originally Posted by Ollie
I suppose the first thing would be to point to some simple examples:
Originally Posted by Ollie
Miles Davis: So What, Flamenco Sketches
Herbie Hancock: Maiden Voyage
Freddie Hubbard: Little Sunflower
More complex examples would be:
Wayne Shorter: Ana Maria
John Coltrane: Naima
Joe Henderson: Black Narcissus
Simpler stuff from rock:
Santana: Oye Como Va
Beatles: She Said She Said, Norwegian Wood, Tomorrow Never Knows, Within You Without You.
Rolling Stones: The Last Time
Pink Floyd: Set the Controls for the Heart of the Sun
Joe Satriani: Flying in a Blue Dream
"Modal rock" usually has an obvious key centre, but not with the usual major or minor key sets of chords. IOW, interpreting from the chords or pitch collection alone would point in the wrong direction. But it would be easy to identify the most common chord root used, which is usually a good guide.
With modal jazz, key centres shift, often chord by chord. In tunes where there is a consistent bass pedal (resembling a tonal centre) the chord/scales will change over the top.
Eg, Little Sunflower is pretty clearly "in the key of D", but it isn't D major or D minor (strictly speaking). It's D dorian mode (mostly). So What is also mostly D dorian, with a shift to Eb dorian. Broadly speaking you could say they're both "in D minor" (kind of), but they're nothing like a normal D minor key piece.Last edited by JonR; 07-31-2015 at 09:44 AM.
-
Originally Posted by Jonah
It might be better of me to say that I'm not really trying to define the key here. I'm trying to define the melodic behaviours that a song does NOT deviate from. This could be as simple as the song not deviating from the notes of a key, which needs two conditions: Every note played belongs to one particular key, and every note of the key in questions has been used at least once. Those circumstances lead to absolute certainty. If we get into jazz where the key is less distinguishable, then we just need to be able to describe the boundaries of the music; we say what the music does not do under any circumstances, leaving it free to do anything else by definition.
-
If you take early classical or high baroque period it will be relatively simple.. all you have to describe in codes is
1) principles of main types of cadences
2) stipulations for 'change of key' - that should be repeated cadence in the new key I think - modulation
3) stipulation for 'key deviation' - I do not know if there's term in English - Ausweichung in German - when there is meant a key change but still it is not considered to be real modualtion (oin classical transitional or developing sections, in jazz stadards often 'bridges')
In general most of old jazz stadards that were originally songs will also fit this set-up - of course if we approach them from point of view of functional tonality.
But it will be very simple and in many cases deceptive... especially point 3) - there are arguable cases..
I can dnamely point you a Mozart sonata where I discussed what might be the key in some bars of the transitional section - and we could not precisely name it - though people were experienced...
The idea is it's ambivalent -
PS
Don't forget please that the notion 'key' at least in the meaning we use here (if I am not mistaken about what we're talkng about) - it is tonality - and there's music that might use the same notes but do not use the keys... that is key is a notion of complex harmonic and semantic conception, not just 'notes that fit the key'. Context on all levels is important.Last edited by Jonah; 07-31-2015 at 10:23 AM.
-
The answer to that question is long enough to fill an honours project proposal (and it will). I'll try and give an overview, but I'll have to leave out technical details and get to the point. The data model its self will serve as my degree project. The applications of the data model are first and foremost for me to use extensively in two projects - a MIDI utility plugin, and a video game.
Originally Posted by JonR
The main function of the data model is essentially to take different songs, and use them to write new music. I have no interest in randomly splicing MIDI together; what I'm trying to make possible is for the musically meaningful aspects of the music to be taken into consideration in order to intelligently 'compose' new music. Now, this is where the plugin comes in. I'm planning on making a device for Reason that allows the user to use MIDI files as musical templates for new music. The user interface would be intended to give songwriters the ability to take all their half-finished guitar pro tabs, export the MIDI, and import them into the plugin bank for manipulation. Again, I have no interest in having songs written for me as a musician, so the plugin would be mostly designed as a creative tool. I mostly find the need for this because I write quite technical music, and often have to program it before I can here what my idea really sounds like. The overall ethos here is that there comes a time when a mathematician would stop doing mental arithmetic, and start using a scientific calculator. I want to create the equivalent of that calculator for music. How it is used is up to the individual.
There is not much I want to say about the game, because I'm not ready to tell anyone those details yet, basically. What I can say is that I want the game world to be controlled entirely by music, and for the player character to alter music, and by proxy, the world. I hope to subliminally teach gamers music theory/to compose music, so that when the achievement pops up at the bottom of the screen saying "You completed the game!", it actually means that the player MUST have learned something about music, otherwise they could not have solved the puzzles.
Lastly, I have one more idea for a project using this model. Details are extremely sketchy on the what, but crystal clear on the why: to combine all the music in the world. If you imagine that I will take two songs (as MIDI files) and turn them into two new types of file. This new type of file is a MIDI file, but with additional information about the music it has inside it. Imagine that one of these new files can be created from two old ones... This is the same concept as the plugin, however, the purpose is to allow an artists music to be recognized as belonging to just that - an artist. Not a genre, or any other way we might try to categorize music collectively. I want to see what happens when you take the collective musical efforts of all my favourite artists, and I'd like to give other people the same opportunity, so I imagined this project taking the form of a website, where artists can upload and contribute their music to the collective musical intelligence. What can I say - this one's a bit out there...
Additionally, I hope to be able to release some of the code as open source, and would probably do so in association with the C++ MIDI library I'm using, and also release any appropriate music or audio scripting objects I create for the game in Unreal 4. I can't promise anything, but otherwise all of the above is a fair summary of my intentions.
-
My friend - an outstanfing composer, musicologist and musician (violinist) - has been long interested in the problems of musical semantics but from point of view of aesthetical perception...
and once he was approached neurologists and IT-guys who were doing researches on human perceprion of music and possibility to develope mind creative enough to compose music...
they invited him as a consultant - it was an interesting period and he shared with me with this expereinece and he even himself became interested in it for some time...
But that was really complex unversity research or something..
I mention it because I think that to create really effective programm who would compose music it must be really very complex...
You see in my opinio artistic creative process is connected with personality - even collective...
and to realize this process artificially you have actually to imitate human person... no need to say how complex human person is.
What will be the power that brings integrity to music composed by such a program?
My questions here are not to discourage.. I am really interested in what you're doing and wish you succes even though I may critical about it.
-
Key? That word is ambiguous.
'Key Signature' does not always equal 'Tonal Center'.
Key Signature is somewhat at the discretion of the person that notates the music.
For example a G-blues could have the key signature of one sharp (key signature of G) or no sharps or flats (key signature of C). It just depends on the preference of whoever notated it. Regardless of the key signature, the G-blues tonal center is G.
-
Yes sureKey? That word is ambiguous.
'Key Signature' does not always equal 'Tonal Center'.
Key Signature is somewhat at the discretion of the person that notates the music.
For example a G-blues could have the key signature of one sharp (key signature of G) or no sharps or flats (key signature of C). It just depends on the preference of whoever notated it. Regardless of the key signature, the G-blues tonal center is G.
But it's more about notation... l
I think in the context here it makes sence to speak of the 'key' as of 'tonality'.. or no?
-
Originally Posted by Jonah
Yes, I completely agree! This is not something I can claim to emulate, however, I think that by extracting musical data from all of one particular artists songs, there may be mathematical similarities/musically specific idiosyncrasies shared among the different songs that make the collective data for that artist quite unique in comparison with another set of collective data for another artist. I don't find the questions discouraging - I'm pretty obsessed with the idea (have been a while) so I've visualized a great deal of the concepts. I forget sometimes that others haven't imagined this in the same detail. It's expected that people would be sceptical, but the only real way to know how this ends up is to try and do it, right? To try and answer you directly anyway, I don't think of the program as composing the music, so much as the music composing the music. The program will not be able to improvise a lead or anything like generative music would normally do, but instead it will perhaps treat one song file as a listener than can be 'influenced' by other music. It's gonna take years really...
Just to add, I'm appreciating all the replies, suggestions and music examples from everyone. As Jonah mentioned last, I'm not so much concerned with notation as tonality.
-
good luck with the programing
its gonna be tough !
eg.
if you play ....
|C |C |D7 |D7 |
|Dmin7 |G7 |C |C |
(like in A train)
(secondary Dom II chord innit)
are you in the key of C throughout ??
[strictly speaking .. when your in bars 3 & 4
you're in G]
but it sure doesn't feel that way to me
it feels like i'm still in C
-
C = C E G (C)
D7 = D - F# - A - C
Dmin7 = D - F - A - C
G7 = G - B - D - F
Okay...
-Every note of C and G major keys have been used .
-The note C is used in 3 out of 4 chords (once as root, twice as 7th).
-The note G is used in 2 out of 4 chords (once as root, once as 5th where C is the root)
- C is the 1st chord played
- There are 4 C chords, 3 D chords (2 belonging to key of G, one belonging to key of C), and only one G chord
- Whilst we might argue that as stated above, the two D7 chords 'belong to the key of G', we can also say that this
chord belongs to C, but has one altered note (F >F#).
- If you add up the total number of times each F/F# note is played, they are both used equally.
- In this example, F is C's 4th augmenting OR F# is G's 7th flattening (I haven't really figured out the significance of this)
We have four options the way I see it:
-The key is C
-The key is G
-The music has two keys
-The music has no key
We could argue any one of these, but surely we'd draw a conclusion in the end. Maybe I'm being biased in some way,
but on paper it looks like the key of C to me. It sounds like the key of C to you. I think if I ran this through my code that I've done already
it would come out C and G as being equal, so a few logical checks based on what I've listed would end up with the computer deciding it's in C.
Is it in C though? Haha!
-
i only meant to show how complex
your quest will be !
if you put in 'fee fi fo fum' i'll bet you get
a lot of ambiguity out !
(mind you , i can't work out what to play
on it myself !)
btw the F# is G's 7th (not its flat 7)



Reply With Quote

“Shearing style”
Today, 05:26 PM in Comping, Chords & Chord Progressions