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.

116 lines
3.3KB

  1. // grains.cpp
  2. //
  3. // A simple test program for the STK Granulate class.
  4. #include "Granulate.h"
  5. #include "RtAudio.h"
  6. #include <cstdlib>
  7. using namespace stk;
  8. // This tick() function handles sample computation only. It will be
  9. // called automatically when the system needs a new buffer of audio
  10. // samples.
  11. int tick( void *outputBuffer, void *inputBuffer, unsigned int nBufferFrames,
  12. double streamTime, RtAudioStreamStatus status, void *dataPointer )
  13. {
  14. Granulate *grani = (Granulate *) dataPointer;
  15. register StkFloat *samples = (StkFloat *) outputBuffer;
  16. const StkFrames& lastframe = grani->lastFrame();
  17. unsigned int nChannels = lastframe.channels();
  18. unsigned int j;
  19. for ( unsigned int i=0; i<nBufferFrames; i++ ) {
  20. grani->tick();
  21. for ( j=0; j<nChannels; j++ )
  22. *samples++ = lastframe[j];
  23. }
  24. return 0;
  25. }
  26. void usage( void ) {
  27. // Error function in case of incorrect command-line
  28. // argument specifications.
  29. std::cout << "\nuseage: grains file N dur ramp offset delay stretch ramdomness\n";
  30. std::cout << " where file = a soundfile to granulate,\n";
  31. std::cout << " N = the number of grain voices to use,\n";
  32. std::cout << " dur = the grain duration (ms),\n";
  33. std::cout << " ramp = the envelope percent (0-100),\n";
  34. std::cout << " offset = hop time between grains (ms),\n";
  35. std::cout << " delay = pause time between grains (ms),\n";
  36. std::cout << " stretch = stetch factor (1-1000),\n";
  37. std::cout << " and randomness = factor between 0 - 1.0 to control grain parameter randomness.\n\n";
  38. exit( 0 );
  39. }
  40. int main( int argc, char *argv[] )
  41. {
  42. // Minimal command-line checking.
  43. if (argc != 9) usage();
  44. unsigned int N = (unsigned int) atoi(argv[2]);
  45. unsigned int duration = (unsigned int) atoi(argv[3]);
  46. unsigned int ramp = (unsigned int) atoi(argv[4]);
  47. unsigned int offset = (unsigned int) atoi(argv[5]);
  48. unsigned int delay = (unsigned int) atoi(argv[6]);
  49. unsigned int stretch = (unsigned int) atoi(argv[7]);
  50. StkFloat random = (StkFloat) atof(argv[8]);
  51. // Set the global sample rate before creating class instances.
  52. Stk::setSampleRate( 44100.0 );
  53. RtAudio dac;
  54. Granulate grani;
  55. grani.setRandomFactor( random );
  56. grani.setStretch( stretch );
  57. grani.setGrainParameters( duration, ramp, offset, delay );
  58. try {
  59. grani.openFile( argv[1] );
  60. }
  61. catch ( StkError& ) {
  62. exit( 1 );
  63. }
  64. grani.setVoices( N );
  65. // Figure out how many bytes in an StkFloat and setup the RtAudio stream.
  66. RtAudio::StreamParameters parameters;
  67. parameters.deviceId = dac.getDefaultOutputDevice();
  68. parameters.nChannels = grani.channelsOut();
  69. RtAudioFormat format = ( sizeof(StkFloat) == 8 ) ? RTAUDIO_FLOAT64 : RTAUDIO_FLOAT32;
  70. unsigned int bufferFrames = RT_BUFFER_SIZE;
  71. try {
  72. dac.openStream( &parameters, NULL, format, (unsigned int)Stk::sampleRate(), &bufferFrames, &tick, (void *)&grani );
  73. }
  74. catch ( RtAudioError &error ) {
  75. error.printMessage();
  76. goto cleanup;
  77. }
  78. try {
  79. dac.startStream();
  80. }
  81. catch ( RtAudioError &error ) {
  82. error.printMessage();
  83. goto cleanup;
  84. }
  85. // Block waiting here.
  86. char keyhit;
  87. std::cout << "\nPlaying ... press <enter> to quit.\n";
  88. std::cin.get( keyhit );
  89. // Shut down the callback and output stream.
  90. try {
  91. dac.closeStream();
  92. }
  93. catch ( RtAudioError &error ) {
  94. error.printMessage();
  95. }
  96. cleanup:
  97. return 0;
  98. }