001package jmri.jmrit.audio;
002
003import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
004import java.util.SortedSet;
005import java.util.TreeSet;
006import jmri.Audio;
007import jmri.AudioManager;
008import jmri.InstanceManager;
009import org.slf4j.Logger;
010import org.slf4j.LoggerFactory;
011
012/**
013 * This is the null audio system specific AudioFactory.
014 *
015 * It is a dummy factory which provides the necessary object generation but does
016 * not produce any sound. This will normally only be used when running on a
017 * system that has no sound-card installed.
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 NullAudioFactory extends AbstractAudioFactory {
033
034    private static boolean initialised = false;
035
036    private NullAudioListener activeAudioListener;
037
038    @Override
039    public boolean init() {
040        if (initialised) {
041            return true;
042        }
043
044        log.info("Initialised Null audio system - no sounds will be available.");
045
046        super.init();
047        setInit(true);
048        return true;
049    }
050
051    private synchronized static void setInit(boolean newVal) {
052        initialised = newVal;
053    }
054
055    @Override
056    public String toString() {
057        return "NullAudioFactory:"
058                + " vendor - JMRI Community"
059                + " version - " + jmri.Version.name(); // NOI18N
060    }
061
062    @SuppressFBWarnings(value = "ST_WRITE_TO_STATIC_FROM_INSTANCE_METHOD",
063            justification = "OK to write to static variables to record static library status")
064    @Override
065    public void cleanup() {
066        // Stop the command thread
067        super.cleanup();
068
069        // Get the active AudioManager
070        AudioManager am = InstanceManager.getDefault(jmri.AudioManager.class);
071
072        // Retrieve list of AudioSource objects and remove the sources
073        SortedSet<Audio> sources = new TreeSet<>(am.getNamedBeanSet(Audio.SOURCE));
074        for (Audio source: sources) {
075            if (log.isDebugEnabled()) {
076                log.debug("Removing NullAudioSource: {}", source.getSystemName());
077            }
078            // Includes cleanup
079            source.dispose();
080        }
081
082        // Now, retrieve list of AudioBuffer objects and remove the buffers
083        SortedSet<Audio> buffers = new TreeSet<>(am.getNamedBeanSet(Audio.BUFFER));
084        for (Audio buffer : buffers) {
085            if (log.isDebugEnabled()) {
086                log.debug("Removing NullAudioBuffer: {}", buffer.getSystemName());
087            }
088            // Includes cleanup
089            buffer.dispose();
090        }
091
092        // Lastly, retrieve list of AudioListener objects and remove listener.
093        SortedSet<Audio> listeners = new TreeSet<>(am.getNamedBeanSet(Audio.LISTENER));
094        for (Audio listener : listeners) {
095            if (log.isDebugEnabled()) {
096                log.debug("Removing NullAudioListener: {}", listener.getSystemName());
097            }
098            // Includes cleanup
099            listener.dispose();
100        }
101
102        // Finally, shutdown NullAudio and close the output device
103        log.debug("Shutting down NullAudio");
104        // Do nothing
105        initialised = false;
106    }
107
108    @Override
109    public boolean isInitialised() {
110        return initialised;
111    }
112
113    @Override
114    public AudioBuffer createNewBuffer(String systemName, String userName) {
115        return new NullAudioBuffer(systemName, userName);
116    }
117
118    @Override
119    public AudioListener createNewListener(String systemName, String userName) {
120        activeAudioListener = new NullAudioListener(systemName, userName);
121        return activeAudioListener;
122    }
123
124    @Override
125    public AudioListener getActiveAudioListener() {
126        return activeAudioListener;
127    }
128
129    @Override
130    public AudioSource createNewSource(String systemName, String userName) {
131        return new NullAudioSource(systemName, userName);
132    }
133
134    private static final Logger log = LoggerFactory.getLogger(NullAudioFactory.class);
135
136}