arduino-audio-tools
Loading...
Searching...
No Matches
VolumeStream.h
Go to the documentation of this file.
1
2#pragma once
8
9namespace audio_tools {
10
21 bool allow_boost = false;
22 float volume=1.0; // start_volume
23};
24
25
36 public:
38 VolumeStream() = default;
39
42 setOutput(out);
43 }
44
47 setStream(in);
48 }
49
52 Print *p_print = &out;
53 setOutput(*p_print);
55 }
56
59 Stream *p_stream = &io;
60 setStream(*p_stream);
62 }
63
65 void setStream(Stream &in) override {
66 p_in = ∈
67 p_out = p_in;
68 }
69
71 void setOutput(Print &out) override {
72 p_out = &out;
73 }
74
76 void setOutput(Stream &in){
77 p_in = ∈
78 p_out = p_in;
79 }
80
82 void setStream(Print &out){
83 p_out = &out;
84 }
85
90
91 bool begin() override {
92 return begin(info);
93 }
94
95 bool begin(AudioInfo cfg) {
97 return begin(cfg1);
98 }
99
102 TRACED();
104 // usually we use a exponential volume control - except if we allow values > 1.0
105 if (cfg.allow_boost){
107 } else {
109 }
110
111 // set start volume
112 setVolume(cfg.volume);
113 is_started = true;
114 return true;
115 }
116
117 void end() override {
118 is_started = false;
119 }
120
121
126
131
133 virtual size_t readBytes(uint8_t *data, size_t len) override {
134 TRACED();
135 if (data==nullptr || p_in==nullptr){
136 LOGE("NPE");
137 return 0;
138 }
139 size_t result = p_in->readBytes(data, len);
140 if (isVolumeUpdate()) applyVolume(data, result);
141 return result;
142 }
143
145 virtual size_t write(const uint8_t *data, size_t len) override {
146 LOGD("VolumeStream::write: %zu", len);
147 if (data==nullptr || p_out==nullptr){
148 LOGE("NPE");
149 return 0;
150 }
151 if (isVolumeUpdate()) applyVolume(data,len);
152 return p_out->write(data, len);
153 }
154
156 virtual int availableForWrite() override {
157 return p_out==nullptr? 0 : p_out->availableForWrite();
158 }
159
161 virtual int available() override {
162 return p_in==nullptr? 0 : p_in->available();
163 }
164
166 void setAudioInfo(AudioInfo cfg) override {
167 TRACED();
168 // pass on notification
170
171 if (is_started){
172 int old_n = volume_values.size();;
174 // update config and max values
177 // Only set default volume for new channels if channel count increased
178 int new_n = info.channels;
179 for (int j = old_n; j < new_n; ++j) {
181 }
182 } else {
183 begin(cfg);
184 }
185 }
186
188 bool setVolume(float vol) override {
189 LOGI("setVolume: %f", vol);
190 bool result = true;
191 // just to make sure that we have a valid start volume before begin
192 info.volume = vol;
193 for (int j=0; j<info.channels; j++){
194 result = setVolume(vol, j);
195 }
196 return result;
197 }
198
200 bool setVolume(float vol, int channel){
201 if ((vol > 1.0f && !info.allow_boost) || vol < 0.0f) {
202 LOGE("Invalid volume: %f", vol);
203 return false;
204 }
205 if (channel < info.channels){
207 float volume_value = volumeValue(vol);
208 if (volume_values[channel] != volume_value){
209 LOGI("setVolume: %f at %d", volume_value, channel);
212 #if PREFER_FIXEDPOINT
213 // q1_14_t saturates at ~1.99994 (its Q1.14 range), so boost
214 // is capped there (factor can only be >1 if allow_boost==true)
215 factor_for_channel[channel] = q1_14_t(factor);
216 #else
217 factor_for_channel[channel]=factor;
218 #endif
219 }
220 return true;
221 } else {
222 LOGE("Invalid channel %d - max: %d", channel, info.channels-1);
223 return false;
224 }
225 }
226
228 float volume() override {
229 // prevent npe
230 if (volume_values.size()==0) return info.volume;
231 // calculate avg
232 float total = 0;
233 int cnt = volume_values.size();
234 for (int j=0; j<cnt; j++){
235 total += volume_values[j];
236 }
237 return total / static_cast<float>(cnt);
238 }
239
241 float volume(int channel) {
242 return channel>=info.channels? 0 : volume_values[channel];
243 }
244
245 protected:
246 Print *p_out=nullptr;
247 Stream *p_in=nullptr;
253 #if PREFER_FIXEDPOINT
254 Vector<q1_14_t> factor_for_channel; // Q1.14 fixed point
255 #else
257 #endif
258 bool is_started = false;
259 // max value for clipping: integer on the fixed-point path, so the
260 // per-sample clip in applyVolume16/24/32 stays pure-integer instead
261 // of promoting the int32_t/int64_t result to float on every sample.
262 #if PREFER_FIXEDPOINT
263 int64_t max_value = 32767;
264 #else
265 float max_value = 32767;
266 #endif
267
268 // checks if volume needs to be updated
270 if (!is_started) return false;
271 if (isAllChannelsFullVolume()) return false;
272 return true;
273 }
274
276 int channels = min((int)info.channels, volume_values.size());
277 for (int ch=0;ch < channels;ch++){
278 if (volume_values[ch]!=1.0) return false;
279 }
280 return true;
281 }
282
284 void resizeVectors(int channels) {
285 factor_for_channel.resize(channels);
286 volume_values.resize(channels);
287 }
288
292 cfg1.channels = cfg.channels;
293 cfg1.sample_rate = cfg.sample_rate;
295 // keep volume which might habe been defined befor calling begin
296 cfg1.volume = info.volume;
298 return cfg1;
299 }
300
306
307 float volumeValue(float vol){
308 if (!info.allow_boost && vol>1.0f) vol = 1.0;
309 if (vol<0.0f) vol = 0.0;
310
311 // round to 2 digits
312 float value = (int)(vol * 100 + .5f);
313 float volume_value = (float)value / 100;
314 return volume_value;
315 }
316
320
321 #if PREFER_FIXEDPOINT
322 q1_14_t factorForChannel(int channel){
323 #else
324 float factorForChannel(int channel){
325 #endif
326 if (factor_for_channel.size()==0) return 1.0;
327 if (channel >= factor_for_channel.size()) return 1.0;
328 return factor_for_channel[channel];
329 }
330
331 void applyVolume(const uint8_t *buffer, size_t size){
332 switch(info.bits_per_sample){
333 case 16:
334 applyVolume16((int16_t*)buffer, size/2);
335 break;
336 case 24:
337 applyVolume24((int24_t*)buffer, size/sizeof(int24_t));
338 break;
339 case 32:
340 applyVolume32((int32_t*)buffer, size/4);
341 break;
342 default:
343 LOGE("Unsupported bits_per_sample: %d", info.bits_per_sample);
344 }
345 }
346
347 void applyVolume16(int16_t* data, size_t size){
348 for (size_t j=0;j<size;j++){
349 #if PREFER_FIXEDPOINT
350 int32_t result = factorForChannel(j%info.channels).scale(data[j]);
351 #else
352 float result = factorForChannel(j%info.channels) * data[j];
353 #endif
354 if (!info.allow_boost){
355 if (result>max_value) result = max_value;
356 if (result<-max_value) result = -max_value;
357 }
358 data[j]= static_cast<int16_t>(result);
359 }
360 }
361
362 void applyVolume24(int24_t* data, size_t size) {
363 for (size_t j=0;j<size;j++){
364 #if PREFER_FIXEDPOINT
365 int32_t result = factorForChannel(j%info.channels).scale(data[j]);
366 #else
367 float result = factorForChannel(j%info.channels) * data[j];
368 #endif
369 if (!info.allow_boost){
370 if (result>max_value) result = max_value;
371 if (result<-max_value) result = -max_value;
372 }
373 int32_t result1 = result;
374 data[j]= static_cast<int24_t>(result1);
375 }
376 }
377
378 void applyVolume32(int32_t* data, size_t size) {
379 for (size_t j=0;j<size;j++){
380 #if PREFER_FIXEDPOINT
381 int64_t result = factorForChannel(j%info.channels).scale(data[j]);
382 #else
383 float result = factorForChannel(j%info.channels) * data[j];
384 #endif
385 if (!info.allow_boost){
386 if (result>max_value) result = max_value;
387 if (result<-max_value) result = -max_value;
388 }
389 data[j]= static_cast<int32_t>(result);
390 }
391 }
392};
393
394}
#define TRACED()
Definition AudioLoggerIDF.h:31
#define LOGI(...)
Definition AudioLoggerIDF.h:28
#define LOGD(...)
Definition AudioLoggerIDF.h:27
#define LOGE(...)
Definition AudioLoggerIDF.h:30
Definition Arduino.h:56
virtual int availableForWrite()
Definition Arduino.h:128
virtual size_t write(const uint8_t *data, size_t len)
Definition Arduino.h:120
Definition Arduino.h:136
virtual size_t readBytes(uint8_t *data, size_t len)
Definition Arduino.h:140
virtual int available()
Definition Arduino.h:139
virtual void addNotifyAudioChange(AudioInfoSupport &bi)
Adds target to be notified about audio changes.
Definition AudioTypes.h:149
virtual void setAudioInfo(AudioInfo info)=0
Defines the input AudioInfo.
Abstract Audio Ouptut class.
Definition AudioOutput.h:25
Base class for all Audio Streams. It support the boolean operator to test if the object is ready with...
Definition BaseStream.h:120
In order to optimize the processing time we cache the last input & factor and recalculate the new fac...
Definition VolumeControl.h:42
void setVolumeControl(VolumeControl &vc)
Definition VolumeControl.h:52
The simplest possible implementation of a VolumeControl: The input = output which describes a linear ...
Definition VolumeControl.h:150
Abstract class: Objects can be put into a pipleline.
Definition AudioStreams.h:68
static int64_t maxValue(int value_bits_per_sample)
provides the biggest number for the indicated number of bits
Definition AudioTypes.h:297
Simple simulated audio pot volume control inspired by https://eepower.com/resistor-guide/resistor-typ...
Definition VolumeControl.h:124
Vector implementation which provides the most important methods as defined by std::vector....
Definition Vector.h:21
bool resize(size_t newSize, T value)
Definition Vector.h:266
int size()
Definition Vector.h:178
Abstract class for handling of the linear input volume to determine the multiplication factor which s...
Definition VolumeControl.h:19
virtual float getVolumeFactor(float volume)=0
determines a multiplication factor (0.0 to 1.0) from an input value (0.0 to 1.0).
Adjust the volume of the related input or output: To work properly the class needs to know the bits p...
Definition VolumeStream.h:35
float factorForChannel(int channel)
Definition VolumeStream.h:324
void setVolumeStreamConfig(VolumeStreamConfig cfg)
Stores the local variable and calculates some max values.
Definition VolumeStream.h:302
virtual size_t write(const uint8_t *data, size_t len) override
Writes raw PCM audio data, which will be the input for the volume control.
Definition VolumeStream.h:145
void setOutput(Print &out) override
Defines/Changes the output target.
Definition VolumeStream.h:71
VolumeControl & volumeControl()
Definition VolumeStream.h:317
Vector< float > volume_values
Definition VolumeStream.h:252
void resizeVectors(int channels)
Resizes the vectors.
Definition VolumeStream.h:284
void setOutput(Stream &in)
same as setStream
Definition VolumeStream.h:76
VolumeStreamConfig defaultConfig()
Definition VolumeStream.h:86
VolumeStreamConfig toVolumeStreamConfig(AudioInfo cfg)
Provides a VolumeStreamConfig based on a AudioInfo.
Definition VolumeStream.h:290
Stream * p_in
Definition VolumeStream.h:247
bool isVolumeUpdate()
Definition VolumeStream.h:269
bool isAllChannelsFullVolume()
Definition VolumeStream.h:275
bool is_started
Definition VolumeStream.h:258
void applyVolume16(int16_t *data, size_t size)
Definition VolumeStream.h:347
virtual size_t readBytes(uint8_t *data, size_t len) override
Read raw PCM audio data, which will be the input for the volume control.
Definition VolumeStream.h:133
void end() override
Definition VolumeStream.h:117
void applyVolume(const uint8_t *buffer, size_t size)
Definition VolumeStream.h:331
void setStream(Print &out)
same as set Output
Definition VolumeStream.h:82
virtual int availableForWrite() override
Provides the nubmer of bytes we can write.
Definition VolumeStream.h:156
float volume() override
Provides the current (avg) volume accross all channels.
Definition VolumeStream.h:228
bool begin(VolumeStreamConfig cfg)
starts the processing
Definition VolumeStream.h:101
VolumeStream()=default
Default Constructor.
VolumeStream(AudioStream &io)
Constructor which assigns Stream input or output.
Definition VolumeStream.h:58
void applyVolume24(int24_t *data, size_t size)
Definition VolumeStream.h:362
LinearVolumeControl linear_vc
Definition VolumeStream.h:249
CachedVolumeControl cached_volume
Definition VolumeStream.h:251
Print * p_out
Definition VolumeStream.h:246
float volumeValue(float vol)
Definition VolumeStream.h:307
Vector< float > factor_for_channel
Definition VolumeStream.h:256
float max_value
Definition VolumeStream.h:265
VolumeStreamConfig info
Definition VolumeStream.h:248
bool begin() override
Definition VolumeStream.h:91
VolumeStream(AudioOutput &out)
Constructor which assigns Print output.
Definition VolumeStream.h:51
void resetVolumeControl()
Resets the volume control to use the standard logic.
Definition VolumeStream.h:128
bool setVolume(float vol) override
Defines the volume for all channels: needs to be in the range of 0 to 1.0 (if allow boost has not bee...
Definition VolumeStream.h:188
VolumeStream(Stream &in)
Constructor which assigns Stream input or output.
Definition VolumeStream.h:46
void applyVolume32(int32_t *data, size_t size)
Definition VolumeStream.h:378
void setStream(Stream &in) override
Defines/Changes the input & output.
Definition VolumeStream.h:65
void setAudioInfo(AudioInfo cfg) override
Detines the Audio info - The bits_per_sample are critical to work properly!
Definition VolumeStream.h:166
SimulatedAudioPot pot_vc
Definition VolumeStream.h:250
VolumeStream(Print &out)
Constructor which assigns Print output.
Definition VolumeStream.h:41
bool setVolume(float vol, int channel)
Sets the volume for one channel.
Definition VolumeStream.h:200
void setVolumeControl(VolumeControl &vc)
Defines the volume control logic.
Definition VolumeStream.h:123
float volume(int channel)
Provides the current volume setting for the indicated channel.
Definition VolumeStream.h:241
bool begin(AudioInfo cfg)
Definition VolumeStream.h:95
virtual int available() override
Provides the nubmer of bytes we can write.
Definition VolumeStream.h:161
Supports the setting and getting of the volume.
Definition AudioTypes.h:187
float volume_value
Definition AudioTypes.h:198
24bit integer which is used for I2S sound processing. The values are represented as int32_t,...
Definition Int24_4bytes_t.h:22
Fixed-point Q1.14 number: a plain 16-bit signed integer holding 1 integer bit and 14 fractional bits ...
Definition Q1_14.h:16
Generic Implementation of sound input and output for desktop environments using portaudio.
Definition LMSEchoCancellationStream.h:6
Basic Audio information which drives e.g. I2S.
Definition AudioTypes.h:51
sample_rate_t sample_rate
Sample Rate: e.g 44100.
Definition AudioTypes.h:53
uint16_t channels
Number of channels: 2=stereo, 1=mono.
Definition AudioTypes.h:55
uint8_t bits_per_sample
Number of bits per sample (int16_t = 16 bits)
Definition AudioTypes.h:57
Config for VolumeStream.
Definition VolumeStream.h:16
float volume
Definition VolumeStream.h:22
bool allow_boost
Definition VolumeStream.h:21
VolumeStreamConfig()
Definition VolumeStream.h:17