Next  |  Prev  |  Up  |  Top  |  Index  |  JOS Index  |  JOS Pubs  |  JOS Home  |  Search

Testing Out a New STK Project

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).

Figure A.2: Contents of the file echo/Makefile.

 
MAIN = echo
ARGS = 2
OFILES = 
include ../Makefile.proj

Figure A.3: Contents of the file echo/echo.cpp.

 
/* 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:

Note that StkFloat is an STK typedef which is normally bound to type double. Also, every STK library class which processes or synthesizes sound has a method named tick, which computes one sample of sound per call. (It is also possible to operate on more than one sample at a time, as illustrated in the myproj/cla example. Additionally, several STK programming examples illustrate the handling of multiple channels, such as for stereo or quad (grep for tickFrame in projects/examples/*.cpp.)

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
$ make
Similarly, all subdirectories can be ``cleaned up'' by typing make clean in the myproj directory.


Next  |  Prev  |  Up  |  Top  |  Index  |  JOS Index  |  JOS Pubs  |  JOS Home  |  Search

[How to cite and copy this work] 
``Physical Audio Signal Processing for Virtual Musical Instruments and Digital Audio Effects'', by Julius O. Smith III, (December 2005 Edition).
Copyright © 2006-07-01 by Julius O. Smith III
Center for Computer Research in Music and Acoustics (CCRMA),   Stanford University
CCRMA  [Automatic-links disclaimer]