import processing.serial.*; import beads.*; AudioContext ac; Glide carrierFreq, modFreqRatio; Serial myPort; // Create object from Serial class int val=0; int val2=0; int val3=0; int val4=0; int low=0; int high=0; int value=0; void setup() { String portName = Serial.list()[4]; myPort = new Serial(this, portName, 9600); size(600,600); ac = new AudioContext(); //we use the Glide object because it smooths the input. carrierFreq = new Glide(ac, 1000); modFreqRatio = new Glide(ac, 1); if ( myPort.available()>0) { // If data is available, println(val); val = val2; val2 = val3; val3 = val4; val4 = myPort.read(); // read it and store it in val } Function modFreq = new Function(carrierFreq, modFreqRatio) { public float calculate() { return x[0] * x[1]; } }; if(val==1 && val2==2 && val3==3 && val4==4){ background(255); // Set background to white low=myPort.read(); high=myPort.read(); value=256*high + low; } WavePlayer freqModulator = new WavePlayer(ac, modFreq, Buffer.SINE); Function carrierMod = new Function(freqModulator, carrierFreq) { public float calculate() { return x[0] * 400.0 + x[1]; } }; WavePlayer wp = new WavePlayer(ac, carrierMod, Buffer.SINE); Gain g = new Gain(ac, 1, 0.1); g.addInput(wp); ac.out.addInput(g); ac.start(); } /* * Drawing colors. */ color fore = color(0,0,0); color back = color(255,255,255); /* * Just do the work straight into Processing's draw() method. */ void draw() { loadPixels(); //set the background Arrays.fill(pixels, back); //scan across the pixels for(int i = 0; i < width; i++) { //for each pixel work out where in the current audio buffer we are int buffIndex = i * ac.getBufferSize() / width; //then work out the pixel height of the audio data at that point int vOffset = (int)((1 + ac.out.getValue(0, buffIndex)) * height / 2); //draw into Processing's convenient 1-D array of pixels pixels[vOffset * height + i] = fore; } updatePixels(); //mouse listening code here //println(value); carrierFreq.setValue((float)15000*value); }