PlayWithMozart

I just completed a new art-toy: PlayWithMozart. To play, click on young Mozart’s image, below.

MozartAt11

Be sure to come back here and leave a comment if you enjoy the program of if you have any questions or suggestions. I plan to make the code available in a few weeks, after I have a chance to comment it.

The idea

The idea was simply to try to adapt Mozart’s Musical Dice Game, so that the choice of measures is not random, but deterministic in some fashion. Thus came the idea of drawing a melody line and have the program choose the measure that best fits the melody line.Mozart wrote a minuet with 176 measures. He also created two matrices of 11 rows and 8 columns (look at it as one matrix with 16 columns). Each cell in the matrix contains a serial number referring to one of the 176 measures in the minuet. In the Musical Dice Game, the player rolls two die, thus producing a number between 2 and 12 — corresponding to one of the 11 rows of the matrix. For the first measure, one throws the die and selects the measure in the appropriate row of the first column. For the second measure, one throws the die again and selects the measure in the appropriate row of the second column, and so on. The resulting composition is a re-arrangement of the measures of the original minuet. The brilliance of Mozart was writing 16 sets of 11 measures, such that any of the 11 measures of a set can succeed any 11 measures of the previous set, while sounding good musically (actually Mozart cheated a little bit; the last set only has 3 unique measures, as some measures are repeated in the set). Several sites explain this game and its history; I found this site and this one very useful.Instead of “playing the dice”, the program PlayWithMozart selects the measure that best fits the outline of the melody you have drawn, taking into account whether the melody line is rising or falling, and the average pitch of the melody line for that measure. It does constrain the choices to the set of 11 measures available at any point. In addition, it also penalizes measures previously chosen, so as to force more variety, so that all 176 measures will eventually be used. So, how satisfactory are the melodies resulting from this collaboration with Mozart? Try it for yourself!

Coding tools

After playing with ActionScript a few years ago, I switched to Java and later Processing, because of their then faster execution speed, wonderful IDE (eclipse) and wide variety of open-source libraries. While I find that Java really works well on the desktop, I have grown a little impatient with the time it takes to load applets to the browser and the fact that the browser locks while the applet is being loaded. With ActionScript 3.0, the recent improvements in the Flash Player, and he ability to use Eclipse with Flex, I decided to try Flex. I found the experience of programming Flex/ActionScript quite enjoyable, and was pleasantly surprised by the speed and responsiveness of the result (PlayWithMozart). I do miss the more advanced syntax checkers, style checkers, code formatters, and refactoring tools available for Java – though I expect Flex to catch up.

Producing sound with Flash/ActionScript

I first tried to use Flash’s built-in sound library for PlayWithMozart and was quite disappointed with my inability to control timing precisely. I tried a variety of strategies, but I found it difficult to synchronize separate sound clips. It sounded like timing was off by a significant amount. To confirm this, I wrote a quick Flash program to produce a repetitive beat and recorded the output. Looking at the attacks on the waveforms produced, I found that the beats were off by up to +/- 16 ms. I was about to abandon my project when I stumbled upon Andre Michelle and Joa Ebert’s wonderful Popforge library. With it, I am able to play and apply sound effects to 8 simultaneous stereo tracks, at 20500 Hz, using about 10 to 25% of the CPU. If you want to dynamically sequence sounds, or even synthesize them on the fly, using Flash, I recommend you look at the Popforge library.On a related subject – the piano sounds. Originally, I had planned to embed the waveforms for all 88 piano keys. However the resulting sound file was large and the Flex application was taking too long to load in the browser. So I decided to only use one waveform per octave and have the program pitch-shift that waveform dynamically, as needed, for the other keys of the octave. Some richness of the sound was lost, because all keys in the same octave a clones of each other, but this approach reduced the loading speed by about 90%. On reflection, I think a better compromise would have been to use two keys (e.g. C and G) per octave.

