chihauccisoilconte

Untitled

May 4th, 2020
109
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.79 KB | None | 0 0
  1. import ddf.minim.*;
  2. import ddf.minim.ugens.*;
  3. import processing.serial.*;
  4. import cc.arduino.*;
  5.  
  6. Arduino arduino;
  7.  
  8. color off = color(4, 79, 111);
  9. color on = color(200, 200, 200);
  10. int q1fill;
  11. int q2fill;
  12. int q3fill;
  13. int q4fill;
  14.  
  15. Minim minim;
  16. AudioOutput out;
  17.  
  18. // to make an Instrument we must define a class
  19. // that implements the Instrument interface.
  20. class SineInstrument implements Instrument
  21. {
  22.   Oscil wave;
  23.   Line  ampEnv;
  24.  
  25.   SineInstrument( float frequency )
  26.   {
  27.     // make a sine wave oscillator
  28.     // the amplitude is zero because
  29.     // we are going to patch a Line to it anyway
  30.     wave   = new Oscil( frequency, 0, Waves.SINE );
  31.     ampEnv = new Line();
  32.     ampEnv.patch( wave.amplitude );
  33.   }
  34.  
  35.   // this is called by the sequencer when this instrument
  36.   // should start making sound. the duration is expressed in seconds.
  37.   void noteOn( float duration )
  38.   {
  39.     // start the amplitude envelope
  40.     ampEnv.activate( duration, 0.5f, 0 );
  41.     // attach the oscil to the output so it makes sound
  42.     wave.patch( out );
  43.   }
  44.  
  45.   // this is called by the sequencer when the instrument should
  46.   // stop making sound
  47.   void noteOff()
  48.   {
  49.     wave.unpatch( out );
  50.   }
  51. }
  52.  
  53. void setup()
  54. {
  55.   size(512, 480, P3D);
  56.  
  57.   minim = new Minim(this);
  58.     arduino = new Arduino(this, "/dev/ttyUSB0", 57600);
  59.  
  60.   // use the getLineOut method of the Minim object to get an AudioOutput object
  61.   out = minim.getLineOut();
  62.  
  63.    for (int i = 0; i <= 13; i++)
  64.     arduino.pinMode(i, Arduino.INPUT);
  65.  
  66.   // when providing an Instrument, we always specify start time and duration
  67.  
  68. }
  69.  
  70. void draw()
  71. {
  72.   background(0);
  73.  stroke(on);
  74.  
  75.  if (arduino.digitalRead(4) == Arduino.HIGH) {  // When 'i' is less than 35...
  76.      out.playNote( 0.0, 0.9, new SineInstrument( 97.99 ) );
  77.      q1fill = on;
  78.  
  79.   }  else   {q1fill = 0;}
  80.    if (arduino.digitalRead(5) == Arduino.HIGH) {  // When 'i' is less than 35...
  81.   out.playNote( 0.0, 0.9, new SineInstrument( 123.47 ) );
  82. q2fill = on;
  83.  
  84.   } else   {q2fill = 0;}
  85.  
  86.      if (arduino.digitalRead(2) == Arduino.HIGH) {  // When 'i' is less than 35...
  87.   out.playNote( 0.0, 0.9, new SineInstrument( 200 ) );
  88. q3fill = on;
  89.  
  90.   } else   {q3fill = 0;}
  91.  
  92.        if (arduino.digitalRead(7) == Arduino.HIGH) {  // When 'i' is less than 35...
  93.   out.playNote( 0.0, 0.9, new SineInstrument( 500 ) );
  94. q4fill = on;
  95.  
  96.   } else   {q4fill = 0;}
  97.   // draw the waveforms
  98.   for(int i = 0; i < out.bufferSize() - 1; i++)
  99.   {
  100.     line( i, 50 + out.left.get(i)*50, i+1, 50 + out.left.get(i+1)*50 );
  101.     line( i, 150 + out.right.get(i)*50, i+1, 150 + out.right.get(i+1)*50 );
  102.   }
  103.  
  104.  fill(q1fill);
  105. rect(100, 300, 50, 50);
  106.  
  107.  fill(q2fill);
  108. rect(200, 300, 50, 50);
  109.  
  110.  fill(q3fill);
  111. rect(300, 300, 50, 50);
  112.  
  113.  fill(q4fill);
  114. rect(400, 300, 50, 50);
  115.  
  116.  
  117.  
  118.  
  119. }
Advertisement
Add Comment
Please, Sign In to add comment