Speuce
Posts: 3
Joined: Wed Mar 27, 2019 11:09 pm

Pi won't play sounds from Java Midi Library

Wed Mar 27, 2019 11:15 pm

Using the following code I found online as a quick test for sound, I get no sound from my pi (I have headphones plugged into the jack). I can get the pi to make sounds from other programs such as sonic pi. The program makes a sound when I run it on my mac. But does not on my pi. Instead, I get a 'MidiUnavailableException'. This mustn't be the code. Any idea how I can fix it?


Code:

Code: Select all

import javax.sound.midi.Instrument;
import javax.sound.midi.MidiChannel;
import javax.sound.midi.MidiSystem;
import javax.sound.midi.MidiUnavailableException;
import javax.sound.midi.Synthesizer;

public class MidiTest{

    public static void main(String[] args) { 
      try{
        /* Create a new Sythesizer and open it. Most of 
         * the methods you will want to use to expand on this 
         * example can be found in the Java documentation here: 
         * https://docs.oracle.com/javase/7/docs/api/javax/sound/midi/Synthesizer.html
         */
        Synthesizer midiSynth = MidiSystem.getSynthesizer(); 
        midiSynth.open();

        //get and load default instrument and channel lists
        Instrument[] instr = midiSynth.getDefaultSoundbank().getInstruments();
        MidiChannel[] mChannels = midiSynth.getChannels();

        midiSynth.loadInstrument(instr[0]);//load an instrument
        System.out.print("hi");

        mChannels[0].noteOn(60, 100);//On channel 0, play note number 60 with velocity 100 
        try { Thread.sleep(1000); // wait time in milliseconds to control duration
        } catch( InterruptedException e ) { }
        mChannels[0].noteOff(60);//turn of the note


      } catch (MidiUnavailableException e) {
    	  e.printStackTrace();
      }
   }

}    
Error:

Code: Select all

javax.sound.midi.MidiUnavailableException: Can not open line
	at com.sun.media.sound.SoftSynthesizer.open(SoftSynthesizer.java:1155)
	at com.sun.media.sound.SoftSynthesizer.open(SoftSynthesizer.java:1066)
	at MidiTest.main(MidiTest.java:17)
Caused by: javax.sound.sampled.LineUnavailableException
	at org.classpath.icedtea.pulseaudio.PulseAudioMixer.openImpl(PulseAudioMixer.java:714)
	at org.classpath.icedtea.pulseaudio.PulseAudioMixer.openLocal(PulseAudioMixer.java:588)
	at org.classpath.icedtea.pulseaudio.PulseAudioMixer.openLocal(PulseAudioMixer.java:584)
	at org.classpath.icedtea.pulseaudio.PulseAudioMixer.open(PulseAudioMixer.java:579)
	at org.classpath.icedtea.pulseaudio.PulseAudioDataLine.open(PulseAudioDataLine.java:94)
	at org.classpath.icedtea.pulseaudio.PulseAudioSourceDataLine.open(PulseAudioSourceDataLine.java:75)
	at com.sun.media.sound.SoftSynthesizer.open(SoftSynthesizer.java:1106)
	... 2 more

Thanks in advance for the help :)

Speuce
Posts: 3
Joined: Wed Mar 27, 2019 11:09 pm

Re: Pi won't play sounds from Java Midi Library

Thu Mar 28, 2019 7:06 pm

I am still looking around for a solution! Can anybody help me?

User avatar
Joel_Mckay
Posts: 289
Joined: Mon Nov 12, 2012 10:22 pm
Contact: Website

Re: Pi won't play sounds from Java Midi Library

Thu Mar 28, 2019 8:40 pm

In general,
most MIDI is rendered using a soft-bank of audio samples in a large file.
Although configuring VLC to find and use fluidsynth files (example: /usr/share/sounds/sf2/FluidR3_GM.sf2 ) is fairly straight forward, creating a jack interface to emulate generic MIDI hardware is a bit more cpu intensive.

Also, Java is not a good language for multimedia, and I would highly recommend looking into regular C++ or Unity game-engine .NET APIs.

There are even pre-made mixers you can use on most OS platforms (or with Timidity ):
https://www.libsdl.org/
note for game dev many render midi to mod/xm files so they only need the internal stand-alone library modules.

Best of luck,
J

Speuce
Posts: 3
Joined: Wed Mar 27, 2019 11:09 pm

Re: Pi won't play sounds from Java Midi Library

Fri Mar 29, 2019 2:54 pm

Thanks for your response!

My goal isn't quite related to game dev. I'm looking to make an instrument sort-of-thing where pitch is played based on serial input (coming from an arduino)

I'm comfortable using java and python. c++ — less so. What would you recommend using in my situation?
So far I've tried interfacing my code with Sonic Pi, but the latency this creates is far too much for my purpose.
My reason for using java is that their library allows for a .stop() option. I want the sound to play until the arduino tells it to stop.

Should I look into Timidity?

knute
Posts: 462
Joined: Thu Oct 23, 2014 12:14 am
Location: Texas
Contact: Website

Re: Pi won't play sounds from Java Midi Library

Fri Mar 29, 2019 8:13 pm

Your MidiTest code works fine on my RaspberryPi. I tested it with Oracle Java 8, OpenJDK 8 and OpenJDK 11. I am sending my audio through the HDMI port so maybe your problem is with the headphone jack or directing the audio there. If you are using OpenJDK 8 from the repository you need to modify the associated sound.properties file to get sound to work at all (if you need that fix, post again).

User avatar
Joel_Mckay
Posts: 289
Joined: Mon Nov 12, 2012 10:22 pm
Contact: Website

Re: Pi won't play sounds from Java Midi Library

Fri Mar 29, 2019 8:28 pm

Languages like Java and Python almost always rely on wrapped lower level C/C++ libraries.
So much so, an automated tool was created to make this easier: http://swig.org/

If you don''t need to watch mixing/loading times, than Playing any media though a libVLC high-level instance is fairly simple:
https://wiki.videolan.org/LibVLC_Tutorial/

Also, set up and tear down of emulated interfaces with fluidsynth may make your project a bit more complex.
https://github.com/FluidSynth/fluidsynt ... plications
http://fluidsynth.sourceforge.net/api/i ... altimeMIDI
http://andrewdotni.ch/blog/2015/02/28/m ... spberry-p/
https://github.com/MarquisdeGeek/FluidPi

TiMidity++ is often linked into game engines like Zdoom, but documentation can be an issue,
YMMV
;-)

Return to “Troubleshooting”