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 * @author Klaus Killinger Copyright (C) 2025
026 */
027abstract public class VSDSound {
028
029    final static String SrcSysNamePrefix = "IAS$VSD:";
030    final static String BufSysNamePrefix = "IAB$VSD:";
031    final static String SrcUserNamePrefix = "IVSDS_";
032    final static String BufUserNamePrefix = "IVSDB_";
033
034    final static float default_exponent = 1.0f;
035    final static float default_gain = 0.8f;
036    final static float default_reference_distance = 1.0f;
037    final static float tunnel_volume = 0.5f;
038    final static int default_sleep_interval = 50; // time in ms
039
040    Timer t;
041
042    boolean is_tunnel;
043    String name;
044    float gain; // this is the (fixed) gain relative to the other sounds in this Profile
045    float volume; // this is the (active) volume level (product of fixed gain and volume slider).
046
047    PhysicalLocation myposition;
048
049    public VSDSound(String name) {
050        this.name = name;
051        gain = default_gain;
052        t = null;
053    }
054
055    protected Timer newTimer(int time, boolean repeat, ActionListener al) {
056        time = Math.max(1, time); // make sure the time is > zero
057        t = new Timer(time, al);
058        t.setInitialDelay(time);
059        t.setRepeats(repeat);
060        return t;
061    }
062
063    // Required methods - abstract because all subclasses MUST implement
064    abstract public void play();
065
066    abstract public void loop();
067
068    abstract public void stop();
069
070    abstract public void fadeIn();
071
072    abstract public void fadeOut();
073
074    abstract public void mute(boolean m);
075
076    abstract public void setVolume(float g);
077
078    abstract public void shutdown(); // called on window close.  Cease playing immediately.
079
080    public void setPosition(PhysicalLocation p) {
081        myposition = p;
082    }
083
084    public PhysicalLocation getPosition() {
085        return myposition;
086    }
087
088    // Optional methods - overridden in subclasses where needed.  Do nothing otherwise
089    public void changeNotch(int new_notch) {
090    }
091
092    public void changeThrottle(float t) {
093    }
094
095    public void setName(String n) {
096        name = n;
097    }
098
099    public String getName() {
100        return name;
101    }
102
103    public float getGain() {
104        return gain;
105    }
106
107    public void setGain(float g) {
108        gain = g;
109    }
110
111    public void setTunnel(boolean t) {
112        is_tunnel = t;
113    }
114
115    boolean getTunnel() {
116        return is_tunnel;
117    }
118
119    boolean checkForFreeBuffer() {
120        jmri.AudioManager am = jmri.InstanceManager.getDefault(jmri.AudioManager.class);
121        if (am.getNamedBeanSet(jmri.Audio.BUFFER).size() < jmri.AudioManager.MAX_BUFFERS) {
122            return true;
123        } else {
124            return false;
125        }
126    }
127
128    public Element getXml() {
129        Element me = new Element("Sound");
130
131        me.setAttribute("name", name);
132        me.setAttribute("type", "empty");
133        return me;
134    }
135
136    public void setXml(Element e) {
137        // Default: do nothing
138    }
139
140}