Search in sources :

Example 6 with AudioException

use of jmri.AudioException in project JMRI by JMRI.

the class DefaultAudioManager method init.

/**
     * Method used to initialise the manager
     */
@SuppressFBWarnings(value = "ST_WRITE_TO_STATIC_FROM_INSTANCE_METHOD")
// OK to write to static variables as we only do so if not initialised
@Override
public synchronized void init() {
    if (!initialised) {
        //            // First try to initialise LWJGL
        //            activeAudioFactory = new LWJGLAudioFactory();
        //            log.debug("Try to initialise LWJGLAudioFactory");
        //
        //            // If LWJGL fails, fall-back to JOAL
        //            if (!activeAudioFactory.init()) {
        activeAudioFactory = new JoalAudioFactory();
        log.debug("Try to initialise JoalAudioFactory");
        // If JOAL fails, fall-back to JavaSound
        if (!activeAudioFactory.init()) {
            activeAudioFactory = new JavaSoundAudioFactory();
            log.debug("Try to initialise JavaSoundAudioFactory");
            // Finally, if JavaSound fails, fall-back to a Null sound system
            if (!activeAudioFactory.init()) {
                activeAudioFactory = new NullAudioFactory();
                log.debug("Try to initialise NullAudioFactory");
                activeAudioFactory.init();
            }
        }
        // Create default Listener and save in map
        try {
            Audio s = createNewAudio("IAL$", "Default Audio Listener");
            register(s);
        } catch (AudioException ex) {
            log.error("Error creating Default Audio Listener: " + ex);
        }
        // Finally, create and register a shutdown task to ensure clean exit
        if (audioShutDownTask == null) {
            audioShutDownTask = new QuietShutDownTask("AudioFactory Shutdown") {

                @Override
                public boolean execute() {
                    InstanceManager.getDefault(jmri.AudioManager.class).cleanUp();
                    return true;
                }
            };
        }
        if (InstanceManager.getNullableDefault(jmri.ShutDownManager.class) != null) {
            InstanceManager.getDefault(jmri.ShutDownManager.class).register(audioShutDownTask);
        }
        initialised = true;
        if (log.isDebugEnabled()) {
            log.debug("Initialised AudioFactory type: " + activeAudioFactory.getClass().getSimpleName());
        }
    }
}
Also used : AudioException(jmri.AudioException) QuietShutDownTask(jmri.implementation.QuietShutDownTask) Audio(jmri.Audio) SuppressFBWarnings(edu.umd.cs.findbugs.annotations.SuppressFBWarnings)

Example 7 with AudioException

use of jmri.AudioException in project JMRI by JMRI.

the class SoundBite method init.

public final boolean init(VSDFile vf, BufferMode mode) {
    AudioManager am = jmri.InstanceManager.getDefault(jmri.AudioManager.class);
    if (!initialized) {
        try {
            sound_src = (AudioSource) am.provideAudio(SrcSysNamePrefix + system_name);
            sound_src.setUserName(BufUserNamePrefix + user_name);
            setLooped(false);
            if (mode == BufferMode.BOUND_MODE) {
                sound_buf = (AudioBuffer) am.provideAudio(BufSysNamePrefix + system_name);
                sound_buf.setUserName(BufUserNamePrefix + user_name);
                if (vf == null) {
                    log.debug("VSD file is null! Filename: {}", filename);
                    // Path must be provided by caller.
                    sound_buf.setURL(filename);
                } else {
                    java.io.InputStream ins = vf.getInputStream(filename);
                    if (ins != null) {
                        sound_buf.setInputStream(ins);
                    } else {
                        return (false);
                    }
                }
                sound_src.setAssignedBuffer(sound_buf);
                setLength();
            }
        } catch (AudioException | IllegalArgumentException ex) {
            log.warn("Problem creating SoundBite: " + ex);
        }
    }
    return (true);
}
Also used : AudioManager(jmri.AudioManager) AudioException(jmri.AudioException)

Example 8 with AudioException

use of jmri.AudioException in project JMRI by JMRI.

the class AudioUtil method getAudioBufferList.

/**
     * Take a list of AudioByteBuffers and provide a 1:1 corresponding List of
     * AudioBuffers
     *
     * @param prefix : prefix to use when generating AudioBuffer system names.
     * @param blist  : list of AudioByteBuffers to convert.
     *
     * @return List of AudioBuffers
     */
