Search in sources :

Example 11 with Soundbank

use of javax.sound.midi.Soundbank in project jdk8u_jdk by JetBrains.

the class SoftSynthesizer method getAvailableInstruments.

public Instrument[] getAvailableInstruments() {
    Soundbank defsbk = getDefaultSoundbank();
    if (defsbk == null)
        return new Instrument[0];
    Instrument[] inslist_array = defsbk.getInstruments();
    Arrays.sort(inslist_array, new ModelInstrumentComparator());
    return inslist_array;
}
Also used : Soundbank(javax.sound.midi.Soundbank) Instrument(javax.sound.midi.Instrument)

Example 12 with Soundbank

use of javax.sound.midi.Soundbank in project jdk8u_jdk by JetBrains.

the class JARSoundbankReader method getSoundbank.

public Soundbank getSoundbank(URL url) throws InvalidMidiDataException, IOException {
    if (!isZIP(url))
        return null;
    ArrayList<Soundbank> soundbanks = new ArrayList<Soundbank>();
    URLClassLoader ucl = URLClassLoader.newInstance(new URL[] { url });
    InputStream stream = ucl.getResourceAsStream("META-INF/services/javax.sound.midi.Soundbank");
    if (stream == null)
        return null;
    try {
        BufferedReader r = new BufferedReader(new InputStreamReader(stream));
        String line = r.readLine();
        while (line != null) {
            if (!line.startsWith("#")) {
                try {
                    Class<?> c = Class.forName(line.trim(), false, ucl);
                    if (Soundbank.class.isAssignableFrom(c)) {
                        Object o = ReflectUtil.newInstance(c);
                        soundbanks.add((Soundbank) o);
                    }
                } catch (ClassNotFoundException ignored) {
                } catch (InstantiationException ignored) {
                } catch (IllegalAccessException ignored) {
                }
            }
            line = r.readLine();
        }
    } finally {
        stream.close();
    }
    if (soundbanks.size() == 0)
        return null;
    if (soundbanks.size() == 1)
        return soundbanks.get(0);
    SimpleSoundbank sbk = new SimpleSoundbank();
    for (Soundbank soundbank : soundbanks) sbk.addAllInstruments(soundbank);
    return sbk;
}
Also used : Soundbank(javax.sound.midi.Soundbank) InputStreamReader(java.io.InputStreamReader) InputStream(java.io.InputStream) ArrayList(java.util.ArrayList) URLClassLoader(java.net.URLClassLoader) BufferedReader(java.io.BufferedReader)

Example 13 with Soundbank

use of javax.sound.midi.Soundbank in project jdk8u_jdk by JetBrains.

the class SoftSynthesizer method openStream.

public AudioInputStream openStream(AudioFormat targetFormat, Map<String, Object> info) throws MidiUnavailableException {
    if (isOpen())
        throw new MidiUnavailableException("Synthesizer is already open");
    synchronized (control_mutex) {
        gmmode = 0;
        voice_allocation_mode = 0;
        processPropertyInfo(info);
        open = true;
        implicitOpen = false;
        if (targetFormat != null)
            setFormat(targetFormat);
        if (load_default_soundbank) {
            Soundbank defbank = getDefaultSoundbank();
            if (defbank != null) {
                loadAllInstruments(defbank);
            }
        }
        voices = new SoftVoice[maxpoly];
        for (int i = 0; i < maxpoly; i++) voices[i] = new SoftVoice(this);
        mainmixer = new SoftMainMixer(this);
        channels = new SoftChannel[number_of_midi_channels];
        for (int i = 0; i < channels.length; i++) channels[i] = new SoftChannel(this, i);
        if (external_channels == null) {
            // when the synhtesizer is closed.
            if (channels.length < 16)
                external_channels = new SoftChannelProxy[16];
            else
                external_channels = new SoftChannelProxy[channels.length];
            for (int i = 0; i < external_channels.length; i++) external_channels[i] = new SoftChannelProxy();
        } else {
            // into the new one
            if (channels.length > external_channels.length) {
                SoftChannelProxy[] new_external_channels = new SoftChannelProxy[channels.length];
                for (int i = 0; i < external_channels.length; i++) new_external_channels[i] = external_channels[i];
                for (int i = external_channels.length; i < new_external_channels.length; i++) {
                    new_external_channels[i] = new SoftChannelProxy();
                }
            }
        }
        for (int i = 0; i < channels.length; i++) external_channels[i].setChannel(channels[i]);
        for (SoftVoice voice : getVoices()) voice.resampler = resampler.openStreamer();
        for (Receiver recv : getReceivers()) {
            SoftReceiver srecv = ((SoftReceiver) recv);
            srecv.open = open;
            srecv.mainmixer = mainmixer;
            srecv.midimessages = mainmixer.midimessages;
        }
        return mainmixer.getInputStream();
    }
}
Also used : MidiUnavailableException(javax.sound.midi.MidiUnavailableException) Soundbank(javax.sound.midi.Soundbank) Receiver(javax.sound.midi.Receiver)

