@@ -32,39 +32,67 @@ void GOSoundFader::Process(
32
32
unsigned nFrames, float *buffer, float externalVolume) {
33
33
// setup process
34
34
35
- // Consider the velocity volume as part of the external volume
36
- externalVolume *= m_VelocityVolume;
37
-
38
35
float startTargetVolumePoint = m_LastTargetVolumePoint;
36
+
39
37
// Calculate new m_LastTargetVolumePoint
40
38
// the target volume will be changed from startTargetVolumePoint to
41
39
// m_LastTargetVolumePoint during the nFrames
42
- float targetVolumeDeltaPerFrame
43
- = m_IncreasingDeltaPerFrame + m_DecreasingDeltaPerFrame;
44
40
45
- if (targetVolumeDeltaPerFrame != 0 .0f ) {
46
- m_LastTargetVolumePoint = std::clamp (
47
- startTargetVolumePoint + targetVolumeDeltaPerFrame * nFrames,
48
- 0 .0f ,
49
- m_TargetVolume);
41
+ unsigned framesLeftAfterIncreasing = nFrames;
42
+
43
+ // increasing section
44
+ if (m_IncreasingDeltaPerFrame > 0 .0f ) {
45
+ // we are increasing now
46
+ float newTargetVolumePoint
47
+ = m_LastTargetVolumePoint + m_IncreasingDeltaPerFrame * nFrames;
48
+
49
+ if (newTargetVolumePoint < m_TargetVolume) {
50
+ framesLeftAfterIncreasing = 0 ; // we are increase during the whole period
51
+ m_LastTargetVolumePoint = newTargetVolumePoint;
52
+ } else {
53
+ // we reach m_TargetVolume inside this nFrames period
50
54
51
- if (m_LastTargetVolumePoint >= m_TargetVolume)
52
- // target volume is reached. Stop increasing
55
+ // stop increasing
53
56
m_IncreasingDeltaPerFrame = 0 .0f ;
54
- else if (m_LastTargetVolumePoint <= 0 .0f )
55
- // Decreasing is finished. Stop it.
57
+
58
+ // calculate how many frames left after increasing
59
+ framesLeftAfterIncreasing = nFrames
60
+ - unsigned ((m_TargetVolume - m_LastTargetVolumePoint)
61
+ / m_IncreasingDeltaPerFrame);
62
+
63
+ m_LastTargetVolumePoint = m_TargetVolume;
64
+ }
65
+ }
66
+
67
+ // decreasing section
68
+ if (framesLeftAfterIncreasing && m_DecreasingDeltaPerFrame > 0 .0f ) {
69
+ // decrease the volume
70
+ float newTargetVolumePoint = m_LastTargetVolumePoint
71
+ - m_DecreasingDeltaPerFrame * framesLeftAfterIncreasing;
72
+
73
+ if (newTargetVolumePoint > 0 .0f ) {
74
+ m_LastTargetVolumePoint = newTargetVolumePoint;
75
+ } else {
76
+ // stop decreasing
56
77
m_DecreasingDeltaPerFrame = 0 .0f ;
78
+ m_LastTargetVolumePoint = 0 .0f ;
79
+ }
57
80
}
58
81
59
- if (m_LastExternalVolumePoint < 0 .0f ) // The first Process() call
60
- m_LastExternalVolumePoint = externalVolume;
82
+ // Calculate the external volume
83
+ float targetExternalVolume = m_VelocityVolume * externalVolume;
84
+
85
+ if (m_LastExternalVolumePoint < 0 .0f )
86
+ m_LastExternalVolumePoint = targetExternalVolume;
61
87
62
88
float startExternalVolumePoint = m_LastExternalVolumePoint;
89
+
63
90
// Calculate new m_LastExternalVolumePoint
64
91
// the target volume will be changed from startExternalVolumePoint to
65
92
// m_LastExternalVolumePoint during the nFrames period
66
- if (externalVolume != startExternalVolumePoint)
67
- m_LastExternalVolumePoint += (externalVolume - startExternalVolumePoint)
93
+ if (targetExternalVolume != startExternalVolumePoint)
94
+ m_LastExternalVolumePoint
95
+ += (targetExternalVolume - startExternalVolumePoint)
68
96
// Assume that external volume is to be reached in MAX_FRAME_SIZE frames
69
97
* std::max (nFrames, EXTERNAL_VOLUME_CHANGE_FRAMES)
70
98
/ EXTERNAL_VOLUME_CHANGE_FRAMES;
0 commit comments