# Java und MIDI unter Linux (wie verwende ich meinen emu10k1 synth?)



## tomiondrums (15. Jul 2009)

Hi!
Ich versuche mich gerade in die Funktionsweise der MIDI-API von Java einzuarbeiten, habe dabei aber einige Probleme. Ich verwende Linux (Gentoo mit derzeit aktuellstem Kernel 2.6.30.1) und habe in meinem Rechner eine SB512 Soundkarte (emu10k1-Chipsatz, mit HW-Wavetable-Synth). Der Wavetable-Synthesizer lässt sich mit Programmen, wie beispielsweise Rosegarden problemlos verwenden, vorausgesetzt, man lädt vorher mit sfxload einen Soundfont. Um in die Materie Linux&Midi&Java einsteigen zu können, habe ich mir im Web ein paar Beispielschnipsel gesucht, die aber bei mir nicht das tun, was sie sollen, womit wir beim Problem wären.

Das erste Beispielprogramm 

```
MidiDevice.Info[] infos = MidiSystem.getMidiDeviceInfo();
for( int i = 0; i < infos.length; i++ ){
      	System.out.println( i + " name='" + infos[i].getName() + "' (" + infos[i].getVendor() + "):    "  + infos[i].getDescription() + "\n\n" );
}
```
läuft einwandfrei durch, aber die von mir gesuchten Infos fördert es leider nicht zu Tage. Ich wüsste nämlich sehr gerne, welches von den vielen gefundenen Devices mein Wavetable-Synthesizer vom emu10k1 ist.

