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.

124 lines
2.6KB

  1. #include "RtAudio.h"
  2. #include <unistd.h>
  3. int engineGetSampleRate() { return 44100; }
  4. struct StepHandler
  5. {
  6. virtual ~StepHandler() { };
  7. virtual int dostep( void *outputBuffer, void *inputBuffer, unsigned int nBufferFrames,
  8. double streamTime, RtAudioStreamStatus status ) = 0;
  9. static int step( void *outputBuffer, void *inputBuffer, unsigned int nBufferFrames,
  10. double streamTime, RtAudioStreamStatus status, void* userData )
  11. {
  12. StepHandler *sh = (StepHandler *)userData;
  13. return sh->dostep( outputBuffer, inputBuffer, nBufferFrames, streamTime, status );
  14. }
  15. RtAudio startDac()
  16. {
  17. RtAudio dac;
  18. if ( dac.getDeviceCount() < 1 ) {
  19. std::cout << "\nNo audio devices found!\n";
  20. exit( 0 );
  21. }
  22. RtAudio::StreamParameters parameters;
  23. parameters.deviceId = dac.getDefaultOutputDevice();
  24. parameters.nChannels = 1;
  25. parameters.firstChannel = 0;
  26. unsigned int sampleRate = 44100;
  27. unsigned int bufferFrames = 256; // 256 sample frames
  28. try {
  29. dac.openStream( &parameters, NULL, RTAUDIO_FLOAT64,
  30. sampleRate, &bufferFrames, &StepHandler::step, (void *)this );
  31. dac.startStream();
  32. }
  33. catch ( RtAudioError& e ) {
  34. e.printMessage();
  35. exit( 0 );
  36. }
  37. return dac;
  38. }
  39. void stopDac( RtAudio dac )
  40. {
  41. try {
  42. // Stop the stream
  43. dac.stopStream();
  44. }
  45. catch (RtAudioError& e) {
  46. e.printMessage();
  47. }
  48. if ( dac.isStreamOpen() ) dac.closeStream();
  49. }
  50. int playAudioUntilStepsDone()
  51. {
  52. RtAudio dac = startDac();
  53. while( dac.isStreamRunning() )
  54. {
  55. usleep( 100 );
  56. }
  57. if ( dac.isStreamOpen() ) dac.closeStream();
  58. return 0;
  59. }
  60. int playAudioUntilEnterPressed()
  61. {
  62. RtAudio dac = startDac();
  63. char input;
  64. std::cout << "\nPlaying ... press <enter> to quit.\n";
  65. std::cin.get( input );
  66. stopDac( dac );
  67. return 0;
  68. }
  69. };
  70. struct StandaloneModule
  71. {
  72. struct thing
  73. {
  74. float value;
  75. bool active;
  76. };
  77. typedef std::vector< thing > values_t;
  78. typedef std::vector< values_t > results_t;
  79. void multiStep( size_t stepCount, results_t &into )
  80. {
  81. for( size_t i=0; i<stepCount; ++i )
  82. {
  83. step();
  84. into.push_back( outputs );
  85. }
  86. }
  87. values_t params;
  88. values_t lights;
  89. values_t inputs;
  90. values_t outputs;
  91. StandaloneModule( int nparam, int ninp, int nout, int nlight )
  92. {
  93. params.resize( nparam );
  94. lights.resize( nlight );
  95. inputs.resize( ninp );
  96. outputs.resize( nout );
  97. }
  98. virtual void step() { };
  99. };