In this post I will show you how to do this :

I am not the first to do that, but most people who play their computer on Youtube use either very expensive programs, or programs that won’t run on your computer, or not-so-efficient programs with not-that-much possibilities of extension, or cheap programs with a big lag between pressing the key and actually hearing the note.

So here is a very small Python script which will run fine even on a basic netbook. If you are not familiar with Python, you should take online courses , it is really worth it 🙂 !

If you are faminiliar with Python, then you are welcome to improve the code on its Github page.

Transforming your keyboard into a mixtable

My original idea was to transform my computer keyboard into a mixtable, to make something like in this very awesome video:

So my first move was to make a program that would take a configuration file my_configuration.cf containing this:



q, dog.wav

w, cat.wav

e, tarzan.wav

r, scream.mp3



And then if you hit q you would hear a dog from the dog.wav soundfile, if you hit w you’d hear a cat, etc…

This is pretty easy to do with Python’s pygame package. Here is my code (inspired by similar stuff from the package Mingus):

import pygame as pg import csv import time SAMPLE_WIDTH = 16 FPS = 44100 N_CHANNELS = 2 BUFFER = 2**9 def minimix(config_file,mode = 'instrument'): """ Opens an interface that lets you press the keys of your keyboard to plays the related soundfiles as sepcified in the provided configuration file. Args: config_file (str): A file associating keyboard keys with file names. Each line must be of the form key , filename. mode (str) : instrument -- causes autorepeat of samples as long as the key is pressed. sustain -- causes autorepeat of the samples until its key is pressed again. player -- striking the key causes the sound to play once. Returns: a list of the (time,events). """ repeat = 0 if (mode is 'player') else (-1) pg.mixer.pre_init(FPS,-SAMPLE_WIDTH,N_CHANNELS,BUFFER) pg.init() screen = pg.display.set_mode((640,480)) ##### READ CONF FILE key2sound = {} key2file = {} config = csv.reader(open(config_file, 'rb'), delimiter=',') for key, soundfile in config: key,soundfile = key.strip(' '),soundfile.strip(' ') if key is not '#': key2file[key] = soundfile key2sound[key] = pg.mixer.Sound(soundfile) events_list = [] currently_playing = {k : False for k in key2sound.iterkeys()} ##### MAIN LOOP while True: event = pg.event.wait() if event.type in (pg.KEYDOWN,pg.KEYUP): key = pg.key.name(event.key) if key in key2sound: if event.type == pg.KEYDOWN: if (mode == 'sustain') and currently_playing[key]: key2sound[key].fadeout(20) currently_playing[key] = False else: key2sound[key].play(repeat) currently_playing[key] = True events_list.append((time.time(),key2file[key])) elif event.type == pg.KEYUP and (mode == 'instrument'): key2sound[key].stop() currently_playing[key] = False events_list.append((time.time(),key2file[key])) elif event.key == pg.K_ESCAPE: break pg.quit() return events_list

Transforming your keyboard into a musical instrument

If instead of using various noises like cat and dog you use different notes from the same instrument, then you turned your computer into some kind of piano. The problem is that a set of soundfiles with all the notes of an instrument is difficult to find on the internet, so I wrote a script that makes as many notes as you want from just one sound by shifting its pitch up or down. It uses the audio processing program Soundstretch that you will need to install first :

import os def shift_wav(wavfile,output,shifts,verbose=False): """ Makes new sounds by shifting the pitch of a sound. Requires soundstrech installed. Args: wavfile : Name of the file containing the original sound output: name to use as a prefix for the output files and for the output folder name shifts (list of int): specifies of how many half-tones the pitch shifts should be. For instance [-2,-1,1,2] will produce 4 files containing the sound 2 half-tones lower, one halftone lower, one halftone higher and two halftones higher. """ folder = os.path.dirname(output) if not os.path.exists(folder): os.makedirs(folder) for i,s in enumerate(shifts): outputfile = '%s%02d.wav'%(output,i) command = 'soundstretch %s %s -pitch=%d'%(wavfile,outputfile,s) if verbose: print command os.system(command)

Going further

There is so much one could do to improve the program.

On the musical side, for instance, finding configurations of the keyboard that are particularly ergonomic. In the video above I used this configuration:

I called it typewriter because it enables you to play very fast things while moving your hands a minimum (the video is a bad example :P) . But maybe there is better to find !

Also, one could start listing every cool piece of music that can be played on a typing keyboard. I use 46 keys ( almost 4 octaves !), that makes a lot of possibilities !

On the programming side, there is a lot of little things I can think of, like automatizing scale changes, introducing nuances, designing nice interfaces (why not a guitar-hero-like game where you would actually be playing music on a playback ?), writing a script that would take some sheet music (in a nice format, like ABC, MIDI, lylipond) and return the list of the keys you should strike to play it.

I actually wrote a lot more code, for instance to make it easier to write configuration files, for sound processing, etc., but since it is not strictly necessary I am not reporting it here (I’ll certainly put a working version on GitHub, or such, some day).

Philosophy of the musical keyboard

Your typing keyboard is a real instrument. Of course it is not its primary use, but our voice’s primary purpose was not to sing, either. Now do the math : how many people out there have a piano at home ? And how many have a computer ? That gives you an idea of how many people would like to play the piano, cannot, but could play their computers instead.

So promoting computer-keyboardism is ultimately about bringing music to the masses. It is about providing everyone with an instrument that you can practice at home, in the train, at work , and that will be familiar to anyone everywhere in the world.

There is more : how many of you, pianist readers, have started the piano for seduction purposes ? (yeah, sure, me neither…) But public places with a piano on which you could show off your mad skills are getting pretty rare, aren’t they ? Especially since most bars have traded their good old piano for a TV. But think about all these places with a computer at hand ! Yep, time for you to develop a talent that will be useful in real life !

So practice, get good, be one of the first composers for tomorrow’s instrument, impress your friends and spread the good news ! If you are still reading me after so much gibberish , then do not hesitate : you just proved how little you value your free time, you are the right person for the task !