Music in Python

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) # C
winsound.Beep(262, beatlength) # C
winsound.Beep(294, beatlength) # D
winsound.Beep(330, beatlength) # E

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

winsound.Beep(262, beatlength) # C
winsound.Beep(262, beatlength) # C
winsound.Beep(294, beatlength) # D
winsound.Beep(330, beatlength) # E

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

winsound.Beep(262, beatlength) # C
winsound.Beep(262, beatlength) # C
winsound.Beep(294, beatlength) # D
winsound.Beep(330, beatlength) # E

winsound.Beep(349, beatlength) # F
winsound.Beep(330, beatlength) # E
winsound.Beep(294, beatlength) # D
winsound.Beep(262, beatlength) # C

winsound.Beep(247, beatlength) # B
winsound.Beep(196, beatlength) # G
winsound.Beep(220, beatlength) # A
winsound.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… 

One thought on “Music in Python

  1. 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

Leave a Reply

Your email address will not be published. Required fields are marked *