```
0 name='Live [hw:0,0]' (ALSA (http://www.alsa-project.org)):    SB PCI512 [CT4790], EMU10K1 MPU-401 (UART), EMU10K1 MPU-401 (UART)
1 name='Live [hw:0,1]' (ALSA (http://www.alsa-project.org)):    SB PCI512 [CT4790], VirMidi, Emu10k1 Synth MIDI
2 name='Live [hw:0,1]' (ALSA (http://www.alsa-project.org)):    SB PCI512 [CT4790], VirMidi, Emu10k1 Synth MIDI
3 name='Live [hw:0,1]' (ALSA (http://www.alsa-project.org)):    SB PCI512 [CT4790], VirMidi, Emu10k1 Synth MIDI
4 name='Live [hw:0,1]' (ALSA (http://www.alsa-project.org)):    SB PCI512 [CT4790], VirMidi, Emu10k1 Synth MIDI
5 name='Live [hw:0,1]' (ALSA (http://www.alsa-project.org)):    SB PCI512 [CT4790], VirMidi, Emu10k1 Synth MIDI
6 name='Live [hw:0,1]' (ALSA (http://www.alsa-project.org)):    SB PCI512 [CT4790], VirMidi, Emu10k1 Synth MIDI
7 name='Live [hw:0,1]' (ALSA (http://www.alsa-project.org)):    SB PCI512 [CT4790], VirMidi, Emu10k1 Synth MIDI
8 name='Live [hw:0,1]' (ALSA (http://www.alsa-project.org)):    SB PCI512 [CT4790], VirMidi, Emu10k1 Synth MIDI
9 name='Live [hw:0,1]' (ALSA (http://www.alsa-project.org)):    SB PCI512 [CT4790], VirMidi, Emu10k1 Synth MIDI
10 name='Live [hw:0,1]' (ALSA (http://www.alsa-project.org)):    SB PCI512 [CT4790], VirMidi, Emu10k1 Synth MIDI
11 name='Live [hw:0,1]' (ALSA (http://www.alsa-project.org)):    SB PCI512 [CT4790], VirMidi, Emu10k1 Synth MIDI
12 name='Live [hw:0,1]' (ALSA (http://www.alsa-project.org)):    SB PCI512 [CT4790], VirMidi, Emu10k1 Synth MIDI
13 name='Live [hw:0,1]' (ALSA (http://www.alsa-project.org)):    SB PCI512 [CT4790], VirMidi, Emu10k1 Synth MIDI
14 name='Live [hw:0,1]' (ALSA (http://www.alsa-project.org)):    SB PCI512 [CT4790], VirMidi, Emu10k1 Synth MIDI
15 name='Live [hw:0,1]' (ALSA (http://www.alsa-project.org)):    SB PCI512 [CT4790], VirMidi, Emu10k1 Synth MIDI
16 name='Live [hw:0,1]' (ALSA (http://www.alsa-project.org)):    SB PCI512 [CT4790], VirMidi, Emu10k1 Synth MIDI
17 name='Live [hw:0,2]' (ALSA (http://www.alsa-project.org)):    SB PCI512 [CT4790], VirMidi, Emu10k1 Synth MIDI
18 name='Live [hw:0,2]' (ALSA (http://www.alsa-project.org)):    SB PCI512 [CT4790], VirMidi, Emu10k1 Synth MIDI
19 name='Live [hw:0,2]' (ALSA (http://www.alsa-project.org)):    SB PCI512 [CT4790], VirMidi, Emu10k1 Synth MIDI
20 name='Live [hw:0,2]' (ALSA (http://www.alsa-project.org)):    SB PCI512 [CT4790], VirMidi, Emu10k1 Synth MIDI
21 name='Live [hw:0,2]' (ALSA (http://www.alsa-project.org)):    SB PCI512 [CT4790], VirMidi, Emu10k1 Synth MIDI
22 name='Live [hw:0,2]' (ALSA (http://www.alsa-project.org)):    SB PCI512 [CT4790], VirMidi, Emu10k1 Synth MIDI
23 name='Live [hw:0,2]' (ALSA (http://www.alsa-project.org)):    SB PCI512 [CT4790], VirMidi, Emu10k1 Synth MIDI
24 name='Live [hw:0,2]' (ALSA (http://www.alsa-project.org)):    SB PCI512 [CT4790], VirMidi, Emu10k1 Synth MIDI
25 name='Live [hw:0,2]' (ALSA (http://www.alsa-project.org)):    SB PCI512 [CT4790], VirMidi, Emu10k1 Synth MIDI
26 name='Live [hw:0,2]' (ALSA (http://www.alsa-project.org)):    SB PCI512 [CT4790], VirMidi, Emu10k1 Synth MIDI
27 name='Live [hw:0,2]' (ALSA (http://www.alsa-project.org)):    SB PCI512 [CT4790], VirMidi, Emu10k1 Synth MIDI
28 name='Live [hw:0,2]' (ALSA (http://www.alsa-project.org)):    SB PCI512 [CT4790], VirMidi, Emu10k1 Synth MIDI
29 name='Live [hw:0,2]' (ALSA (http://www.alsa-project.org)):    SB PCI512 [CT4790], VirMidi, Emu10k1 Synth MIDI
30 name='Live [hw:0,2]' (ALSA (http://www.alsa-project.org)):    SB PCI512 [CT4790], VirMidi, Emu10k1 Synth MIDI
31 name='Live [hw:0,2]' (ALSA (http://www.alsa-project.org)):    SB PCI512 [CT4790], VirMidi, Emu10k1 Synth MIDI
32 name='Live [hw:0,2]' (ALSA (http://www.alsa-project.org)):    SB PCI512 [CT4790], VirMidi, Emu10k1 Synth MIDI
33 name='UART [hw:1,0]' (ALSA (http://www.alsa-project.org)):    MPU-401 UART, MPU-401U, MPU-401 UART MIDI
34 name='U0x170b0x11 [hw:2,0]' (ALSA (http://www.alsa-project.org)):    USB Device 0x170b:0x11, USB MIDI, USB Device 0x170b:0x11
35 name='Live [hw:0,0]' (ALSA (http://www.alsa-project.org)):    SB PCI512 [CT4790], EMU10K1 MPU-401 (UART), EMU10K1 MPU-401 (UART)
36 name='Live [hw:0,1]' (ALSA (http://www.alsa-project.org)):    SB PCI512 [CT4790], VirMidi, Emu10k1 Synth MIDI
37 name='Live [hw:0,1]' (ALSA (http://www.alsa-project.org)):    SB PCI512 [CT4790], VirMidi, Emu10k1 Synth MIDI
38 name='Live [hw:0,1]' (ALSA (http://www.alsa-project.org)):    SB PCI512 [CT4790], VirMidi, Emu10k1 Synth MIDI
39 name='Live [hw:0,1]' (ALSA (http://www.alsa-project.org)):    SB PCI512 [CT4790], VirMidi, Emu10k1 Synth MIDI
40 name='Live [hw:0,1]' (ALSA (http://www.alsa-project.org)):    SB PCI512 [CT4790], VirMidi, Emu10k1 Synth MIDI
41 name='Live [hw:0,1]' (ALSA (http://www.alsa-project.org)):    SB PCI512 [CT4790], VirMidi, Emu10k1 Synth MIDI
42 name='Live [hw:0,1]' (ALSA (http://www.alsa-project.org)):    SB PCI512 [CT4790], VirMidi, Emu10k1 Synth MIDI
43 name='Live [hw:0,1]' (ALSA (http://www.alsa-project.org)):    SB PCI512 [CT4790], VirMidi, Emu10k1 Synth MIDI
44 name='Live [hw:0,1]' (ALSA (http://www.alsa-project.org)):    SB PCI512 [CT4790], VirMidi, Emu10k1 Synth MIDI
45 name='Live [hw:0,1]' (ALSA (http://www.alsa-project.org)):    SB PCI512 [CT4790], VirMidi, Emu10k1 Synth MIDI
46 name='Live [hw:0,1]' (ALSA (http://www.alsa-project.org)):    SB PCI512 [CT4790], VirMidi, Emu10k1 Synth MIDI
47 name='Live [hw:0,1]' (ALSA (http://www.alsa-project.org)):    SB PCI512 [CT4790], VirMidi, Emu10k1 Synth MIDI
48 name='Live [hw:0,1]' (ALSA (http://www.alsa-project.org)):    SB PCI512 [CT4790], VirMidi, Emu10k1 Synth MIDI
49 name='Live [hw:0,1]' (ALSA (http://www.alsa-project.org)):    SB PCI512 [CT4790], VirMidi, Emu10k1 Synth MIDI
50 name='Live [hw:0,1]' (ALSA (http://www.alsa-project.org)):    SB PCI512 [CT4790], VirMidi, Emu10k1 Synth MIDI
51 name='Live [hw:0,1]' (ALSA (http://www.alsa-project.org)):    SB PCI512 [CT4790], VirMidi, Emu10k1 Synth MIDI
52 name='Live [hw:0,2]' (ALSA (http://www.alsa-project.org)):    SB PCI512 [CT4790], VirMidi, Emu10k1 Synth MIDI
53 name='Live [hw:0,2]' (ALSA (http://www.alsa-project.org)):    SB PCI512 [CT4790], VirMidi, Emu10k1 Synth MIDI
54 name='Live [hw:0,2]' (ALSA (http://www.alsa-project.org)):    SB PCI512 [CT4790], VirMidi, Emu10k1 Synth MIDI
55 name='Live [hw:0,2]' (ALSA (http://www.alsa-project.org)):    SB PCI512 [CT4790], VirMidi, Emu10k1 Synth MIDI
56 name='Live [hw:0,2]' (ALSA (http://www.alsa-project.org)):    SB PCI512 [CT4790], VirMidi, Emu10k1 Synth MIDI
57 name='Live [hw:0,2]' (ALSA (http://www.alsa-project.org)):    SB PCI512 [CT4790], VirMidi, Emu10k1 Synth MIDI
58 name='Live [hw:0,2]' (ALSA (http://www.alsa-project.org)):    SB PCI512 [CT4790], VirMidi, Emu10k1 Synth MIDI
59 name='Live [hw:0,2]' (ALSA (http://www.alsa-project.org)):    SB PCI512 [CT4790], VirMidi, Emu10k1 Synth MIDI
60 name='Live [hw:0,2]' (ALSA (http://www.alsa-project.org)):    SB PCI512 [CT4790], VirMidi, Emu10k1 Synth MIDI
61 name='Live [hw:0,2]' (ALSA (http://www.alsa-project.org)):    SB PCI512 [CT4790], VirMidi, Emu10k1 Synth MIDI
62 name='Live [hw:0,2]' (ALSA (http://www.alsa-project.org)):    SB PCI512 [CT4790], VirMidi, Emu10k1 Synth MIDI
63 name='Live [hw:0,2]' (ALSA (http://www.alsa-project.org)):    SB PCI512 [CT4790], VirMidi, Emu10k1 Synth MIDI
64 name='Live [hw:0,2]' (ALSA (http://www.alsa-project.org)):    SB PCI512 [CT4790], VirMidi, Emu10k1 Synth MIDI
65 name='Live [hw:0,2]' (ALSA (http://www.alsa-project.org)):    SB PCI512 [CT4790], VirMidi, Emu10k1 Synth MIDI
66 name='Live [hw:0,2]' (ALSA (http://www.alsa-project.org)):    SB PCI512 [CT4790], VirMidi, Emu10k1 Synth MIDI
67 name='Live [hw:0,2]' (ALSA (http://www.alsa-project.org)):    SB PCI512 [CT4790], VirMidi, Emu10k1 Synth MIDI
68 name='UART [hw:1,0]' (ALSA (http://www.alsa-project.org)):    MPU-401 UART, MPU-401U, MPU-401 UART MIDI
69 name='U0x170b0x11 [hw:2,0]' (ALSA (http://www.alsa-project.org)):    USB Device 0x170b:0x11, USB MIDI, USB Device 0x170b:0x11
70 name='Real Time Sequencer' (Sun Microsystems):    Software sequencer
71 name='Java Sound Synthesizer' (Sun Microsystems):    Software wavetable synthesizer and receiver
```

