Arduino Xylophone audio playback

Hello all!

I am a complete newbie at Arduino programming, but I have an urgent issue. I have built an electronic xylophone that uses piezo sensors and an Adrunio Mega 2560 to detect when a note is struck and generate a MIDI output. For my project, I am wanting to record live vocals, convert the audio into small MIDI files (four seconds long) and then have the Arduino playback a particular MIDI file when a piezo ‘note’ is struck on the xylophone. However, the code I am using only triggers MIDI notes, and only when used with a DAW (logic, garageband). How do I alter the code to make it play MIDI files? And how do I convert live audio into 4 second, 8-bit, 16 khz MIDI files? Here is the code I am currently using.

int pinRead;
char pinAssignments[16] ={
  'A0','A1','A2','A3','A4','A5','A6','A7','A8','A9','A10','A11','A12','A13','A14','A15'};
byte PadNote[16] = {
  57,58,59,60,61,62,63,64,65,66,67,68,69,70,71,72};         // MIDI notes from 0 to 127 (Mid C = 60)
int PadCutOff[16] = 
{
  100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100};           // Minimum Analog value to cause a drum hit
int MaxPlayTime[16] = {
  90,90,90,90,90,90,90,90,90,90,90,90,90,90,90,90};               // Cycles before a 2nd hit is allowed
#define  midichannel 1;                              // MIDI channel from 0 to 15 (+1 in "real world")
boolean VelocityFlag  = true;                           // Velocity ON (true) or OFF (false)

//*******************************************************************************************************************
// Internal Use Variables
//*******************************************************************************************************************
boolean activePad[16] = {
  0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0};                   // Array of flags of pad currently playing
int PinPlayTime[16] = {
  0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0};                     // Counter since pad started to play
byte status1;

int pin = 0;     
int hitavg = 0;
//*******************************************************************************************************************
// Setup
//*******************************************************************************************************************
void setup() 
{
                                 
  Serial.begin(115200);

}
//*******************************************************************************************************************
// Main Program
//*******************************************************************************************************************
void loop() 
{
  for(int pin=0; pin < 16; pin++)                          //
  {
    //int pin = 3;
    //   for (pinRead=0; pinRead < 16, pin++){
    hitavg = analogRead(pinAssignments[pin]);  
    //Serial.println(hitavg);   
    // read the input pin

    if((hitavg > PadCutOff[pin]))
    {
      if((activePad[pin] == false))
      {
        if(VelocityFlag == true)
        {
          //          hitavg = 127 / ((1023 - PadCutOff[pin]) / (hitavg - PadCutOff[pin]));    // With full range (Too sensitive ?)
          hitavg = (hitavg / 8) -1 ;                                                 // Upper range
        }
        else
        {
          hitavg = 127;
        }
        MIDI_TX(144,PadNote[pin],hitavg); //note on

        PinPlayTime[pin] = 0;
        activePad[pin] = true;
      }
      else
      {
        PinPlayTime[pin] = PinPlayTime[pin] + 1;
      }
    }
    else if((activePad[pin] == true))
    {
      PinPlayTime[pin] = PinPlayTime[pin] + 1;
      if(PinPlayTime[pin] > MaxPlayTime[pin])
      {
        activePad[pin] = false;
        MIDI_TX(144,PadNote[pin],0); 
      }
    }
  } 
}

//*******************************************************************************************************************
// Transmit MIDI Message
//*******************************************************************************************************************
void MIDI_TX(byte MESSAGE, byte PITCH, byte VELOCITY) 
{
  status1 = MESSAGE + midichannel;
  Serial.write(status1);
  Serial.write(PITCH);
  Serial.write(VELOCITY);

}

And how do I convert live audio into 4 second, 8-bit, 16 khz MIDI files?

Basically you can't especially if these are, as you say vocals, and not audio files of notes.

How do I alter the code to make it play MIDI files?

Do you mean a MIDI file stored in the Arduino and converted to sound out on the arduino? If so this is a very very advanced project and you will only achieve limited success with it. I suspect coding it is beyond your programming skills at the moment.

How do I alter the code to make it play MIDI files?

What hardware do you have that can do this? The Arduino can't.

Thank you so much for your post. I was trying to use a similar sketch from instructables and it wasnt working . I see now the are those initial values should be set to 16 to correspond to the 16 inputs.