When exporting to a .jar file, different MIDI instruments are different.

I have created a java program that synthesizes sounds using the MIDI package in the java sound API, however, when I export it to a .jar file, the sound played is very different from what happens when I run it in eclipse. Does anyone know why this is being done or how to fix this problem?

A list of tools can be found here: http://www.hittrax.com.au/images/artists/gmgs.pdf

Below is a section of my code

try {
        Synthesizer synth = MidiSystem.getSynthesizer();
        synth.open();
        MidiChannel[] channels = synth.getChannels();
        channels[0].programChange(123); // Set the instrument to be played (integer between 0 and 127)

        channels[0].noteOn(60, 80); // Play Middle C
        Thread.sleep(duration);
        channels[0].noteOff(60);
        Thread.sleep(500);

        synth.close();

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

      

The picture below shows the sound when I record it, the first is what should sound like on eclipse, the second is what the sound when exporting to .jar

Sound Waves

+3


source to share


1 answer


As you just learned, Java returns a different default synthesizer when running the jar file outside of eclipse.

It can be caused by a property javax.sound.midi.Synthesizer

or sound.properties

on the classpath.

As a workaround, you can print the property value when you run your application inside eclipse and set it manually so that the jar file uses the same synthesizer.

Edit:

If you want to use com.sun.media.sound.MixerSynth

the default, create a property



javax.sound.midi.Synthesizer=com.sun.media.sound.MixerSynth

      

Example:

Properties props = System.getProperties();
props.setProperty("javax.sound.midi.Synthesizer", "com.sun.media.sound.MixerSynth");
Synthesizer synth = MidiSystem.getSynthesizer();
....

      

More information: http://docs.oracle.com/javase/7/docs/api/javax/sound/midi/MidiSystem.html )

+1


source







All Articles