musiquemeuble
Active member
Hello,
I'm building an electronic drum kit with Teensy + piezo + Ableton live. It's quite working well, but the main problem is latency (enough to disturb the play) ..
Here is my code
What do you think ? Do you have a solution ? I'm on a Macbook pro.
Thanks
Florent
I'm building an electronic drum kit with Teensy + piezo + Ableton live. It's quite working well, but the main problem is latency (enough to disturb the play) ..
Here is my code
Code:
/*
MIDI Toy Piano Hack Code
*/
#include <MIDI.h>
MIDI_CREATE_DEFAULT_INSTANCE();
//========================================================================================
//Values you may need to change
//Set this value to the number of keys/piezos you are using
const int NUM_OF_KEYS = 16;
//Adjust this value to change the sensitivity of the piezos
const int THRESHOLD = 10;
//Set this value to the number of microseconds you want each MIDI note to last for
const int NOTE_LENGTH = 3;
//Adjust this value to set the range of MIDI note velocity values
const int VEL_SENSE_VAL = 2;
//Change this number to set what MIDI channel the MIDI notes are set to
const int midiChan = 1;
//Change these numbers to set what MIDI note number each key/piezo will send.
//Also make sure that the total number of numbers here matches the value of NUM_OF_KEYS
const int midiNote[NUM_OF_KEYS] = {86};
//Change these values to set which analog input pins you are using
//Also make sure that the total number of values here matches the value of NUM_OF_KEYS
const int triggerSensor[NUM_OF_KEYS] = {A0};
//=======================================================================================
//Variables for storing certain values
int triggerVal[NUM_OF_KEYS] = {0};
bool noteIsOn[NUM_OF_KEYS] = {false};
int midiNoteTime[NUM_OF_KEYS] = {0};
int midiVelocityVal[NUM_OF_KEYS] = {0};
void setup()
{
MIDI.begin(MIDI_CHANNEL_OMNI);
Serial.begin(9600); //REMOVE THIS LINE IF USING HIDUINO INSTEAD OF HAIRLESS
}
void loop()
{
//repeat the below code for each anaolog input/piezo sensor
for (int count; count < NUM_OF_KEYS; count++)
{
//read the piezo value
triggerVal[count] = analogRead(triggerSensor[count]);
//if the value is over the threshold and there isn't currently a note on for this piezo
if (triggerVal[count] > THRESHOLD && noteIsOn[count] == false)
{
//get a velocity value based on the value
midiVelocityVal[count] = triggerVal[count] * (127.0 / 1023.0);
//increase sensitivity
midiVelocityVal[count] *= VEL_SENSE_VAL;
//make sure we don't go out of range
if (midiVelocityVal[count] > 127)
midiVelocityVal[count] = 127;
//send a MIDI note-on message
MIDI.sendNoteOn (midiNote[count], midiVelocityVal[count], midiChan);
//flag that the note is on
noteIsOn[count] = true;
//start a timer for the note to be on for
midiNoteTime[count] = NOTE_LENGTH;
}
//if the note is currently on
if (noteIsOn[count] == true)
{
//reduce the time value by 1
midiNoteTime[count]--;
//if time value equals 0
if (midiNoteTime[count] == 0)
{
//turn off the note
MIDI.sendNoteOff (midiNote[count], 0, midiChan);
noteIsOn[count] = false;
}
}
}
//pause the loop
delay(1);
}
What do you think ? Do you have a solution ? I'm on a Macbook pro.
Thanks
Florent