Guest
Public paste!

Untitled

By: a guest | Jun 19th, 2010 | Syntax: C | Size: 33.37 KB | Hits: 114 | Expires: Never
Copy text to clipboard
  1. import ddf.minim.signals.*;
  2. import ddf.minim.*;
  3. import ddf.minim.analysis.*;
  4. import ddf.minim.effects.*;
  5. import processing.serial.*;
  6.  
  7. AudioPlayer playnow;
  8. AudioPlayer progression;
  9. AudioPlayer chord1;
  10. AudioPlayer chord2;
  11. AudioPlayer chord3;
  12. AudioPlayer chord4;
  13. AudioPlayer a13;
  14. AudioPlayer a16;
  15. AudioPlayer a18;
  16. AudioPlayer a20;
  17. AudioPlayer a24;
  18. AudioPlayer a33;
  19. AudioPlayer a43;
  20. AudioPlayer a45;
  21. AudioPlayer a47;
  22. AudioPlayer a49;
  23. AudioPlayer a52;
  24. AudioPlayer a57;
  25. AudioPlayer a59;
  26. AudioPlayer a65;
  27. AudioPlayer a67;
  28. AudioPlayer a68;
  29. AudioPlayer a69;
  30. AudioPlayer a0;
  31.  
  32. Minim minim;
  33. Serial myPort;
  34. Serial keyboard;
  35. byte[] buf;
  36. int bufPos;
  37. int firstValue;
  38. int firstSample;
  39. int secondSample;
  40. int counter;
  41. int justPlayed;
  42. int initial = 1;
  43. boolean temp;
  44. int possibleNotes;
  45. int measure1FirstSample, measure1SecondSample, measure2FirstSample, measure2SecondSample, measure3FirstSample, measure3SecondSample, measure4FirstSample, measure4SecondSample;
  46. int packetCount;
  47. int currentNote;
  48. int noteLength;
  49. boolean firstChunk;
  50. float beatsLeft;
  51. int[] noteSequence = new int[8];
  52. int sequenceCount;
  53. int currentPitch;
  54. int[] pitchSequence = new int[8];
  55. int numberNotes;
  56.  
  57. void setup(){
  58.  
  59.     println("Here's the list of availible ports: ");
  60.     println(Serial.list());
  61.  
  62.   myPort = new Serial(this, Serial.list()[1], 57600);
  63.   buf = new byte[40];
  64.   bufPos = 0;
  65.  
  66.   minim = new Minim(this);
  67.     progression = minim.loadFile("progression.mp3", 1024);
  68.     chord1 = minim.loadFile("chord1.mp3", 1024);
  69.     chord2 = minim.loadFile("chord2.mp3", 1024);
  70.     chord3 = minim.loadFile("chord3.mp3", 1024);
  71.     chord4 = minim.loadFile("chord4.mp3", 1024);
  72.     a45 = minim.loadFile("high_f.mp3", 1024);
  73.     a20 = minim.loadFile("high_e.mp3", 1024);
  74.     a13 = minim.loadFile("high_d.mp3", 1024);
  75.     a16 = minim.loadFile("high_c_sharp.mp3", 1024);
  76.     a18 = minim.loadFile("high_c.mp3", 1024);
  77.     a24 = minim.loadFile("g_sharp.mp3", 1024);
  78.     a33 = minim.loadFile("g.mp3", 1024);
  79.     a43 = minim.loadFile("f_sharp.mp3", 1024);
  80.     a47 = minim.loadFile("f.mp3", 1024);
  81.     a49 = minim.loadFile("e.mp3", 1024);
  82.     a52 = minim.loadFile("d_sharp.mp3", 1024);
  83.     a57 = minim.loadFile("d.mp3", 1024);
  84.     a59 = minim.loadFile("c_sharp.mp3", 1024);
  85.     a65 = minim.loadFile("c.mp3", 1024);
  86.     a67 = minim.loadFile("b_flat.mp3", 1024);
  87.     a68 = minim.loadFile("b.mp3", 1024);
  88.     a69 = minim.loadFile("a.mp3", 1024);
  89.     a0 = minim.loadFile("high_d_sharp.mp3", 1024);
  90.     playnow=a0;
  91. }
  92.  
  93. void draw()
  94. {
  95.  
  96.   while (myPort.available() > 0)
  97.   {
  98.     int ch = myPort.read();
  99.     if (ch == 10)
  100.     {
  101.       String str = new String(buf, 0, bufPos);
  102.       bufPos = 0;
  103.       int[] fields = int(split(trim(str), ' '));
  104.       println(fields);
  105.       int firstValue = fields[0];
  106.       int secondValue = fields[1];
  107.      
  108.       //ok, so now we have the first 2 instantaneous values from the headset. Let's do something with them.
  109.      
  110.       //so how this thing works is we play a chord progression. Based on what chord we're on, we allow
  111.       //a certain range of notes to be played. So let's first restrict the note pool to make sure they
  112.       //will all sound good.
  113.        
  114.    //start playing the loop
  115.  
  116.   if(initial == 1){ //first time
  117.     playnow = progression;
  118.    progression.rewind();
  119.    progression.play();
  120.    initial = 0;
  121.   }
  122.  
  123.    if(progression.position() >= 16161){ //yeah, yeah, I know there's a blip between loops. I don't know how to fix that yet.
  124.     playnow = progression;
  125.    progression.rewind();
  126.    progression.play();
  127.   }
  128.      println("current position is " + progression.position()); //position gives back time in milliseconds that has elapsed in the current track playing
  129.    
  130.   //ok, lets constrain what notes can be played based on the current chord
  131.    
  132.    if((progression.position() >= 0) && (progression.position() <= 3600 )){
  133.     println("possible notes are everything in a c minor scale");
  134.     possibleNotes = 1;
  135.    
  136.     //First, let's figure out how long the note(s) will be. We have 4 beats, so based on the 1st number from the Force Trainer, it'll either be a whole note,
  137.     //half notes, quarter, or 8th note. We'll take an average and that will choose which of those we are playing. If it picks a note value less than a whole note, we'll
  138.     //take an average over only a few numbers to generate enough notes to fill the measure. Since it takes 15 "packets" per measure, shorter notes are representitive of a
  139.     //more instantaneous brainwave. So since there needs to be at least 1 packet per 8th note, 2 per quarter, 3 per half and 4 per whole note.
  140.    
  141.      measure1FirstSample = measure1FirstSample + firstValue;
  142.      packetCount++;
  143.      
  144.      if(packetCount == 3){
  145.        currentNote = measure1FirstSample/3;
  146.        firstChunk = true;
  147.      }
  148.      
  149.      if(firstChunk){
  150.      sequenceCount = 0;
  151.      for(int i=0; i<8; i=i++){ //fill the note sequence with 0's.
  152.      noteSequence[i] = 0;
  153.      }
  154.      
  155.      if((currentNote >= 0) && (currentNote < 20)){
  156.        noteLength = 1; //whole note
  157.        beatsLeft = 0;
  158.        noteSequence[sequenceCount] = 1;
  159.        sequenceCount++;
  160.      }
  161.      
  162.      if((currentNote >= 20) && (currentNote < 40)){
  163.        noteLength = 2; //half note
  164.        beatsLeft = 2;
  165.        noteSequence[sequenceCount] = 2;
  166.        sequenceCount++;
  167.      }
  168.      
  169.      if((currentNote >= 40) && (currentNote < 60)){
  170.        noteLength = 4; //quarter note
  171.        beatsLeft = 3;
  172.        noteSequence[sequenceCount] = 4;
  173.        sequenceCount++;
  174.      }
  175.      
  176.      if((currentNote >= 60) && (currentNote < 80)){
  177.        noteLength = 8; //8th note
  178.        beatsLeft = 3.5;
  179.        noteSequence[sequenceCount] = 8;
  180.        sequenceCount++;
  181.      }
  182.    currentNote = 0;
  183.  }
  184.  
  185.     if(packetCount > 4){ //now we're done with the first note, let's figure out the others
  186.      
  187.      //no matter what, a whole note won't fit.
  188.      
  189.      //at the moment, all of these values are only using the instantaneous output from the
  190.      //force trainer. Eventually, make them averages over the largest possible data set given the time
  191.      //constraint.
  192.      
  193.     if(beatsLeft == .5){
  194.       noteLength = 8;
  195.       beatsLeft = beatsLeft - .5;
  196.       noteSequence[sequenceCount] = 1;
  197.        sequenceCount++;
  198.     }
  199.    
  200.     if((beatsLeft == 1) || (beatsLeft == 1.5)){
  201.       if((firstSample >=0) && (firstSample <=40)){
  202.         noteLength = 4;
  203.         beatsLeft = beatsLeft - 1;
  204.         noteSequence[sequenceCount] = 4;
  205.        sequenceCount++;
  206.       }
  207.       if((firstSample > 40) && (firstSample <=80)){
  208.         noteLength = 8;
  209.         beatsLeft = beatsLeft - .5;
  210.         noteSequence[sequenceCount] = 8;
  211.        sequenceCount++;
  212.       }
  213.     }
  214.    
  215.     if((beatsLeft < 4) && (beatsLeft > 1.5)){
  216.      if((firstSample >= 0) && (firstSample < 26)){
  217.        noteLength = 8;
  218.        beatsLeft  = beatsLeft - .5;
  219.        noteSequence[sequenceCount] = 8;
  220.        sequenceCount++;
  221.      }
  222.      if((firstSample >= 27) && (firstSample < 52)){
  223.        noteLength = 4;
  224.        beatsLeft = beatsLeft - 1;
  225.        noteSequence[sequenceCount] = 4;
  226.        sequenceCount++;
  227.      }
  228.      
  229.      if((firstSample >= 53) && (firstSample < 78 )){
  230.        noteLength = 2;
  231.        beatsLeft = beatsLeft - 2;
  232.        noteSequence[sequenceCount] = 2;
  233.        sequenceCount++;
  234.      }
  235.     }
  236.    currentNote = 0;
  237.  }
  238.      
  239.      
  240.        
  241.    
  242.     //Next, we figure out what the note actually is. This is done by looking at the number of notes there are going to be in this measure and then
  243.     //generating their pitch. We can do this by dividing the number of notes to be played by the number of samples we get in and taking an average over the result.
  244.     //So if there were going to be 4 notes in a particular measure and I know that in the amount of time it takes to play a measure the Force Trainer will
  245.     //send over about 10 values, then 10/4 = 2.5, so take an average over every 2 samples to get the numbers for figuring out pitch.
  246.    
  247.     //now seperate each note and get the pitch for it, then store it in an array to be decoded and played back next.
  248.    
  249.    //in the pitch array, 1=C, 2 = B flat, 3 = E flat, 4 = high C, 5 = G
  250.    
  251.   if(beatsLeft == 0){
  252.       numberNotes = 0;
  253.     for(int j=0; j<pitchSequence.length; j++){
  254.       if(pitchSequence[j] == 0){
  255.          pitchSequence[j] = 0;
  256.         } else{
  257.        
  258.       numberNotes++;
  259.        
  260.        if((secondSample >= 0) && (secondSample <=20)){
  261.          pitchSequence[j] = 1; //play a C
  262.        }
  263.        if((secondSample >20) && (secondSample <= 40)){
  264.          pitchSequence[j] = 2; //play a B flat
  265.        }
  266.        if((secondSample >40) && (secondSample <=60)){
  267.          pitchSequence[j] = 3;// play an E flat
  268.        }
  269.        if((secondSample >60) && (secondSample <=80)){
  270.          pitchSequence[j] = 4; //play a high C
  271.        }
  272.        if((secondSample >80) && (secondSample <=100)){
  273.          pitchSequence[j] = 5; //play a G
  274.        }  
  275.       }
  276.        
  277.       }
  278.     }
  279.    
  280.     //Finally, play the note sequence we just generated.
  281.    
  282.      //first let's make sure that there weren't any errors up to this point encoding the notes. the noteSequence and pitchSequence arrays should be the same length  
  283.     if((noteSequence.length) != (pitchSequence.length)){
  284.       println("Oh no! The pitch and length arrays aren't the same size. Halp.");
  285.     }
  286.    
  287.     for(int k=0; k<noteSequence.length; k++){ //traverse through the pitch and length arrays, decode the correct note sequence, then play it.
  288.     //first we'll narrow down by length and then by pitch
  289.     //possible values in the noteSequence are 1,2,4, and 8
  290.     if((noteSequence[k]) == 1){
  291.       //so it's a whole note. Now decode pitch.
  292.       //possible pitch values are 1,2,3,4,5
  293.        if(pitchSequence[k] == 1){
  294.          //play a C
  295.          playnow=a65;
  296.          a65.play();
  297.        }
  298.        if(pitchSequence[k] == 2){
  299.          //play a B flat
  300.          playnow=a67;
  301.          a67.play();
  302.        }
  303.        if(pitchSequence[k] == 3){
  304.          //play an E flat
  305.          playnow = a52;
  306.          a52.play();
  307.        }
  308.        if(pitchSequence[k] == 4){
  309.          //play a high c
  310.          playnow= a18;
  311.          a18.play();
  312.        }
  313.        if(pitchSequence[k] == 5){
  314.          //play a G
  315.          playnow= a33;
  316.          a33.play();
  317.        }
  318.     }
  319.      if(noteSequence[k] == 2){
  320.       //got a half note. Now decode pitch.
  321.       //possible pitch values are 1,2,3,4,5
  322.        if(pitchSequence[k] == 1){
  323.          //play a C
  324.          playnow=a65;
  325.          a65.play();
  326.        }
  327.        if(pitchSequence[k] == 2){
  328.          //play a B flat
  329.          playnow=a67;
  330.          a67.play();
  331.        }
  332.        if(pitchSequence[k] == 3){
  333.          //play an E flat
  334.          playnow = a52;
  335.          a52.play();
  336.        }
  337.        if(pitchSequence[k] == 4){
  338.          //play a high c
  339.          playnow= a18;
  340.          a18.play();
  341.        }
  342.        if(pitchSequence[k] == 5){
  343.          //play a G
  344.          playnow= a33;
  345.          a33.play();
  346.        }
  347.     }
  348.      if(noteSequence[k] == 4){
  349.       //Quarter note. Now decode pitch.
  350.       //possible pitch values are 1,2,3,4,5
  351.        if(pitchSequence[k] == 1){
  352.          //play a C
  353.          playnow=a65;
  354.          a65.play();
  355.        }
  356.        if(pitchSequence[k] == 2){
  357.          //play a B flat
  358.          playnow=a67;
  359.          a67.play();
  360.        }
  361.        if(pitchSequence[k] == 3){
  362.          //play an E flat
  363.          playnow = a52;
  364.          a52.play();
  365.        }
  366.        if(pitchSequence[k] == 4){
  367.          //play a high c
  368.          playnow= a18;
  369.          a18.play();
  370.        }
  371.        if(pitchSequence[k] == 5){
  372.          //play a G
  373.          playnow= a33;
  374.          a33.play();
  375.        }
  376.     }
  377.      if(noteSequence[k] == 8){
  378.       //8th note. Now decode pitch.
  379.       //possible pitch values are 1,2,3,4,5
  380.        if(pitchSequence[k] == 1){
  381.          //play a C
  382.          playnow=a65;
  383.          a65.play();
  384.        }
  385.        if(pitchSequence[k] == 2){
  386.          //play a B flat
  387.          playnow=a67;
  388.          a67.play();
  389.        }
  390.        if(pitchSequence[k] == 3){
  391.          //play an E flat
  392.          playnow = a52;
  393.          a52.play();
  394.        }
  395.        if(pitchSequence[k] == 4){
  396.          //play a high c
  397.          playnow= a18;
  398.          a18.play();
  399.        }
  400.        if(pitchSequence[k] == 5){
  401.          //play a G
  402.          playnow= a33;
  403.          a33.play();
  404.        }
  405.     }
  406.     }
  407.    }
  408.    
  409.    
  410.    
  411.          
  412.  
  413.    
  414.    
  415.    
  416.    
  417.    
  418.    
  419.     //now do that whole thing all over again for each chord change!
  420.    
  421.    if((progression.position() > 3600) && (progression.position() <= 7800 )){
  422.     println("possible notes are everything in a c minor augmented scale");
  423.     possibleNotes = 2;
  424.    
  425.      measure1FirstSample = measure1FirstSample + firstValue;
  426.      packetCount++;
  427.      
  428.      if(packetCount == 3){
  429.        currentNote = measure1FirstSample/3;
  430.        firstChunk = true;
  431.      }
  432.      
  433.      if(firstChunk){
  434.      sequenceCount = 0;
  435.      for(int i=0; i<8; i=i++){ //fill the note sequence with 0's.
  436.      noteSequence[i] = 0;
  437.      }
  438.      
  439.      if((currentNote >= 0) && (currentNote < 20)){
  440.        noteLength = 1; //whole note
  441.        beatsLeft = 0;
  442.        noteSequence[sequenceCount] = 1;
  443.        sequenceCount++;
  444.      }
  445.      
  446.      if((currentNote >= 20) && (currentNote < 40)){
  447.        noteLength = 2; //half note
  448.        beatsLeft = 2;
  449.        noteSequence[sequenceCount] = 2;
  450.        sequenceCount++;
  451.      }
  452.      
  453.      if((currentNote >= 40) && (currentNote < 60)){
  454.        noteLength = 4; //quarter note
  455.        beatsLeft = 3;
  456.        noteSequence[sequenceCount] = 4;
  457.        sequenceCount++;
  458.      }
  459.      
  460.      if((currentNote >= 60) && (currentNote < 80)){
  461.        noteLength = 8; //8th note
  462.        beatsLeft = 3.5;
  463.        noteSequence[sequenceCount] = 8;
  464.        sequenceCount++;
  465.      }
  466.    currentNote = 0;
  467.  }
  468.  
  469.     if(packetCount > 4){
  470.      
  471.     if(beatsLeft == .5){
  472.       noteLength = 8;
  473.       beatsLeft = beatsLeft - .5;
  474.       noteSequence[sequenceCount] = 1;
  475.        sequenceCount++;
  476.     }
  477.    
  478.     if((beatsLeft == 1) || (beatsLeft == 1.5)){
  479.       if((firstSample >=0) && (firstSample <=40)){
  480.         noteLength = 4;
  481.         beatsLeft = beatsLeft - 1;
  482.         noteSequence[sequenceCount] = 4;
  483.        sequenceCount++;
  484.       }
  485.       if((firstSample > 40) && (firstSample <=80)){
  486.         noteLength = 8;
  487.         beatsLeft = beatsLeft - .5;
  488.         noteSequence[sequenceCount] = 8;
  489.        sequenceCount++;
  490.       }
  491.     }
  492.    
  493.     if((beatsLeft < 4) && (beatsLeft > 1.5)){
  494.      if((firstSample >= 0) && (firstSample < 26)){
  495.        noteLength = 8;
  496.        beatsLeft  = beatsLeft - .5;
  497.        noteSequence[sequenceCount] = 8;
  498.        sequenceCount++;
  499.      }
  500.      if((firstSample >= 27) && (firstSample < 52)){
  501.        noteLength = 4;
  502.        beatsLeft = beatsLeft - 1;
  503.        noteSequence[sequenceCount] = 4;
  504.        sequenceCount++;
  505.      }
  506.      
  507.      if((firstSample >= 53) && (firstSample < 78 )){
  508.        noteLength = 2;
  509.        beatsLeft = beatsLeft - 2;
  510.        noteSequence[sequenceCount] = 2;
  511.        sequenceCount++;
  512.      }
  513.     }
  514.    currentNote = 0;
  515.  }
  516.      
  517.    
  518.    //in the pitch array, 1=C, 2 = A flat, 3 = F sharp, 4 = E flat, 5 = C
  519.    
  520.   if(beatsLeft == 0){
  521.       numberNotes = 0;
  522.     for(int j=0; j<=8; j++){
  523.       if(pitchSequence[j] != 0){
  524.         numberNotes++;
  525.        
  526.        if((secondSample >= 0) && (secondSample <=20)){
  527.          pitchSequence[j] = 1; //play a C
  528.        }
  529.        if((secondSample >20) && (secondSample <= 40)){
  530.          pitchSequence[j] = 2; //play an A flat
  531.        }
  532.        if((secondSample >40) && (secondSample <=60)){
  533.          pitchSequence[j] = 3;// play an F shart
  534.        }
  535.        if((secondSample >60) && (secondSample <=80)){
  536.          pitchSequence[j] = 4; //play an E flat
  537.        }
  538.        if((secondSample >80) && (secondSample <=100)){
  539.          pitchSequence[j] = 5; //play a C
  540.        }
  541.         } else{
  542.         pitchSequence[j] = 0;
  543.         }
  544.        
  545.       }
  546.     }
  547.    
  548.    
  549.     if((noteSequence.length) != (pitchSequence.length)){
  550.       println("Oh no! The pitch and length arrays aren't the same size. Halp.");
  551.     }
  552.    
  553.     for(int k=0; k<=noteSequence.length; k++){ //traverse through the pitch and length arrays, decode the correct note sequence, then play it.
  554.     //first we'll narrow down by length and then by pitch
  555.     //possible values in the noteSequence are 1,2,4, and 8
  556.     if(noteSequence[k] == 1){
  557.       //so it's a whole note. Now decode pitch.
  558.       //possible pitch values are 1,2,3,4,5
  559.        if(pitchSequence[k] == 1){
  560.          //play a C
  561.          playnow=a65;
  562.          a65.play();
  563.        }
  564.        if(pitchSequence[k] == 2){
  565.          //play an A flat
  566.          playnow=a24;
  567.          a24.play();
  568.        }
  569.        if(pitchSequence[k] == 3){
  570.          //play an F sharp
  571.          playnow = a43;
  572.          a43.play();
  573.        }
  574.        if(pitchSequence[k] == 4){
  575.          //play an e flat
  576.          playnow= a52;
  577.          a52.play();
  578.        }
  579.        if(pitchSequence[k] == 5){
  580.          //play a C
  581.          playnow= a18;
  582.          a18.play();
  583.        }
  584.     }
  585.      if(noteSequence[k] == 2){
  586.       //got a half note. Now decode pitch.
  587.       //possible pitch values are 1,2,3,4,5
  588.       if(pitchSequence[k] == 1){
  589.          //play a C
  590.          playnow=a65;
  591.          a65.play();
  592.        }
  593.        if(pitchSequence[k] == 2){
  594.          //play an A flat
  595.          playnow=a24;
  596.          a24.play();
  597.        }
  598.        if(pitchSequence[k] == 3){
  599.          //play an F sharp
  600.          playnow = a43;
  601.          a43.play();
  602.        }
  603.        if(pitchSequence[k] == 4){
  604.          //play an e flat
  605.          playnow= a52;
  606.          a52.play();
  607.        }
  608.        if(pitchSequence[k] == 5){
  609.          //play a C
  610.          playnow= a18;
  611.          a18.play();
  612.        }
  613.     }
  614.      if(noteSequence[k] == 4){
  615.       //Quarter note. Now decode pitch.
  616.       //possible pitch values are 1,2,3,4,5
  617.       if(pitchSequence[k] == 1){
  618.          //play a C
  619.          playnow=a65;
  620.          a65.play();
  621.        }
  622.        if(pitchSequence[k] == 2){
  623.          //play an A flat
  624.          playnow=a24;
  625.          a24.play();
  626.        }
  627.        if(pitchSequence[k] == 3){
  628.          //play an F sharp
  629.          playnow = a43;
  630.          a43.play();
  631.        }
  632.        if(pitchSequence[k] == 4){
  633.          //play an e flat
  634.          playnow= a52;
  635.          a52.play();
  636.        }
  637.        if(pitchSequence[k] == 5){
  638.          //play a C
  639.          playnow= a18;
  640.          a18.play();
  641.        }
  642.     }
  643.      if(noteSequence[k] == 8){
  644.       //8th note. Now decode pitch.
  645.       //possible pitch values are 1,2,3,4,5
  646.       if(pitchSequence[k] == 1){
  647.          //play a C
  648.          playnow=a65;
  649.          a65.play();
  650.        }
  651.        if(pitchSequence[k] == 2){
  652.          //play an A flat
  653.          playnow=a24;
  654.          a24.play();
  655.        }
  656.        if(pitchSequence[k] == 3){
  657.          //play an F sharp
  658.          playnow = a43;
  659.          a43.play();
  660.        }
  661.        if(pitchSequence[k] == 4){
  662.          //play an e flat
  663.          playnow= a52;
  664.          a52.play();
  665.        }
  666.        if(pitchSequence[k] == 5){
  667.          //play a C
  668.          playnow= a18;
  669.          a18.play();
  670.        }
  671.     }
  672.     }
  673.    
  674.    
  675.  
  676.    }  
  677.      
  678.    if((progression.position() > 7800) && (progression.position() <= 12000 )){
  679.     println("possible notes are everything in a c minor scale");
  680.     possibleNotes = 3;
  681.  
  682.      measure1FirstSample = measure1FirstSample + firstValue;
  683.      packetCount++;
  684.      
  685.      if(packetCount == 3){
  686.        currentNote = measure1FirstSample/3;
  687.        firstChunk = true;
  688.      }
  689.      
  690.      if(firstChunk){
  691.      sequenceCount = 0;
  692.      for(int i=0; i<8; i=i++){ //fill the note sequence with 0's.
  693.      noteSequence[i] = 0;
  694.      }
  695.      
  696.      if((currentNote >= 0) && (currentNote < 20)){
  697.        noteLength = 1; //whole note
  698.        beatsLeft = 0;
  699.        noteSequence[sequenceCount] = 1;
  700.        sequenceCount++;
  701.      }
  702.      
  703.      if((currentNote >= 20) && (currentNote < 40)){
  704.        noteLength = 2; //half note
  705.        beatsLeft = 2;
  706.        noteSequence[sequenceCount] = 2;
  707.        sequenceCount++;
  708.      }
  709.      
  710.      if((currentNote >= 40) && (currentNote < 60)){
  711.        noteLength = 4; //quarter note
  712.        beatsLeft = 3;
  713.        noteSequence[sequenceCount] = 4;
  714.        sequenceCount++;
  715.      }
  716.      
  717.      if((currentNote >= 60) && (currentNote < 80)){
  718.        noteLength = 8; //8th note
  719.        beatsLeft = 3.5;
  720.        noteSequence[sequenceCount] = 8;
  721.        sequenceCount++;
  722.      }
  723.    currentNote = 0;
  724.  }
  725.  
  726.     if(packetCount > 4){
  727.      
  728.     if(beatsLeft == .5){
  729.       noteLength = 8;
  730.       beatsLeft = beatsLeft - .5;
  731.       noteSequence[sequenceCount] = 1;
  732.        sequenceCount++;
  733.     }
  734.    
  735.     if((beatsLeft == 1) || (beatsLeft == 1.5)){
  736.       if((firstSample >=0) && (firstSample <=40)){
  737.         noteLength = 4;
  738.         beatsLeft = beatsLeft - 1;
  739.         noteSequence[sequenceCount] = 4;
  740.        sequenceCount++;
  741.       }
  742.       if((firstSample > 40) && (firstSample <=80)){
  743.         noteLength = 8;
  744.         beatsLeft = beatsLeft - .5;
  745.         noteSequence[sequenceCount] = 8;
  746.        sequenceCount++;
  747.       }
  748.     }
  749.    
  750.     if((beatsLeft < 4) && (beatsLeft > 1.5)){
  751.      if((firstSample >= 0) && (firstSample < 26)){
  752.        noteLength = 8;
  753.        beatsLeft  = beatsLeft - .5;
  754.        noteSequence[sequenceCount] = 8;
  755.        sequenceCount++;
  756.      }
  757.      if((firstSample >= 27) && (firstSample < 52)){
  758.        noteLength = 4;
  759.        beatsLeft = beatsLeft - 1;
  760.        noteSequence[sequenceCount] = 4;
  761.        sequenceCount++;
  762.      }
  763.      
  764.      if((firstSample >= 53) && (firstSample < 78 )){
  765.        noteLength = 2;
  766.        beatsLeft = beatsLeft - 2;
  767.        noteSequence[sequenceCount] = 2;
  768.        sequenceCount++;
  769.      }
  770.     }
  771.    currentNote = 0;
  772.  }
  773.      
  774.    
  775.    //in the pitch array, 1=C, 2 = B flat, 3 = E flat, 4 = high C, 5 = G
  776.    
  777.   if(beatsLeft == 0){
  778.       numberNotes = 0;
  779.     for(int j=0; j<=8; j++){
  780.       if(pitchSequence[j] != 0){
  781.         numberNotes++;
  782.        
  783.        if((secondSample >= 0) && (secondSample <=20)){
  784.          pitchSequence[j] = 1; //play a C
  785.        }
  786.        if((secondSample >20) && (secondSample <= 40)){
  787.          pitchSequence[j] = 2; //play a B flat
  788.        }
  789.        if((secondSample >40) && (secondSample <=60)){
  790.          pitchSequence[j] = 3;// play an E flat
  791.        }
  792.        if((secondSample >60) && (secondSample <=80)){
  793.          pitchSequence[j] = 4; //play a high C
  794.        }
  795.        if((secondSample >80) && (secondSample <=100)){
  796.          pitchSequence[j] = 5; //play a G
  797.        }
  798.         } else{
  799.         pitchSequence[j] = 0;
  800.         }
  801.        
  802.       }
  803.     }
  804.    
  805.     //Finally, play the note sequence we just generated.
  806.    
  807.      //first let's make sure that there weren't any errors up to this point encoding the notes. the noteSequence and pitchSequence arrays should be the same length  
  808.     if((noteSequence.length) != (pitchSequence.length)){
  809.       println("Oh no! The pitch and length arrays aren't the same size. Halp.");
  810.     }
  811.    
  812.     for(int k=0; k<=noteSequence.length; k++){ //traverse through the pitch and length arrays, decode the correct note sequence, then play it.
  813.     //first we'll narrow down by length and then by pitch
  814.     //possible values in the noteSequence are 1,2,4, and 8
  815.     if(noteSequence[k] == 1){
  816.       //so it's a whole note. Now decode pitch.
  817.       //possible pitch values are 1,2,3,4,5
  818.        if(pitchSequence[k] == 1){
  819.          //play a C
  820.          playnow=a65;
  821.          a65.play();
  822.        }
  823.        if(pitchSequence[k] == 2){
  824.          //play a B flat
  825.          playnow=a67;
  826.          a67.play();
  827.        }
  828.        if(pitchSequence[k] == 3){
  829.          //play an E flat
  830.          playnow = a52;
  831.          a52.play();
  832.        }
  833.        if(pitchSequence[k] == 4){
  834.          //play a high c
  835.          playnow= a18;
  836.          a18.play();
  837.        }
  838.        if(pitchSequence[k] == 5){
  839.          //play a G
  840.          playnow= a33;
  841.          a33.play();
  842.        }
  843.     }
  844.      if(noteSequence[k] == 2){
  845.       //got a half note. Now decode pitch.
  846.       //possible pitch values are 1,2,3,4,5
  847.        if(pitchSequence[k] == 1){
  848.          //play a C
  849.          playnow=a65;
  850.          a65.play();
  851.        }
  852.        if(pitchSequence[k] == 2){
  853.          //play a B flat
  854.          playnow=a67;
  855.          a67.play();
  856.        }
  857.        if(pitchSequence[k] == 3){
  858.          //play an E flat
  859.          playnow = a52;
  860.          a52.play();
  861.        }
  862.        if(pitchSequence[k] == 4){
  863.          //play a high c
  864.          playnow= a18;
  865.          a18.play();
  866.        }
  867.        if(pitchSequence[k] == 5){
  868.          //play a G
  869.          playnow= a33;
  870.          a33.play();
  871.        }
  872.     }
  873.      if(noteSequence[k] == 4){
  874.       //Quarter note. Now decode pitch.
  875.       //possible pitch values are 1,2,3,4,5
  876.        if(pitchSequence[k] == 1){
  877.          //play a C
  878.          playnow=a65;
  879.          a65.play();
  880.        }
  881.        if(pitchSequence[k] == 2){
  882.          //play a B flat
  883.          playnow=a67;
  884.          a67.play();
  885.        }
  886.        if(pitchSequence[k] == 3){
  887.          //play an E flat
  888.          playnow = a52;
  889.          a52.play();
  890.        }
  891.        if(pitchSequence[k] == 4){
  892.          //play a high c
  893.          playnow= a18;
  894.          a18.play();
  895.        }
  896.        if(pitchSequence[k] == 5){
  897.          //play a G
  898.          playnow= a33;
  899.          a33.play();
  900.        }
  901.     }
  902.      if(noteSequence[k] == 8){
  903.       //8th note. Now decode pitch.
  904.       //possible pitch values are 1,2,3,4,5
  905.        if(pitchSequence[k] == 1){
  906.          //play a C
  907.          playnow=a65;
  908.          a65.play();
  909.        }
  910.        if(pitchSequence[k] == 2){
  911.          //play a B flat
  912.          playnow=a67;
  913.          a67.play();
  914.        }
  915.        if(pitchSequence[k] == 3){
  916.          //play an E flat
  917.          playnow = a52;
  918.          a52.play();
  919.        }
  920.        if(pitchSequence[k] == 4){
  921.          //play a high c
  922.          playnow= a18;
  923.          a18.play();
  924.        }
  925.        if(pitchSequence[k] == 5){
  926.          //play a G
  927.          playnow= a33;
  928.          a33.play();
  929.        }
  930.     }
  931.     }
  932.  
  933.    }    
  934.    
  935.    if((progression.position() > 12000) && (progression.position() <= 16161 )){
  936.     println("possible notes are everything in a b flat major scale");
  937.     possibleNotes = 4;
  938.     //beginning of paste
  939.       measure1FirstSample = measure1FirstSample + firstValue;
  940.      packetCount++;
  941.      
  942.      if(packetCount == 3){
  943.        currentNote = measure1FirstSample/3;
  944.        firstChunk = true;
  945.      }
  946.      
  947.      if(firstChunk){
  948.      sequenceCount = 0;
  949.      for(int i=0; i<8; i=i++){ //fill the note sequence with 0's.
  950.      noteSequence[i] = 0;
  951.      }
  952.      
  953.      if((currentNote >= 0) && (currentNote < 20)){
  954.        noteLength = 1; //whole note
  955.        beatsLeft = 0;
  956.        noteSequence[sequenceCount] = 1;
  957.        sequenceCount++;
  958.      }
  959.      
  960.      if((currentNote >= 20) && (currentNote < 40)){
  961.        noteLength = 2; //half note
  962.        beatsLeft = 2;
  963.        noteSequence[sequenceCount] = 2;
  964.        sequenceCount++;
  965.      }
  966.      
  967.      if((currentNote >= 40) && (currentNote < 60)){
  968.        noteLength = 4; //quarter note
  969.        beatsLeft = 3;
  970.        noteSequence[sequenceCount] = 4;
  971.        sequenceCount++;
  972.      }
  973.      
  974.      if((currentNote >= 60) && (currentNote < 80)){
  975.        noteLength = 8; //8th note
  976.        beatsLeft = 3.5;
  977.        noteSequence[sequenceCount] = 8;
  978.        sequenceCount++;
  979.      }
  980.    currentNote = 0;
  981.  }
  982.  
  983.     if(packetCount > 4){
  984.      
  985.     if(beatsLeft == .5){
  986.       noteLength = 8;
  987.       beatsLeft = beatsLeft - .5;
  988.       noteSequence[sequenceCount] = 1;
  989.        sequenceCount++;
  990.     }
  991.    
  992.     if((beatsLeft == 1) || (beatsLeft == 1.5)){
  993.       if((firstSample >=0) && (firstSample <=40)){
  994.         noteLength = 4;
  995.         beatsLeft = beatsLeft - 1;
  996.         noteSequence[sequenceCount] = 4;
  997.        sequenceCount++;
  998.       }
  999.       if((firstSample > 40) && (firstSample <=80)){
  1000.         noteLength = 8;
  1001.         beatsLeft = beatsLeft - .5;
  1002.         noteSequence[sequenceCount] = 8;
  1003.        sequenceCount++;
  1004.       }
  1005.     }
  1006.    
  1007.     if((beatsLeft < 4) && (beatsLeft > 1.5)){
  1008.      if((firstSample >= 0) && (firstSample < 26)){
  1009.        noteLength = 8;
  1010.        beatsLeft  = beatsLeft - .5;
  1011.        noteSequence[sequenceCount] = 8;
  1012.        sequenceCount++;
  1013.      }
  1014.      if((firstSample >= 27) && (firstSample < 52)){
  1015.        noteLength = 4;
  1016.        beatsLeft = beatsLeft - 1;
  1017.        noteSequence[sequenceCount] = 4;
  1018.        sequenceCount++;
  1019.      }
  1020.      
  1021.      if((firstSample >= 53) && (firstSample < 78 )){
  1022.        noteLength = 2;
  1023.        beatsLeft = beatsLeft - 2;
  1024.        noteSequence[sequenceCount] = 2;
  1025.        sequenceCount++;
  1026.      }
  1027.     }
  1028.    currentNote = 0;
  1029.  }
  1030.      
  1031.    
  1032.    //in the pitch array, 1=b flat, 2 = C, 3 = D, 4 = F, 5 = E flat
  1033.    
  1034.   if(beatsLeft == 0){
  1035.       numberNotes = 0;
  1036.     for(int j=0; j<=8; j++){
  1037.       if(pitchSequence[j] != 0){
  1038.         numberNotes++;
  1039.        
  1040.        if((secondSample >= 0) && (secondSample <=20)){
  1041.          pitchSequence[j] = 1; //play a b flat
  1042.        }
  1043.        if((secondSample >20) && (secondSample <= 40)){
  1044.          pitchSequence[j] = 2; //play an C
  1045.        }
  1046.        if((secondSample >40) && (secondSample <=60)){
  1047.          pitchSequence[j] = 3;// play an D
  1048.        }
  1049.        if((secondSample >60) && (secondSample <=80)){
  1050.          pitchSequence[j] = 4; //play an F
  1051.        }
  1052.        if((secondSample >80) && (secondSample <=100)){
  1053.          pitchSequence[j] = 5; //play a E flat
  1054.        }
  1055.         } else{
  1056.         pitchSequence[j] = 0;
  1057.         }
  1058.        
  1059.       }
  1060.     }
  1061.    
  1062.    
  1063.     if((noteSequence.length) != (pitchSequence.length)){
  1064.       println("Oh no! The pitch and length arrays aren't the same size. Halp.");
  1065.     }
  1066.    
  1067.     for(int k=0; k<=noteSequence.length; k++){ //traverse through the pitch and length arrays, decode the correct note sequence, then play it.
  1068.     //first we'll narrow down by length and then by pitch
  1069.     //possible values in the noteSequence are 1,2,4, and 8
  1070.     if(noteSequence[k] == 1){
  1071.       //so it's a whole note. Now decode pitch.
  1072.       //possible pitch values are 1,2,3,4,5
  1073.        if(pitchSequence[k] == 1){
  1074.          //play a b flat
  1075.          playnow=a67;
  1076.          a67.play();
  1077.        }
  1078.        if(pitchSequence[k] == 2){
  1079.          //play an C
  1080.          playnow=a65;
  1081.          a65.play();
  1082.        }
  1083.        if(pitchSequence[k] == 3){
  1084.          //play an d
  1085.          playnow = a57;
  1086.          a57.play();
  1087.        }
  1088.        if(pitchSequence[k] == 4){
  1089.          //play an f
  1090.          playnow= a47;
  1091.          a47.play();
  1092.        }
  1093.        if(pitchSequence[k] == 5){
  1094.          //play an e flat
  1095.          playnow= a52;
  1096.          a52.play();
  1097.        }
  1098.     }
  1099.      if(noteSequence[k] == 2){
  1100.       //got a half note. Now decode pitch.
  1101.       //possible pitch values are 1,2,3,4,5
  1102.        if(pitchSequence[k] == 1){
  1103.          //play a b flat
  1104.          playnow=a67;
  1105.          a67.play();
  1106.        }
  1107.        if(pitchSequence[k] == 2){
  1108.          //play an C
  1109.          playnow=a65;
  1110.          a65.play();
  1111.        }
  1112.        if(pitchSequence[k] == 3){
  1113.          //play an d
  1114.          playnow = a57;
  1115.          a57.play();
  1116.        }
  1117.        if(pitchSequence[k] == 4){
  1118.          //play an f
  1119.          playnow= a47;
  1120.          a47.play();
  1121.        }
  1122.        if(pitchSequence[k] == 5){
  1123.          //play an e flat
  1124.          playnow= a52;
  1125.          a52.play();
  1126.        }
  1127.     }
  1128.      if(noteSequence[k] == 4){
  1129.       //Quarter note. Now decode pitch.
  1130.       //possible pitch values are 1,2,3,4,5
  1131.    if(pitchSequence[k] == 1){
  1132.          //play a b flat
  1133.          playnow=a67;
  1134.          a67.play();
  1135.        }
  1136.        if(pitchSequence[k] == 2){
  1137.          //play an C
  1138.          playnow=a65;
  1139.          a65.play();
  1140.        }
  1141.        if(pitchSequence[k] == 3){
  1142.          //play an d
  1143.          playnow = a57;
  1144.          a57.play();
  1145.        }
  1146.        if(pitchSequence[k] == 4){
  1147.          //play an f
  1148.          playnow= a47;
  1149.          a47.play();
  1150.        }
  1151.        if(pitchSequence[k] == 5){
  1152.          //play an e flat
  1153.          playnow= a52;
  1154.          a52.play();
  1155.        }
  1156.     }
  1157.      if(noteSequence[k] == 8){
  1158.       //8th note. Now decode pitch.
  1159.       //possible pitch values are 1,2,3,4,5
  1160.        if(pitchSequence[k] == 1){
  1161.          //play a b flat
  1162.          playnow=a67;
  1163.          a67.play();
  1164.        }
  1165.        if(pitchSequence[k] == 2){
  1166.          //play an C
  1167.          playnow=a65;
  1168.          a65.play();
  1169.        }
  1170.        if(pitchSequence[k] == 3){
  1171.          //play an d
  1172.          playnow = a57;
  1173.          a57.play();
  1174.        }
  1175.        if(pitchSequence[k] == 4){
  1176.          //play an f
  1177.          playnow= a47;
  1178.          a47.play();
  1179.        }
  1180.        if(pitchSequence[k] == 5){
  1181.          //play an e flat
  1182.          playnow= a52;
  1183.          a52.play();
  1184.        }
  1185.     }
  1186.     }
  1187.    
  1188.     //end of paste
  1189.    
  1190.    }    
  1191.      
  1192.       firstSample = firstSample + firstValue;
  1193.       secondSample = secondSample + secondValue;
  1194.      counter++;
  1195.      //now we have been accumulating some values of the sensor. If we have enough for a nice sample,
  1196.      //then jump into another if statement to decide what to play.
  1197.      
  1198.  
  1199.  
  1200.  
  1201.     } else {
  1202.       buf[bufPos++] = (byte)ch;
  1203.     }
  1204.    }  
  1205. }
  1206.  
  1207.  
  1208.  void stop(){
  1209.  
  1210.    progression.close();
  1211.    chord1.close();
  1212.    chord2.close();
  1213.    chord3.close();
  1214.    chord4.close();
  1215.     a13.close();
  1216.     a16.close();
  1217.     a18.close();
  1218.     a20.close();
  1219.     a24.close();
  1220.     a33.close();
  1221.     a43.close();
  1222.     a45.close();
  1223.     a47.close();
  1224.     a49.close();
  1225.     a52.close();
  1226.     a57.close();
  1227.     a59.close();
  1228.     a65.close();
  1229.     a67.close();
  1230.     a68.close();
  1231.     a69.close();
  1232.  
  1233.    minim.stop();
  1234.    
  1235.    super.stop();
  1236.    
  1237.  }