public static List<AudioBuffer> getAudioBufferList(String prefix, List<AudioByteBuffer> blist) {
    // Sanity check the prefix, since if it's wrong we'll get a casting error below.
    if (prefix.charAt(2) != Audio.BUFFER) {
        log.warn("Not a Buffer request! " + prefix);
        return null;
    }
    List<AudioBuffer> rlist = new ArrayList<>();
    // Index used for the sub-buffer system names
    int i = 0;
    for (AudioByteBuffer b : blist) {
        try {
            AudioBuffer buf = (AudioBuffer) jmri.InstanceManager.getDefault(jmri.AudioManager.class).provideAudio(prefix + "_sbuf" + i);
            i++;
            if (buf == null) {
                log.debug("provideAudio returned null!");
                return null;
            }
            // might be redundant with the try/catch.
            if (buf.getLength() > 0) {
                log.debug("provideAudio found already-built buffer:" + buf.getSystemName() + " ... skipping load.");
            } else {
                buf.loadBuffer(b.data, b.format, b.frequency);
                if (log.isDebugEnabled()) {
                    log.debug("Loaded buffer: " + buf.getSystemName());
                    log.debug(" from file: " + buf.getURL());
                    log.debug(" format: " + b.format + ", " + b.frequency + " Hz");
                    log.debug(" length: " + b.data.limit());
                }
            }
            rlist.add(buf);
        } catch (AudioException | IllegalArgumentException e) {
            log.warn("Error on provideAudio! " + e.toString());
            if (log.isDebugEnabled()) {
                jmri.InstanceManager.getDefault(jmri.AudioManager.class).getSystemNameList(Audio.BUFFER).stream().forEach((s) -> {
                    log.debug("\tBuffer: " + s);
                });
            }
            return null;
        }
    }
    return rlist;
}
Also used : AudioException(jmri.AudioException) Arrays(java.util.Arrays) Logger(org.slf4j.Logger) LoggerFactory(org.slf4j.LoggerFactory) Audio(jmri.Audio) ByteBuffer(java.nio.ByteBuffer) AL(com.jogamp.openal.AL) ArrayList(java.util.ArrayList) ByteOrder(java.nio.ByteOrder) List(java.util.List) ALut(com.jogamp.openal.util.ALut) AudioBuffer(jmri.jmrit.audio.AudioBuffer) ALException(com.jogamp.openal.ALException) InputStream(java.io.InputStream) AudioException(jmri.AudioException) ArrayList(java.util.ArrayList) AudioBuffer(jmri.jmrit.audio.AudioBuffer)

Example 9 with AudioException

use of jmri.AudioException in project JMRI by JMRI.

the class AbstractAudioManager method newAudio.

@Override
public Audio newAudio(String systemName, String userName) throws AudioException {
    if (log.isDebugEnabled()) {
        log.debug("new Audio:" + // NOI18N
        ((systemName == null) ? "null" : systemName) + ";" + // NOI18N
        ((userName == null) ? "null" : userName));
    }
    checkSystemName(systemName, userName);
    // is system name in correct format?
    if ((!systemName.startsWith("" + getSystemPrefix() + typeLetter() + Audio.BUFFER)) && (!systemName.startsWith("" + getSystemPrefix() + typeLetter() + Audio.SOURCE)) && (!systemName.startsWith("" + getSystemPrefix() + typeLetter() + Audio.LISTENER))) {
        log.error("Invalid system name for Audio: " + systemName + " needed either " + getSystemPrefix() + typeLetter() + // NOI18N
        Audio.BUFFER + " or " + getSystemPrefix() + typeLetter() + // NOI18N
        Audio.SOURCE + " or " + getSystemPrefix() + typeLetter() + // NOI18N
        Audio.LISTENER);
        throw new AudioException("Invalid system name for Audio: " + systemName + " needed either " + getSystemPrefix() + typeLetter() + Audio.BUFFER + " or " + getSystemPrefix() + typeLetter() + Audio.SOURCE + " or " + getSystemPrefix() + typeLetter() + Audio.LISTENER);
    }
    // return existing if there is one
    Audio s;
    if ((userName != null) && ((s = getByUserName(userName)) != null)) {
        if (getBySystemName(systemName) != s) {
            log.error("inconsistent user (" + userName + ") and system name (" + systemName + ") results; userName related to (" + s.getSystemName() + ")");
        }
        log.debug("Found existing Audio (" + s.getSystemName() + "). Returning existing (1).");
        return s;
    }
    if ((s = getBySystemName(systemName)) != null) {
        if ((s.getUserName() == null) && (userName != null)) {
            s.setUserName(userName);
        } else if (userName != null) {
            log.warn("Found audio via system name (" + systemName + ") with non-null user name (" + userName + // NOI18N
            ")");
        }
        log.debug("Found existing Audio (" + s.getSystemName() + "). Returning existing (2).");
        return s;
    }
    log.debug("Existing audio not found. Creating new. (" + systemName + ")");
    // doesn't exist, make a new one
    s = createNewAudio(systemName, userName);
    // save in the maps
    if (s != null) {
        register(s);
    } else {
        // must have failed to create
        throw new IllegalArgumentException("can't create audio with System Name \"" + systemName + "\"");
    }
    return s;
}
Also used : AudioException(jmri.AudioException) Audio(jmri.Audio)

Aggregations

AudioException (jmri.AudioException)9 Audio (jmri.Audio)5 AudioManager (jmri.AudioManager)4 AudioBuffer (jmri.jmrit.audio.AudioBuffer)3 AudioListener (jmri.jmrit.audio.AudioListener)2 AudioSource (jmri.jmrit.audio.AudioSource)2 AL (com.jogamp.openal.AL)1 ALException (com.jogamp.openal.ALException)1 ALut (com.jogamp.openal.util.ALut)1 SuppressFBWarnings (edu.umd.cs.findbugs.annotations.SuppressFBWarnings)1 InputStream (java.io.InputStream)1 ByteBuffer (java.nio.ByteBuffer)1 ByteOrder (java.nio.ByteOrder)1 ArrayList (java.util.ArrayList)1 Arrays (java.util.Arrays)1 List (java.util.List)1 Vector3f (javax.vecmath.Vector3f)1 QuietShutDownTask (jmri.implementation.QuietShutDownTask)1 Attribute (org.jdom2.Attribute)1 Element (org.jdom2.Element)1