Advertisement
Guest User

Untitled

a guest
Jun 26th, 2017
63
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.72 KB | None | 0 0
  1. #include <SerialFlash.h>
  2. #include <Audio.h>
  3. #include <Wire.h>
  4. #include <SPI.h>
  5. #include <SD.h>
  6.  
  7. const int myInput = AUDIO_INPUT_LINEIN;
  8.  
  9. // Create the Audio components. These should be created in the
  10. // order data flows, inputs/sources -> processing -> outputs
  11. //
  12. AudioInputI2S audioInput;
  13. AudioAnalyzeNoteFrequency notefreq;
  14.  
  15. // Connect either the live input or synthesized sine wave
  16. AudioConnection patchCord1(audioInput, 0, notefreq, 0);
  17.  
  18. AudioControlSGTL5000 audioShield;
  19.  
  20. double val = 0; // variable to store the values from sensor(initially zero)
  21. float thing = 0;
  22. float max_voltage = 5;
  23. float min_voltage = 2.1;
  24. float max_note = 101;
  25. float min_note = 84;
  26. float lin_interp = (max_note - min_note) / (max_voltage - min_voltage);
  27. int note = int(min_note);
  28. int channel = 1; // Defines the MIDI channel to send messages on (values from 1-16)
  29. int velocity = 100; // Defines the velocity that the note plays at (values from 0-127)
  30. int prev_note = int(min_note);
  31. float avg = 0.0;
  32. int buzz_avg = 0;
  33. const int WINDOW_LENGTH = 5; // this number times 25ms is the time averaged over
  34.  
  35. void setup()
  36. {
  37. delay(250);
  38. AudioMemory(30);
  39. delay(250);
  40.  
  41.  
  42. // Enable the audio shield and set the output volume.
  43. audioShield.enable();
  44. audioShield.inputSelect(myInput);
  45. // audioShield.volume(0.5);
  46.  
  47. notefreq.begin(0.15);
  48.  
  49. Serial.begin(9600); // starts the serial monitor
  50. }
  51.  
  52. void loop()
  53. {
  54. delay(100);
  55. avg = 0.0;
  56. buzz_avg = 0;
  57. // Get an average
  58. int mouthPieceRegister = 0;
  59. if (notefreq.available()) {
  60. for (int i = 0; i < WINDOW_LENGTH; i++) {
  61. int freq = notefreq.read();
  62. // Serial.println(freq);
  63. buzz_avg += freq;
  64. val = analogRead(A7);
  65. // Serial.println(val*5/1024);
  66. thing = pow(val, 0.935);
  67. avg += (thing * 5.0) / 1024.0;
  68. delay(10);
  69. }
  70. // Serial.println(avg/5);
  71. buzz_avg = buzz_avg/WINDOW_LENGTH;
  72.  
  73. if ( buzz_avg < 450) {
  74. mouthPieceRegister = 84;
  75. } else {
  76. mouthPieceRegister = 92;
  77. }
  78. }
  79.  
  80. if (mouthPieceRegister != 0) {
  81. // Get the note
  82. note = (((avg / WINDOW_LENGTH) - min_voltage) * lin_interp);
  83. // increase the note
  84. note = note + mouthPieceRegister;
  85.  
  86. if (note >= min_note && note <= max_note) {
  87. if (note != prev_note)
  88. {
  89. Serial.println(note);
  90. usbMIDI.sendNoteOff(prev_note, 0, channel);
  91. prev_note = note;
  92. usbMIDI.sendNoteOn(note, velocity, channel);
  93. }
  94. } else {
  95. usbMIDI.sendNoteOff(prev_note, 0, channel);
  96. }
  97. delay(50);
  98. }
  99. else {
  100. Serial.println("no fam");
  101. usbMIDI.sendNoteOff(prev_note, 0, channel);
  102. prev_note = 0;
  103. delay(100);
  104. }
  105. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement