Fog Creek Software
Discussion Board




Creating Wave (.wav) Files, sound theory

Hello everybody.

I have found so much advice here that I thought I might give it a try myself.

I am trying to write a program that can write .wav Files. The instruments should be generated by the program. First, I thought, that should be possible with something like

  y(t) = a1 * sin(w*t) + a2 *sin(2*w*t) + a3 * sin(3*w*t)

Although I am able to generate tunes this way, the instruments sound very boring. Also, it's almost impossible to generate something that sounds like a snare, hihat and so on. Searching the internet, I found out that there is much more to sound theory than that formula. Unfortunately, these pages go in deep details on things that require a degree in physics/maths and also much experience on the field. I don't know enough about physics, so I want to ask if there is a website, or source code, that does explain what I need.

I'd also appreciate if someone pointed me to a site where I can download free wav samples of instruments in order to analyze their wave shape.

Any help is appreciated

Here Th. Ere (e-Very where)
Friday, May 16, 2003

Did MIDI die?

Simon Lucy
Friday, May 16, 2003

Most sounds are not simple sine waves.  Wind instruments tend to have sinusoidal waveforms since they operate on the principle of simple resonance in a tube, but a guitar, for example, looks more like a triangular wave, because that's the shape of a string as it is plucked, and percussion instruments have very random waveforms.  In music, this is refered to as the timbre of the sound.

If you read into signal processing, you'll learn that all periodic wave forms can be written as the sum of many sine waves, each with a different frequency.  For example, a triangular wave at frequency w is actually a sine wave at frequency w, plus a sine wave at three times the frequency and a one ninth the amplitude, plus a sine wave at five times the frequency and one twenty-fifth the amplitude, and so on to infinity, or a reasonable approximation thereof. 

It's often easier to think in terms of frequencies instead of waveforms -- a triangular wave at 440Hz has frequencies at 440, 1320, 2200Hz, etc.  These are called "harmonics".

More complicated waveforms can be composed of thousands of frequencies.  Even a random looking waveform, such as a percussion beat, will show some structure if looked at as a sum of frequencies.  For example, a high-hat sounds very tinny because it is composed of many high-frequency sounds, whereas a bass drum has many low-frequency sounds.

Another thing you have to keep in mind is that sound evolves over time.  When a note is first hit, for example, it will be unusually loud for the first hundred milliseconds, then hit a plateau, and then slowly fade.  To a simple approximation, you can program a sound "envelope" in which the amplitude rises to an initial high value (attack), then drops to a plateau (decay), maintains that plateau for an interval (sustain), and then eventually dies out (release).  This is referred to as an ADSR envelope.  Of course, real sounds are a lot more complicated than that, but adding this as a rough guide will add more depth to your sound.

Also, sounds evolve over time, not only in loudness (amplitude), but also frequency (pitch).  Even though the primary tone of the sound may stay constant at 440Hz, the higher frequencies (harmonics) may decay, rounding a triangular wave down into a sine wave.  A close look at the frequency spectrum of a real instrument will give you a better idea what real music is composed of.

This is very complex.  Many synthesizers don't even try to mathematically generate sounds, but rather simply play back recorded sounds at altered frequencies.  Still, hopefully this will give you some ideas as to how to add some depth to the sounds you are generating.

Alyosha`
Friday, May 16, 2003

If you want delve deeper into the interesting subject of digital signal processing, I'd recommend either Richard Lyons "Understanding Digital Signal Processing" or Steven W. Smith's "The Scientist and Engineer's Guide to Digital Signal Processing" which, by the way, is accessible for free here (http://www.dspguide.com/). Both are excellent introductory texts.

They're very well written and provide easily understandable introductions into the field. Neither is an "easy read" in the sense  that you can just tear through in an evening; it helps to have pencil and paper and something like gnuplot at your disposabl while reading it to play around with the examples, which, of course slows down reading. But it's definately possible for a technically interested non-mathematician to gain a solid understanding of the topic from either of these texts.

Neither of the provide any specific pointers to *.wav, but you can find specs for those at wotsit.org. As for *.wavs of specific instruments, I'd just digitize them myself, you could try recording the aoutput of your computers MIDI device, audio_out->audio_in. Depending on your soundcard's MIDI processor, the result will sound nothing like the intended instrument, but saves work looking for samples and isolating them.

-tim
Friday, May 16, 2003

*  Recent Topics

*  Fog Creek Home