Code for MIDI Messages?


#1

Hey guys, I’m sorry if this is the wrong place to post this and also if it’s a bit basic (I’m a complete novice with coding), but I’m struggling to find online any code for MIDI messages. I’m trying to get my application to play an audio sample whenever a MIDI key is struck but for that I need to know what the code for MIDI is so I can set it as an if statement. I believe there’s different numbers assigned for different keys (like 60 is middle C) so I imagine that has something to do with it. If it’s any help I’m coding in C++ using xCode on OS X (running the introjucer before hand so it accepts MIDI Input).

Thanks in advance!


#2

Hi, I hope I understood you correctly.

Here is a good page that presents the components of a midi message: http://www.midi.org/techspecs/midimessages.php .

A great starting point for concepts between MIDI and coding can be found here: https://docs.oracle.com/javase/tutorial/sound/overview-MIDI.html . It is for Java, but it gives good language agnostic concepts. The whole Java documentation for MIDI is quite good in my opinion.

If you want to play sounds depending on a key stroke, you should set up the Juce demo, and see how the "AudioSynthesiserDemo.cpp" file works.


#3

The juce MidiMessage class is a good place to start, and search for it in the demo apps to see examples of it in use.


#4

Hi toni07, thanks for your reply.

 

Sorry about this but where can I download this "AudioSynthesiserDemo.cpp" file? I ran a google search and couldn't find it.

 

Also, I'm looking for some sort of source code of the whole reading MIDI messages so then I can copy it into the if statement with some changes (like changing which MIDI number it receives that corresponds to the key struck on the MIDI controller). Would the "AudioSynthesiserDemo.cpp" file cover this?

 

Thanks again!


#5

Hi Jules, thanks for your reply.

 

How would I implement the MidiMessage class in the code? Would it be something like "if MidiMessage = 60 then etc."? Being a complete novice I'm struggling to understand the class reference internet page.


#6

Very hard to know what to reply when people don't know where to begin with even the language itself!

Read some C++ books, look through the codebase for places where MidiMessage is used and see it in action..


#7

Yes, you need to learn the basics of programming first. Get yourself some books as suggested by Jules. “Accelerated C++” is a good one I started with.

Also to play a sample or samples with a midi keyboard see the Synthesiser class and SamplerSound, SamplerVoice.


#8

Hi, sorry for the delay. 

As I told you, the "AudioSynthesiserDemo.cpp" file belongs to the source code of the Juce Demo application, you have this application when you download the whole Juce framework.

I do not have the file in front of me now so I cannot tell you if it contains exactly what you want, but yes, this file "plays" with midi messages, so it can be a good starting point.

Do not forget to check the classes API page for MidiMessage: https://www.juce.com/api/classMidiMessage.html. You should also check the classes MidiOutput and MidiBuffer, and see how these 3 classes are linked together. You can get very basic source code for playing a single midi sound in the forum thread I started: http://www.juce.com/forum/topic/midi-accuracy. It contains source code that is playing midi sounds (but not doing exactly what I want though...).


#9

Did you check out the Examples on the Learn link at the top of the page?

http://www.juce.com/learn/tutorials-code-examples/create-basic-audio-midi-plugin-part-2-coding-your-plugin

Rail


#10

A MidiMessage is not only a number, so you cannot test "if midiMessage == 60". You should check the class page: https://www.juce.com/api/classMidiMessage.html. Here you can see a MidiMessage is composed of 3 bytes and a TimeStamp.

If your "60" code is the number of the note for the midiMessage (one of the 3 bytes), you should write something like "if midiMessage.getNoteNumber() == 60". 


#11

You guys may laugh, but I found Bucky Roberts' C++ tutorials on YouTube a great intro to C++.