001package jmri.jmrit.audio;
002
003import javax.vecmath.Vector3f;
004import jmri.Audio;
005import jmri.AudioManager;
006import jmri.InstanceManager;
007import org.slf4j.Logger;
008import org.slf4j.LoggerFactory;
009
010/**
011 * JavaSound implementation of the Audio Listener sub-class.
012 * <p>
013 * For now, no system-specific implementations are forseen - this will remain
014 * internal-only
015 * <p>
016 * For more information about the JavaSound API, visit
017 * <a href="http://java.sun.com/products/java-media/sound/">http://java.sun.com/products/java-media/sound/</a>
018 *
019 * <hr>
020 * This file is part of JMRI.
021 * <p>
022 * JMRI is free software; you can redistribute it and/or modify it under the
023 * terms of version 2 of the GNU General Public License as published by the Free
024 * Software Foundation. See the "COPYING" file for a copy of this license.
025 * <p>
026 * JMRI is distributed in the hope that it will be useful, but WITHOUT ANY
027 * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
028 * A PARTICULAR PURPOSE. See the GNU General Public License for more details.
029 * <p>
030 *
031 * @author Matthew Harris copyright (c) 2009
032 */
033public class JavaSoundAudioListener extends AbstractAudioListener {
034
035    /**
036     * Constructor for new JavaSoundAudioListener with system name
037     *
038     * @param systemName AudioListener object system name (e.g. IAL)
039     */
040    public JavaSoundAudioListener(String systemName) {
041        super(systemName);
042        if (log.isDebugEnabled()) {
043            log.debug("New JavaSoundAudioListener: {}", systemName);
044        }
045    }
046
047    /**
048     * Constructor for new JavaSoundAudioListener with system name and user name
049     *
050     * @param systemName AudioListener object system name (e.g. IAL)
051     * @param userName   AudioListener object user name
052     */
053    public JavaSoundAudioListener(String systemName, String userName) {
054        super(systemName, userName);
055        if (log.isDebugEnabled()) {
056            log.debug("New JavaSoundAudioListener: {} ({})", userName, systemName);
057        }
058    }
059
060    @Override
061    protected void changePosition(Vector3f pos) {
062        recalculateSources();
063    }
064
065    @Override
066    public void setGain(float gain) {
067        super.setGain(gain);
068        recalculateSources();
069    }
070
071    /**
072     * Private method to loop through all sources and recalculate gain & pan
073     */
074    private void recalculateSources() {
075        // Loop through each AudioSource and recalculate their gain & pan
076        AudioManager am = InstanceManager.getDefault(jmri.AudioManager.class);
077        for (Audio audio : am.getNamedBeanSet()) {
078            if (audio.getSubType() == Audio.SOURCE
079                    && audio instanceof JavaSoundAudioSource) {
080                ((JavaSoundAudioSource) audio).calculateGain();
081                ((JavaSoundAudioSource) audio).calculatePan();
082                if (log.isDebugEnabled()) {
083                    log.debug("Recalculating gain & pan for JavaSoundAudioSource {}", audio.getSystemName());
084                }
085            }
086        }
087    }
088
089    @Override
090    protected void cleanup() {
091        // no clean-up needed for Listener
092        if (log.isDebugEnabled()) {
093            log.debug("Cleanup JavaSoundAudioListener ({})", this.getSystemName());
094        }
095    }
096
097    private static final Logger log = LoggerFactory.getLogger(JavaSoundAudioListener.class);
098
099}