/
EngineSound.java
275 lines (234 loc) · 7.36 KB
/
EngineSound.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
package jmri.jmrit.vsdecoder;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.SwingUtilities;
import org.jdom2.Element;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
/**
* <hr>
* This file is part of JMRI.
* <P>
* JMRI is free software; you can redistribute it and/or modify it under
* the terms of version 2 of the GNU General Public License as published
* by the Free Software Foundation. See the "COPYING" file for a copy
* of this license.
* <P>
* JMRI is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* for more details.
* <P>
*
* @author Mark Underwood Copyright (C) 2011
* @author Klaus Killinger Copyright (C) 2018
*/
// Usage:
// EngineSound() : constructor
// play() : plays short horn pop
// loop() : starts extended sustain horn
// stop() : ends extended sustain horn (plays end sound)
class EngineSound extends VSDSound {
// Engine Sounds
boolean initialized = false;
boolean engine_started = false;
boolean auto_start_engine = false;
boolean is_auto_start; // Can be used in config.xml
boolean is_first = false;
int fade_length = 100;
int fade_in_time = 100;
int fade_out_time = 100;
EnginePane engine_pane;
public EngineSound(String name) {
super(name);
is_playing = false;
engine_started = false;
initialized = init();
}
public boolean init() {
auto_start_engine = VSDecoderManager.instance().getVSDecoderPreferences().isAutoStartingEngine();
return true;
}
// Note: Play and Loop do the same thing, since all of the notch sounds are set to loop.
@Override
public void play() {
log.debug("EngineSound Play");
//if (engine_started || auto_start_engine) {
// is_playing = true;
//}
}
// Note: Play and Loop do the same thing, since all of the notch sounds are set to loop.
@Override
public void loop() {
log.debug("EngineSound Loop");
//if (engine_started || auto_start_engine) {
// is_playing = true;
//}
}
@Override
public void stop() {
is_playing = false;
}
@Override
public void fadeIn() {
this.play();
}
@Override
public void fadeOut() {
this.stop();
}
public int getFadeInTime() {
return this.fade_in_time;
}
public int getFadeOutTime() {
return this.fade_out_time;
}
protected void setFadeInTime(int t) {
this.fade_in_time = t;
}
protected void setFadeInTime(String s) {
if (s == null) {
log.debug("setFadeInTime null string");
return;
}
try {
this.setFadeInTime(Integer.parseInt(s));
} catch (NumberFormatException e) {
log.debug("setFadeInTime Failed to parse int from: {}", s);
}
}
protected void setFadeOutTime(int t) {
this.fade_out_time = t;
}
protected void setFadeOutTime(String s) {
if (s == null) {
log.debug("setFadeInTime null string");
return;
}
try {
this.setFadeOutTime(Integer.parseInt(s));
} catch (NumberFormatException e) {
log.debug("setFadeOutTime Failed to parse int from: {}", s);
}
}
static final public int calcEngineNotch(final float throttle) {
// This will convert to a value 0-8.
int notch = ((int) Math.rint(throttle * 8)) + 1;
if (notch < 1) {
notch = 1;
}
log.debug("Throttle: {}, Notch: {}", throttle, notch);
return notch;
}
static final public int calcEngineNotch(final double throttle) {
// This will convert from a % to a value 0-8.
int notch = ((int) Math.rint(throttle * 8)) + 1;
if (notch < 1) {
notch = 1;
}
//log.warn("Throttle: {}, Notch: {}", throttle, notch);
return notch;
}
// This is the default behavior. Subclasses can do fancier things
// if they want.
public void handleSpeedChange(Float s, EnginePane e) {
engine_pane = e;
engine_pane.setSpeed(s);
}
void setFirstSpeed(boolean f) {
is_first = f;
}
boolean getFirstSpeed() {
return is_first;
}
public void startEngine() {
log.debug("Starting Engine");
}
public void stopEngine() {
}
public boolean isEngineStarted() {
return engine_started;
}
public void setEngineStarted(boolean es) {
engine_started = es;
}
public void functionKey(String e, boolean v, String n) {
}
public void changeLocoDirection(int d) {
}
@Override
public void shutdown() {
// do nothing.
}
@Override
public void mute(boolean m) {
// do nothing.
}
@Override
public void setVolume(float v) {
// do nothing.
}
// Note: We have to invoke engine_pane later because everything's not really setup yet
// Need some more time to get the speed from the assigned throttle
void autoStartCheck() {
if (auto_start_engine || is_auto_start) {
SwingUtilities.invokeLater(() -> {
t = newTimer(40, false, new ActionListener() {
public void actionPerformed(ActionEvent e) {
if (engine_pane != null && getFirstSpeed()) {
engine_pane.startButtonClick();
} else {
log.warn("engine pane or speed not found");
}
}
});
t.start();
});
}
}
protected boolean setXMLAutoStart(Element e) {
String a = e.getChildText("auto-start");
if ((a != null) && (a.equals("yes"))) {
return true;
} else {
return false;
}
}
protected float setXMLGain(Element e) {
String g = e.getChildText("gain");
log.debug(" gain: {}", g);
if ((g != null) && !(g.isEmpty())) {
return Float.parseFloat(g);
} else {
return default_gain;
}
}
protected float setXMLReferenceDistance(Element e) {
String a = e.getChildText("reference-distance");
if ((a != null) && (!a.isEmpty())) {
return Float.parseFloat(a);
} else {
return default_reference_distance;
}
}
@Override
public Element getXml() {
Element me = new Element("sound");
me.setAttribute("name", this.getName());
me.setAttribute("type", "engine");
// Do something, eventually...
return me;
}
public void setXml(Element e, VSDFile vf) {
// Do only the stuff common...
if (this.getName() == null) {
this.setName(e.getAttributeValue("name"));
}
//log.debug("EngineSound: " + this.getName());
this.setFadeInTime(e.getChildText("fade-in-time"));
this.setFadeOutTime(e.getChildText("fade-out-time"));
log.debug("Name: {}, Fade-In-Time: {}, Fade-Out-Time: {}", this.getName(),
this.getFadeInTime(), this.getFadeOutTime());
}
private static final Logger log = LoggerFactory.getLogger(EngineSound.class);
}