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.

344 lines
10KB

  1. /************** Test Main Program Individual Voice *********************/
  2. #include "SKINImsg.h"
  3. #include "Instrmnt.h"
  4. #include "JCRev.h"
  5. #include "Drone.h"
  6. #include "Sitar.h"
  7. #include "Tabla.h"
  8. #include "VoicDrum.h"
  9. #include "Messager.h"
  10. #include "RtAudio.h"
  11. #include <signal.h>
  12. #include <cstring>
  13. #include <iostream>
  14. #include <algorithm>
  15. #include <cstdlib>
  16. using std::min;
  17. using namespace stk;
  18. StkFloat float_random(StkFloat max) // Return random float between 0.0 and max
  19. {
  20. StkFloat temp = (StkFloat) (max * rand() / (RAND_MAX + 1.0) );
  21. return temp;
  22. }
  23. void usage(void) {
  24. // Error function in case of incorrect command-line argument specifications.
  25. std::cout << "\nuseage: ragamat flags \n";
  26. std::cout << " where flag = -s RATE to specify a sample rate,\n";
  27. std::cout << " flag = -ip for realtime SKINI input by pipe\n";
  28. std::cout << " (won't work under Win95/98),\n";
  29. std::cout << " and flag = -is <port> for realtime SKINI input by socket.\n";
  30. exit(0);
  31. }
  32. bool done;
  33. static void finish(int ignore){ done = true; }
  34. // The TickData structure holds all the class instances and data that
  35. // are shared by the various processing functions.
  36. struct TickData {
  37. JCRev reverbs[2];
  38. Drone drones[3];
  39. Sitar sitar;
  40. VoicDrum voicDrums;
  41. Tabla tabla;
  42. Messager messager;
  43. Skini::Message message;
  44. StkFloat lastSample;
  45. StkFloat t60;
  46. int counter;
  47. bool settling;
  48. bool haveMessage;
  49. StkFloat droneChance, noteChance;
  50. StkFloat drumChance, voiceChance;
  51. int tempo;
  52. int chanceCounter;
  53. int key;
  54. int ragaStep;
  55. int ragaPoint;
  56. int endPhase;
  57. StkFloat rateScaler;
  58. // Default constructor.
  59. TickData()
  60. : t60(4.0), counter(0),
  61. settling( false ), haveMessage( false ), droneChance(0.01), noteChance(0.01),
  62. drumChance(0.0), voiceChance(0.0), tempo(3000), chanceCounter(3000), key(0), ragaPoint(6), endPhase(0) {}
  63. };
  64. // Raga key numbers and drone frequencies.
  65. const int ragaUp[2][13] = {{57, 60, 62, 64, 65, 68, 69, 71, 72, 76, 77, 81},
  66. {52, 54, 55, 57, 59, 60, 63, 64, 66, 67, 71, 72}};
  67. const int ragaDown[2][13] = {{57, 60, 62, 64, 65, 67, 69, 71, 72, 76, 79, 81},
  68. {48, 52, 53, 55, 57, 59, 60, 64, 66, 68, 70, 72}};
  69. StkFloat droneFreqs[3] = { 55.0, 82.5, 220.0 };
  70. #define DELTA_CONTROL_TICKS 64 // default sample frames between control input checks
  71. // The processMessage() function encapsulates the handling of control
  72. // messages. It can be easily relocated within a program structure
  73. // depending on the desired scheduling scheme.
  74. void processMessage( TickData* data )
  75. {
  76. register unsigned int value1 = data->message.intValues[0];
  77. register StkFloat value2 = data->message.floatValues[1];
  78. register StkFloat temp = value2 * ONE_OVER_128;
  79. switch( data->message.type ) {
  80. case __SK_Exit_:
  81. if ( data->settling == false ) goto settle;
  82. if ( data->endPhase < 5 ) return;
  83. done = true;
  84. return;
  85. case __SK_ControlChange_:
  86. switch ( value1 ) {
  87. case 1:
  88. data->droneChance = temp;
  89. break;
  90. case 2:
  91. data->noteChance = temp;
  92. break;
  93. case 4:
  94. data->voiceChance = temp;
  95. break;
  96. case 7:
  97. data->tempo = (int) (11025 - value2 * 70.0 );
  98. break;
  99. case 11:
  100. data->drumChance = temp;
  101. break;
  102. case 64:
  103. if ( value2 == 0.0 ) {
  104. data->key = 1;
  105. droneFreqs[0] = 55.0;
  106. droneFreqs[1] = 82.5;
  107. droneFreqs[2] = 220.0;
  108. }
  109. else {
  110. data->key = 0;
  111. droneFreqs[0] = 82.5;
  112. droneFreqs[1] = 123.5;
  113. droneFreqs[2] = 330.0;
  114. }
  115. break;
  116. default:
  117. break;
  118. }
  119. } // end of type switch
  120. data->haveMessage = false;
  121. return;
  122. settle:
  123. // Exit and program change messages are preceeded with a short settling period.
  124. data->counter = (int) (data->t60 * Stk::sampleRate());
  125. data->drones[1].noteOn( droneFreqs[1], 0.1 );
  126. data->settling = true;
  127. std::cout << "What Need Have I for This?" << std::endl;
  128. }
  129. // The tick() function handles sample computation and scheduling of
  130. // control updates. It will be called automatically by RtAudio when
  131. // the system needs a new buffer of audio samples.
  132. int tick( void *outputBuffer, void *inputBuffer, unsigned int nBufferFrames,
  133. double streamTime, RtAudioStreamStatus status, void *dataPointer )
  134. {
  135. TickData *data = (TickData *) dataPointer;
  136. register StkFloat temp, outs[2], *samples = (StkFloat *) outputBuffer;
  137. int i, voiceNote, counter, nTicks = (int) nBufferFrames;
  138. while ( nTicks > 0 && !done ) {
  139. if ( !data->haveMessage ) {
  140. data->messager.popMessage( data->message );
  141. if ( data->message.type > 0 ) {
  142. data->counter = (long) (data->message.time * Stk::sampleRate());
  143. data->haveMessage = true;
  144. }
  145. else
  146. data->counter = DELTA_CONTROL_TICKS;
  147. }
  148. counter = min( nTicks, data->counter );
  149. data->counter -= counter;
  150. for ( i=0; i<counter; i++ ) {
  151. outs[0] = data->reverbs[0].tick( data->drones[0].tick() + data->drones[2].tick()
  152. + data->sitar.tick() );
  153. outs[1] = data->reverbs[1].tick( 1.5 * data->drones[1].tick() + 0.5 * data->voicDrums.tick()
  154. + 0.5 * data->tabla.tick() );
  155. // Mix a little left to right and back.
  156. *samples++ = outs[0] + 0.3 * outs[1];
  157. *samples++ = outs[1] + 0.3 * outs[0];
  158. nTicks--;
  159. // Do a bunch of random controls unless settling down to end.
  160. if ( data->settling ) {
  161. if ( data->counter == 0 ) {
  162. data->counter = (int) (data->t60 * Stk::sampleRate());
  163. if ( data->endPhase == 0 ) {
  164. data->drones[2].noteOn( droneFreqs[2], 0.1 );
  165. std::cout << "What Need Have I for This?" << std::endl;
  166. }
  167. else if ( data->endPhase == 1 ) {
  168. data->drones[0].noteOn( droneFreqs[0], 0.1 );
  169. std::cout << "RagaMatic finished ... " << std::endl;
  170. }
  171. else if ( data->endPhase == 2 ) {
  172. std::cout << "All is Bliss ... " << std::endl;
  173. }
  174. else if ( data->endPhase == 3 ) {
  175. std::cout << "All is Bliss ..." << std::endl;
  176. }
  177. data->endPhase++;
  178. }
  179. }
  180. else {
  181. data->chanceCounter--;
  182. if (data->chanceCounter == 0) {
  183. data->chanceCounter = (int) ( data->tempo / data->rateScaler );
  184. if ( float_random(1.0) < data->droneChance )
  185. data->drones[0].noteOn( droneFreqs[0], 0.1 );
  186. if ( float_random(1.0) < data->droneChance )
  187. data->drones[1].noteOn( droneFreqs[1], 0.1 );
  188. if ( float_random(1.0) < data->droneChance )
  189. data->drones[2].noteOn( droneFreqs[2], 0.1 );
  190. if ( float_random(1.0) < data->noteChance ) {
  191. temp = float_random(1.0);
  192. if ( temp < 0.1) data->ragaStep = 0;
  193. else if (temp < 0.5) data->ragaStep = 1;
  194. else data->ragaStep = -1;
  195. data->ragaPoint += data->ragaStep;
  196. if ( data->ragaPoint < 0 )
  197. data->ragaPoint -= ( 2 * data->ragaStep );
  198. if ( data->ragaPoint > 11 ) data->ragaPoint = 11;
  199. if ( data->ragaStep > 0 )
  200. data->sitar.noteOn( Midi2Pitch[ragaUp[data->key][data->ragaPoint]],
  201. 0.05 + float_random(0.3) );
  202. else
  203. data->sitar.noteOn( Midi2Pitch[ragaDown[data->key][data->ragaPoint]],
  204. 0.05 + float_random(0.3) );
  205. }
  206. if ( float_random(1.0) < data->voiceChance ) {
  207. voiceNote = (int) float_random(11);
  208. data->voicDrums.noteOn( voiceNote, 0.3 + (0.4 * data->drumChance) +
  209. float_random(0.3 * data->voiceChance));
  210. }
  211. if ( float_random(1.0) < data->drumChance ) {
  212. voiceNote = (int) float_random(TABLA_NUMWAVES);
  213. data->tabla.noteOn( voiceNote, 0.2 + (0.2 * data->drumChance) +
  214. float_random(0.6 * data->drumChance));
  215. }
  216. }
  217. }
  218. }
  219. if ( nTicks == 0 ) break;
  220. // Process control messages.
  221. if ( data->haveMessage ) processMessage( data );
  222. }
  223. return 0;
  224. }
  225. int main( int argc, char *argv[] )
  226. {
  227. TickData data;
  228. RtAudio dac;
  229. int i;
  230. if ( argc < 2 || argc > 6 ) usage();
  231. // If you want to change the default sample rate (set in Stk.h), do
  232. // it before instantiating any objects! If the sample rate is
  233. // specified in the command line, it will override this setting.
  234. Stk::setSampleRate( 44100.0 );
  235. // Parse the command-line arguments.
  236. unsigned int port = 2001;
  237. for ( i=1; i<argc; i++ ) {
  238. if ( !strcmp( argv[i], "-is" ) ) {
  239. if ( i+1 < argc && argv[i+1][0] != '-' ) port = atoi(argv[++i]);
  240. data.messager.startSocketInput( port );
  241. }
  242. else if (!strcmp( argv[i], "-ip" ) )
  243. data.messager.startStdInput();
  244. else if ( !strcmp( argv[i], "-s" ) && ( i+1 < argc ) && argv[i+1][0] != '-')
  245. Stk::setSampleRate( atoi(argv[++i]) );
  246. else
  247. usage();
  248. }
  249. // Allocate the dac here.
  250. RtAudioFormat format = ( sizeof(StkFloat) == 8 ) ? RTAUDIO_FLOAT64 : RTAUDIO_FLOAT32;
  251. RtAudio::StreamParameters parameters;
  252. parameters.deviceId = dac.getDefaultOutputDevice();
  253. parameters.nChannels = 2;
  254. unsigned int bufferFrames = RT_BUFFER_SIZE;
  255. try {
  256. dac.openStream( &parameters, NULL, format, (unsigned int)Stk::sampleRate(), &bufferFrames, &tick, (void *)&data );
  257. }
  258. catch ( RtAudioError& error ) {
  259. error.printMessage();
  260. goto cleanup;
  261. }
  262. data.reverbs[0].setT60( data.t60 );
  263. data.reverbs[0].setEffectMix( 0.5 );
  264. data.reverbs[1].setT60( 2.0 );
  265. data.reverbs[1].setEffectMix( 0.2 );
  266. data.drones[0].noteOn( droneFreqs[0], 0.1 );
  267. data.drones[1].noteOn( droneFreqs[1], 0.1 );
  268. data.drones[2].noteOn( droneFreqs[2], 0.1 );
  269. data.rateScaler = 22050.0 / Stk::sampleRate();
  270. // Install an interrupt handler function.
  271. (void) signal( SIGINT, finish );
  272. // If realtime output, set our callback function and start the dac.
  273. try {
  274. dac.startStream();
  275. }
  276. catch ( RtAudioError &error ) {
  277. error.printMessage();
  278. goto cleanup;
  279. }
  280. // Setup finished.
  281. while ( !done ) {
  282. // Periodically check "done" status.
  283. Stk::sleep( 50 );
  284. }
  285. // Shut down the output stream.
  286. try {
  287. dac.closeStream();
  288. }
  289. catch ( RtAudioError& error ) {
  290. error.printMessage();
  291. }
  292. cleanup:
  293. return 0;
  294. }