19 Comments

  1. Posted November 25, 2009 at 05 pm | Permalink

    Jan – I sent you the source code. PlayWithMozart uses sampled sounds; of course, to emulate the KAOS you will want to synthesize your own sounds rather than using samples — if you haven’t already, you can find examples of simple software synths in the popforge library. Good luck with your project!

  2. Gerald
    Posted January 14, 2010 at 05 am | Permalink

    Great project! I’ve been creating a BPM timer/keeper… my approach is to create an empty sound sample using bytearray and popforge’s SoundFactory, and using sound_complete. So far it’s somewhat accurate with the ms intervals being quite constant.

    I’d like to know how you did yours. I can send you mine for comparison.

  3. Posted January 14, 2010 at 10 am | Permalink

    Gerald – I sent you the source code. To see how I implemented the timer look for the file KClock.as. The timer gets incremented by the main loop in MainLoop.as. I look forward to seeing your BPM timer keeper.

  4. Adam
    Posted March 21, 2010 at 11 am | Permalink

    Hello!

    I know it’s been asked a few dozen times before – but I’m also beginning to work with the Popforge library and would be very interested in seeing your code to analyze as a basis for a piano song composition program I’m in the process of working on!

    Best Regards,
    Adam

  5. Posted March 21, 2010 at 06 pm | Permalink

    Adam – Am glad to share the source code — I’ve sent it to you. I’d be interested in seeing your piano song composition program when it is finished.

  6. Chris Ellis
    Posted September 3, 2010 at 01 pm | Permalink

    Hey, any chance you could share your source? I am struggling synching multiple sound and it sounds like you’ve got it sorted!

  7. Posted September 6, 2010 at 10 pm | Permalink

    Hi Chris – I sent you the source code for PlayWithMozart – I hope it helps. Just to make sure credit goes where it is due: Andre Michelle and Joa Ebert sorted out the sound timing issues with their Popforge library, so I did not have to worry about that. Good luck with your project!

  8. Don Park
    Posted September 20, 2010 at 01 am | Permalink

    Hi there. Could I also take a look at your code to see what I can learn from it. It sounded unusually ‘seamless’. BTW, any reason why you are not using github to avoid the hassle? My thanks in advance.

  9. Posted September 29, 2010 at 01 pm | Permalink

    Don – I just sent you the source code for PlayWithMozart – I hope it helps in your project.

  10. Posted December 14, 2010 at 03 am | Permalink

    Hello

    Your application is really cool, I am working on StandingWave3 now, it’s another cool library. I’d like to take a peek at your source files if possible. I think it could help me to study Popforge quickly. Thanks.

  11. Posted December 14, 2010 at 12 pm | Permalink

    Hi Michael – Thanks for your comments. I just sent you the source code so that you can take a peek.

  12. Damion Murray
    Posted May 23, 2011 at 11 pm | Permalink

    That is some amazing work. I too would like to take a peek at the code.

  13. Posted May 24, 2011 at 12 am | Permalink

    Damion – I e-mailed you the code – I hope it is helpful.

  14. Prashanth
    Posted July 9, 2011 at 02 pm | Permalink

    This is just awesome. I have been working on a sequencer app where in people can load difference songs into different tracks and play. I am currently using Flash audio sequencer created by Andre and his team. But I am kind of having problems with mixing more than 2 songs. Can I use popforge to do this? And also could you please share your sample code. It would be of great help.

    Thanks
    Prashanth Nethi

  15. Posted July 18, 2011 at 06 pm | Permalink

    Prashanth – PlayWithMozart, which uses Andre Michelle’s Popforge library can play 8 voices concurrently + apply delay and feedback to the mixed stereo audio stream, on a moderately fast CPU. Depending how many voices each of your songs have, this might work for you. I am away from my computer right now but will send you PlayWithMozart’s source code when I get back in a week or so.

  16. John
    Posted October 11, 2011 at 12 pm | Permalink

    Awesome!
    This is very cool!
    I’ve been studying algorithmic composition.
    Could you send me the source code?

  17. Posted October 11, 2011 at 03 pm | Permalink

    John – Thanks; of course, Mozart also had something to do with it! I sent you the source code. Enjoy.

  18. george
    Posted February 11, 2012 at 02 pm | Permalink

    Very nice, wonderful.
    Anyway IMHO it’s interesting to using to use whole synthesizer version, ex: https://github.com/maxl0rd/standingwave3, Ca you send source to check up? 🙂
    Thanks in advance.

  19. Posted February 12, 2012 at 11 am | Permalink

    George, thanks for your comments. Agree, standingwave3 is a more up-to-date library and is probably what I would use if I were to rewrite PlayWithMozart today. I sent you the source code. Enjoy!

One Trackback

  1. By young mozart - StartTags.com on January 25, 2010 at 07 pm

    […] a young Mozart and are contained in a manuscript owned by the Mozarteum for more than 100 years. …CODEMUSE PlayWithMozartTo play, click on young Mozart’s image, below. Be sure to come back here and leave a comment if you […]

Post a Comment

Your email is never shared. Required fields are marked *

*
*

*
To prove you're a person (not a spam script), type the answer to the math equation shown in the picture. Click on the picture to hear an audio file of the equation.
Click to hear an audio file of the anti-spam equation