Go Down

Topic: Midi file to L.E.D. output (Read 2244 times) previous topic - next topic

arnottkid

I am doing a major project in my engineering class which requires us to sequence music to 8 different L.E.D. strips. I am prototyping my code with an arduino uno with 8 L.E.D.s. I am trying to find a way to take a simple midi file such as " Im a Little Teapot" and convert it to where a channel on the midi corresponds to an L.E.D. I can build from there into our final project, but after my research all I can find is using midi to make sounds with a piezo buzzer. Any help would be greatly appreciated!

DVDdoug

#1
Nov 08, 2017, 10:03 pm Last Edit: Nov 08, 2017, 10:11 pm by DVDdoug
Quote
but after my research all I can find is using midi to make sounds with a piezo buzzer. Any help would be greatly appreciated!
That's a good start...  What are you having trouble with?   If you can read/decode MIDI message and then play the note, it shouldn't be too hard to turn-on an LED instead of making a sound.

This has been done before, but most lighting uses DMX or some other direct or custom connection.   But, if you want a particular light/LED to come-on when you play a C-note on a trumpet, then you'd at-least want MIDI on the input, even if you're wired to DMX lights.

Quote
which requires us to sequence music to 8 different L.E.D. strips.
There are also ways to make "sound activated" lighting effects that react to the loudness or pitch.   I've done several of those effects.

arnottkid

The project we are doing is an L.E.D. light suit for our theater department. One of our ideas was to use a sound activated led, but the theater director really wants it to be pre synced with the music for two reasons:
 1) the speakers are facing away from the stage which will make it hard to interpret the right sound.
 2) There will be 12 suits when we are done and they will have different effects.
Basically what I want to do is use midi to have an easier way of programming the lights to the rhythm of the music, so that after I graduate the classes under neath us will still be able to use the suits withut having to code out every bit of the sequence ( I am a senior).

My problem with decoding the midi to piezo code is that most of the people that I have found that have done that either use a library made specifically for that, or have created a library that is very project specific.

A better question for me to ask is, what is my first step in converting a midi file into something I can work with in my arduino code,as a pin outputs, that will keep the time values consistent with the midi file?

slipstick

MIDI data contains lots of different types of message so you need to decide which ones you're going to do anything with. I guess Note On/Off at least.

Although MIDI messages aren't difficult to decode I still find it easiest to use a library like MIDI.h If you use the callback methods in that it's all very straightforward. The standard Note On handler returns channel, note number and velocity to you and then you do with them what you like.

Here's simplistic test program I wrote to switch different LEDs on and off for different notes on channel 16. That might give you a starting point. This uses standard serial MIDI input but MIDI.h will also a handle MIDI over USB if that's what you're using.

Code: [Select]

#include <SoftwareSerial.h>
#include <MIDI.h>

byte greenLED = 10;
byte redLED = 11;

SoftwareSerial softSerial (2, 3);
MIDI_CREATE_INSTANCE(SoftwareSerial, softSerial, MIDI);

void handleNoteOn(byte channel, byte pitch, byte velocity)
{
  Serial.print("Channel: ");
  Serial.print(channel);

  if (channel == 16) // check channel first
  {
    // Do functions based on note received
    // Simple case - switch appropriate LED on depending on note 60=green, 64=red
    if (pitch == 60) {
      digitalWrite(greenLED, HIGH);
    }
    if (pitch == 64) {
      digitalWrite(redLED, HIGH);
    }
    Serial.print("NoteOn: ");
    Serial.println(pitch);
  }
}
void handleNoteOff(byte channel, byte pitch, byte velocity)
{
  Serial.print("Channel: ");
  Serial.print(channel);

  if (channel == 16) // check channel first if listening on all channels
  {
    // Do functions based on note received
    // Switch appropriate note's LED off
    if (pitch == 60) {
      digitalWrite(greenLED, LOW);
    }
    if (pitch == 64) {
      digitalWrite(redLED, LOW);
    }

    Serial.print(" NoteOff: ");
    Serial.println(pitch);
  }
}
void setup() {
  Serial.begin (57600);
  // Connect the handleNoteOn function to the library,
  // so it is called upon reception of a NoteOn.
  MIDI.setHandleNoteOn(handleNoteOn);  // Put only the name of the function

  // Do the same for NoteOffs
  MIDI.setHandleNoteOff(handleNoteOff);

  // Initiate MIDI communications, listen to all channels
  // change to e.g. MIDI.begin(16); to listen to just ch16.
  MIDI.begin(MIDI_CHANNEL_OMNI);
}

