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.

71 lines
1.7KB

  1. // crtsine.cpp STK tutorial program
  2. #include "SineWave.h"
  3. #include "RtAudio.h"
  4. using namespace stk;
  5. // This tick() function handles sample computation only. It will be
  6. // called automatically when the system needs a new buffer of audio
  7. // samples.
  8. int tick( void *outputBuffer, void *inputBuffer, unsigned int nBufferFrames,
  9. double streamTime, RtAudioStreamStatus status, void *dataPointer )
  10. {
  11. SineWave *sine = (SineWave *) dataPointer;
  12. register StkFloat *samples = (StkFloat *) outputBuffer;
  13. for ( unsigned int i=0; i<nBufferFrames; i++ )
  14. *samples++ = sine->tick();
  15. return 0;
  16. }
  17. int main()
  18. {
  19. // Set the global sample rate before creating class instances.
  20. Stk::setSampleRate( 44100.0 );
  21. SineWave sine;
  22. RtAudio dac;
  23. // Figure out how many bytes in an StkFloat and setup the RtAudio stream.
  24. RtAudio::StreamParameters parameters;
  25. parameters.deviceId = dac.getDefaultOutputDevice();
  26. parameters.nChannels = 1;
  27. RtAudioFormat format = ( sizeof(StkFloat) == 8 ) ? RTAUDIO_FLOAT64 : RTAUDIO_FLOAT32;
  28. unsigned int bufferFrames = RT_BUFFER_SIZE;
  29. try {
  30. dac.openStream( &parameters, NULL, format, (unsigned int)Stk::sampleRate(), &bufferFrames, &tick, (void *)&sine );
  31. }
  32. catch ( RtAudioError &error ) {
  33. error.printMessage();
  34. goto cleanup;
  35. }
  36. sine.setFrequency(440.0);
  37. try {
  38. dac.startStream();
  39. }
  40. catch ( RtAudioError &error ) {
  41. error.printMessage();
  42. goto cleanup;
  43. }
  44. // Block waiting here.
  45. char keyhit;
  46. std::cout << "\nPlaying ... press <enter> to quit.\n";
  47. std::cin.get( keyhit );
  48. // Shut down the output stream.
  49. try {
  50. dac.closeStream();
  51. }
  52. catch ( RtAudioError &error ) {
  53. error.printMessage();
  54. }
  55. cleanup:
  56. return 0;
  57. }