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 *
030 * @author Matthew Harris copyright (c) 2009
031 */
032public class JavaSoundAudioListener extends AbstractAudioListener {
033
034    /**
035     * Constructor for new JavaSoundAudioListener with system name
036     *
037     * @param systemName AudioListener object system name (e.g. IAL)
038     */
039    public JavaSoundAudioListener(String systemName) {
040        super(systemName);
041        if (log.isDebugEnabled()) {
042            log.debug("New JavaSoundAudioListener: {}", systemName);
043        }
044    }
045
046    /**
047     * Constructor for new JavaSoundAudioListener with system name and user name
048     *
049     * @param systemName AudioListener object system name (e.g. IAL)
050     * @param userName   AudioListener object user name
051     */
052    public JavaSoundAudioListener(String systemName, String userName) {
053        super(systemName, userName);
054        if (log.isDebugEnabled()) {
055            log.debug("New JavaSoundAudioListener: {} ({})", userName, systemName);
056        }
057    }
058
059    @Override
060    protected void changePosition(Vector3f pos) {
061        recalculateSources();
062    }
063
064    @Override
065    public void setGain(float gain) {
066        super.setGain(gain);
067        recalculateSources();
068    }
069
070    /**
071     * Private method to loop through all sources and recalculate gain & pan
072     */
073    private void recalculateSources() {
074        // Loop through each AudioSource and recalculate their gain & pan
075        AudioManager am = InstanceManager.getDefault(jmri.AudioManager.class);
076        for (Audio audio : am.getNamedBeanSet()) {
077            if (audio.getSubType() == Audio.SOURCE
078                    && audio instanceof JavaSoundAudioSource) {
079                ((JavaSoundAudioSource) audio).calculateGain();
080                ((JavaSoundAudioSource) audio).calculatePan();
081                if (log.isDebugEnabled()) {
082                    log.debug("Recalculating gain & pan for JavaSoundAudioSource {}", audio.getSystemName());
083                }
084            }
085        }
086    }
087
088    @Override
089    protected void cleanup() {
090        // no clean-up needed for Listener
091        if (log.isDebugEnabled()) {
092            log.debug("Cleanup JavaSoundAudioListener ({})", this.getSystemName());
093        }
094    }
095
096    private static final Logger log = LoggerFactory.getLogger(JavaSoundAudioListener.class);
097
098}