Wednesday, February 15, 2012

Simple Audio Synthesizer in Java

This is about a program I wrote in 2005. The objective was to generate sounds that resemble the sounds of real musical instruments. This blog post includes an executable JAR file and the source code for this. If you computer is Java-ready you can start by trying it here right away [YASS]. (UI in English).

This is how the UI should look like (Except this one is the original UI in Spanish)
It uses a bottom-up approach to recreate the sounds of real musical instruments by starting from the most basic constructs - simple sinusoidal functions. This means that more complex wave functions are created by aggregating sinusoidal waves with different frequencies and modulating the amplitude of the resulting waves using envelope functions. The UI allows to modify the basic wave function by modifying the individual sinusoidal waves or choosing from a preset list of wave functions. It also allows for choosing from a preset list of envelope functions. Finally it allows to choose from a preset list of musical instruments. This latter option just chooses the appropriate wave functions and envelope functions that make the resulting sound resemble a musical instrument. This last part was done just by using my own judgement and not any machine learning (Disclaimer: I don't pride myself of having a sense of musical aptitude). And this is all this program can do.

One thing that is most likely wrong is the keyboard, I wouldn't trust the mapping of the tones in the keyboard to the actual tones, this is a quick fix though. I frankly don't remember where did I get the mapping for this keyboard. This document from the University of Tennessee explains how to make the mapping correctly: http://web.eecs.utk.edu/~qi/ece505/project/proj1.pdf.
Another thing that I definitely have to credit is Manfred Thole's demo on Fourier Synthesis, while I clearly took inspiration on the sinusoidal editor from his demo I totally borrowed his function to convert integers to the μ-law scalehttp://www.thole.org/manfred/fourier/en_idx.html

Finally I was not sure if I should post the source code for two reasons: 1) Function definitions, variable names, etc are all in Spanish and I'm writing my blog in English. 2) I actually lost the original source code and had to decompile the class files inside the JAR file to get source code and recompile it again with the UI in English, thus additionally losing comments and therefore potentially not acknowledging some sources of inspiration and some wisdom for the future. Still I'm including here the source code in case somebody finds it useful despite of cons 1) and 2), one good thing is that it still compiles in the Java SDK 7 even after decompilation: [Yass-src.zip].