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.

95 lines
3.8KB

  1. /*! \page hello Hello Sine!
  2. We'll continue our introduction to the Synthesis ToolKit with a simple
  3. sine-wave oscillator program. STK provides two different classes for
  4. sine-wave generation. We will first look at a generic waveform
  5. oscillator class, stk::FileLoop, that can load a variety of common file
  6. types. In this example, we load a sine "table" from an STK RAW file
  7. (defined as monophonic, 16-bit, big-endian data). We use the class
  8. stk::FileWvOut to write the result to a 16-bit, WAV formatted audio file.
  9. \code
  10. // sineosc.cpp
  11. #include "FileLoop.h"
  12. #include "FileWvOut.h"
  13. using namespace stk;
  14. int main()
  15. {
  16. // Set the global sample rate before creating class instances.
  17. Stk::setSampleRate( 44100.0 );
  18. FileLoop input;
  19. FileWvOut output;
  20. // Load the sine wave file.
  21. input.openFile( "rawwaves/sinewave.raw", true );
  22. // Open a 16-bit, one-channel WAV formatted output file
  23. output.openFile( "hellosine.wav", 1, FileWrite::FILE_WAV, Stk::STK_SINT16 );
  24. input.setFrequency( 440.0 );
  25. // Run the oscillator for 40000 samples, writing to the output file
  26. for ( int i=0; i<40000; i++ )
  27. output.tick( input.tick() );
  28. return 0;
  29. }
  30. \endcode
  31. stk::FileLoop is a subclass of stk::FileWvIn, which supports WAV, SND
  32. (AU), AIFF, MAT-file (Matlab), and RAW file formats with 8-, 16-, and
  33. 32-bit integer and 32- and 64-bit floating-point data types.
  34. stk::FileWvIn provides interpolating, read-once ("oneshot")
  35. functionality, as well as methods for setting the read rate and read
  36. position.
  37. stk::FileWvIn provides a "tick level" and interpolating interface to
  38. the stk::FileRead class. Likewise, stk::FileWvOut provides a "tick
  39. level" interface to the stk::FileWrite class. stk::FileRead and
  40. FileWrite both support WAV, SND(AU), AIFF, MAT-file (Matlab), and RAW
  41. file formats with 8-, 16-, and 32-bit integer and 32- and 64-bit
  42. floating-point data types. stk::FileWvOut does not currently offer
  43. data interpolation functionality.
  44. A number of STK parent classes, including stk::WvIn, stk::WvOut,
  45. stk::Instrmnt, stk::Generator, and stk::Effect, (and some or all of
  46. their subclasses) support multi-channel sample frames. If a
  47. single-sample version of the <TT>tick()</TT> function is called for
  48. these classes, a full sample frame is computed but only a single value
  49. is either input and/or output. For example, if the single-sample
  50. <TT>tick()</TT> function is called for subclasses of stk::WvOut, the
  51. sample argument is written to all channels in the one computed frame.
  52. For classes returning values, an optional \c channel argument
  53. specifies which channel value is returned from the computed frame (the
  54. default is always channel 0). To input and/or output multichannel data
  55. to these classes, the overloaded <TT>tick()</TT> functions taking
  56. StkFrames reference arguments should be used.
  57. Nearly all STK classes inherit from the stk::Stk base class. Stk
  58. provides a static sample rate that is queried by subclasses as needed.
  59. Because many classes use the current sample rate value during
  60. instantiation, it is important that the desired value be set at the
  61. beginning of a program. The default STK sample rate is 44100 Hz.
  62. \section error Error Handling
  63. The ToolKit has some basic C++ error handling functionality built in.
  64. Classes that access files and/or hardware are most prone to runtime
  65. errors. To properly "catch" such errors, the above example should be
  66. rewritten as shown below.
  67. \include sineosc.cpp
  68. In this particular case, we simply exit the program if an error occurs
  69. (an error message is automatically printed to stderr). A more refined
  70. program might attempt to recover from or fix a particular problem and,
  71. if successful, continue processing. See the \ref classes to determine
  72. which constructors and functions can throw an error.
  73. [<A HREF="fundamentals.html">Main tutorial page</A>] &nbsp; [<A HREF="compile.html">Next tutorial</A>]
  74. */