- The standard C library lacks functions for using the mouse
or accessing sound hardware.
To program such features, you must venture out
into the internet and look for a suitable library.
I might add that sound hardware is very specific
and not the easiest thing to code.
Rather than learn how to program sound by using
an operating system's API, I chose the PortAudio library.
Available at portaudio.com, the PortAudio library
bills itself as free, cross platform and open source,
and it's actually a great little library.
The problem is that PortAudio is designed
to record and play sound snippets.
It doesn't sport routines to generate tones,
although you can create such routines, which is what I did.
And while it has sound on and sound off functions,
you can't change the sound while it's playing.
Further, the documentation recommends that your code
not do anything serious while a sound is playing.
That's pretty limiting, but it's often what C programmers
have to tolerate if you're going to take a generic approach
to solving this type of problem.
My first experiment involved creating a sine wave.
Based on sample code from the PortAudio site,
I wrote sine.c.
The beep function creates a buffer
that contains sine data, a wave form.
The buffer is passed to the PortAudio stream
which plays the tone for a given duration.
Here's a sample run.
(low beep)
Once I knew I could generate a tone,
I had to figure out to change the tone's pitch.
Pitch is similar to frequency, so I had to figure out
how to make the sine wave shorter.
The key was to adjust the size of the table.
Shorter tables make higher sounds.
And longer tables make lower sounds.
The table size is proportional to the frequency.
My second test cycled through varying table sizes
to try and approximate half-step pitches in tone.
I used a guitar tuner app to gauge the tones as they played,
but I wasn't very successful at matching them up.
In the end, I just cycled through some wave table dimensions
to generate different tones.
And here's the result.
(varying tones of beeping)
I tried several times to smooth over the pause
between tones, and it's not part of my code,
alas, the pauses are due to starting
and stopping the PortAudio stream.
I looked for other audio libraries to try as well,
but many of them weren't free,
and I looked into some operating system routines,
but in the end I chose to stick with this method
of manipulating PortAudio to generate tones.
By conducting further experiments, I was able
to make the wave sound louder or softer.
Basically, I just set the wave height
which isn't a true volume adjustment,
but it has a similar effect on the eardrums.
After confirming that I could generate tones,
the next step was to work out
a text screen interface that used the mouse.
I'll cover that solution in the next movie.