Human
Human

Reputation: 565

How to check is transmitter is active?

I am creating a MIDI piano within which I use a transmitter to recieve MIDI keybaord key presses, and receivers to send those messages to the speakers after some byte manipulation.

My problem is that I want to detect if the user is holding down on a key or not. If the user is holding down a key, then the note should be sustained. If they are not then the note should gradually fade to silence.

Java MIDI does not have a method for a transmitter which I can call such as isRunning(), or isActive() to tell is the transmitter, i.e. the user is holding down a key. Is there any way I can detect this?

Within my code I have an infinite while loop which constantly checks if the transmitter has sent any data to the receiver, if it has then process that data, else don't do nothing. However, when a key is pressed and held on a MIDI keyboard the MIDI data is sent to the receiver upon pressing the key, but when when the while loop starts again, no MIDI data is received because the key is held in place.

Upvotes: 1

Views: 63

Answers (1)

SSteve
SSteve

Reputation: 10738

When the user presses a key, a Note On message is sent. When the key is released, a Note Off message (or a Note On message with velocity 0) is sent. You can use that information to determine if a note is being held.

One method: Every time you see a Note On (with velocity > 0) increment a counter. Every time you see a Note Off (or a Note On with velocity 0) decrement the counter. If you see All Notes Off, set the counter to zero. Whenever the counter is greater than zero, a key is being held.

Upvotes: 1

Related Questions