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
|
/*
Copyright (C) 2000 Stefan Westerfeld
stefan@space.twc.de
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Library General Public
License as published by the Free Software Foundation; either
version 2 of the License, or (at your option) any later version.
This library 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
Library General Public License for more details.
You should have received a copy of the GNU Library General Public License
along with this library; see the file COPYING.LIB. If not, write to
the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
Boston, MA 02110-1301, USA.
*/
#include <math.h>
#include "artsflow.h"
#include "stdsynthmodule.h"
#include "flowsystem.h"
#include <debug.h>
#include <cstring>
using namespace Arts;
namespace Arts {
class StereoVolumeControl_impl : virtual public StereoVolumeControl_skel,
virtual public StdSynthModule
{
float _scaleFactor;
float _currentVolumeLeft;
float _currentVolumeRight;
bool _virtualized;
bool _calcVolume;
public:
StereoVolumeControl_impl() :_scaleFactor(1.0), _currentVolumeLeft(0.0),
_currentVolumeRight(0.0), _virtualized( false ),
_calcVolume( false )
{
virtualize();
}
/*attribute float scaleFactor;*/
void scaleFactor(float newFactor) {
if(_scaleFactor != newFactor)
{
_scaleFactor = newFactor;
scaleFactor_changed(newFactor);
if( _scaleFactor == 1.0 )
{
if( !_calcVolume && !_virtualized )
virtualize();
}
else if( _virtualized )
devirtualize();
}
}
float scaleFactor() { return _scaleFactor; }
/*
readonly attribute float currentVolumeLeft;
readonly attribute float currentVolumeRight;
*/
float currentVolumeLeft()
{
if( _virtualized && !_calcVolume )
needVolumeCalculation();
return _currentVolumeLeft;
}
float currentVolumeRight()
{
if( _virtualized && !_calcVolume )
needVolumeCalculation();
return _currentVolumeRight;
}
void needVolumeCalculation()
{
arts_debug( "calculate Volume in StereoVolumeControl" );
_calcVolume = true;
devirtualize();
}
void virtualize()
{
arts_debug( "virtualize StereoVolumeControl" );
_virtualized = true;
_node()->virtualize( "inleft", _node(), "outleft" );
_node()->virtualize( "inright", _node(), "outright" );
_currentVolumeRight = _currentVolumeLeft = 0.0;
}
void devirtualize()
{
arts_debug( "devirtualize StereoVolumeControl" );
_virtualized = false;
_node()->devirtualize( "inleft", _node(), "outleft" );
_node()->devirtualize( "inright", _node(), "outright" );
}
/*
in audio stream inleft, inright;
out audio stream outleft, outright;
*/
void calculateBlock(unsigned long samples)
{
if( _scaleFactor == 1.0 )
{
if( _calcVolume )
{
/*
* make sure that we reach zero sometimes - otherwise there are
* performance issues with calculations close to zero
*/
if(fabs(_currentVolumeLeft) < 0.001) _currentVolumeLeft = 0.0;
if(fabs(_currentVolumeRight) < 0.001) _currentVolumeRight = 0.0;
for( unsigned long i = 0; i < samples; i += 10 )
{
/* measure volume */
float delta;
// left
delta = fabs( outleft[ i ] ) - _currentVolumeLeft;
if( delta > 0.0 )
_currentVolumeLeft += 0.1 * delta;
else
_currentVolumeLeft += 0.003 * delta;
// right
delta = fabs( outright[ i ] ) - _currentVolumeRight;
if( delta > 0.0 )
_currentVolumeRight += 0.1 * delta;
else
_currentVolumeRight += 0.003 * delta;
}
memcpy( outleft, inleft, samples * sizeof( float ) );
memcpy( outright, inright, samples * sizeof( float ) );
}
return;
}
unsigned long i;
/*
* make sure that we reach zero sometimes - otherwise there are
* performance issues with calculations close to zero
*/
if(fabs(_currentVolumeLeft) < 0.001) _currentVolumeLeft = 0.0;
if(fabs(_currentVolumeRight) < 0.001) _currentVolumeRight = 0.0;
for(i=0;i<samples;i++)
{
/* scale */
outleft[i] = inleft[i] * _scaleFactor;
outright[i] = inright[i] * _scaleFactor;
/* measure volume */
float delta;
// left
delta = fabs(outleft[i])-_currentVolumeLeft;
if(delta > 0.0)
_currentVolumeLeft += 0.01 * delta;
else
_currentVolumeLeft += 0.0003 * delta;
// right
delta = fabs(outright[i])-_currentVolumeRight;
if(delta > 0.0)
_currentVolumeRight += 0.01 * delta;
else
_currentVolumeRight += 0.0003 * delta;
}
}
AutoSuspendState autoSuspend() {
return (_currentVolumeLeft+_currentVolumeRight<0.001)?
asSuspend : asNoSuspend;
}
};
REGISTER_IMPLEMENTATION(StereoVolumeControl_impl);
}
|