It's surprisingly hard to find clean and simple code to do this, but it's surprisingly easy to do. Here it is, if anyone has ever wanted to write their own sound files in c++.
My code formatting in this blog is horrible, hence linking it as a separate file. Now all you do is call writeWAVData like this:
writeWAVData("mySound.wav", mySampleBuffer, mySampleBufferSize, 44100, 1);
Which would write a 44.1khz mono wav ("CD quality"). mySampleBuffer should be an array of signed shorts for 16-bit sound, floats for 32-bit, unsigned chars for 8-bit. Since the function is templatized, it automatically detects the format and takes care of the relevant fields in the WAV header.
Oh, and this only works on a little-endian machine, since WAV is expected to be little...but that probably doesn't matter to anyone these days, right?
It had to happen sooner or later...audio synthesis (not composition, but actual synthesis) is one of the only realms of procedural generation that I have yet to touch...well, at least, that was true yesterday. But not anymore! 😀
Yesterday I finally indulged myself in trying out audio synthesis. I read a few basic materials on the subject, but, much to my surprise, audio synthesis is incredibly simple! It seems like the audio people really enjoy making up their own fancy terminology for everything, but, honestly, it's dead simple: audio synthesis is the study of scalar functions of one variable. Period. That's literally all there is to it: a sound wave is f(t), nothing more, nothing less. Wow! That's great, because I'm already pretty darn familiar with Mr. f(t) from my work in other fields of procedural generation. Is it coincidence that it always comes back to pure math? I think not 😉
Here are a few sound effects that represent my first-ever endeavors into audio synthesis. I know, they're terrible, but they were made from scratch by someone who has only known how to synthesize audio for less than 24 hours, so maybe that makes them a little better. In the fancy audio terms, I guess you could say that I used "additive synthesis," combined with "subtractive synthesis," and even a little "frequency modulation" on top of that. But really, all I did was write some math. Sums of sines ("additive synthesis")...multiplications of those sums ("subtractive synthesis")...domain distortions ("frequency modulation"). All the usual procedural tricks. Heck, I even did the equivalent of fractal noise! Turns out it works just as well in audio. Those magical fractals
Well, here they are. Someday they'll get better, but certainly not in time to have procedural sound effects for LT1. Oh well. Maybe next time.
Procedural Sound 1
Procedural Sound 2
Procedural Sound 3
Procedural Sound 4
Procedural Sound 5