1 /******************************************/
2 /*
3   Example program to write N sine tones to
4   an N channel soundfile.
5 
6   By default, the program will write an
7   N channel WAV file.  However, it is
8   simple to change the file type argument
9   in the FileWvOut constructor.
10 
11   By Gary P. Scavone, 2000 - 2002.
12 */
13 /******************************************/
14 
15 #include "SineWave.h"
16 #include "FileWvOut.h"
17 #include <cstdlib>
18 
19 using namespace stk;
20 
usage(void)21 void usage(void) {
22   // Error function in case of incorrect command-line
23   // argument specifications.
24   std::cout << "\nuseage: sine N file time fs\n";
25   std::cout << "    where N = number of channels (sines),\n";
26   std::cout << "    file = the .wav file to create,\n";
27   std::cout << "    time = the amount of time to record (in seconds),\n";
28   std::cout << "    and fs = the sample rate (in Hz).\n\n";
29   exit( 0 );
30 }
31 
main(int argc,char * argv[])32 int main( int argc, char *argv[] )
33 {
34   float base_freq = 220.0;
35   int i;
36 
37   // Minimal command-line checking.
38   if ( argc != 5 ) usage();
39 
40   int channels = (int) atoi( argv[1] );
41   double time = atof( argv[3] );
42   double srate = atof( argv[4] );
43 
44   // Create our object instances.
45   FileWvOut output;
46   SineWave **oscs = (SineWave **) malloc( channels * sizeof(SineWave *) );
47   for ( i=0; i<channels; i++ ) oscs[i] = 0;
48 
49   // If you want to change the default sample rate (set in Stk.h), do
50   // it before instantiating any objects!!
51   Stk::setSampleRate( srate );
52 
53   // Define the sinewaves.
54   for ( i=0; i<channels; i++ )
55     oscs[i] = new SineWave;
56 
57   // Set oscillator frequency(ies) here ... somewhat random.
58   for ( i=0; i<channels; i++ )
59     oscs[i]->setFrequency( base_freq + i*(45.0) );
60 
61   long nFrames = (long) ( time * Stk::sampleRate() );
62   StkFrames frames( nFrames, channels );
63 
64   // Open the soundfile for output.  Other file format options
65   // include: FILE_SND, FILE_AIF, FILE_MAT, and FILE_RAW.  Other data
66   // type options include: STK_SINT8, STK_INT24, STK_SINT32,
67   // STK_FLOAT32, and STK_FLOAT64.
68   try {
69     output.openFile( argv[2], channels, FileWrite::FILE_WAV, Stk::STK_SINT16 );
70   }
71   catch ( StkError & ) {
72     goto cleanup;
73   }
74 
75   // Here's the runtime code ... no loop
76   for ( i=0; i<channels; i++ )
77     oscs[i]->tick( frames, i );
78 
79   output.tick( frames );
80 
81  cleanup:
82   for ( i=0; i<channels; i++ )
83     delete oscs[i];
84   free( oscs );
85 
86   return 0;
87 }
88