Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // this example is in the public domain
- int port = 2;
- import processing.serial.*; // import the Processing serial library
- import ddf.minim.*;
- import ddf.minim.ugens.*;
- import themidibus.*; //Import the library
- Serial myPort; // The serial port
- Minim minim;
- AudioOutput out;
- Oscil fm;
- MidiBus myBus; // The MidiBus
- float bgcolor; // Background color
- float fgcolor; // Fill color
- float xpos, ypos; // Starting position of the ball
- void setup() {
- size(640,480);
- // List all the available serial ports
- println(Serial.list());
- // I know that the first port in the serial list on my mac
- // is always my Arduino module, so I open Serial.list()[0].
- // Change the 0 to the appropriate number of the serial port
- // that your microcontroller is attached to.
- myPort = new Serial(this, Serial.list()[port], 9600);
- // read bytes into a buffer until you get a linefeed (ASCII 10):
- myPort.bufferUntil('\n');
- // draw with smooth edges:
- smooth();
- // audio stuff
- //
- // initialize the minim and out objects
- minim = new Minim( this );
- out = minim.getLineOut();
- // make the Oscil we will hear.
- // arguments are frequency, amplitude, and waveform
- Oscil wave = new Oscil( 200, 0.8, Waves.SINE );
- // make the Oscil we will use to modulate the frequency of wave.
- // the frequency of this Oscil will determine how quickly the
- // frequency of wave changes and the amplitude determines how much.
- // since we are using the output of fm directly to set the frequency
- // of wave, you can think of the amplitude as being expressed in Hz.
- fm = new Oscil( 10, 2, Waves.SINE );
- // set the offset of fm so that it generates values centered around 200 Hz
- fm.offset.setLastValue( 200 );
- // patch it to the frequency of wave so it controls it
- fm.patch( wave.frequency );
- // and patch wave to the output
- wave.patch( out );
- // MIDI stuff
- MidiBus.list(); // List all available Midi devices on STDOUT. This will show each device's index and name.
- myBus = new MidiBus(this, 0, 0); // Create a new MidiBus
- }
- void draw() {
- background(bgcolor);
- fill(fgcolor);
- // Draw the shape
- ellipse(xpos, ypos, 20, 20);
- // draw using a white stroke
- stroke( 255 );
- // draw the waveforms
- for( int i = 0; i < out.bufferSize() - 1; i++ )
- {
- // find the x position of each buffer value
- float x1 = map( i, 0, out.bufferSize(), 0, width );
- float x2 = map( i+1, 0, out.bufferSize(), 0, width );
- // draw a line from one buffer position to the next for both channels
- line( x1, 150 + out.left.get(i)*50, x2, 150 + out.left.get(i+1)*50);
- line( x1, 250 + out.right.get(i)*50, x2, 250 + out.right.get(i+1)*50);
- }
- text( "Modulation frequency: " + fm.frequency.getLastValue(), 5, 15 );
- text( "Modulation amplitude: " + fm.amplitude.getLastValue(), 5, 30 );
- }
- // serialEvent method is run automatically by the Processing applet
- // whenever the buffer reaches the byte value set in the bufferUntil()
- // method in the setup():
- void serialEvent(Serial myPort) {
- // read the serial buffer:
- String myString = myPort.readStringUntil('\n');
- // if you got any bytes other than the linefeed:
- myString = trim(myString);
- // split the string at the commas
- // and convert the sections into integers:
- int sensors[] = int(split(myString, ','));
- // print out the values you got:
- //for (int sensorNum = 0; sensorNum < sensors.length; sensorNum++) {
- // print("Sensor " + sensorNum + ": " + sensors[sensorNum] + "\t");
- //}
- // add a linefeed after all the sensor values are printed:
- //println();
- if (sensors.length > 1) {
- xpos = map(sensors[0], 0, 600, 0, width);
- ypos = map(sensors[1], 0, 600, 0, height);
- fgcolor = 255;
- float modulateAmount = map( ypos, 0, height, 1, 512 );
- float modulateFrequency = map( xpos, 0, width, 0.1, 100 );
- fm.setFrequency( modulateFrequency );
- fm.setAmplitude( modulateAmount );
- int channel = 0;
- int pitch = int(constrain(map(xpos, 0, width, 0, 127), 0, 127));
- int velocity = 127;
- myBus.sendNoteOn(channel, pitch, velocity); // Send a Midi noteOn
- delay(200);
- myBus.sendNoteOff(channel, pitch, velocity); // Send a Midi nodeOff
- channel = 1;
- pitch = int(constrain(map(ypos, 0, height, 0, 127), 0, 127));
- velocity = 127;
- myBus.sendNoteOn(channel, pitch, velocity); // Send a Midi noteOn
- delay(200);
- myBus.sendNoteOff(channel, pitch, velocity); // Send a Midi nodeOff
- }
- // send a byte to ask for more data:
- myPort.write("A");
- }
- void delay(int time) {
- int current = millis();
- while (millis () < current+time) Thread.yield();
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement