In the preceding section, we built the STK library and all the demos, and we tried out one of them (Physical.bat). In this section, we will create a new STK project and go through the steps of compiling, linking, and trying it out. Debugging programs using gdb is introduced in Appendix B.
The file myproj/README briefly describes the simple examples in its subdirectories. Let's focus for now on the echo example:
$ cd ~/stk/stk/myproj/echo $ ls -F Makefile echo.cpp test.wav $ make echo echo g++ -g -Wall -I../../include/ ... g++ -g -Wall -I../../include/ ... g++ -g -Wall -I../../include/ ... \\ echo.cpp ../../src/libstk.a -lpthread -lm -o echo $ \echo 2 Creating WAV file: echo.wav 8.009705 Seconds Computed $ play echo.wav(Since ``echo'' is a built-in command in the shell, our program echo must be quoted with back-slash, as shown above, or invoked as ./echo.) The command make test is equivalent to the last three commands above. Also, make test is the default ``make target'', so simply saying make will compile (if necessary) and test the program.
Figure A.2 lists the complete makefile for this example. As we see, most of the actual work is done in Makefile.proj in the parent directory. All we need in this makefile is the program name, any program arguments (the number 2 in this case), and the names of any additional object files to be compiled and linked with the main program (including the .o extension in each filename, separating filenames with space).
MAIN = echo ARGS = 2 OFILES = include ../Makefile.proj |
/* echo.cpp - simple test program for echo simulation. Compatible with STK version 4.2.1. Usage: echo 2 Plays the input soundfile (hard wired to test.wav) summed with itself delayed by 2 seconds. You thus hear two instances of the input sound separated by the echo delay. If not specified, the echo delay defaults to 1 second. It can be any nonnegative real value. */ #include "FileWvIn.h" #include "FileWvOut.h" #include "Delay.h" #include <stdlib.h> // for atof() #include <math.h> // for floor() int main(int argc,char *argv[]) { Delay *delayLine; FileWvIn input("test.wav"); FileWvOut output(argv[0]); // Writes main.wav double del = 1.0; if (argc>1) { del = atof(argv[1]); } long delsamps = (long)floor(del*sample_rate+0.5); delayLine = new Delay(delsamps,delsamps); long nsamps = input.getSize(); for (long i=0;i<nsamps;i++) { StkFloat insamp = input.tick(); output.tick(0.5*insamp + 0.5*delayLine->tick(insamp)); } for (long i=0;i<delsamps;i++) { output.tick(0.5*delayLine->tick(0)); } return(0); } |
Figure A.3 lists our example program, echo.cpp. Below is a description of how the program works:
The makefile in the myproj directory forwards the specified make target to all subdirectories listed in the makefile. For example, all of the examples in myproj can be compiled, linked, and tried out as follows:
$ cd ~/stk/stk/myproj $ makeSimilarly, all subdirectories can be ``cleaned up'' by typing
make clean
in the myproj directory.