Guest User

Untitled

a guest
May 18th, 2013
69
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import beads.*;
  2. import java.util.Arrays;
  3. import processing.serial.*;
  4. Serial port;
  5. float val;
  6.  
  7. AudioContext ac;
  8. Glide carrierFreq, modFreqRatio;
  9.  
  10. void setup() {
  11. size(300,300);
  12. ac = new AudioContext();
  13. /*
  14. * This is a copy of Lesson 3 with some mouse control.
  15. */
  16. //this time we use the Glide object because it smooths the mouse input.
  17. carrierFreq = new Glide(ac, 500);
  18. modFreqRatio = new Glide(ac, 1);
  19. Function modFreq = new Function(carrierFreq, modFreqRatio) {
  20. public float calculate() {
  21. return x[0] * x[1];
  22.  
  23. String arduinoPort = Serial.list()[0];
  24. port = new Serial(this, arduinoPort, 9600);
  25. }
  26. };
  27. WavePlayer freqModulator = new WavePlayer(ac, modFreq, Buffer.SINE);
  28. Function carrierMod = new Function(freqModulator, carrierFreq) {
  29. public float calculate() {
  30. return x[0] * 400.0 + x[1];
  31. }
  32. };
  33. WavePlayer wp = new WavePlayer(ac, carrierMod, Buffer.SINE);
  34. Gain g = new Gain(ac, 1, 0.1);
  35. g.addInput(wp);
  36. ac.out.addInput(g);
  37. ac.start();
  38. }
  39.  
  40. /*
  41. * The drawing code also has some mouse listening code now.
  42. */
  43. color fore = color(255, 102, 204);
  44. color back = color(0,0,0);
  45.  
  46. /*
  47. * Just do the work straight into Processing's draw() method.
  48. */
  49. void draw() {
  50. loadPixels();
  51. //set the background
  52. Arrays.fill(pixels, back);
  53. //scan across the pixels
  54. for(int i = 0; i < width; i++) {
  55. //for each pixel work out where in the current audio buffer we are
  56. int buffIndex = i * ac.getBufferSize() / width;
  57. //then work out the pixel height of the audio data at that point
  58. int vOffset = (int)((1 + ac.out.getValue(0, buffIndex)) * height / 2);
  59. //draw into Processing's convenient 1-D array of pixels
  60. pixels[vOffset * height + i] = fore;
  61. }
  62. updatePixels();
  63. //mouse listening code here
  64. carrierFreq.setValue((float)mouseX / width * 1000 + 50);
  65. modFreqRatio.setValue((1 - (float)mouseY / height) * 10 + 0.1);
  66. }
RAW Paste Data