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    public boolean isPlaying() {
053        return is_playing;
054    }
055
056    protected Timer newTimer(int time, boolean repeat, ActionListener al) {
057        time = Math.max(1, time);  // make sure the time is > zero
058        t = new Timer(time, al);
059        t.setInitialDelay(time);
060        t.setRepeats(repeat);
061        return t;
062    }
063
064    // Required methods - abstract because all subclasses MUST implement
065    abstract public void play();
066
067    abstract public void loop();
068
069    abstract public void stop();
070
071    abstract public void fadeIn();
072
073    abstract public void fadeOut();
074
075    abstract public void mute(boolean m);
076
077    abstract public void setVolume(float g);
078
079    abstract public void shutdown(); // called on window close.  Cease playing immediately.
080
081    public void setPosition(PhysicalLocation p) {
082        myposition = p;
083    }
084
085    public PhysicalLocation getPosition() {
086        return myposition;
087    }
088
089    // Optional methods - overridden in subclasses where needed.  Do nothing otherwise
090    public void changeNotch(int new_notch) {
091    }
092
093    public void changeThrottle(float t) {
094    }
095
096    public void setName(String n) {
097        name = n;
098    }
099
100    public String getName() {
101        return name;
102    }
103
104    public float getGain() {
105        return gain;
106    }
107
108    public void setGain(float g) {
109        gain = g;
110    }
111
112    double speedCurve(float s) {
113        return s;
114    }
115
116    public void setTunnel(boolean t) {
117        is_tunnel = t;
118    }
119
120    boolean getTunnel() {
121        return is_tunnel;
122    }
123
124    public Element getXml() {
125        Element me = new Element("Sound");
126
127        me.setAttribute("name", name);
128        me.setAttribute("type", "empty");
129        return me;
130    }
131
132    public void setXml(Element e) {
133        // Default: do nothing
134    }
135
136    //private static final Logger log = LoggerFactory.getLogger(VSDSound.class);
137}