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.

103 lines
2.5KB

  1. // bethree.cpp STK tutorial program
  2. #include "BeeThree.h"
  3. #include "RtAudio.h"
  4. using namespace stk;
  5. // The TickData structure holds all the class instances and data that
  6. // are shared by the various processing functions.
  7. struct TickData {
  8. Instrmnt *instrument;
  9. StkFloat frequency;
  10. StkFloat scaler;
  11. long counter;
  12. bool done;
  13. // Default constructor.
  14. TickData()
  15. : instrument(0), scaler(1.0), counter(0), done( false ) {}
  16. };
  17. // This tick() function handles sample computation only. It will be
  18. // called automatically when the system needs a new buffer of audio
  19. // samples.
  20. int tick( void *outputBuffer, void *inputBuffer, unsigned int nBufferFrames,
  21. double streamTime, RtAudioStreamStatus status, void *userData )
  22. {
  23. TickData *data = (TickData *) userData;
  24. register StkFloat *samples = (StkFloat *) outputBuffer;
  25. for ( unsigned int i=0; i<nBufferFrames; i++ ) {
  26. *samples++ = data->instrument->tick();
  27. if ( ++data->counter % 2000 == 0 ) {
  28. data->scaler += 0.025;
  29. data->instrument->setFrequency( data->frequency * data->scaler );
  30. }
  31. }
  32. if ( data->counter > 80000 )
  33. data->done = true;
  34. return 0;
  35. }
  36. int main()
  37. {
  38. // Set the global sample rate and rawwave path before creating class instances.
  39. Stk::setSampleRate( 44100.0 );
  40. Stk::setRawwavePath( "../../rawwaves/" );
  41. TickData data;
  42. RtAudio dac;
  43. // Figure out how many bytes in an StkFloat and setup the RtAudio stream.
  44. RtAudio::StreamParameters parameters;
  45. parameters.deviceId = dac.getDefaultOutputDevice();
  46. parameters.nChannels = 1;
  47. RtAudioFormat format = ( sizeof(StkFloat) == 8 ) ? RTAUDIO_FLOAT64 : RTAUDIO_FLOAT32;
  48. unsigned int bufferFrames = RT_BUFFER_SIZE;
  49. try {
  50. dac.openStream( &parameters, NULL, format, (unsigned int)Stk::sampleRate(), &bufferFrames, &tick, (void *)&data );
  51. }
  52. catch ( RtAudioError& error ) {
  53. error.printMessage();
  54. goto cleanup;
  55. }
  56. try {
  57. // Define and load the BeeThree instrument
  58. data.instrument = new BeeThree();
  59. }
  60. catch ( StkError & ) {
  61. goto cleanup;
  62. }
  63. data.frequency = 220.0;
  64. data.instrument->noteOn( data.frequency, 0.5 );
  65. try {
  66. dac.startStream();
  67. }
  68. catch ( RtAudioError &error ) {
  69. error.printMessage();
  70. goto cleanup;
  71. }
  72. // Block waiting until callback signals done.
  73. while ( !data.done )
  74. Stk::sleep( 100 );
  75. // Shut down the callback and output stream.
  76. try {
  77. dac.closeStream();
  78. }
  79. catch ( RtAudioError &error ) {
  80. error.printMessage();
  81. }
  82. cleanup:
  83. delete data.instrument;
  84. return 0;
  85. }