jdk/src/java.desktop/share/classes/com/sun/media/sound/SoftSynthesizer.java
changeset 26037 508779ce6619
parent 26030 576ffa819e23
parent 25859 3317bb8137f4
child 29922 7b9c1e1532cf
equal deleted inserted replaced
25992:e9b05e933ddd 26037:508779ce6619
     1 /*
     1 /*
     2  * Copyright (c) 2008, 2013, Oracle and/or its affiliates. All rights reserved.
     2  * Copyright (c) 2008, 2014, Oracle and/or its affiliates. All rights reserved.
     3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
     3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
     4  *
     4  *
     5  * This code is free software; you can redistribute it and/or modify it
     5  * This code is free software; you can redistribute it and/or modify it
     6  * under the terms of the GNU General Public License version 2 only, as
     6  * under the terms of the GNU General Public License version 2 only, as
     7  * published by the Free Software Foundation.  Oracle designates this
     7  * published by the Free Software Foundation.  Oracle designates this
    26 package com.sun.media.sound;
    26 package com.sun.media.sound;
    27 
    27 
    28 import java.io.BufferedInputStream;
    28 import java.io.BufferedInputStream;
    29 import java.io.File;
    29 import java.io.File;
    30 import java.io.FileInputStream;
    30 import java.io.FileInputStream;
       
    31 import java.io.FileNotFoundException;
    31 import java.io.FileOutputStream;
    32 import java.io.FileOutputStream;
    32 import java.io.IOException;
    33 import java.io.IOException;
    33 import java.io.InputStream;
    34 import java.io.InputStream;
    34 import java.io.OutputStream;
    35 import java.io.OutputStream;
    35 import java.lang.ref.WeakReference;
    36 import java.lang.ref.WeakReference;
   730             if (defaultSoundBank != null) {
   731             if (defaultSoundBank != null) {
   731                 /*
   732                 /*
   732                  * Save generated soundbank to disk for faster future use.
   733                  * Save generated soundbank to disk for faster future use.
   733                  */
   734                  */
   734                 OutputStream out = AccessController
   735                 OutputStream out = AccessController
   735                         .doPrivileged(new PrivilegedAction<OutputStream>() {
   736                         .doPrivileged((PrivilegedAction<OutputStream>) () -> {
   736                             public OutputStream run() {
   737                             try {
   737                                 try {
   738                                 File userhome = new File(System
   738                                     File userhome = new File(System
   739                                         .getProperty("user.home"), ".gervill");
   739                                             .getProperty("user.home"),
   740                                 if (!userhome.exists()) {
   740                                             ".gervill");
   741                                     userhome.mkdirs();
   741                                     if (!userhome.exists())
       
   742                                         userhome.mkdirs();
       
   743                                     File emg_soundbank_file = new File(
       
   744                                             userhome, "soundbank-emg.sf2");
       
   745                                     if (emg_soundbank_file.exists())
       
   746                                         return null;
       
   747                                     return new FileOutputStream(
       
   748                                             emg_soundbank_file);
       
   749                                 } catch (IOException e) {
       
   750                                 } catch (SecurityException e) {
       
   751                                 }
   742                                 }
   752                                 return null;
   743                                 File emg_soundbank_file = new File(
       
   744                                         userhome, "soundbank-emg.sf2");
       
   745                                 if (emg_soundbank_file.exists()) {
       
   746                                     return null;
       
   747                                 }
       
   748                                 return new FileOutputStream(emg_soundbank_file);
       
   749                             } catch (final FileNotFoundException ignored) {
   753                             }
   750                             }
       
   751                             return null;
   754                         });
   752                         });
   755                 if (out != null) {
   753                 if (out != null) {
   756                     try {
   754                     try {
   757                         ((SF2Soundbank) defaultSoundBank).save(out);
   755                         ((SF2Soundbank) defaultSoundBank).save(out);
   758                         out.close();
   756                         out.close();
   759                     } catch (IOException e) {
   757                     } catch (final IOException ignored) {
   760                     }
   758                     }
   761                 }
   759                 }
   762             }
   760             }
   763         }
   761         }
   764         return defaultSoundBank;
   762         return defaultSoundBank;
   844         return info;
   842         return info;
   845     }
   843     }
   846 
   844 
   847     private Properties getStoredProperties() {
   845     private Properties getStoredProperties() {
   848         return AccessController
   846         return AccessController
   849                 .doPrivileged(new PrivilegedAction<Properties>() {
   847                 .doPrivileged((PrivilegedAction<Properties>) () -> {
   850                     public Properties run() {
   848                     Properties p = new Properties();
   851                         Properties p = new Properties();
   849                     String notePath = "/com/sun/media/sound/softsynthesizer";
   852                         String notePath = "/com/sun/media/sound/softsynthesizer";
   850                     try {
   853                         try {
   851                         Preferences prefroot = Preferences.userRoot();
   854                             Preferences prefroot = Preferences.userRoot();
   852                         if (prefroot.nodeExists(notePath)) {
   855                             if (prefroot.nodeExists(notePath)) {
   853                             Preferences prefs = prefroot.node(notePath);
   856                                 Preferences prefs = prefroot.node(notePath);
   854                             String[] prefs_keys = prefs.keys();
   857                                 String[] prefs_keys = prefs.keys();
   855                             for (String prefs_key : prefs_keys) {
   858                                 for (String prefs_key : prefs_keys) {
   856                                 String val = prefs.get(prefs_key, null);
   859                                     String val = prefs.get(prefs_key, null);
   857                                 if (val != null) {
   860                                     if (val != null)
   858                                     p.setProperty(prefs_key, val);
   861                                         p.setProperty(prefs_key, val);
       
   862                                 }
   859                                 }
   863                             }
   860                             }
   864                         } catch (BackingStoreException e) {
       
   865                         } catch (SecurityException e) {
       
   866                         }
   861                         }
   867                         return p;
   862                     } catch (final BackingStoreException ignored) {
   868                     }
   863                     }
       
   864                     return p;
   869                 });
   865                 });
   870     }
   866     }
   871 
   867 
   872     public AudioSynthesizerPropertyInfo[] getPropertyInfo(Map<String, Object> info) {
   868     public AudioSynthesizerPropertyInfo[] getPropertyInfo(Map<String, Object> info) {
   873         List<AudioSynthesizerPropertyInfo> list =
   869         List<AudioSynthesizerPropertyInfo> list =
  1042                 implicitOpen = false;
  1038                 implicitOpen = false;
  1043             }
  1039             }
  1044             return;
  1040             return;
  1045         }
  1041         }
  1046         synchronized (control_mutex) {
  1042         synchronized (control_mutex) {
  1047             Throwable causeException = null;
       
  1048             try {
  1043             try {
  1049                 if (line != null) {
  1044                 if (line != null) {
  1050                     // can throw IllegalArgumentException
  1045                     // can throw IllegalArgumentException
  1051                     setFormat(line.getFormat());
  1046                     setFormat(line.getFormat());
  1052                 }
  1047                 }
  1115                 {
  1110                 {
  1116                     weakstream.pusher = pusher;
  1111                     weakstream.pusher = pusher;
  1117                     weakstream.sourceDataLine = sourceDataLine;
  1112                     weakstream.sourceDataLine = sourceDataLine;
  1118                 }
  1113                 }
  1119 
  1114 
  1120             } catch (LineUnavailableException e) {
  1115             } catch (final LineUnavailableException | SecurityException
  1121                 causeException = e;
  1116                     | IllegalArgumentException e) {
  1122             } catch (IllegalArgumentException e) {
  1117                 if (isOpen()) {
  1123                 causeException = e;
       
  1124             } catch (SecurityException e) {
       
  1125                 causeException = e;
       
  1126             }
       
  1127 
       
  1128             if (causeException != null) {
       
  1129                 if (isOpen())
       
  1130                     close();
  1118                     close();
       
  1119                 }
  1131                 // am: need MidiUnavailableException(Throwable) ctor!
  1120                 // am: need MidiUnavailableException(Throwable) ctor!
  1132                 MidiUnavailableException ex = new MidiUnavailableException(
  1121                 MidiUnavailableException ex = new MidiUnavailableException(
  1133                         "Can not open line");
  1122                         "Can not open line");
  1134                 ex.initCause(causeException);
  1123                 ex.initCause(e);
  1135                 throw ex;
  1124                 throw ex;
  1136             }
  1125             }
  1137 
       
  1138         }
  1126         }
  1139     }
  1127     }
  1140 
  1128 
  1141     public AudioInputStream openStream(AudioFormat targetFormat,
  1129     public AudioInputStream openStream(AudioFormat targetFormat,
  1142             Map<String, Object> info) throws MidiUnavailableException {
  1130             Map<String, Object> info) throws MidiUnavailableException {