void loop() {
  // Only need to MIDI.read in here, as fast as you can for real-time performance.
  MIDI.read();
  // Note: you could add callback handlers for other message types but
  // currently only NoteOn and NoteOff are set up so
  // any other MIDI messages will be ignored
}


Steve

Grumpy_Mike

A MIDI file is a whole different ball game to MIDI messages.

http://www.ccarh.org/courses/253/handout/smf/

And a lot harder to decode.

slipstick

A MIDI file is a whole different ball game to MIDI messages.
....
And a lot harder to decode.
Ah, true enough. I was thinking of letting something that knew what it was doing, like a DAW/player that was playing the music, synch things and sort the MIDI file out, sending just the messages to the Arduino(s).

But if the "suits" have to be completely standalone and not synched to any master then it is indeed trickier (and way outside my level of competence).

Oh well, I tried.

Steve

marco_c

#6
Nov 09, 2017, 10:44 pm Last Edit: Nov 09, 2017, 10:46 pm by marco_c
MIDI files on SD cards can be read and played through the Arduino. The playing is a callback into user code and you can process the MIDI messages however you want after that.

Library at https://github.com/MajicDesigns/MD_MIDIFile
Arduino Libraries https://github.com/MajicDesigns?tab=Repositories
Parola for Arduino https://github.com/MajicDesigns/Parola
Arduino++ blog https://arduinoplusplus.wordpress.com

arnottkid

 
Quote
The playing is a callback into user code and you can process the MIDI messages however you want after that.
Is there any way you could give me an example code of this? I am trying to use a midi file to output different pins on an arduino and this code looks very promising!

marco_c

Every example with the library already uses callbacks. Pick the simplest to look at.
Arduino Libraries https://github.com/MajicDesigns?tab=Repositories
Parola for Arduino https://github.com/MajicDesigns/Parola
Arduino++ blog https://arduinoplusplus.wordpress.com

DVDdoug

#9
Nov 10, 2017, 09:03 pm Last Edit: Nov 10, 2017, 09:50 pm by DVDdoug
Quote
The project we are doing is an L.E.D. light suit for our theater department.
DMX is the theater/stage standard, but it's up to you if you want to add another layer of conversion from MIDI to DMX.

I'd say it's worth leaving something standard for "future generations".

Quote
One of our ideas was to use a sound activated led, but the theater director really wants it to be pre synced with the music for two reasons:
 1) the speakers are facing away from the stage which will make it hard to interpret the right sound.
Typically you'd use a line-output from the audio mixer, not a microphone picking-up acoustic sound.  ;)   

Quote
2) There will be 12 suits when we are done and they will have different effects.
Basically what I want to do is use midi to have an easier way of programming the lights to the rhythm of the music,
That's understandable.    It's the only way to have total control of what the lights are doing.   Of course, the disadvantage is they have to be programmed.   Unless...  You're playing MIDI music, and you just want to use the existing music files.   

"Big shows" usually have someone "running the lights", and they may have some pre-programmed (DMX) sequences.   I assume you also have someone running the lights.   

DJs use sound activated lights because they want to play a wide variety of music without making a lighting program for each song.   And, usually the DJ has some control over the effects (switching them on & off, etc.).   BTW - Sound activated effects are often DMX controlled.   

P.S.
Your director already knows what he/she wants, but there is a "compromise"...   My sound activated lights have 7 different randomly-selected effects/modes with lots of random variations.    The way I built it, there is a switch to "lock-in" an effect after it comes-up, but I could have had 7 pushbuttons to manually select the function.    (The first-time through, it's not random so if I want a VU meter effect or Knight Rider effect I can wait for the effect to come-around and lock it in....   Not appropriate for a theatrical performance, but that's how mine works.)

Go Up