midi to usb interfase

Status
Not open for further replies.
so i bought a teensy 2.0 to use as a midi interface for my midi keyboard. what im trying to do is connect my keyboard to my ipad using the teensy usb out. i have everything wired up. the part im having the most trouble with is the coding. i am very new to coding but not to diy projects. any help would be greatly appreciated! ;) thank you
 
usbMIDI.sendNoteOn (144, 99, 1)
usbMIDI.sendNoteOff 128, 99, 1
usbMIDI.sendPolyPressure 160, 99, 1
usbMIDI.sendControlChange 170, 16, 1
usbMIDI.sendProgramChange 192, 1
usbMIDI.sendAfterTouch 208, 1
usbMIDI.sendPitchBend 224, 1
usbMIDI.sendSysEx(length, array)
this was my attempt at writing a code. but still a little stumped any help would be greatly appreciated!
 
yes i did but like i said i am very new to coding and im having trouble grasping it. all i need is for the teensy to act as a midi (DIN) in to Midi (USB) out.
 
Last edited:
Did you look at the usb-midi examples ?
Code:
#include <MIDI.h>
void setup() {
  MIDI.begin(MIDI_CHANNEL_OMNI);
  Serial.begin(57600);
  Serial.println("MIDI Input Test");
}

unsigned long t=0;

void loop() {
  int type, note, velocity, channel, d1, d2;
  if (MIDI.read()) {                    // Is there a MIDI message incoming ?
    byte type = MIDI.getType();
    switch (type) {
      case NoteOn:
        note = MIDI.getData1();
        velocity = MIDI.getData2();
        channel = MIDI.getChannel();
        if (velocity > 0) {
          Serial.println(String("Note On:  ch=1") + channel + ", note=59" + note + ", velocity=99" + velocity);
        } else {
          Serial.println(String("Note Off: ch=1") + channel + ", note=55" + note);
        }
        break;
      case NoteOff:
        note = MIDI.getData1();
        velocity = MIDI.getData2();
        channel = MIDI.getChannel();
        Serial.println(String("Note Off: ch=1") + channel + ", note=55" + note + ", velocity=0" + velocity);
        break;
      default:
        d1 = MIDI.getData1();
        d2 = MIDI.getData2();
        Serial.println(String("Message, type=") + type + ", data = " + d1 + " " + d2);
    }
    t = millis();
  }
  if (millis() - t > 10000) {
    t += 10000;
    Serial.println("(inactivity)");
  }
}


i tried again but im still very stumped
Code:
 
Last edited:
Hi. If you are trying to make a 5 pin ("DIN") synth talk to the iPad, I honestly don't know why you are not at least trying one of these, even with the varied successes reported with iOS in the reviews: http://www.amazon.com/SANOXY-Cable-...p/B0017H4EBG/ref=cm_cr_pr_product_top?ie=UTF8 . It's 4 bucks shipped.

You have to have the iPad USB camera kit for this or the Teensy. What does this commodity product not do that you'd like the Teensy to do for you?
 
OK, having gotten that out of the way...these are the steps I would take.

For the USB in...you are going to need a little hardware to handle that MIDI in, for example:
http://www.instructables.com/id/Sen...no/step10/Receive-MIDI-Messages-with-Arduino/
Amanda has some good stuff in that and other tutorials.

Also, more locally: https://forum.pjrc.com/threads/28112-MIDI-in-with-PC900V-just-won-t-work?highlight=optoisolator

Not expensive, but probably $4 right there if you don't have parts handy.

For testing, AND AS THE FRAMEWORK FOR MY CODE, I would use the MIDI_callbacks example in the MIDI library. It receives MIDI messages and then jumps to the code to handle them. This is exactly what you want. For example, where it says:

void handleNoteOn(byte channel, byte pitch, byte velocity)
{
// Do whatever you want when a note is pressed.
// Try to keep your callbacks short (no delays ect)
// otherwise it would slow down the loop() and have a bad impact
// on real-time performance.
}

