Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <SerialFlash.h>
- #include <Audio.h>
- #include <Wire.h>
- #include <SPI.h>
- #include <SD.h>
- const int myInput = AUDIO_INPUT_LINEIN;
- // Create the Audio components. These should be created in the
- // order data flows, inputs/sources -> processing -> outputs
- //
- AudioInputI2S audioInput;
- AudioAnalyzeNoteFrequency notefreq;
- // Connect either the live input or synthesized sine wave
- AudioConnection patchCord1(audioInput, 0, notefreq, 0);
- AudioControlSGTL5000 audioShield;
- double val = 0; // variable to store the values from sensor(initially zero)
- float thing = 0;
- float max_voltage = 5;
- float min_voltage = 2.1;
- float max_note = 101;
- float min_note = 84;
- float lin_interp = (max_note - min_note) / (max_voltage - min_voltage);
- int note = int(min_note);
- int channel = 1; // Defines the MIDI channel to send messages on (values from 1-16)
- int velocity = 100; // Defines the velocity that the note plays at (values from 0-127)
- int prev_note = int(min_note);
- float avg = 0.0;
- int buzz_avg = 0;
- const int WINDOW_LENGTH = 5; // this number times 25ms is the time averaged over
- void setup()
- {
- delay(250);
- AudioMemory(30);
- delay(250);
- // Enable the audio shield and set the output volume.
- audioShield.enable();
- audioShield.inputSelect(myInput);
- // audioShield.volume(0.5);
- notefreq.begin(0.15);
- Serial.begin(9600); // starts the serial monitor
- }
- void loop()
- {
- delay(100);
- avg = 0.0;
- buzz_avg = 0;
- // Get an average
- int mouthPieceRegister = 0;
- if (notefreq.available()) {
- for (int i = 0; i < WINDOW_LENGTH; i++) {
- int freq = notefreq.read();
- // Serial.println(freq);
- buzz_avg += freq;
- val = analogRead(A7);
- // Serial.println(val*5/1024);
- thing = pow(val, 0.935);
- avg += (thing * 5.0) / 1024.0;
- delay(10);
- }
- // Serial.println(avg/5);
- buzz_avg = buzz_avg/WINDOW_LENGTH;
- if ( buzz_avg < 450) {
- mouthPieceRegister = 84;
- } else {
- mouthPieceRegister = 92;
- }
- }
- if (mouthPieceRegister != 0) {
- // Get the note
- note = (((avg / WINDOW_LENGTH) - min_voltage) * lin_interp);
- // increase the note
- note = note + mouthPieceRegister;
- if (note >= min_note && note <= max_note) {
- if (note != prev_note)
- {
- Serial.println(note);
- usbMIDI.sendNoteOff(prev_note, 0, channel);
- prev_note = note;
- usbMIDI.sendNoteOn(note, velocity, channel);
- }
- } else {
- usbMIDI.sendNoteOff(prev_note, 0, channel);
- }
- delay(50);
- }
- else {
- Serial.println("no fam");
- usbMIDI.sendNoteOff(prev_note, 0, channel);
- prev_note = 0;
- delay(100);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement