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

Listing of test main program: main.cpp

/********************************************/  
/* main.cpp                                 */
/*  Test main program for SimpString.cpp    */
/*  Compatible with STK version 4.2.1.      */
/*  Julius Smith, 2000-2005.                */
/********************************************/  

#include "FileWvOut.h"
#include "SimpString.h"
#include "Stk.h"
#include <stdlib.h>
#include <ctype.h>

int main(int argc,char *argv[])
{
  long i;
  FileWvOut output("test"); /* creates test.wav */
  SimpString *simpString =  new SimpString(/* lowest pitch */ 50.0);

  StkFloat srate = Stk::sampleRate();
  simpString->noteOn(srate/100.0  /* 100-sample period */, 
                     1.0 /* amplitude */, 
                     0.25 /* pluck position (0:1) */
                     );

  output.tick(simpString->tick(1.0)); /* impulse */

  for (i=1;i<srate;i++)   {
    output.tick(simpString->tick(0));
  }

  delete simpString;
  return(0);
}

In the STK, every sound-producing module is expected to implement the tick function (or ``method'', if you prefer) which computes and returns one sample of audio output. The argument of the tick function, if present, is normally a single sample of audio input for the module (such as is needed by a digital filter module). Multichannel output may be handled by the tickFrame function, and both tick and tickFrame can handle multiple samples per call. See the STK documentation for details.

When the main program is executed, it creates a soundfile in the current working directory by performing the following steps:

  1. An instance of FileWvOut called output is created for writing sound to disk.

  2. An instance of the class SimpString is initialized with a lowest-pitch setting of $ 50$ Hz. This determines the length of the allocated internal delay lines.

  3. The SimpString object is sent a noteOn messageC.1 to set the pitch, amplitude, and relative ``pick position''. The noteOn does not cause any audio to be computed; instead, it only sets internal state parameters to allow full string vibration. The string is excited externally by the input to its tick function. The noteOff function, on the other hand, sets the damping in the string very high so that the note decays to zero essentially immediately.

  4. The SimpString object is sent the message tick(1.0) which feeds a unit sample to the virtual string at the desired ``plucking point'' and returns one audio sample (which will be zero since the string imposes a delay between the virtual ``pick'' and ``pick-up'').

  5. A for loop computes one second of sound. The sampling rate is available from the Stk object. It may be changed using the following message:
              \texttt{Stk::setSampleRate(newsrate);}
    

  6. Also defined in Stk.h is the typedef StkFloat, which is normally defined to be double, for double-precision floating-point numbers. (In modern processors, double-precision floating point is normally faster than single-precision, due to FPU architecture.)

  7. The string object simpString is deallocated merely for ``good form'' since exiting the program will free all resources.

  8. The main program returns 0, indicating ``success'' according to UNIX command-line program conventions.


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]