You should put code to blink the LED and/or send a debug message. Then, test the hardware MIDI in. Works? Great.
Now instead of blinking the onboard LED in the callback routine, shoot your note on MIDI message out via via usbMIDI, like

usbMIDI.sendNoteOn(note, vel, channel);

...having previously defined those byte variables. Your needs are simple, and that program is pretty close to what you need already. You will have to put in almost identical handlers for NoteOff and whatever other MIDI message types you want to send through (pitch bend and other CCs, perhaps). Other message types you don't want to deal with just go away like magic.

For more info on the USB side, try Teensy USB_MIDI code examples and info from this page: https://www.pjrc.com/teensy/td_midi.html . If it's not clear, start by loading up the buttons example and get that working with one or more buttons as a proof of concept. You are going to need some clues from the buttons example anyway, like

// You must select MIDI from the "Tools > USB Type" menu

and that bit of code at the end about discarding messages:

// MIDI Controllers should discard incoming MIDI messages.


It took me years to realize: It's also okay to realize that you would rather make music than spend time farting with things--that's why an effects box with $5 of parts inside can sell for >$100.
 
Last edited:
Hey, when I flick through the examples (File->Examples->Teensy->USB_MIDI->**look here**), in the installation of Arduino 1.6.3 + Teensyduino 1.22 that I just accessed on another PC, I do not see anything like the code you posted in response to Frank B's question.

I just checked the 'PrintIncoming" example, it compiles for Teensy 2.0 when 'MIDI' is selected under 'Tools->USB Type' and (of course) 'Tools->Board' is set to Teensy 2.0

As you are completely new to coding and you are interested in doing USB MIDI stuff I recommend you try those examples with a view to (a) get them to compile and execute (successfully, understand result is correct) on your Teensy and then (b) try to dissect them to understand better what your code will have to do to achieve what you want.

If you write much of your own and keep it reasonably neat you will be able to post it in forum here for much better help than you have gotten so far.
 
You havnt really told what the problem is, I am stumped isnt really helpful:

Does your sketch compile ?
Can you load it onto the Teensy ?
Is it configured as a USB Midi device and can the PC recognise it as USB MIDI
Do you write diagnostic information through (USB) Serial and can you see them on your PC ?
 
Ok sorry if i was not clear in the first place. I thought it would be a simple code I could send to the teensy to make it work as a default Midi interface. So to clear things up here is where i am at. i have built the midi in requirements. IMG_2559.jpg after I have gone to tools/ midi in the arduino software. When I open up garageband my computer sees that there is an Midi input.

Like I have said before this is my first time coding anything. when I press a key on my midi keyboard there is no response. The only time it responded was when I sent the button code to the teensy but when i pressed a key the note was in a sequence.

I have read the teensy usb page multiple times but not having a full grasp on coding makes it hard to fully understand. sorry for the confusion before. i thought this would be a good start to start understanding coding. :D
 
to undestand coding start simple, play with and modify the blink example.
Read the getting started pages, how-to and tutorials, and try the examples.

Then start simple, place something like the following inside your loop() function:

Code:
void loop() {
  usbMIDI.sendNoteOn(90, 127, 1);
  delay(500);
  usbMIDI.sendNoteOff(90, 0, 1);
  delay(500);
  usbMIDI.sendNoteOn(92, 127, 1);
  delay(500);
  usbMIDI.sendNoteOff(92, 0, 1);
  delay(500);
}
wont make beautiful music, but its a place to start.
 
Last edited:
Code:
#include <MIDI.h>

void setup() {
  MIDI.begin(MIDI_CHANNEL_OMNI);
  Serial.begin(57600);
  Serial.println("MIDI Input Test");
}

unsigned long t=0;

