0

What I wish to do is write a simple program where whenever I press keys on my external midi controller, an internal receiver accepts the note on messages and somehow allows them to printed as byte information. Ive set up a receiver which is set to my external hardware midi controller's transmitter. I'm not sure how to access the midi information from the receiver after it has arrived there. In terms of research which I have done on the subject, Ive read oracles midi over view and looked at various examples on StackOverFlow. The Oracle article explains connecting the receiver and the transmitter and how to send this info to a synth, but does not cover how to access the Midi Short Messages from the receiver itself without a synth or a sequencer. The StackOverFlow examples have relevant info but I haven't seen anything which explains how to get to extract info from the receiver itself. If someone where to write some example code, and write a detailed explanation, I think it would be a great service to beginners like myself, because it would show how to make the first step in writing code that is compatible with midi i.e. How to receive the midi data in Java and then the first steps in being able to use it. Here is some sample code of what I am already trying.

package receivemidiattempt;

import javax.sound.midi.*;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;

public class ReceiveMidiAttempt {




public static void main(String[] args)   {



 // My internal receiver which I set up with my own MidiReceiver class   which implements 
 // Receiver

 Receiver r = new MidiReceiver ();




   //My Transmitter 
   Transmitter trans = null;

   try {
  //Setting up transmitter with the external midi controller
       trans= MidiSystem.getTransmitter();

   }
   catch(MidiUnavailableException e)
   {
       System.out.println("No Midi port Detected");
   }

  //contecting the transmitter to my internal receiver
   trans.setReceiver(r);
   }
   }

Ive got the transmitter and the receiver connected, but am not sure what to do from here to extract the midi info from the receiver so I can print it. The receiver interface only has two methods, close and send, where close closes the receiver and Send sends a message to the receiver itself. Hoping I can find some way to take the midi info arriving at the receiver and store it in a byte variable called midiInfo which i can then print.

Alright Ive messed around with Java for the past hour and Ive almost figured it out. I think what I'm confused about is the way methods work in a class when an object of that class has been instantiated. For example just by putting println(msg) in the method Send() from the class I made which implements receiver, without invoking the method, every time I press a key on my controller a strange message gets printed which looks like a@787fd and changes every time. If i use msg.getLength i can see that every time I press a key the number 3 is printed. This makes sense considering that a Short message has three bytes The status byte, the pitch byte and the velocity byte. What I still can't figure out is how to get java to print the actual bytes. Almost there though!

Jonathan Hall
  • 75,165
  • 16
  • 143
  • 189
AlexC
  • 39
  • 4
  • One thing that confuses me is why Send is executed even though i don't invoke it in the main method with r.send() ; – AlexC Oct 05 '16 at 03:05
  • Maybe whats happening is that setting the transmitter to my receiver invokes the method send. – AlexC Oct 05 '16 at 03:08
  • Alright I figured it out !!!! I made a byte array bArray. Then In the method i set bArray = msg.getMessage. Now I can print any part of the array i want using bArray[1],bArray[2] etc! This is great because now I can begin to use this information to cause more complicated things to happen !! hahaha i did it !!!!!!! yeses!!!!! – AlexC Oct 05 '16 at 03:33
  • Possible duplicate of [Receiving com.sun.media.sound.FastShortMessage from MIDI Controller in Java, how to decode?](http://stackoverflow.com/questions/23223116/receiving-com-sun-media-sound-fastshortmessage-from-midi-controller-in-java-how) – CL. Oct 05 '16 at 06:43

1 Answers1

2

The easiest way to do this would be to use JFugue:

// You'll need some try/catches around this block. This is traditional Java Midi code.
MidiDevice.Info[] infos = MidiSystem.getMidiDeviceInfo();
MidiDevice device = MidiSystem.getMidiDevice(infos[0]); // You'll have to get the right device for your MIDI controller. 

// Here comes the JFugue code
MusicTransmitterToParserListener m = new MusicTransmitterToParserListener(device);
m.addParserListener(new ParserListenerAdapter() {
    @Override public void onNotePressed(Note note) {
        System.out.println(note + " pressed");
    }
    @Override public void onNoteReleased(Note note) {
        System.out.println(note + " released");
    }
} );

// Choose either this option:
m.startListening();
...do stuff...
m.stopListening();

// Or choose this option:
m.listenForMillis(5000); // Listen for 5000 milliseconds (5 seconds)
David Koelle
  • 20,726
  • 23
  • 93
  • 130
  • Thanks!!!!. Didn't think about using listeners. This will help me a lot! I up voted you . It won't show it because i'm a noob but thanks anyways. – AlexC Oct 16 '16 at 08:04
  • All the best Alex – AlexC Oct 16 '16 at 08:04
  • @AlexC You cannot upvote answer because you don't have enough reputation. If this answered your question, you should accept this answer. [This is how to do it](http://meta.stackexchange.com/a/5235) – Programmer Nov 02 '16 at 04:13