File: sine.cpp

package info (click to toggle)
stk 5.0.1%2Bdfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: sid, trixie
  • size: 5,908 kB
  • sloc: cpp: 33,513; ansic: 3,216; sh: 2,900; tcl: 2,444; perl: 114; objc: 60; makefile: 54
file content (87 lines) | stat: -rw-r--r-- 2,348 bytes parent folder | download | duplicates (7)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
/******************************************/
/*
  Example program to write N sine tones to
  an N channel soundfile.

  By default, the program will write an
  N channel WAV file.  However, it is
  simple to change the file type argument
  in the FileWvOut constructor.

  By Gary P. Scavone, 2000 - 2002.
*/
/******************************************/

#include "SineWave.h"
#include "FileWvOut.h"
#include <cstdlib>

using namespace stk;

void usage(void) {
  // Error function in case of incorrect command-line
  // argument specifications.
  std::cout << "\nuseage: sine N file time fs\n";
  std::cout << "    where N = number of channels (sines),\n";
  std::cout << "    file = the .wav file to create,\n";
  std::cout << "    time = the amount of time to record (in seconds),\n";
  std::cout << "    and fs = the sample rate (in Hz).\n\n";
  exit( 0 );
}

int main( int argc, char *argv[] )
{
  float base_freq = 220.0;
  int i;

  // Minimal command-line checking.
  if ( argc != 5 ) usage();

  int channels = (int) atoi( argv[1] );
  double time = atof( argv[3] );
  double srate = atof( argv[4] );

  // Create our object instances.
  FileWvOut output;
  SineWave **oscs = (SineWave **) malloc( channels * sizeof(SineWave *) );
  for ( i=0; i<channels; i++ ) oscs[i] = 0;

  // If you want to change the default sample rate (set in Stk.h), do
  // it before instantiating any objects!!
  Stk::setSampleRate( srate );

  // Define the sinewaves.
  for ( i=0; i<channels; i++ )
    oscs[i] = new SineWave;

  // Set oscillator frequency(ies) here ... somewhat random.
  for ( i=0; i<channels; i++ )
    oscs[i]->setFrequency( base_freq + i*(45.0) );

  long nFrames = (long) ( time * Stk::sampleRate() );
  StkFrames frames( nFrames, channels );

  // Open the soundfile for output.  Other file format options
  // include: FILE_SND, FILE_AIF, FILE_MAT, and FILE_RAW.  Other data
  // type options include: STK_SINT8, STK_INT24, STK_SINT32,
  // STK_FLOAT32, and STK_FLOAT64.
  try {
    output.openFile( argv[2], channels, FileWrite::FILE_WAV, Stk::STK_SINT16 );
  }
  catch ( StkError & ) {
    goto cleanup;
  }

  // Here's the runtime code ... no loop
  for ( i=0; i<channels; i++ )
    oscs[i]->tick( frames, i );

  output.tick( frames );

 cleanup:
  for ( i=0; i<channels; i++ )
    delete oscs[i];
  free( oscs );

  return 0;
}