By Vasudev Ram
This program is meant for kids of all ages ...
The program, piano.py (shown below), lets you play music on your computer, something like a piano, by pressing the keys C D E F G A B, as in Western classical music.
I've written various versions of such a program in the past, starting from my first home computer, in different languages (BASIC, Turbo Pascal, Turbo C), for fun.
The program works on Windows and uses the Python winsound module. I got to know about the winsound module a while ago, and tried it then, and wrote a program like this.
To run this program, you need Windows and Python, and a working sound system; built-in speakers on a PC or laptop will work, but you will get better sound quality from external speakers.
The program is rudimentary and is not meant to simulate a full piano; that would be much more complex.
Here is the piano program in Python:
# piano.py
# Description: A simple program to make the computer act like
# a piano. Plays a note for each of the keys C, D, E, F, G, A, B,
# according to the Western music middle octave.
# References:
# http://en.wikipedia.org/wiki/Frequency_of_notes
# Author: Vasudev Ram - http://www.dancingbison.com
import string
from time import sleep
from winsound import Beep
from msvcrt import getch
def play_note(frequency, duration):
Beep(frequency, duration)
"""
Compute all the frequencies for the notes C D E F G A B,
for the middle C octave, where note A = 400 Hz.
Middle octave note frequencies computed from A = 440 Hz,
using the info at http://en.wikipedia.org/wiki/Frequency_of_notes
(Multiply a note frequency by the square of the twelfth root
of 2 to get the next higher note, and divide it by the same
to get the next lower note).
"""
a = 440
# twelfth_root_2 is the 12th root of 2
twelfth_root_2 = 2 ** (1.0/12.0)
# tr2 is a shorter name for twelfth_root_2
tr2 = twelfth_root_2
# tr2sqrd is tr2 squared
# tr2sqrd is the factor by which to multiply (or divide) the
# frequency of any note, to get the next higher (or lower) note
# in the same octave.
# E.g. multiplying the frequency for note A in the middle C octave,
# i.e. 440 Hz, by tr2sqrd, gives the frequency for the next higher
# note in the same octave, i.e. note B.
tr2sqrd = tr2 * tr2
b = int(round(a * tr2sqrd))
# C D E F G A B
g = int(round(a / tr2sqrd))
f = int(round(g / tr2sqrd))
e = int(round(f / tr2sqrd))
d = int(round(e / tr2sqrd))
c = int(round(d / tr2sqrd))
"""
print "c =", c, "d =", d, "e =", e, "f =", f
print "g =", g, "a =", a, "b =", b
"""
# Set up the notes dict mapping letter keys to notes to play
notes = {}
notes['c'] = c
notes['d'] = d
notes['e'] = e
notes['f'] = f
notes['g'] = g
notes['a'] = a
notes['b'] = b
print
print "Simple piano program in Python, by Vasudev Ram"
print
print "To play, keep on pressing any of these keys: c d e f g a b"
print "Make sure the Caps Lock light on your keyboard is off"
print
print "Press q to stop"
print
print
print " The floor is yours, Maestro!"
print chr(7) # bell
print
duration = 300 # milliseconds
note = string.lower(getch())
while True:
print note,
if note in notes:
play_note(notes[note], duration)
elif note == 'q':
break
note = string.lower(getch())
print
print
# And farewell tune ...
sleep(1.5)
for note in ('c', 'c', 'd', 'd', 'g', 'a', 'b'):
play_note(notes[note], duration)
sleep(0.1)
sleep(1.5)
for note in ('d', 'd', 'c', 'c', 'b', 'a', 'g'):
play_note(notes[note], duration)
sleep(0.1)
print
print "Encore!"
print
#--------------- EOF: piano.py ----------
You can run the piano program with the command:
python piano.py
It gives instructions on how to use it.
It's easy - just keep pressing any of the keys c d e f g a b, or q to quit.
Enjoy.
Note 1: I computed the frequencies for the notes (C D E F G A B) from the Wikipedia link (in the code above), but there are multiple notes with almost the same name (even in the same octave), and I don't have much knowledge of music, so sorry if some of the notes are off key. Feel free to give suggestions in the comments, and I'll try to implement them.
Note 2: I just whipped up the code quickly, it can be improved, I may do that later.
Also, if you are into Python and music, check this post:
Pyknon music library for Python (MIDI)
I also came across a couple of other music-related Python modules, but that is a subject for another post.
- Vasudev Ram - Dancing Bison Enterprises
