Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import ddf.minim.*;
- import ddf.minim.ugens.*;
- import processing.serial.*;
- import cc.arduino.*;
- Arduino arduino;
- color off = color(4, 79, 111);
- color on = color(200, 200, 200);
- int q1fill;
- int q2fill;
- int q3fill;
- int q4fill;
- Minim minim;
- AudioOutput out;
- // to make an Instrument we must define a class
- // that implements the Instrument interface.
- class SineInstrument implements Instrument
- {
- Oscil wave;
- Line ampEnv;
- SineInstrument( float frequency )
- {
- // make a sine wave oscillator
- // the amplitude is zero because
- // we are going to patch a Line to it anyway
- wave = new Oscil( frequency, 0, Waves.SINE );
- ampEnv = new Line();
- ampEnv.patch( wave.amplitude );
- }
- // this is called by the sequencer when this instrument
- // should start making sound. the duration is expressed in seconds.
- void noteOn( float duration )
- {
- // start the amplitude envelope
- ampEnv.activate( duration, 0.5f, 0 );
- // attach the oscil to the output so it makes sound
- wave.patch( out );
- }
- // this is called by the sequencer when the instrument should
- // stop making sound
- void noteOff()
- {
- wave.unpatch( out );
- }
- }
- void setup()
- {
- size(512, 480, P3D);
- minim = new Minim(this);
- arduino = new Arduino(this, "/dev/ttyUSB0", 57600);
- // use the getLineOut method of the Minim object to get an AudioOutput object
- out = minim.getLineOut();
- for (int i = 0; i <= 13; i++)
- arduino.pinMode(i, Arduino.INPUT);
- // when providing an Instrument, we always specify start time and duration
- }
- void draw()
- {
- background(0);
- stroke(on);
- if (arduino.digitalRead(4) == Arduino.HIGH) { // When 'i' is less than 35...
- out.playNote( 0.0, 0.9, new SineInstrument( 97.99 ) );
- q1fill = on;
- } else {q1fill = 0;}
- if (arduino.digitalRead(5) == Arduino.HIGH) { // When 'i' is less than 35...
- out.playNote( 0.0, 0.9, new SineInstrument( 123.47 ) );
- q2fill = on;
- } else {q2fill = 0;}
- if (arduino.digitalRead(2) == Arduino.HIGH) { // When 'i' is less than 35...
- out.playNote( 0.0, 0.9, new SineInstrument( 200 ) );
- q3fill = on;
- } else {q3fill = 0;}
- if (arduino.digitalRead(7) == Arduino.HIGH) { // When 'i' is less than 35...
- out.playNote( 0.0, 0.9, new SineInstrument( 500 ) );
- q4fill = on;
- } else {q4fill = 0;}
- // draw the waveforms
- for(int i = 0; i < out.bufferSize() - 1; i++)
- {
- line( i, 50 + out.left.get(i)*50, i+1, 50 + out.left.get(i+1)*50 );
- line( i, 150 + out.right.get(i)*50, i+1, 150 + out.right.get(i+1)*50 );
- }
- fill(q1fill);
- rect(100, 300, 50, 50);
- fill(q2fill);
- rect(200, 300, 50, 50);
- fill(q3fill);
- rect(300, 300, 50, 50);
- fill(q4fill);
- rect(400, 300, 50, 50);
- }
Advertisement
Add Comment
Please, Sign In to add comment