3

Hy i want to implement a Midi Synthesizer in Java. (Synthesizer = new Device that represents one or Multiple Instruments)

What i implemented is a javax.sound.midi.Receiver:

package at.bachmann.se.midi.smc;

import javax.sound.midi.MetaMessage;
import javax.sound.midi.MidiEvent;
import javax.sound.midi.MidiMessage;
import javax.sound.midi.Receiver;
import javax.sound.midi.ShortMessage;
import javax.sound.midi.SysexMessage;

public class SmcReceiver implements Receiver {

    public static final int NOTE_ON = 0x90;
    public static final int NOTE_OFF = 0x80;
    public static final String[] NOTE_NAMES = {"C", "C#", "D", "D#", "E", "F", "F#", "G", "G#", "A", "A#", "B"};

    @Override
    public void close() {
        System.out.println();
    }

    @Override
    public void send(MidiMessage message, long timeStamp) {
        if(message instanceof ShortMessage) {
            ShortMessage sMessage = (ShortMessage) message;
            System.out.print("Channel: " + sMessage.getChannel() + " ");
            int cmd = sMessage.getCommand();
            int key = sMessage.getData1();
            int octave = (key / 12)-1;
            int note = key % 12;
            String noteName = NOTE_NAMES[note];
            int velocity = sMessage.getData2();
            switch(cmd) {
                case NOTE_ON:
                    System.out.println("Note on, " + noteName + octave + " key=" + key + " velocity: " + velocity);
                    break;
                case NOTE_OFF:
                    System.out.println("Note off, " + noteName + octave + " key=" + key + " velocity: " + velocity);
                    break;
            }
        } else if(message instanceof MetaMessage) {
            ;//TODO
        } else if(message instanceof SysexMessage) {
            ;//TODO
        }
    }

}

This is how you play a midi file with the defaults:

public class MidiPlayerTest {
    @Test
    public void playMidiTst() throws InvalidMidiDataException, IOException, MidiUnavailableException {
        Sequencer sequencer = MidiSystem.getSequencer();

        System.out.println(sequencer.getClass().getName());

        sequencer.open();

        sequencer.setSequence(MidiSystem.getSequence(new File("tst.midi")));


        sequencer.start();

        while(true) {
            if(sequencer.isRunning()) {
                try {
                    Thread.sleep(200); // Check every second
                } catch(InterruptedException ignore) {
                    break;
                }
            } else {
                break;
            }
        }
        System.out.println("DONE!");
    }
}

So my Problem is how do i tell a Sequencer to use my Receiver?

Things i tried/found:

  • I found no way to tell the sequencer to use my Receiver.
  • I found that the Receiver is used by javax.sound.midi.Synthesizer but there to i found no way to attach a custom Synthesizer Class to the Sequencer.
maxbit89
  • 748
  • 1
  • 11
  • 31
  • Looking your code (not the API) looks like that Receiver is a player, you might try to implement Sequence instead. – Marcos Vasconcelos Oct 06 '17 at 15:01
  • Hm i think you mean the Sequencer? Yes i could implement a Sequencer but then i would have to do all the ugly timing calculation by my self :S... – maxbit89 Oct 06 '17 at 15:14
  • 1
    No i mean the https://docs.oracle.com/javase/7/docs/api/javax/sound/midi/Sequence.html – Marcos Vasconcelos Oct 06 '17 at 15:16
  • A ok no thats not what i want. The Sequence is technicaly a parsed Midi File with the parsed Header and Tracks(Track Header, TrackData). A friend just sent me the link to the docs i couldn't find :S but thanks any way :D – maxbit89 Oct 06 '17 at 16:59
  • 1
    Here's [another reference](http://www.oracle.com/technetwork/java/index-139508.html) that has example source codes for midi and others. There's also [this reference](http://docs.oracle.com/javase/7/docs/technotes/guides/sound/programmer_guide/contents.html) scroll down to sections 2 and 3 for midi-related information. – Paul T. Oct 06 '17 at 18:38

1 Answers1

2

A friend of mine send me the documentation i couldn't find :S: https://docs.oracle.com/javase/tutorial/sound/MIDI-messages.html

So here is the working code:

public class MidiPlayerTest {
    @Test
    public void playMidiTst() throws InvalidMidiDataException, IOException, MidiUnavailableException {
        Sequencer sequencer = MidiSystem.getSequencer();

        Transmitter transmitter = sequencer.getTransmitter();
        transmitter.setReceiver(new SmcReceiver());
        System.out.println(sequencer.getClass().getName());

        sequencer.open();

        sequencer.setSequence(MidiSystem.getSequence(new File("tst.midi")));


        sequencer.start();

        while(true) {
            if(sequencer.isRunning()) {
                try {
                    Thread.sleep(200); // Check every second
                } catch(InterruptedException ignore) {
                    break;
                }
            } else {
                break;
            }
        }
        System.out.println("DONE!");
    }
}
maxbit89
  • 748
  • 1
  • 11
  • 31