Example 14 with Soundbank

use of javax.sound.midi.Soundbank in project jdk8u_jdk by JetBrains.

the class SoftSynthesizer method getDefaultSoundbank.

public Soundbank getDefaultSoundbank() {
    synchronized (SoftSynthesizer.class) {
        if (defaultSoundBank != null)
            return defaultSoundBank;
        List<PrivilegedAction<InputStream>> actions = new ArrayList<PrivilegedAction<InputStream>>();
        actions.add(new PrivilegedAction<InputStream>() {

            public InputStream run() {
                File javahome = new File(System.getProperties().getProperty("java.home"));
                File libaudio = new File(new File(javahome, "lib"), "audio");
                if (libaudio.exists()) {
                    File foundfile = null;
                    File[] files = libaudio.listFiles();
                    if (files != null) {
                        for (int i = 0; i < files.length; i++) {
                            File file = files[i];
                            if (file.isFile()) {
                                String lname = file.getName().toLowerCase();
                                if (lname.endsWith(".sf2") || lname.endsWith(".dls")) {
                                    if (foundfile == null || (file.length() > foundfile.length())) {
                                        foundfile = file;
                                    }
                                }
                            }
                        }
                    }
                    if (foundfile != null) {
                        try {
                            return new FileInputStream(foundfile);
                        } catch (IOException e) {
                        }
                    }
                }
                return null;
            }
        });
        actions.add(new PrivilegedAction<InputStream>() {

            public InputStream run() {
                if (System.getProperties().getProperty("os.name").startsWith("Linux")) {
                    File[] systemSoundFontsDir = new File[] { /* Arch, Fedora, Mageia */
                    new File("/usr/share/soundfonts/"), new File("/usr/local/share/soundfonts/"), /* Debian, Gentoo, OpenSUSE, Ubuntu */
                    new File("/usr/share/sounds/sf2/"), new File("/usr/local/share/sounds/sf2/") };
                    /*
                         * Look for a default.sf2
                         */
                    for (File systemSoundFontDir : systemSoundFontsDir) {
                        if (systemSoundFontDir.exists()) {
                            File defaultSoundFont = new File(systemSoundFontDir, "default.sf2");
                            if (defaultSoundFont.exists()) {
                                try {
                                    return new FileInputStream(defaultSoundFont);
                                } catch (IOException e) {
                                // continue with lookup
                                }
                            }
                        }
                    }
                }
                return null;
            }
        });
        actions.add(new PrivilegedAction<InputStream>() {

            public InputStream run() {
                if (System.getProperties().getProperty("os.name").startsWith("Windows")) {
                    File gm_dls = new File(System.getenv("SystemRoot") + "\\system32\\drivers\\gm.dls");
                    if (gm_dls.exists()) {
                        try {
                            return new FileInputStream(gm_dls);
                        } catch (IOException e) {
                        }
                    }
                }
                return null;
            }
        });
        actions.add(new PrivilegedAction<InputStream>() {

            public InputStream run() {
                /*
                     * Try to load saved generated soundbank
                     */
                File userhome = new File(System.getProperty("user.home"), ".gervill");
                File emg_soundbank_file = new File(userhome, "soundbank-emg.sf2");
                if (emg_soundbank_file.exists()) {
                    try {
                        return new FileInputStream(emg_soundbank_file);
                    } catch (IOException e) {
                    }
                }
                return null;
            }
        });
        for (PrivilegedAction<InputStream> action : actions) {
            try {
                InputStream is = AccessController.doPrivileged(action);
                if (is == null)
                    continue;
                Soundbank sbk;
                try {
                    sbk = MidiSystem.getSoundbank(new BufferedInputStream(is));
                } finally {
                    is.close();
                }
                if (sbk != null) {
                    defaultSoundBank = sbk;
                    return defaultSoundBank;
                }
            } catch (Exception e) {
            }
        }
        try {
            /*
                 * Generate emergency soundbank
                 */
            defaultSoundBank = EmergencySoundbank.createSoundbank();
        } catch (Exception e) {
        }
        if (defaultSoundBank != null) {
            /*
                 * Save generated soundbank to disk for faster future use.
                 */
            OutputStream out = AccessController.doPrivileged((PrivilegedAction<OutputStream>) () -> {
                try {
                    File userhome = new File(System.getProperty("user.home"), ".gervill");
                    if (!userhome.exists()) {
                        userhome.mkdirs();
                    }
                    File emg_soundbank_file = new File(userhome, "soundbank-emg.sf2");
                    if (emg_soundbank_file.exists()) {
                        return null;
                    }
                    return new FileOutputStream(emg_soundbank_file);
                } catch (final FileNotFoundException ignored) {
                }
                return null;
            });
            if (out != null) {
                try {
                    ((SF2Soundbank) defaultSoundBank).save(out);
                    out.close();
                } catch (final IOException ignored) {
                }
            }
        }
    }
    return defaultSoundBank;
}
Also used : Soundbank(javax.sound.midi.Soundbank) BufferedInputStream(java.io.BufferedInputStream) AudioInputStream(javax.sound.sampled.AudioInputStream) FileInputStream(java.io.FileInputStream) InputStream(java.io.InputStream) OutputStream(java.io.OutputStream) FileOutputStream(java.io.FileOutputStream) ArrayList(java.util.ArrayList) FileNotFoundException(java.io.FileNotFoundException) IOException(java.io.IOException) FileInputStream(java.io.FileInputStream) BackingStoreException(java.util.prefs.BackingStoreException) LineUnavailableException(javax.sound.sampled.LineUnavailableException) IOException(java.io.IOException) MidiUnavailableException(javax.sound.midi.MidiUnavailableException) FileNotFoundException(java.io.FileNotFoundException) PrivilegedAction(java.security.PrivilegedAction) BufferedInputStream(java.io.BufferedInputStream) FileOutputStream(java.io.FileOutputStream) File(java.io.File)