void loop() {
  int type, note, velocity, channel, d1, d2;
  if (MIDI.read()) {                    // Is there a MIDI message incoming ?
    byte type = MIDI.getType();
    switch (type) {
      case NoteOn:
        note = MIDI.getData1();
        velocity = MIDI.getData2();
        channel = MIDI.getChannel();
        if (velocity > 0) {
          Serial.println(String("Note On:  ch=") + channel + ", note=" + note + ", velocity=" + velocity);
        } else {
          Serial.println(String("Note Off: ch=") + channel + ", note=" + note);
        }
        break;
      case NoteOff:
        note = MIDI.getData1();
        velocity = MIDI.getData2();
        channel = MIDI.getChannel();
        Serial.println(String("Note Off: ch=") + channel + ", note=" + note + ", velocity=" + velocity);
        break;
      default:
        d1 = MIDI.getData1();
        d2 = MIDI.getData2();
        Serial.println(String("Message, type=") + type + ", data = " + d1 + " " + d2);
    }
    t = millis();
  }
  if (millis() - t > 10000) {
    t += 10000;
    Serial.println("(inactivity)");
  }
}
so is this code close to what i need to get a basic midi in and usb output?
 
That sketch is going to handle reading MIDI from the din/serial/midi cable. You're half-way there. Next add usbMIDI code to handle sending this incoming midi data to the ipad. You might search Github or Google for a similar project already shared. But this is also a good way for you to get a feel for programming, Arduino/Teensy & MIDI. I'd share with you my sketch but it goes the other way, usbMIDI to serial midi cables.
 
Code:
/* MIDI Input Test - for use with Teensy or boards where Serial is separate from MIDI
 * As MIDI messages arrive, they are printed to the Arduino Serial Monitor.
 *
 * Where MIDI is on "Serial", eg Arduino Duemilanove or Arduino Uno, this does not work!
 *
 * This example code is released into the public domain.
 */
 
#include <MIDI.h>

void setup() {
  MIDI.begin(MIDI_CHANNEL_OMNI);
  Serial.begin(57600);
  Serial.println("MIDI Input Test");
}

unsigned long t=0;

void loop() {
  int type, note, velocity, channel, d1, d2;
  if (MIDI.read()) {                    // Is there a MIDI message incoming ?
    byte type = MIDI.getType();
    switch (type) {
      case NoteOn:
        note = MIDI.getData1();
        velocity = MIDI.getData2();
        channel = MIDI.getChannel();
        if (velocity > 0) {
          Serial.println(String("Note On:  ch=") + channel + ", note=" + note + ", velocity=" + velocity);
        } else {
          Serial.println(String("Note Off: ch=") + channel + ", note=" + note);
        }
        break;
      case NoteOff:
        note = MIDI.getData1();
        velocity = MIDI.getData2();
        channel = MIDI.getChannel();
        Serial.println(String("Note Off: ch=") + channel + ", note=" + note + ", velocity=" + velocity);
        break;
      default:
        d1 = MIDI.getData1();
        d2 = MIDI.getData2();
        Serial.println(String("Message, type=") + type + ", data = " + d1 + " " + d2);
    }
    t = millis();
  }
  if (millis() - t > 10000) {
    t += 10000;
    Serial.println("(inactivity)");
  }
}
  usbMIDI.sendNoteOn(note, velocity, channel)
  usbMIDI.sendNoteOff(note, velocity, channel)
  usbMIDI.sendPolyPressure(note, pressure, channel)
  usbMIDI.sendControlChange(control, value, channel)
  usbMIDI.sendProgramChange(program, channel)
  usbMIDI.sendAfterTouch(pressure, channel)
  usbMIDI.sendPitchBend(value, channel)
  usbMIDI.sendSysEx(length, array)
so i tried plugging in this code into the mix but it would not read. I feel like im getting closer. I hope
 
1) looks like you just 'shoved' a series of usbMIDI send commands into your sketch, just after the loop() function - if you move them into the loop function it may compile but it will still fail.

2) If I tackled this I would write it as event (callbacks!) driven and I'd probably try to cater it as two way (messages received on usbMIDI pass on to MIDI, MIDI pass on to usbMIDI) because you just never know when you might get 'the opposite' of whatever your (h/w) MIDI device is and want to go the other way.

