Computing · Teaching

Python Gangnam Style!

Yesterday I got ridiculously excited by computers beeping. (Yeah, I know, sad!)

I noticed that on the OCR F452 module spec (section 4, part j) there was the topic “Output data”. So far, outputting data has involved printing things to the console, and a bit of slightly dodgy ASCII artwork. However, I realised there must be more to life and I started to investigate whether it was possible to play sounds easily using Python. Turns out that yes, there are a lot of libraries which you can use to do this but none more simple than the winsound library!

So, I happily started off my program with the function Beep:

winsound.Beep(frequencyduration)

Beep the PC’s speaker. The frequency parameter specifies frequency, in hertz, of the sound, and must be in the range 37 through 32,767. The duration parameter specifies the number of milliseconds the sound should last. 

So, this is a function which takes two parameters, the frequency of a sound and the duration in ms. I first started off generating sounds which probably annoyed the neighbourhood dogs, but I quickly discovered that the most comfortably audible sounds are in the range 100-10000Hz.

I then became curious about what frequencies the different musical notes were at, and I came across a really interesting site about where Maths meets Music. I never realised that notes sound good together because their frequency waves match up at regular intervals, as you can see from this graph of the notes C and G (taken from the same website linked above):

How cool is that? I also never realised that the notes in a scale are separated by a regular interval, exponentially. Something to think about if you are planning to do a collaboration lesson with Maths, Physics or even Music! I also realised that you would need some way to pause (a rest) so I went with the time library which has a function sleep(seconds) that worked nicely.

Anyhow, I decided to try to write a bit of music. I originally went with “Doe, a deer” from the Sound of Music but my Year 13 didn’t think that was cool enough. They requested Gangnam Style, so here it (partly) is. I have found that it does vary slightly from computer to computer in terms of timings:

import winsound
import time

winsound.Beep(293, 200) # D
winsound.Beep(293, 200) # D
winsound.Beep(293, 200) # D
winsound.Beep(293, 600) # D
winsound.Beep(246, 600) # B

time.sleep(0.1)

winsound.Beep(369, 200)# F#
winsound.Beep(369, 200)# F#
winsound.Beep(369, 200)# F#
winsound.Beep(369, 600)# F#
winsound.Beep(329, 600) # E

time.sleep(0.1)

winsound.Beep(329, 200) # E
winsound.Beep(329, 200) # E
winsound.Beep(329, 200) # E
winsound.Beep(369, 500) # F#

time.sleep(0.9)

winsound.Beep(369, 200) # F#
winsound.Beep(369, 200) # F#
winsound.Beep(369, 200) # F#
winsound.Beep(369, 600) # F#

time.sleep(0.9)
winsound.Beep(369, 200) # F#
winsound.Beep(369, 200) # F#
winsound.Beep(369, 200) # F#

for i in range(4):
    winsound.Beep(369, 200) # F#
    time.sleep(0.1)

for i in range(4):
    winsound.Beep(369, 100) # F#
    time.sleep(0.1)

winsound.Beep(369, 600) # F#

# PYTHON GANGNAM STYLE!

 

If (like my colleague) you have no idea what Gangnam Style is – it was Number 1, where have you been? – you should probably watch this Youtube video!

Other tasks you could consider:

– Obviously investigating other more sophisticated sound libraries!
– Writing functions to play each note, e.g. c(octave, duration) would play the note C at the specified octave and for the specified duration – you can use this full list of the hertz frequencies to get the other octaves for higher and lower notes
– Instead of writing the music down with a zillion lines of code as above, use proper constructs such as list, dict or tuple to store it
– You could probably go even further with this and get the music read in from a file

Advertisements

4 thoughts on “Python Gangnam Style!

  1. Quickly converted to run in the BASH command line on the PC speaker:
    #!/bin/bash

    beep -f 293 -l 200; # D
    beep -f 293 -l 200; # D
    beep -f 293 -l 200; # D
    beep -f 293 -l 600; # D
    beep -f 246 -l 600; # B

    sleep 0.1;

    beep -f 369 -l 200;# F#
    beep -f 369 -l 200;# F#
    beep -f 369 -l 200;# F#
    beep -f 369 -l 600;# F#
    beep -f 329 -l 600; # E

    sleep 0.1;

    beep -f 329 -l 200; # E
    beep -f 329 -l 200; # E
    beep -f 329 -l 200; # E
    beep -f 369 -l 500; # F#

    sleep 0.9;

    beep -f 369 -l 200; # F#
    beep -f 369 -l 200; # F#
    beep -f 369 -l 200; # F#
    beep -f 369 -l 600; # F#

    sleep 0.9;
    beep -f 369 -l 200;
    beep -f 369 -l 200;
    beep -f 369 -l 200;

    for i in range 4;
    beep -f 369 -l 200;
    sleep 0.1;

    for i in range 4;
    beep -f 369 -l 100;
    sleep 0.1;

    beep -f 369 -l 600;

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s