You are on page 1of 1

#include <Arduino.

h>
#include "midifile.h"

const int numVoices = 8;

// Set up the CV output pins


const int cvPins[numVoices] = {2, 3, 4, 5, 6, 7, 8, 9};

// Initialize the MIDI file


MidiFile midi_file;

void setup() {
// Open the MIDI file
midi_file.read("song.mid");

// Set the CV output pins as outputs


for (int i = 0; i < numVoices; i++) {
pinMode(cvPins[i], OUTPUT);
}
}

void loop() {
static int track = 0;
static int event = 0;

// Check if the event is a note ON event


if (midi_file[track][event].isNoteOn()) {
// Get the note and velocity values
int note = midi_file[track][event][1];
int velocity = midi_file[track][event][2];

// Calculate the CV output


float cv = (note - 21) / 127.0 * 5.0;

// Send the CV output to the corresponding voice


int voice = note % numVoices;
analogWrite(cvPins[voice], cv * 255.0);
}
// Check if the event is a note OFF event
else if (midi_file[track][event].isNoteOff()) {
// Get the note value
int note = midi_file[track][event][1];

// Reset the CV output for the corresponding voice


int voice = note % numVoices;
analogWrite(cvPins[voice], 0);
}

// Move to the next event


event++;
if (event >= midi_file[track].size()) {
event = 0;
track++;
if (track >= midi_file.getTrackCount()) {
track = 0;
}
}
}

You might also like