Allegro.cc - Online Community

Allegro.cc Forums » Programming Questions » A Question About MIDIs

This thread is locked; no one can reply to it. rss feed Print
A Question About MIDIs
Mr. Big
Member #6,196
September 2005

Is there a way to get the value of the note the MIDI player is currently playing?

miran
Member #2,407
June 2002

I have no experience with this, I'm just quoting the manual:

extern void (*midi_msg_callback)(int msg, int byte1, int byte2);
extern void (*midi_meta_callback)(int type, const unsigned char *data, int length);
extern void (*midi_sysex_callback)(const unsigned char *data, int length);

So yes, it is possible. You just write a midi_msg_callback and in it look out for note on messages. I've no idea what the different message constants are though, or what the meaning of byte1 and byte2 is. Probably looking at the Allegro source code would help... :-/

--
sig used to be here

Mark Oates
Member #1,146
March 2001
avatar

those functions will work only if you catch the MIDI message as it's being sent (via allegro functions or any other source).

what is it that you are trying to do?

Mr. Big
Member #6,196
September 2005

I've finally figured out inverse kinematics, so I thought making a simulation of hands playing a piano could be quite amusing.
I need to know which notes the MIDI player is playing, so I could find the corresponding keys on the virtual piano (a 3D model of one) and make the fingers "press" them.
I don't really understand how to use the functions Miran posted to achieve the goal.
Could anyone please explain?

Thanks in advance,
Mr. Big

Steve++
Member #1,816
January 2002

It helps to know that there are two basic events you are interested in - note on and note off. That is, there is one MIDI event that corresponds to pressing a piano key and there is another that corresponds to releasing it.

How are you going to make the fingers find the keys - are you going to move the fingers directly and let the hands and the rest of the body follow? How will you know which fingers to use? A piano player knows which fingers to use by a combination of the finger number being written above certain notes and by reading ahead and making a good judgement based on experience (and perhaps some subconscious mathematics and logic).

Shawn Hargreaves
The Progenitor
April 2000
avatar

You need to write your own MIDI message processor function, and set midi_msg_callback to point to it. Then when you play a MIDI file, Allegro will call your function each time a MIDI event happens, and you can look at that event to see what notes are starting or stopping.

The format of MIDI messages is a well defined standard:

http://en.wikipedia.org/wiki/MIDI
http://www.srm.com/qtma/davidsmidispec.html
http://www.borg.com/%7Ejglatt/tech/midispec.htm

Matt Smith
Member #783
November 2000

Steve++ makes a good point about predicting which fingers to use. I was going to do sth similar with a guitar neck. My plan depended on each string of the guitar having its own MIDI channel, but even that isn't a complete solution for which finger to use.

If you want to make custom MIDI files, then you could use one MIDI channel per finger, and maybe use sysex events to move the hands in anticipation. You could write a program to automate this conversion, but it seems like too big a job for mere amusement. Piano teachers and pupils might want to buy such a program tho, so it could be worthwhile in the end

Mr. Big
Member #6,196
September 2005

Well, I just put my fingers the way it feels convenient when I play the piano.
And I guess convenient would be without stretching and bending the firgers too much...
When a key needs to be pressed, the program could just check which hand is closer to the key, move the hand there and then test which finger it would be more convenient to use to press the key.
This is not at all foolproof, but it should be fine in most cases, preventing the hand and fingers from being in un-natural positions.
Special cases can be taken care of as well.
It's a bit hard to imagine.
I guess i'll just try and see what happens.

Thanks for the help, guys!
Especially for the useful links, Shawn.

miran
Member #2,407
June 2002

No, that's not good. I don+'t know how to play a piano, but I can guess you need to know a bit ahead which notes you're going to play, so you don't run out of fingers at some point... :)

--
sig used to be here

Mr. Big
Member #6,196
September 2005

I can make it think several steps ahead.
But like I said, it's quite hard to imagine.
I need to see what exactly is wrong to fix it.
I'll come up with something.
Not perfect, of course, but reasonable.

Shawn Hargreaves
The Progenitor
April 2000
avatar

You also need to know slightly ahead so you can move the finger near the right key in advance of pressing it - otherwise your fingers would have to teleport!

Mr. Big
Member #6,196
September 2005

That's right.
But usually, music is written in such a way so it would be playable, so you don't need to think THAT much ahead.
And making it think a few steps ahead shouldn't be too hard.

[EDIT]

Making it really put the fingers in a good way could be quite an expensive process, but then, it can be precalculated once for a MIDI file and then used every time it plays.

[EDIT]

How do I write a... MIDI message processor function? ???

Go to: