Next post: Typing Non-Latin Letters

Python Audio Editor

Unlike most of the projects I've written about here, this project was actually for class credit. I'm in a course called Engineers and Orchestra, where we have been studying the physics of sound and oscillation, and using these principles to build physical instruments. I was curious about how sound is represented on a computer, so for one of the open-ended projects, I set off learning a lot about digital audio. The first step was to learn the insides of a .wav file -- I challenged myself to write a song into a valid .wav file from scratch, starting from only bytes. My next project for the class was to write an audio editor!

The GUI isn't very good, but I have most of the important features covered: Adding Silence, Cut, Append File, Mix File, Modulate, Make Louder, Make Softer. It loads and saves standard wav files (16 bit or 8bit), so it is actually practical for editing things you have recorded.

I wrote everything without referencing the code of any other audio editor like Audacity. The unexpected benefit is that when I got something wrong, I sometimes stumbled upon an interesting-sounding other audio effect. (See for example my Alien Voice effect). In the GUI, you can use the following effects: Reverse, Octave up/down, Change pitch (the most challenging to think of how to write), Vibrato, Flanger, Echo, Reverb, Chorus, and Alien Voice (which modulates the samples with a sine wave).

I added code to synthesize sounds. Sine, triangle, square, and sawtooth waves aren't that great by themselves, so I tried combinations of them to get the most interesting voices. The way to make a sine wave sound better is to quietly mix in frequencies at that are slightly different. Surprisingly, the subtle beat frequencies make it sound more expressive and musical. I worked a long time to create an electric organ-like instrument out of sine waves, and finally came up with something.

Some examples generated by my program:
Electric Organ
Red Noise

I put all of the pieces together: I had an audio editor that can modify and arbitrarily change the pitches of sounds.

Does this still qualify as a half hour hack, given that I did it for a course? Well, it's certainly not polished code. This is not a joke, it is actually in the source:

manualsynth(audiodata, nsamples, _extend([[freq, .5*0.9**(8-1)]], [[freq*(i+1),.5*0.1*0.9**((8-1)-i)]  for i in range(1,8)],[[freq*1.00606, .5*0.9**(8-1)]], [[freq*1.00606*(i+1),.5*0.1*0.9**((8-1)-i)]  for i in range(1,8)]))

Maybe a bit longer than PEP-8's recommended maximum line length of 80 characters :)

I've posted the source at GitHub, released under the GPLv3.