001package jmri.jmrit.vsdecoder;
002
003import java.awt.event.ActionListener;
004import javax.swing.Timer;
005import jmri.util.PhysicalLocation;
006import org.jdom2.Element;
007
008/**
009 * Superclass for all Sound types.
010 *
011 * <hr>
012 * This file is part of JMRI.
013 * <p>
014 * JMRI is free software; you can redistribute it and/or modify it under 
015 * the terms of version 2 of the GNU General Public License as published 
016 * by the Free Software Foundation. See the "COPYING" file for a copy
017 * of this license.
018 * <p>
019 * JMRI is distributed in the hope that it will be useful, but WITHOUT 
020 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 
021 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License 
022 * for more details.
023 *
024 * @author Mark Underwood Copyright (C) 2011
025 */
026abstract public class VSDSound {
027
028    public final static String SrcSysNamePrefix = "IAS$VSD:";
029    public final static String BufSysNamePrefix = "IAB$VSD:";
030    public final static String SrcUserNamePrefix = "IVSDS_";
031    public final static String BufUserNamePrefix = "IVSDB_";
032
033    public final static float default_gain = 0.8f;
034    public final static float default_reference_distance = 1.0f;
035    public final static float tunnel_volume = 0.5f;
036
037    Timer t;
038
039    boolean is_playing;
040    boolean is_tunnel;
041    String name;
042    float gain;  // this is the (fixed) gain relative to the other sounds in this Profile
043    float volume; // this is the (active) volume level (product of fixed gain and volume slider).
044
045    PhysicalLocation myposition;
046
047    public VSDSound(String name) {
048        this.name = name;
049        gain = default_gain;
050    }
051
052    /**
053     * Get the audio playing state.
054     * @return the playing state
055     * @deprecated As of 4.99.6, use {Audio.STATE_PLAYING} instead
056     */
057    @Deprecated(since="4.99.6")
058    public boolean isPlaying() {
059        return is_playing;
060    }
061
062    protected Timer newTimer(int time, boolean repeat, ActionListener al) {
063        time = Math.max(1, time);  // make sure the time is > zero
064        t = new Timer(time, al);
065        t.setInitialDelay(time);
066        t.setRepeats(repeat);
067        return t;
068    }
069
070    // Required methods - abstract because all subclasses MUST implement
071    abstract public void play();
072
073    abstract public void loop();
074
075    abstract public void stop();
076
077    abstract public void fadeIn();
078
079    abstract public void fadeOut();
080
081    abstract public void mute(boolean m);
082
083    abstract public void setVolume(float g);
084
085    abstract public void shutdown(); // called on window close.  Cease playing immediately.
086
087    public void setPosition(PhysicalLocation p) {
088        myposition = p;
089    }
090
091    public PhysicalLocation getPosition() {
092        return myposition;
093    }
094
095    // Optional methods - overridden in subclasses where needed.  Do nothing otherwise
096    public void changeNotch(int new_notch) {
097    }
098
099    public void changeThrottle(float t) {
100    }
101
102    public void setName(String n) {
103        name = n;
104    }
105
106    public String getName() {
107        return name;
108    }
109
110    public float getGain() {
111        return gain;
112    }
113
114    public void setGain(float g) {
115        gain = g;
116    }
117
118    double speedCurve(float s) {
119        return s;
120    }
121
122    public void setTunnel(boolean t) {
123        is_tunnel = t;
124    }
125
126    boolean getTunnel() {
127        return is_tunnel;
128    }
129
130    public Element getXml() {
131        Element me = new Element("Sound");
132
133        me.setAttribute("name", name);
134        me.setAttribute("type", "empty");
135        return me;
136    }
137
138    public void setXml(Element e) {
139        // Default: do nothing
140    }
141
142    //private static final Logger log = LoggerFactory.getLogger(VSDSound.class);
143}