Monday, April 28, 2008


When two notes of close frequency are played simultaneously, one can hear what is known as a beat frequency. Say two violinists are playing. One plays an A, 440Hz and the other is playing slightly sharp at 445Hz. One hears a 5Hz "beat" buzzing, that arises from the sum of the two sine waves.

When the two tones are very close, the sound is no longer unpleasant, and the result sounds like a "smooth", flowing note. When the tones are less close, one hears the "buzzing", vibrating beats. When the two tones are significantly separated, one simply hears two notes. In the 19th century, the physicist Helmholtz posed a theory: what if humans dislike the sound of beats, and that the more the beats are noticed, the more "rough" the sound is perceived. He developed a "roughness" curve based on some experiments:

Now, if we go back to the example of the two violins, we can predict how pleasant the two sounds will be to each other. This gets interesting, because a violin plays more than the fundamental note; harmonics are played also. So if the first violin plays an A, and the other plays a slightly offtune E, there will be roughness between the 3rd harmonic of the A (E'), and the 2nd harmonic of the E (E'). If the roughness for each interaction for 6 harmonics is summed, an interesting pattern emerges:

This figure plots the sum of roughness for 6 harmonics, when one plays a fixed note, and the other violin changes pitch. The peaks are areas of high roughness, and the valleys are lower roughness. The notches in this plot line up perfectly with the intervals we use in music! The central valley is the interval of a perfect fifth. The other dips are other familiar intervals. This offers an explanation of why certain intervals are more "consonant" than others, and more. In developing temperament systems, it was found that some approximations of notes are more tolarable than others. For example, we are very perceptive to a slightly-off 5th, but not to an adjusted 3rd. This might correspond graphically with the "steeper" valley of the 5th. Also, the shallower valleys reasonably match with what we consider less consonant intervals.

Using this, Helmholtz was able to explain quantitatively why a major 3rd played by a clarinet and oboe sounds better when the clarinet plays the lower tone (the clarinet sounds only odd harmonics).

The figure is a screenshot from a program I wrote to explore this. When you click on the plot, you hear the interval. It also includes a modern model of roughness, Vassilakis (2007).

Download (Windows, .NET 2.0) Source (including Matlab plots)

But do beats and perceived roughness cause dissonance, or are they just correlated with dissonance? Helmholtz's theory does not explain everything. Even when beats are eliminated, the intervals sound dissonant. The notes of our scale are also found in the harmonic series. One could alternatively say that notes are consonant when their harmonics match. This paper notes many criticisms of Helmholtz and the theory of roughness. I will have to study this further.

References: "On the Sensations of Tone as a Physiological basis for the Theory of Music" by Hermann Helmholtz, translated by Ellis
Roughness Calculation Model by Vassilakis, 2007.
Sensory Theories of Musical Consonance

Saturday, April 26, 2008

Fun with Real-time Audio

Over the past week, I've had fun with real-time audio. I'd worked a bit with sound in the past, but in Python, where synthesizing audio is noticeably slow. This time, I coded in C#, which is a lot faster and has access to DirectX. I use DirectSound to put raw bytes of data right into a buffer, enabling seamless audio.

After experimentation, I came up with a program that plays a continous tone. While it is playing, you can change the pulse-width and "smoothness", creating interesting sounds. Quickly changing pulse-width creates the illusion of motion. The "smoothness" parameter goes from sine wave-like to a square wave (approximated by quadratics). As another experiment, I added a joystick interface - if you plug in a game controller and open the program, you can use the joystick to control these parameters. I encourage the reader to think of more audio effects and interfaces.

I wrote a custom control plotting the waveform, which was useful. The program also contains my implementations of waves such as square, triangle, sawtooth, white noise, red noise.

As an aside, delegates in C# are very convenient, allowing you to easily store and pass refrences to methods. What is great is that there is so little code involved. Here's an example from my sound class:

// Takes integer index, returns double between -1 and 1
delegate double SoundDelegate(int i);
double timeScale = frequency * 2 * Math.PI / (double) 44100;
SoundDelegate fn = delegate(int i)
    return Math.Sin(i * timeScale);
return create16bit_sound(fn);
Notice how the anonymous function can even keep references to variables. It's better than a Python lambda.

Here is a sample. The real thing sounds even better, the rate of updating was limited in the process of recording this sample.

I found that smooth pitch-changing for real-time audio could be complicated. If you just change pitch, you hear a lot of "clicks" - the period doesn't line up with the size of the buffer, causing a click every refresh. I sidestepped this problem by only playing frequencies that fit into the buffer, but that's not a very good solution. If I took the previous phase into account, I could fix this.

Download (Windows, .NET 2.0, DirectX9) Joystick is optional
Source GPLv3

If you want to write DirectC audio in C#,the following may help: To get the Managed DirectX dlls, I downloaded the "DirectX 9.0c Redistributable for Software Developers", ran it, and opened the resulting file. Also, using these in Visual Studio 2005, I would get an exception upon starting the program. One can disable this false excption in Debug->Exceptions->Managed Debugging Assistants, uncheck LoaderLock.

Tuesday, April 15, 2008

Maurader's Map Update

Over 1/4th of the students at Olin are on the Maurader's Map!

We improved the location algorithm. It is successfully at room-level precision, which is really cool, and the accuracy is very good. As you can see, we've added the ability to have custom icons. The client runs on Windows, Linux, and Mac now.

And, you can actually use it to find someone. Now we just have to persuade the professors to use it.