Symphony in C++
August 19th, 2010 Posted in classical music
Every now and then I can’t keep my classical music and science nerd parts apart.
Today was one of those days.
I present: A Symphony in C++:
key get_secondary_key(key home_key){ if (home_key.minor == TRUE){ secondary_key=relative_major(home_key); }else{ secondary_key=home_key+5; } } void sonata_form(key home_key,tempo base_tempo){ //Calculate secondary key secondary_key=get_secondary_key(home_key); //First get people in the mood intro.play(home_key,base_tempo--); //start exposition, introduce the two main themes theme_1.play(home_key); transition.play(home_key,secondary_key); theme_2.play(secondary_key); codetta.play(); //development, mix the themes up for(int i=0;i<development_length;i++){ combine_themes(theme_1,theme_2,key=rand()).play() } //Recapitulation: repeat the themes but in the home key theme_1.play(home_key); transition.play(home_key,home_key); theme_2.play(home_key); //Finish up if(composer == "beethoven"){ coda.length_in_min=10 }else{ coda.length_in_min=1 } coda.play(); }
Want to learn more about classical music but without the code? Go to getintoclassical.com.

August 20th, 2010 at 4:48 pm
That’s pretty awesome. :) The Beethoven part is hilarious.
September 4th, 2010 at 7:39 am
As a fellow Classical music nerd and CompSci major, I find this hilarious. The Beethoven part is awesome.
September 8th, 2010 at 1:43 pm
Hey, great post Ben. I’ll be giving this a shout out this Saturday on my site (www.killingclassicalmusic.com) for the Science & Music feature.
Looking forward on your site!
-Grant
September 11th, 2010 at 10:19 pm
Thanks for the comments everyone!
GCComposer: you’re site looks really interesting, thanks for the link, and keep an eye out for a link back.
Ben
September 12th, 2010 at 12:42 pm
This is amazing! And, you could really expand this :) For instance, get_secondary_key could have a home_key+1 for Prokofiev, and the composers with long codas could become an array including Tchaikovsky.
I’d love to see one for a fugue, too. Maybe I should write these for practice in both musical form and programming (PHP for me, but close enough).
September 12th, 2010 at 12:58 pm
Hi Helen,
Thanks! I would love to expand this — in fact throughout the writing/composing/coding of this post I was intensely wishing that I had more than just a cursory understanding of musicology! It looks like you have the perfect mix of music/coding skills to extend the concept.
In one of the initial drafts of it I tried to actually write a prototype for the combine_theme function in the development but it ended up being too specific and basically like a fugue: if I remember correctly it randomly reversed, inverted, or combined the themes in random proportions, a random number of times. I think it would be awesome to do this specifically for a fugue — and you could even use the code for real but output the combinations as images…
Overall I think the representation of classical music as code is a fantastic way to explain the structure of the music to a lot of people who would not otherwise be interested. Before getting into classical music I didn’t even realize there WAS an underlying structure — learning that there was, and that you could piece it together if you know what to listen for, was a huge and exciting revelation.
Let me know if you’d be interested in collaborating on expanding this…
Ben
September 14th, 2010 at 2:18 pm
Ohhh I would love to work on expanding this! Classical music forms for programmers!
I think fugue would be especially appropriate given how dissect-able it is. I haven’t done anything with C++ in 10 years (hurts to write that, especially since I’m not even remotely old), but it’d be great to see visual output, i.e. in the form of a graph. I could definitely do it on the web with PHP :) Which is so close to C++ that translation work is minimal, anyway.
So many other types of music could be written… aleatoric, 12 tone, theme and variations in various styles, etc. I smell a series!
It’s so great that you’ve learned the art of listening to structure and not just notes – it’s one of the big things they teach us in theory class here in conservatory land and I would definitely consider it game-changing, whether or not you’re heavily invested in classical music.
In any case, I hope you have my e-mail address from this comment, and I’ll watch these comments, too. I’m super interested!
September 26th, 2010 at 8:36 pm
Really nice bro! Never thought C++ would make this good sounds. Studied both music and computer but never think about putting them together.
btw, Chuck would do this easier and more logical. nice job anyway.
July 14th, 2011 at 6:52 pm
Hi,
Nice!
It is just that it looks more like C than C++, except for the member function calling syntax. I would love to see how sonata.h looks like!
Now that I mention it… it woild be certainly more adequate to write it in C, for beginners, and then C# (a half tone up) for more OOP oriented people ;-) I got the idea from comments in barrapunto.com, where this entry was refered: http://ocio.barrapunto.com/article.pl?sid=11/07/14/0734240&from=rss
July 16th, 2011 at 10:28 am
Does not compile:
$ g++ -c symphony.cpp -o symphony.o
symphony.cpp:1: error: ‘key’ does not name a type
symphony.cpp:9: error: variable or field ‘sonata_form’ declared void
symphony.cpp:9: error: ‘key’ was not declared in this scope
symphony.cpp:9: error: ‘tempo’ was not declared in this scope
Unable to parse, so unable to learn.