use of jmri.Audio in project JMRI by JMRI.
the class AudioSourceFrame method applyPressed.
void applyPressed(ActionEvent e) {
String user = userName.getText();
if (user.equals("")) {
user = null;
}
String sName = sysName.getText().toUpperCase();
AudioSource s;
try {
AudioManager am = InstanceManager.getDefault(jmri.AudioManager.class);
try {
s = (AudioSource) am.provideAudio(sName);
} catch (IllegalArgumentException ex) {
throw new AudioException("Problem creating source");
}
if (newSource && am.getByUserName(user) != null) {
am.deregister(s);
synchronized (lock) {
counter--;
}
throw new AudioException("Duplicate user name - please modify");
}
s.setUserName(user);
if (assignedBuffer.getSelectedIndex() > 0) {
Audio a = am.getAudio((String) assignedBuffer.getSelectedItem());
s.setAssignedBuffer(a.getSystemName());
}
s.setMinLoops(loopInfinite.isSelected() ? AudioSource.LOOP_CONTINUOUS : (Integer) loopMin.getValue());
s.setMaxLoops(loopInfinite.isSelected() ? AudioSource.LOOP_CONTINUOUS : (Integer) loopMax.getValue());
// s.setMinLoopDelay((Integer) loopMinDelay.getValue());
// s.setMaxLoopDelay((Integer) loopMaxDelay.getValue());
s.setPosition(position.getValue());
s.setPositionRelative(positionRelative.isSelected());
s.setVelocity(velocity.getValue());
s.setGain(gain.getValue());
s.setPitch(pitch.getValue());
s.setReferenceDistance((Float) refDistance.getValue());
s.setMaximumDistance((Float) maxDistance.getValue());
s.setRollOffFactor((Float) rollOffFactor.getValue());
s.setFadeIn((Integer) fadeInTime.getValue());
s.setFadeOut((Integer) fadeOutTime.getValue());
// Notify changes
model.fireTableDataChanged();
} catch (AudioException ex) {
JOptionPane.showMessageDialog(null, ex.getMessage(), Bundle.getMessage("AudioCreateErrorTitle"), JOptionPane.ERROR_MESSAGE);
}
}
use of jmri.Audio in project JMRI by JMRI.
the class AudioSourceFrame method populateFrame.
/**
* Method to populate the Edit Source frame with current values
*/
@Override
public void populateFrame(Audio a) {
if (!(a instanceof AudioSource)) {
throw new IllegalArgumentException(a.getSystemName() + " is not an AudioSource object");
}
super.populateFrame(a);
AudioSource s = (AudioSource) a;
AudioManager am = InstanceManager.getDefault(jmri.AudioManager.class);
String ab = s.getAssignedBufferName();
Audio b = am.getAudio(ab);
if (b != null) {
assignedBuffer.setSelectedItem(b.getUserName() == null ? ab : b.getUserName());
}
loopInfinite.setSelected((s.getMinLoops() == AudioSource.LOOP_CONTINUOUS));
loopMin.setValue(loopInfinite.isSelected() ? 0 : s.getMinLoops());
loopMax.setValue(loopInfinite.isSelected() ? 0 : s.getMaxLoops());
// loopMinDelay.setValue(s.getMinLoopDelay());
// loopMaxDelay.setValue(s.getMaxLoopDelay());
position.setValue(s.getPosition());
positionRelative.setSelected(s.isPositionRelative());
velocity.setValue(s.getVelocity());
gain.setValue(s.getGain());
pitch.setValue(s.getPitch());
refDistance.setValue(s.getReferenceDistance());
maxDistance.setValue(s.getMaximumDistance());
rollOffFactor.setValue(s.getRollOffFactor());
fadeInTime.setValue(s.getFadeIn());
fadeOutTime.setValue(s.getFadeOut());
this.newSource = false;
}
use of jmri.Audio in project JMRI by JMRI.
the class AbstractAudioManagerConfigXML method store.
/**
* Default implementation for storing the contents of a AudioManager
*
* @param o Object to store, of type AudioManager
* @return Element containing the complete info
*/
@Override
public Element store(Object o) {
Element audio = new Element("audio");
setStoreElementClass(audio);
AudioManager am = (AudioManager) o;
if (am != null) {
java.util.Iterator<String> iter = am.getSystemNameList().iterator();
// don't return an element if there are not any audios to include
if (!iter.hasNext()) {
return null;
}
// (no need to store the automatically created Listener object by itself)
if (am.getSystemNameList(Audio.SOURCE).isEmpty() && am.getSystemNameList(Audio.BUFFER).isEmpty()) {
return null;
}
// finally, don't store if the only Sources and Buffers are for the
// virtual sound decoder (VSD)
int vsdObjectCount = 0;
// count all VSD objects
for (String sname : am.getSystemNameList()) {
if (log.isDebugEnabled()) {
log.debug("Check if " + sname + " is a VSD object");
}
if (sname.length() >= 8 && sname.substring(3, 8).equalsIgnoreCase("$VSD:")) {
log.debug("...yes");
vsdObjectCount++;
}
}
if (log.isDebugEnabled()) {
log.debug("Found " + vsdObjectCount + " VSD objects of " + am.getSystemNameList(Audio.SOURCE).size() + am.getSystemNameList(Audio.BUFFER).size() + " objects");
}
// the number of VSD objects - if so, exit.
if (am.getSystemNameList(Audio.SOURCE).size() + am.getSystemNameList(Audio.BUFFER).size() == vsdObjectCount) {
log.debug("Only VSD objects - nothing to store");
return null;
}
// store global information
audio.setAttribute("distanceattenuated", am.getActiveAudioFactory().isDistanceAttenuated() ? "yes" : "no");
// store the audios
while (iter.hasNext()) {
String sname = iter.next();
if (sname == null) {
log.error("System name null during store");
continue;
}
if (log.isDebugEnabled()) {
log.debug("system name is " + sname);
}
if (sname.length() >= 8 && sname.substring(3, 8).equalsIgnoreCase("$VSD:")) {
if (log.isDebugEnabled()) {
log.debug("Skipping storage of VSD object " + sname);
}
continue;
}
Audio a = am.getBySystemName(sname);
// Transient objects for current element and any children
Element e = null;
Element ce = null;
int type = a.getSubType();
if (type == Audio.BUFFER) {
AudioBuffer ab = (AudioBuffer) a;
e = new Element("audiobuffer").setAttribute("systemName", sname);
e.addContent(new Element("systemName").addContent(sname));
// store common part
storeCommon(ab, e);
// store sub-type specific data
String url = ab.getURL();
ce = new Element("url").addContent("" + (url.isEmpty() ? "" : FileUtil.getPortableFilename(url)));
e.addContent(ce);
ce = new Element("looppoint");
ce.setAttribute("start", "" + ab.getStartLoopPoint());
ce.setAttribute("end", "" + ab.getEndLoopPoint());
e.addContent(ce);
ce = new Element("streamed");
ce.addContent("" + (ab.isStreamed() ? "yes" : "no"));
e.addContent(ce);
} else if (type == Audio.LISTENER) {
AudioListener al = (AudioListener) a;
e = new Element("audiolistener").setAttribute("systemName", sname);
e.addContent(new Element("systemName").addContent(sname));
// store common part
storeCommon(al, e);
// store sub-type specific data
ce = new Element("position");
ce.setAttribute("x", "" + al.getPosition().x);
ce.setAttribute("y", "" + al.getPosition().y);
ce.setAttribute("z", "" + al.getPosition().z);
e.addContent(ce);
ce = new Element("velocity");
ce.setAttribute("x", "" + al.getVelocity().x);
ce.setAttribute("y", "" + al.getVelocity().y);
ce.setAttribute("z", "" + al.getVelocity().z);
e.addContent(ce);
ce = new Element("orientation");
ce.setAttribute("atX", "" + al.getOrientation(Audio.AT).x);
ce.setAttribute("atY", "" + al.getOrientation(Audio.AT).y);
ce.setAttribute("atZ", "" + al.getOrientation(Audio.AT).z);
ce.setAttribute("upX", "" + al.getOrientation(Audio.UP).x);
ce.setAttribute("upY", "" + al.getOrientation(Audio.UP).y);
ce.setAttribute("upZ", "" + al.getOrientation(Audio.UP).z);
e.addContent(ce);
ce = new Element("gain");
ce.addContent("" + al.getGain());
e.addContent(ce);
ce = new Element("metersperunit");
ce.addContent("" + al.getMetersPerUnit());
e.addContent(ce);
} else if (type == Audio.SOURCE) {
AudioSource as = (AudioSource) a;
e = new Element("audiosource").setAttribute("systemName", sname);
e.addContent(new Element("systemName").addContent(sname));
// store common part
storeCommon(as, e);
// store sub-type specific data
ce = new Element("position");
ce.setAttribute("x", "" + as.getPosition().x);
ce.setAttribute("y", "" + as.getPosition().y);
ce.setAttribute("z", "" + as.getPosition().z);
e.addContent(ce);
ce = new Element("velocity");
ce.setAttribute("x", "" + as.getVelocity().x);
ce.setAttribute("y", "" + as.getVelocity().y);
ce.setAttribute("z", "" + as.getVelocity().z);
e.addContent(ce);
ce = new Element("assignedbuffer");
if (as.getAssignedBuffer() != null) {
ce.addContent("" + as.getAssignedBufferName());
}
e.addContent(ce);
ce = new Element("gain");
ce.addContent("" + as.getGain());
e.addContent(ce);
ce = new Element("pitch");
ce.addContent("" + as.getPitch());
e.addContent(ce);
ce = new Element("distances");
ce.setAttribute("ref", "" + as.getReferenceDistance());
float f = as.getMaximumDistance();
ce.setAttribute("max", "" + f);
e.addContent(ce);
ce = new Element("loops");
ce.setAttribute("min", "" + as.getMinLoops());
ce.setAttribute("max", "" + as.getMaxLoops());
// ce.setAttribute("mindelay", ""+as.getMinLoopDelay());
// ce.setAttribute("maxdelay", ""+as.getMaxLoopDelay());
e.addContent(ce);
ce = new Element("fadetimes");
ce.setAttribute("in", "" + as.getFadeIn());
ce.setAttribute("out", "" + as.getFadeOut());
e.addContent(ce);
ce = new Element("dopplerfactor");
ce.addContent("" + as.getDopplerFactor());
e.addContent(ce);
ce = new Element("positionrelative");
ce.addContent("" + (as.isPositionRelative() ? "yes" : "no"));
e.addContent(ce);
}
log.debug("store Audio " + sname);
audio.addContent(e);
}
}
return audio;
}
use of jmri.Audio in project JMRI by JMRI.
the class LogixTableAction method setFileLocation.
/**
* Respond to the [...] button in the Edit Action window action section.
* <p>
* Ask user to select an audio or python script file on disk.
*
* @param e the event heard
*/
void setFileLocation(ActionEvent e) {
ConditionalAction action = _actionList.get(_curActionRowNumber);
JFileChooser currentChooser;
int actionType = action.getType();
if (actionType == Conditional.ACTION_PLAY_SOUND) {
if (sndFileChooser == null) {
sndFileChooser = new JFileChooser(System.getProperty("user.dir") + java.io.File.separator + "resources" + java.io.File.separator + "sounds");
jmri.util.FileChooserFilter filt = new jmri.util.FileChooserFilter("wav sound files");
filt.addExtension("wav");
sndFileChooser.setFileFilter(filt);
}
currentChooser = sndFileChooser;
} else if (actionType == Conditional.ACTION_RUN_SCRIPT) {
if (scriptFileChooser == null) {
scriptFileChooser = new JFileChooser(FileUtil.getScriptsPath());
jmri.util.FileChooserFilter filt = new jmri.util.FileChooserFilter("Python script files");
filt.addExtension("py");
scriptFileChooser.setFileFilter(filt);
}
currentChooser = scriptFileChooser;
} else {
log.warn("Unexpected actionType[" + actionType + "] = " + DefaultConditionalAction.getActionTypeString(actionType));
if (defaultFileChooser == null) {
defaultFileChooser = new JFileChooser(FileUtil.getUserFilesPath());
defaultFileChooser.setFileFilter(new jmri.util.NoArchiveFileFilter());
}
currentChooser = defaultFileChooser;
}
currentChooser.rescanCurrentDirectory();
int retVal = currentChooser.showOpenDialog(null);
// handle selection or cancel
if (retVal == JFileChooser.APPROVE_OPTION) {
// set selected file location in data string
try {
_longActionString.setText(FileUtil.getPortableFilename(currentChooser.getSelectedFile().getCanonicalPath()));
} catch (java.io.IOException ex) {
if (log.isDebugEnabled()) {
log.error("exception setting file location: " + ex);
}
_longActionString.setText("");
}
}
}
use of jmri.Audio in project JMRI by JMRI.
the class DefaultAudioManager method createNewAudio.
@Override
protected synchronized Audio createNewAudio(String systemName, String userName) throws AudioException {
if (activeAudioFactory == null) {
log.debug("Initialise in createNewAudio");
init();
}
Audio a = null;
log.debug("sysName: " + systemName + " userName: " + userName);
if (userName != null && _tuser.containsKey(userName)) {
throw new AudioException("Duplicate name");
}
switch(systemName.charAt(2)) {
case Audio.BUFFER:
{
if (countBuffers >= MAX_BUFFERS) {
log.error("Maximum number of buffers reached (" + countBuffers + ") " + MAX_BUFFERS);
throw new AudioException("Maximum number of buffers reached (" + countBuffers + ") " + MAX_BUFFERS);
}
countBuffers++;
a = activeAudioFactory.createNewBuffer(systemName, userName);
break;
}
case Audio.LISTENER:
{
if (countListeners >= MAX_LISTENERS) {
log.error("Maximum number of Listeners reached (" + countListeners + ") " + MAX_LISTENERS);
throw new AudioException("Maximum number of Listeners reached (" + countListeners + ") " + MAX_LISTENERS);
}
countListeners++;
a = activeAudioFactory.createNewListener(systemName, userName);
break;
}
case Audio.SOURCE:
{
if (countSources >= MAX_SOURCES) {
log.error("Maximum number of Sources reached (" + countSources + ") " + MAX_SOURCES);
throw new AudioException("Maximum number of Sources reached (" + countSources + ") " + MAX_SOURCES);
}
countSources++;
a = activeAudioFactory.createNewSource(systemName, userName);
break;
}
default:
throw new IllegalArgumentException();
}
return a;
}
Aggregations