You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

88 lines
2.3KB

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