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;
}
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;
}
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();
}
}
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;
}
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!");
}
Aggregations