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