621 unloadInstrument(from);
622 ModelMappedInstrument mfrom = new ModelMappedInstrument(
623 (ModelInstrument)to, from.getPatch());
624 return loadInstrument(mfrom);
625 }
626 }
627
628 public Soundbank getDefaultSoundbank() {
629 synchronized (SoftSynthesizer.class) {
630 if (defaultSoundBank != null)
631 return defaultSoundBank;
632
633 List<PrivilegedAction<InputStream>> actions =
634 new ArrayList<PrivilegedAction<InputStream>>();
635
636 actions.add(new PrivilegedAction<InputStream>() {
637 public InputStream run() {
638 File javahome = new File(System.getProperties()
639 .getProperty("java.home"));
640 File libaudio = new File(new File(javahome, "lib"), "audio");
641 if (libaudio.exists()) {
642 File foundfile = null;
643 File[] files = libaudio.listFiles();
644 if (files != null) {
645 for (int i = 0; i < files.length; i++) {
646 File file = files[i];
647 if (file.isFile()) {
648 String lname = file.getName().toLowerCase();
649 if (lname.endsWith(".sf2")
650 || lname.endsWith(".dls")) {
651 if (foundfile == null
652 || (file.length() > foundfile
653 .length())) {
654 foundfile = file;
655 }
656 }
657 }
658 }
659 }
660 if (foundfile != null) {
661 try {
669 });
670
671 actions.add(new PrivilegedAction<InputStream>() {
672 public InputStream run() {
673 if (System.getProperties().getProperty("os.name")
674 .startsWith("Linux")) {
675
676 File[] systemSoundFontsDir = new File[] {
677 /* Arch, Fedora, Mageia */
678 new File("/usr/share/soundfonts/"),
679 new File("/usr/local/share/soundfonts/"),
680 /* Debian, Gentoo, OpenSUSE, Ubuntu */
681 new File("/usr/share/sounds/sf2/"),
682 new File("/usr/local/share/sounds/sf2/"),
683 };
684
685 /*
686 * Look for a default.sf2
687 */
688 for (File systemSoundFontDir : systemSoundFontsDir) {
689 if (systemSoundFontDir.exists()) {
690 File defaultSoundFont = new File(systemSoundFontDir, "default.sf2");
691 if (defaultSoundFont.exists()) {
692 try {
693 return new FileInputStream(defaultSoundFont);
694 } catch (IOException e) {
695 // continue with lookup
696 }
697 }
698 }
699 }
700 }
701 return null;
702 }
703 });
704
705 actions.add(new PrivilegedAction<InputStream>() {
706 public InputStream run() {
707 if (System.getProperties().getProperty("os.name")
708 .startsWith("Windows")) {
709 File gm_dls = new File(System.getenv("SystemRoot")
710 + "\\system32\\drivers\\gm.dls");
711 if (gm_dls.exists()) {
712 try {
713 return new FileInputStream(gm_dls);
714 } catch (IOException e) {
715 }
716 }
717 }
718 return null;
719 }
720 });
721
722 actions.add(new PrivilegedAction<InputStream>() {
723 public InputStream run() {
724 /*
725 * Try to load saved generated soundbank
726 */
727 File userhome = new File(System.getProperty("user.home"),
728 ".gervill");
729 File emg_soundbank_file = new File(userhome,
730 "soundbank-emg.sf2");
731 if (emg_soundbank_file.exists()) {
732 try {
733 return new FileInputStream(emg_soundbank_file);
734 } catch (IOException e) {
735 }
736 }
737 return null;
738 }
739 });
740
741 for (PrivilegedAction<InputStream> action : actions) {
742 try {
743 InputStream is = AccessController.doPrivileged(action);
744 if(is == null) continue;
745 Soundbank sbk;
746 try {
747 sbk = MidiSystem.getSoundbank(new BufferedInputStream(is));
748 } finally {
749 is.close();
750 }
751 if (sbk != null) {
756 }
757 }
758
759 try {
760 /*
761 * Generate emergency soundbank
762 */
763 defaultSoundBank = EmergencySoundbank.createSoundbank();
764 } catch (Exception e) {
765 }
766
767 if (defaultSoundBank != null) {
768 /*
769 * Save generated soundbank to disk for faster future use.
770 */
771 OutputStream out = AccessController
772 .doPrivileged((PrivilegedAction<OutputStream>) () -> {
773 try {
774 File userhome = new File(System
775 .getProperty("user.home"), ".gervill");
776 if (!userhome.exists()) {
777 userhome.mkdirs();
778 }
779 File emg_soundbank_file = new File(
780 userhome, "soundbank-emg.sf2");
781 if (emg_soundbank_file.exists()) {
782 return null;
783 }
784 return new FileOutputStream(emg_soundbank_file);
785 } catch (final FileNotFoundException ignored) {
786 }
787 return null;
788 });
789 if (out != null) {
790 try {
791 ((SF2Soundbank) defaultSoundBank).save(out);
792 out.close();
793 } catch (final IOException ignored) {
794 }
795 }
796 }
797 }
798 return defaultSoundBank;
799 }
800
801 public Instrument[] getAvailableInstruments() {
|
621 unloadInstrument(from);
622 ModelMappedInstrument mfrom = new ModelMappedInstrument(
623 (ModelInstrument)to, from.getPatch());
624 return loadInstrument(mfrom);
625 }
626 }
627
628 public Soundbank getDefaultSoundbank() {
629 synchronized (SoftSynthesizer.class) {
630 if (defaultSoundBank != null)
631 return defaultSoundBank;
632
633 List<PrivilegedAction<InputStream>> actions =
634 new ArrayList<PrivilegedAction<InputStream>>();
635
636 actions.add(new PrivilegedAction<InputStream>() {
637 public InputStream run() {
638 File javahome = new File(System.getProperties()
639 .getProperty("java.home"));
640 File libaudio = new File(new File(javahome, "lib"), "audio");
641 if (libaudio.isDirectory()) {
642 File foundfile = null;
643 File[] files = libaudio.listFiles();
644 if (files != null) {
645 for (int i = 0; i < files.length; i++) {
646 File file = files[i];
647 if (file.isFile()) {
648 String lname = file.getName().toLowerCase();
649 if (lname.endsWith(".sf2")
650 || lname.endsWith(".dls")) {
651 if (foundfile == null
652 || (file.length() > foundfile
653 .length())) {
654 foundfile = file;
655 }
656 }
657 }
658 }
659 }
660 if (foundfile != null) {
661 try {
669 });
670
671 actions.add(new PrivilegedAction<InputStream>() {
672 public InputStream run() {
673 if (System.getProperties().getProperty("os.name")
674 .startsWith("Linux")) {
675
676 File[] systemSoundFontsDir = new File[] {
677 /* Arch, Fedora, Mageia */
678 new File("/usr/share/soundfonts/"),
679 new File("/usr/local/share/soundfonts/"),
680 /* Debian, Gentoo, OpenSUSE, Ubuntu */
681 new File("/usr/share/sounds/sf2/"),
682 new File("/usr/local/share/sounds/sf2/"),
683 };
684
685 /*
686 * Look for a default.sf2
687 */
688 for (File systemSoundFontDir : systemSoundFontsDir) {
689 if (systemSoundFontDir.isDirectory()) {
690 File defaultSoundFont = new File(systemSoundFontDir, "default.sf2");
691 if (defaultSoundFont.isFile()) {
692 try {
693 return new FileInputStream(defaultSoundFont);
694 } catch (IOException e) {
695 // continue with lookup
696 }
697 }
698 }
699 }
700 }
701 return null;
702 }
703 });
704
705 actions.add(new PrivilegedAction<InputStream>() {
706 public InputStream run() {
707 if (System.getProperties().getProperty("os.name")
708 .startsWith("Windows")) {
709 File gm_dls = new File(System.getenv("SystemRoot")
710 + "\\system32\\drivers\\gm.dls");
711 if (gm_dls.isFile()) {
712 try {
713 return new FileInputStream(gm_dls);
714 } catch (IOException e) {
715 }
716 }
717 }
718 return null;
719 }
720 });
721
722 actions.add(new PrivilegedAction<InputStream>() {
723 public InputStream run() {
724 /*
725 * Try to load saved generated soundbank
726 */
727 File userhome = new File(System.getProperty("user.home"),
728 ".gervill");
729 File emg_soundbank_file = new File(userhome,
730 "soundbank-emg.sf2");
731 if (emg_soundbank_file.isFile()) {
732 try {
733 return new FileInputStream(emg_soundbank_file);
734 } catch (IOException e) {
735 }
736 }
737 return null;
738 }
739 });
740
741 for (PrivilegedAction<InputStream> action : actions) {
742 try {
743 InputStream is = AccessController.doPrivileged(action);
744 if(is == null) continue;
745 Soundbank sbk;
746 try {
747 sbk = MidiSystem.getSoundbank(new BufferedInputStream(is));
748 } finally {
749 is.close();
750 }
751 if (sbk != null) {
756 }
757 }
758
759 try {
760 /*
761 * Generate emergency soundbank
762 */
763 defaultSoundBank = EmergencySoundbank.createSoundbank();
764 } catch (Exception e) {
765 }
766
767 if (defaultSoundBank != null) {
768 /*
769 * Save generated soundbank to disk for faster future use.
770 */
771 OutputStream out = AccessController
772 .doPrivileged((PrivilegedAction<OutputStream>) () -> {
773 try {
774 File userhome = new File(System
775 .getProperty("user.home"), ".gervill");
776 if (!userhome.isDirectory()) {
777 if (!userhome.mkdirs()) {
778 return null;
779 }
780 }
781 File emg_soundbank_file = new File(
782 userhome, "soundbank-emg.sf2");
783 if (emg_soundbank_file.isFile()) {
784 return null;
785 }
786 return new FileOutputStream(emg_soundbank_file);
787 } catch (final FileNotFoundException ignored) {
788 }
789 return null;
790 });
791 if (out != null) {
792 try {
793 ((SF2Soundbank) defaultSoundBank).save(out);
794 out.close();
795 } catch (final IOException ignored) {
796 }
797 }
798 }
799 }
800 return defaultSoundBank;
801 }
802
803 public Instrument[] getAvailableInstruments() {
|