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