Sampler in ChucK
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.

153 lines
4.7KB

  1. //TODO: turn off adcThru when recording, and turn it back on afterwards
  2. //TODO: Effects break panning for some unknown reason
  3. //TODO varying number of bars
  4. NRev reverb => LPF lpf => HPF hpf => Dyno outputLimiter => dac;
  5. outputLimiter.limit();
  6. reverb @=> UGen @ outputWet; // Reference to wet output
  7. outputLimiter @=> UGen @ outputDry; // Reference to dry output
  8. outputLimiter @=> UGen @ mainOutput; // Reference to main output
  9. // Capture mic/line in and monitor through DAC. Limit
  10. adc => Dyno inputLimiter => Gain adcThru => mainOutput; // Monitor input
  11. inputLimiter.limit();
  12. inputLimiter @=> UGen @ mainInput;
  13. // Default parameters
  14. 1 => float adcThruLevel;
  15. 1 => int adcThruMute;
  16. adcThruLevel * adcThruMute => adcThru.gain;
  17. 10000 => lpf.freq;
  18. 10 => hpf.freq;
  19. // Plug in the pedals
  20. LoopPedal pedals[4];
  21. for( 0 => int i; i < pedals.cap(); i++ ) {
  22. pedals[i].recordFrom(mainInput);
  23. pedals[i].outputTo(outputWet, outputDry);
  24. }
  25. // Create the metronome
  26. Metronome metronome;
  27. spork ~metronome.run();
  28. // Start listening to OSC messages
  29. OscIn oin; 9000 => oin.port;
  30. oin.listenAll();
  31. OscMsg msg;
  32. // Event loop
  33. while (true) {
  34. oin => now;
  35. while (oin.recv(msg)) {
  36. if (msg.address=="/input") {
  37. msg.getFloat(0) => adc.gain;
  38. msg.getFloat(1) => adcThruLevel;
  39. adcThruLevel * adcThruMute => adcThru.gain;
  40. }
  41. else if(msg.address=="/delay") {
  42. msg.getFloat(0)::second => dur loopTime;
  43. msg.getFloat(1) => float feedback;
  44. for( 0 => int i; i < pedals.cap(); i++ ) {
  45. pedals[i].setLoopPoint(loopTime);
  46. pedals[i].setFeedback(feedback);
  47. }
  48. }
  49. else if(msg.address=="/channel") {
  50. msg.getInt(0) => int i;
  51. pedals[i].setGain(msg.getFloat(1));
  52. pedals[i].setPan(msg.getFloat(2));
  53. pedals[i].setWet(msg.getFloat(3));
  54. }
  55. else if(msg.address=="/arm") {
  56. msg.getInt(0) => int channel;
  57. (channel<0) => adcThruMute;
  58. adcThruLevel * adcThruMute => adcThru.gain;
  59. for( 0 => int i; i < pedals.cap(); i++ ) { pedals[i].arm(i==channel); }
  60. }
  61. else if(msg.address=="/metronome") {
  62. metronome.mute(msg.getInt(0));
  63. }
  64. else if(msg.address=="/clear") {
  65. msg.getInt(0) => int channel;
  66. pedals[channel].clear();
  67. }
  68. else if(msg.address=="/fx") {
  69. (100+msg.getFloat(0)*10000) => lpf.freq;
  70. (100+msg.getFloat(1)*10000) => hpf.freq;
  71. msg.getFloat(2) => reverb.mix;
  72. }
  73. else if(msg.address=="/master") {
  74. msg.getFloat(0) => mainOutput.gain;
  75. }
  76. }
  77. }
  78. class LoopPedal
  79. {
  80. // We are wrapping a live sampler, LiSa
  81. LiSa sample;
  82. sample => Gain wet;
  83. sample => Gain dry;
  84. // Setup
  85. 10::second => sample.duration; // Allocate max 10 secs of memory
  86. 0::second => sample.recPos => sample.playPos;
  87. 1.0 => sample.feedback;
  88. 1 => sample.loop;
  89. setLoopPoint(1::second);
  90. setWet(0.5);
  91. public void setLoopPoint( dur length ) { length => sample.loopEnd => sample.loopEndRec; }
  92. public void setFeedback( float fb ) { fb => sample.feedback; }
  93. public void setGain( float gain ) { gain => sample.gain; }
  94. public void setPan( float pan ) { } //pan => panner.pan; }
  95. public void setWet( float ratio ) { ratio => wet.gain; 1-ratio => dry.gain;}
  96. public void clear() { sample.clear(); }
  97. public void recordFrom(UGen ugen) { ugen => sample; }
  98. public dur remaining() { return sample.loopEnd() - sample.playPos(); }
  99. public void outputTo(UGen wetSink, UGen drySink) {
  100. 1 => sample.play;
  101. wet => wetSink;
  102. dry => drySink;
  103. }
  104. public void arm(int value) {
  105. sample.playPos() => sample.recPos;
  106. value => sample.record;
  107. }
  108. }
  109. class Metronome
  110. {
  111. // A simple metronome
  112. SinOsc s => ADSR a;
  113. 0.5 => s.gain;
  114. a.set(0.001, .1, .5, .05);
  115. 10::ms => dur plipTime;
  116. fun void mute(int value) {
  117. if (value){ a => dac; } else { a =< dac; }
  118. }
  119. fun void run() {
  120. while(true) {
  121. // Compute the beat time
  122. pedals[0].sample.loopEnd()/4. - plipTime => dur beatTime;
  123. // Beep four times
  124. 1000 => s.freq;
  125. a.keyOn(); plipTime => now; a.keyOff();
  126. beatTime => now;
  127. 500 => s.freq;
  128. a.keyOn(); plipTime => now; a.keyOff();
  129. beatTime => now;
  130. a.keyOn(); plipTime => now; a.keyOff();
  131. beatTime => now;
  132. a.keyOn(); plipTime => now; a.keyOff();
  133. pedals[0].remaining() => now; // Sync
  134. }
  135. }
  136. }