- The ncurses library has been around a long time.
It allows you to program full-screen
and real-time keyboard interaction
in the Unix terminal environment,
which includes the cygwin shell for Windows.
Many full-screen terminal programs,
including the popular vi editor,
are written by using ncurses.
In the spirit of text-only C, I chose ncurses
as my Theremin program's interface.
You can obtain the ncurses library
from invisible-island.net.
The main() function in my Theremin C code
initializes the ncurses screen, color output,
and other settings.
The screen size is calculated,
as are increments for the frequency.
So this code works, no matter how large
the terminal window is.
An endless while loop monitors input.
When the Enter key is pressed, the loop breaks.
Otherwise, the mouse location is read.
The tone() function generates a sine wave
for PortAudio.
The frequency is calculated based
on the mouse's vertical position, or mort.y.
The horizontal position, mort.x, sets the volume
or, more correctly, the amplitude.
The tone plays for only half a second.
Graphical feedback is provided
by two bars, vertical and horizontal.
The refresh function updates the display.
And you must call the endwin() function to close ncurses,
otherwise the terminal window gets all hinky.
If I could improve upon this code,
obviously, I would have the tone play constantly.
As I said in an earlier movie,
I'd prefer to have the tone play
and then change without having to stop.
But these are often the limitations you have to face
when you're given an assignment like this.
I'd rather show you something you can compile
and work with in C, rather than something so specific
so specific that it works only with certain hardware
and under certain operating systems.