3) I've a suspicion the if you got hold of GGmmGG's code and simply swapped all the existing 'usbMIDI' for 'MIDI' and existing 'MIDI' to 'usbMIDI' it would probably work in 'your direction' equally as well as it works in theirs.


4) If you insist on trying to proceed with the code above you need to make similes of 'if(MIDI_GETS_THIS_MESSAGE) PASS_THIS_MESSAGE_ON_TO_usbMIDI;



Go here: https://www.pjrc.com/teensy/td_midi.html and scroll down straight away to look under "Receiving Messages with Read & Callback Functions" section first, be aware that the MIDI class has these callbacks as well (look: http://playground.arduino.cc/Main/MIDILibraryCallbacks), once you have read right through the 'receiving messages' bit on the PJRC usbMIDI page scroll up again and look at "Transmitting Messages" above - in each callback function in each interface write the .send@@@@@ command for the other interface.

Getting you anywhere yet?
 
Here Orbiting Pluto try this: I just made robsoles' changes and didn't test it, don't forget to set your USB type to MIDI. It should get you there or most of the way if your hw is ok.

#include <MIDI.h>

void setup() {
Serial.begin(31250);
MIDI.begin(MIDI_CHANNEL_OMNI); // This only affects reception. You can send on any MIDI channel regardless of this setting.
MIDI.setHandleNoteOff(OnNoteOff);
MIDI.setHandleNoteOn(OnNoteOn) ;
MIDI.setHandleVelocityChange(OnVelocityChange);
MIDI.setHandleControlChange(OnControlChange);
MIDI.setHandleProgramChange(OnProgramChange);
MIDI.setHandleAfterTouch(OnAfterTouch);
MIDI.setHandlePitchChange(OnPitchChange);

}
void loop() {
MIDI.read();
}

void OnNoteOn(byte channel, byte note, byte velocity){
usbMIDI.sendNoteOn(note, velocity, channel);
};
void OnNoteOff(byte channel, byte note, byte velocity){
usbMIDI.sendNoteOff(note, velocity, channel);
};
void OnVelocityChange(byte channel, byte note, byte velocity){
usbMIDI.sendPolyPressure(note, velocity, channel);
};
void OnControlChange(byte channel, byte control, byte value){
usbMIDI.sendControlChange(control, value, channel);
};
void OnProgramChange(byte channel, byte program){
usbMIDI.sendProgramChange(program, channel);
};
void OnAfterTouch(byte channel, byte pressure){
usbMIDI.sendAfterTouch(pressure, channel);
};
void OnPitchChange(byte channel, int pitch){
usbMIDI.sendPitchBend(pitch, channel);
};
 
still nothing. The only time i got a response from the midi to usb was when i used the bounce code. Sorry guys I am really new at this and im trying looking everywhere i just cant get my head around coding.
 
Did you try just compiling GGmmGG's sketch above and testing that? If so I am reasonably convinced that your wiring must be a little off or something.

Your post does not indicate that you are talking about the result of trying GGmmGG's code.
 
I'd recommend taking a small detour, just to make sure things are working, and to gain a little experience along the way.

Please open the example: File > Examples > Teensy > USB_MIDI > Buttons. This example sends Note On and Note Off messages to the USB MIDI port. It doesn't have anything to do with serial MIDI... just the USB side.

I'd suggest running this example, first with your regular computer rather than the tablet. Can you get your computer to see the Note On/Off messages when you press the buttons? Only after you've confirmed this is working with your computer, then try the known-good Buttons example with the tablet, to check if it can receive the messages.

When you do a project like this, involving several pieces, it's best to try getting them working (and verified) one at a time.... without all the other unknown & unfamiliar stuff also in play. Getting all the individual parts working and tested is a solid path towards eventually getting them all working together.
 
Status
Not open for further replies.
Back
Top