Skip to content

Commit 8db3df1

Browse files
committed
Fixed not playing releases
1 parent d26bc1d commit 8db3df1

File tree

2 files changed

+53
-17
lines changed

2 files changed

+53
-17
lines changed

src/grandorgue/sound/GOSoundFader.cpp

+37-14
Original file line numberDiff line numberDiff line change
@@ -36,28 +36,51 @@ void GOSoundFader::Process(
3636
externalVolume *= m_VelocityVolume;
3737

3838
float startTargetVolumePoint = m_LastTargetVolumePoint;
39+
3940
// Calculate new m_LastTargetVolumePoint
4041
// the target volume will be changed from startTargetVolumePoint to
4142
// m_LastTargetVolumePoint during the nFrames
42-
float targetVolumeDeltaPerFrame
43-
= m_IncreasingDeltaPerFrame + m_DecreasingDeltaPerFrame;
4443

45-
if (targetVolumeDeltaPerFrame != 0.0f) {
46-
m_LastTargetVolumePoint = std::clamp(
47-
startTargetVolumePoint + targetVolumeDeltaPerFrame * nFrames,
48-
0.0f,
49-
m_TargetVolume);
44+
unsigned framesLeftAfterIncreasing = nFrames;
45+
46+
// increasing section
47+
if (m_IncreasingDeltaPerFrame > 0.0f) {
48+
// we are increasing now
49+
float newTargetVolumePoint
50+
= m_LastTargetVolumePoint + m_IncreasingDeltaPerFrame * nFrames;
51+
52+
if (newTargetVolumePoint < m_TargetVolume) {
53+
framesLeftAfterIncreasing = 0; // we are increase during the whole period
54+
m_LastTargetVolumePoint = newTargetVolumePoint;
55+
} else {
56+
// we reach m_TargetVolume inside this nFrames period
5057

51-
if (m_LastTargetVolumePoint >= m_TargetVolume)
52-
// target volume is reached. Stop increasing
58+
// stop increasing
5359
m_IncreasingDeltaPerFrame = 0.0f;
54-
else if (m_LastTargetVolumePoint <= 0.0f)
55-
// Decreasing is finished. Stop it.
56-
m_DecreasingDeltaPerFrame = 0.0f;
60+
61+
// calculate how many frames left after increasing
62+
framesLeftAfterIncreasing = nFrames
63+
- unsigned((m_TargetVolume - m_LastTargetVolumePoint)
64+
/ m_IncreasingDeltaPerFrame);
65+
66+
m_LastTargetVolumePoint = m_TargetVolume;
67+
}
5768
}
5869

59-
if (m_LastExternalVolumePoint < 0.0f) // The first Process() call
60-
m_LastExternalVolumePoint = externalVolume;
70+
// decreasing section
71+
if (framesLeftAfterIncreasing && m_DecreasingDeltaPerFrame > 0.0f) {
72+
// decrease the volume
73+
float newTargetVolumePoint = m_LastTargetVolumePoint
74+
- m_DecreasingDeltaPerFrame * framesLeftAfterIncreasing;
75+
76+
if (newTargetVolumePoint > 0.0f) {
77+
m_LastTargetVolumePoint = newTargetVolumePoint;
78+
} else {
79+
// stop decreasing
80+
m_DecreasingDeltaPerFrame = 0.0f;
81+
m_LastTargetVolumePoint = 0.0f;
82+
}
83+
}
6184

6285
float startExternalVolumePoint = m_LastExternalVolumePoint;
6386
// Calculate new m_LastExternalVolumePoint

src/grandorgue/sound/GOSoundFader.h

+16-3
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@
88
#ifndef GOSOUNDFADER_H_
99
#define GOSOUNDFADER_H_
1010

11+
#include <assert.h>
12+
1113
/**
1214
* This class is responsible for smoothly changing a volume of samples.
1315
*
@@ -69,11 +71,22 @@ class GOSoundFader {
6971
* @param nFrames number of frames for full decay
7072
*/
7173
inline void StartDecreasingVolume(unsigned nFrames) {
72-
// stop increasing the volume if it has not yet finished
73-
m_IncreasingDeltaPerFrame = 0.0f;
7474
// maybe m_TargetVolume has not yet been reached, but the velocity of
7575
// decreasing should be the same as it has reached
76-
m_DecreasingDeltaPerFrame = -(m_TargetVolume / nFrames);
76+
m_DecreasingDeltaPerFrame = m_TargetVolume / nFrames;
77+
if (m_IncreasingDeltaPerFrame > 0.0f) {
78+
/*
79+
The increasing has not yet finished. We are starting a "virtual"
80+
decreasing from m_TargetVolume to 0.
81+
The increasing processus will continue until it meet the "virtual"
82+
decreasing one at the new m_TargetVolume. Let's calculate it
83+
*/
84+
assert(m_LastTargetVolumePoint < m_TargetVolume);
85+
m_TargetVolume = (m_TargetVolume - m_LastTargetVolumePoint)
86+
* m_IncreasingDeltaPerFrame
87+
/ (m_IncreasingDeltaPerFrame + m_DecreasingDeltaPerFrame)
88+
+ m_LastTargetVolumePoint;
89+
}
7790
}
7891

7992
inline float GetVelocityVolume() const { return m_VelocityVolume; }

0 commit comments

Comments
 (0)