Skip to content

Commit

Permalink
steam update
Browse files Browse the repository at this point in the history
  • Loading branch information
klk32003 committed Apr 25, 2018
1 parent 771c624 commit caebc0c
Showing 1 changed file with 40 additions and 20 deletions.
60 changes: 40 additions & 20 deletions java/src/jmri/jmrit/vsdecoder/SteamSound.java
Expand Up @@ -47,7 +47,7 @@ public RPMSound(SoundBite sb, int min_r, int max_r, boolean chuff) {
use_chuff = chuff;
if (use_chuff) {
sound.setLooped(false);
t = newTimer(1000, true, new ActionListener() {
t = newTimer(1, true, new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
doChuff();
Expand Down Expand Up @@ -79,12 +79,11 @@ public void stopChuff() {

// Engine Sounds
ArrayList<RPMSound> rpm_sounds;
SoundBite idle_sound;
int top_speed;
int driver_diameter;
int num_cylinders;
RPMSound current_rpm_sound;
int current_chuff_time;
float exponent;

public SteamSound(String name) {
super(name);
Expand Down Expand Up @@ -124,34 +123,44 @@ private int calcRPM(float t) {
}

private double speedCurve(float t) {
return Math.pow(t, 2.0) / 1.0;
return Math.pow(t, exponent) / 1.0;
}

private int calcChuffInterval(int rpm) {
return 1000 * num_cylinders / rpm;
return 30000 / num_cylinders / rpm;
}

@Override
public void changeThrottle(float t) {
// Don't do anything, if engine is not started or auto-start is active.
if (engine_started) {
RPMSound rps;
// Yes, I'm checking to see if rps and current_rpm_sound are the *same object*
if (((rps = getRPMSound(calcRPM(t))) != null) && (rps != current_rpm_sound)) {
// Stop the current sound
if ((current_rpm_sound != null) && (current_rpm_sound.sound != null)) {
current_rpm_sound.sound.fadeOut();
if (current_rpm_sound.use_chuff) {
current_rpm_sound.stopChuff();
rps = getRPMSound(calcRPM(t)); // Get the rpm sound.
if (rps != null) {
// Yes, I'm checking to see if rps and current_rpm_sound are the *same object*
if (rps != current_rpm_sound) {
// Stop the current sound
if ((current_rpm_sound != null) && (current_rpm_sound.sound != null)) {
current_rpm_sound.sound.fadeOut();
if (current_rpm_sound.use_chuff) {
current_rpm_sound.stopChuff();
}
}
// Start the new sound.
current_rpm_sound = rps;
if (rps.use_chuff) {
rps.setRPM(calcRPM(t));
rps.startChuff();
}
rps.sound.fadeIn();
} else {
// *same object* - but possibly different rpm (speed) which affects the chuff interval
if (rps.use_chuff) {
rps.setRPM(calcRPM(t)); // Chuff interval need to be recalculated
}
}
// Start the new sound.
current_rpm_sound = rps;
if (rps.use_chuff) {
rps.setRPM(calcRPM(t));
rps.startChuff();
}
rps.sound.fadeIn();
} else {
log.warn("No adequate sound file found for RPM = {}", calcRPM(t));
}
log.debug("RPS: {}, RPM: {}, current_RPM: {}", rps, calcRPM(t), current_rpm_sound);
}
Expand Down Expand Up @@ -216,6 +225,17 @@ public void setXml(Element e, VSDFile vf) {
num_cylinders = Integer.parseInt(n);
log.debug("Num Cylinders: {}", num_cylinders);
}

// Optional value
// Allows to adjust speed via speedCurve(T).
n = e.getChildText("exponent");
if (n != null) {
exponent = Float.parseFloat(n);
} else {
exponent = 2.0f; // default
}
log.debug("exponent: {}", exponent);

// For now, num_rpms is not used.
/*
n = e.getChild("rpm-steps").getValue();
Expand All @@ -228,7 +248,7 @@ public void setXml(Element e, VSDFile vf) {
is_auto_start = setXMLAutoStart(e);
log.debug("config auto-start: {}", is_auto_start);

rpm_sounds = new ArrayList<RPMSound>();
rpm_sounds = new ArrayList<>();

// Get the RPM steps
Iterator<Element> itr = (e.getChildren("rpm-step")).iterator();
Expand Down

0 comments on commit caebc0c

Please sign in to comment.