|
| 1 | +// Copyright (c) 2026 Jakub Filipowicz <jakubf@gmail.com> |
| 2 | +// |
| 3 | +// This program is free software; you can redistribute it and/or modify |
| 4 | +// it under the terms of the GNU General Public License as published by |
| 5 | +// the Free Software Foundation; either version 2 of the License, or |
| 6 | +// (at your option) any later version. |
| 7 | +// |
| 8 | +// This program is distributed in the hope that it will be useful, |
| 9 | +// but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 10 | +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 11 | +// GNU General Public License for more details. |
| 12 | +// |
| 13 | +// You should have received a copy of the GNU General Public License |
| 14 | +// along with this program; if not, write to the Free Software |
| 15 | +// Foundation, Inc., |
| 16 | +// 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA |
| 17 | + |
| 18 | +#include "psu.h" |
| 19 | + |
| 20 | +#define FADE_MS 1000 |
| 21 | +#define FADE_TICK_MS 16 |
| 22 | + |
| 23 | +// ----------------------------------------------------------------------- |
| 24 | +Psu::Psu(const QUrl &snd_start_rs, const QUrl &snd_stop_rs, const QUrl &snd_loop_rs, QObject *parent) |
| 25 | + : QObject{parent} |
| 26 | +{ |
| 27 | + snd_start.setSource(snd_start_rs); |
| 28 | + snd_stop.setSource(snd_stop_rs); |
| 29 | + snd_loop.setSource(snd_loop_rs); |
| 30 | + snd_loop.setLoopCount(QSoundEffect::Infinite); |
| 31 | + |
| 32 | + fade_timer.setInterval(FADE_TICK_MS); |
| 33 | + connect(&fade_timer, &QTimer::timeout, this, &Psu::fade_step); |
| 34 | + connect(&snd_start, &QSoundEffect::playingChanged, this, &Psu::spin_up_finished); |
| 35 | +} |
| 36 | + |
| 37 | +// ----------------------------------------------------------------------- |
| 38 | +void Psu::set_volume(qreal linear_volume) |
| 39 | +{ |
| 40 | + volume = linear_volume; |
| 41 | + snd_loop.setVolume(volume); |
| 42 | + if (fade_timer.isActive()) return; |
| 43 | + snd_start.setVolume(volume); |
| 44 | + snd_stop.setVolume(volume); |
| 45 | +} |
| 46 | + |
| 47 | +// ----------------------------------------------------------------------- |
| 48 | +void Psu::set_enabled(bool on) |
| 49 | +{ |
| 50 | + pending_enabled = on; |
| 51 | +} |
| 52 | + |
| 53 | +// ----------------------------------------------------------------------- |
| 54 | +void Psu::crossfade(QSoundEffect *in, QSoundEffect *out) |
| 55 | +{ |
| 56 | + fade_in = in; |
| 57 | + fade_out = out; |
| 58 | + chaining = false; |
| 59 | + fade_in->stop(); |
| 60 | + fade_in->setVolume(0.0); |
| 61 | + fade_in->play(); |
| 62 | + fade_timer.start(); |
| 63 | +} |
| 64 | + |
| 65 | +// ----------------------------------------------------------------------- |
| 66 | +// The spin-up sample ran to its end; hand off to the sustain loop. Guarded so |
| 67 | +// our own stop()/restart calls (which also flip isPlaying) never trigger it. |
| 68 | +void Psu::spin_up_finished() |
| 69 | +{ |
| 70 | + if (!chaining || snd_start.isPlaying()) return; |
| 71 | + chaining = false; |
| 72 | + snd_loop.setVolume(volume); |
| 73 | + snd_loop.play(); |
| 74 | +} |
| 75 | + |
| 76 | +// ----------------------------------------------------------------------- |
| 77 | +void Psu::fade_step() |
| 78 | +{ |
| 79 | + qreal step = volume * ((qreal) FADE_TICK_MS / FADE_MS); |
| 80 | + qreal vin = qMin(volume, fade_in->volume() + step); |
| 81 | + qreal vout = qMax((qreal) 0.0, fade_out->volume() - step); |
| 82 | + fade_in->setVolume(vin); |
| 83 | + fade_out->setVolume(vout); |
| 84 | + if ((vin >= volume) && (vout <= 0.0)) { |
| 85 | + fade_out->stop(); |
| 86 | + fade_timer.stop(); |
| 87 | + } |
| 88 | +} |
| 89 | + |
| 90 | +// ----------------------------------------------------------------------- |
| 91 | +void Psu::slot_set_power(bool on, bool audible) |
| 92 | +{ |
| 93 | + if (on == running) return; |
| 94 | + running = on; |
| 95 | + if (on) enabled = pending_enabled; |
| 96 | + if (!audible || !enabled) return; |
| 97 | + if (on && snd_stop.isPlaying()) { |
| 98 | + // blend the spin-up in over a still-ringing spin-down; every other |
| 99 | + // transition is a hard cut |
| 100 | + crossfade(&snd_start, &snd_stop); |
| 101 | + chaining = true; |
| 102 | + } else if (on) { |
| 103 | + chaining = false; |
| 104 | + snd_stop.stop(); |
| 105 | + snd_start.stop(); |
| 106 | + snd_start.setVolume(volume); |
| 107 | + snd_start.play(); |
| 108 | + chaining = true; |
| 109 | + } else { |
| 110 | + chaining = false; |
| 111 | + fade_timer.stop(); |
| 112 | + snd_start.stop(); |
| 113 | + snd_loop.stop(); |
| 114 | + snd_stop.stop(); |
| 115 | + snd_stop.setVolume(volume); |
| 116 | + snd_stop.play(); |
| 117 | + } |
| 118 | +} |
| 119 | + |
| 120 | +// vim: tabstop=4 shiftwidth=4 autoindent |
0 commit comments