Andererseits hätte ich damit auch ganz gern mal wenigstens einen Ton erzeugt, wofür ich mir folgendes gebastelt hab:

```
MidiDevice.Info[] infos = MidiSystem.getMidiDeviceInfo();
for( int i = 0; i < infos.length; i++ ){
	ShortMessage myMsg = new ShortMessage();
	try {
		myMsg.setMessage( ShortMessage.NOTE_ON, 0, 60, 93 );
		long timeStamp = -1;
		Receiver rcvr = null;
		try{
			rcvr = MidiSystem.getMidiDevice( infos[i] ).getReceiver();
		}catch( MidiUnavailableException e1 ){
			System.out.println( "Fehler beim Holen des Receivers!" );
			e1.printStackTrace();
		}
		if( rcvr != null ){
			rcvr.send( myMsg, timeStamp );
			try {
				Thread.sleep( 2000 );
			}catch( InterruptedException e2 ){
				System.out.println( "Fehler beim Warten!" );
				e2.printStackTrace();
			}
		}
	}catch( InvalidMidiDataException e3 ){
		System.out.println( "Fehler bei Meldung!" );
		e3.printStackTrace();
	}
}
```
Eigentlich sollte man, wenn man das laufen lässt irgendwann mal was hören, bei mir passierte dergleichen aber bislang noch nicht. Für einige der Devices bekomme ich eine "MidiUnavailableException MIDI IN receiver not available" und für den Java Sound Synthesizer wirft er mir eine "IllegalStateException: Synthesizer is not open.", was ja verständlich ist, aber warum bekomme ich nix zu hören? Mache ich irgendwas falsch? Gibts vielleicht irgendwo eine Einführung in das Thema, die sich auch explizit mit Linux befasst?

Vielen Dank!
MfG
 Tom


----------



## Spacerat (17. Jul 2009)

Das MidiDevice muss ja auch noch geöffnet werden. In deinem Beispielcode also:[JAVA=9]MidiDevice md = MidiSystem.getDevice(infos_);
md.open();
rcvr = md.getReceiver();[/code]_


----------



## tomiondrums (25. Jul 2009)

Stimmt!


----------