Example 15 with Soundbank

use of javax.sound.midi.Soundbank in project jdk8u_jdk by JetBrains.

the class TestCreateSoundbank method main.

public static void main(String[] args) throws Exception {
    Soundbank soundbank = EmergencySoundbank.createSoundbank();
    for (int i = 0; i < 128; i++) {
        Patch patch = new ModelPatch(0, i, false);
        ModelInstrument ins = (ModelInstrument) soundbank.getInstrument(patch);
        if (ins == null)
            throw new Exception("Instrument " + i + " is missing!");
        if (ins.getPerformers().length == 0)
            throw new Exception("Instrument " + i + " doesn't have any performers!");
    }
    Patch patch = new ModelPatch(0, 0, true);
    ModelInstrument ins = (ModelInstrument) soundbank.getInstrument(patch);
    if (ins == null)
        throw new Exception("Drumkit instrument is missing!");
    if (ins.getPerformers().length == 0)
        throw new Exception("Drumkit instrument doesn't have any performers!");
}
Also used : ModelPatch(com.sun.media.sound.ModelPatch) Soundbank(javax.sound.midi.Soundbank) EmergencySoundbank(com.sun.media.sound.EmergencySoundbank) ModelInstrument(com.sun.media.sound.ModelInstrument) Patch(javax.sound.midi.Patch) ModelPatch(com.sun.media.sound.ModelPatch)

Aggregations

Soundbank (javax.sound.midi.Soundbank)24 Patch (javax.sound.midi.Patch)12 File (java.io.File)5 SF2SoundbankReader (com.sun.media.sound.SF2SoundbankReader)4 BufferedInputStream (java.io.BufferedInputStream)3 FileInputStream (java.io.FileInputStream)3 IOException (java.io.IOException)3 InputStream (java.io.InputStream)3 Instrument (javax.sound.midi.Instrument)3 AudioInputStream (javax.sound.sampled.AudioInputStream)3 ArrayList (java.util.ArrayList)2 MidiUnavailableException (javax.sound.midi.MidiUnavailableException)2 UnsupportedAudioFileException (javax.sound.sampled.UnsupportedAudioFileException)2 EmergencySoundbank (com.sun.media.sound.EmergencySoundbank)1 ModelInstrument (com.sun.media.sound.ModelInstrument)1 ModelPatch (com.sun.media.sound.ModelPatch)1 BufferedReader (java.io.BufferedReader)1 FileNotFoundException (java.io.FileNotFoundException)1 FileOutputStream (java.io.FileOutputStream)1 InputStreamReader (java.io.InputStreamReader)1