Music in Python

July 9th, 2007

I was learning C but I got a little bit distracted and spontaneously decided to start writing music in Python code. It’s no Beethoven or Mozart… in fact it’s just Yankee Doodle played through the WinSound library. It only works on Windows. The first argument to the Winsound.Beep function is the frequency of the note; the second is the length of the note. The song plays through your computer’s built in buzzer.

Code follows:

import winsound;

beatlength = 300;

winsound.Beep(262, beatlength) # Cwinsound.Beep(262, beatlength) # Cwinsound.Beep(294, beatlength) # Dwinsound.Beep(330, beatlength) # E

winsound.Beep(262, beatlength) # Cwinsound.Beep(330, beatlength) # Ewinsound.Beep(294, 2*beatlength) # D (double length)

winsound.Beep(262, beatlength) # Cwinsound.Beep(262, beatlength) # Cwinsound.Beep(294, beatlength) # Dwinsound.Beep(330, beatlength) # E

winsound.Beep(262, 2*beatlength) # C (double length)winsound.Beep(247, 2*beatlength) # B (double length)

winsound.Beep(262, beatlength) # Cwinsound.Beep(262, beatlength) # Cwinsound.Beep(294, beatlength) # Dwinsound.Beep(330, beatlength) # E

winsound.Beep(349, beatlength) # Fwinsound.Beep(330, beatlength) # Ewinsound.Beep(294, beatlength) # Dwinsound.Beep(262, beatlength) # C

winsound.Beep(247, beatlength) # Bwinsound.Beep(196, beatlength) # Gwinsound.Beep(220, beatlength) # Awinsound.Beep(247, beatlength) # B

winsound.Beep(262, 2*beatlength) # C (double length)winsound.Beep(262, 2*beatlength) # C (double length)

Now just to give the computer some artificial intelligence and I can create my own top 10 hit… 

  1. Python for scripting in Mozilla

One Response to “Music in Python”

  1. Nicolason 12 Jul 2007 at 6:54 pm

    Hello Khlo,

    I’m Nicolas.

    I work for a web hosting company and I  noticed your image script reflection.js. I like it!

    How could I contact you in private about this?

    Looking forward reading from you, 

    Best regards,

    Nicolas Morel

Trackback URI | Comments RSS

Leave a Reply