arduino-audio-tools
Loading...
Searching...
No Matches
VolumeStream.h
Go to the documentation of this file.
1
2#pragma once
7
8namespace audio_tools {
9
20 bool allow_boost = false;
21 float volume=1.0; // start_volume
22};
23
24
35 public:
37 VolumeStream() = default;
38
41 setOutput(out);
42 }
43
46 setStream(in);
47 }
48
51 Print *p_print = &out;
52 setOutput(*p_print);
54 }
55
58 Stream *p_stream = &io;
59 setStream(*p_stream);
61 }
62
64 void setStream(Stream &in) override {
65 p_in = ∈
66 p_out = p_in;
67 }
68
70 void setOutput(Print &out) override {
71 p_out = &out;
72 }
73
75 void setOutput(Stream &in){
76 p_in = ∈
77 p_out = p_in;
78 }
79
81 void setStream(Print &out){
82 p_out = &out;
83 }
84
89
90 bool begin() override {
91 return begin(info);
92 }
93
94 bool begin(AudioInfo cfg) {
96 return begin(cfg1);
97 }
98
101 TRACED();
103 // usually we use a exponential volume control - except if we allow values > 1.0
104 if (cfg.allow_boost){
106 } else {
108 }
109
110 // set start volume
111 setVolume(cfg.volume);
112 is_started = true;
113 return true;
114 }
115
116 void end() override {
117 is_started = false;
118 }
119
120
125
130
132 virtual size_t readBytes(uint8_t *data, size_t len) override {
133 TRACED();
134 if (data==nullptr || p_in==nullptr){
135 LOGE("NPE");
136 return 0;
137 }
138 size_t result = p_in->readBytes(data, len);
139 if (isVolumeUpdate()) applyVolume(data, result);
140 return result;
141 }
142
144 virtual size_t write(const uint8_t *data, size_t len) override {
145 LOGD("VolumeStream::write: %zu", len);
146 if (data==nullptr || p_out==nullptr){
147 LOGE("NPE");
148 return 0;
149 }
150 if (isVolumeUpdate()) applyVolume(data,len);
151 return p_out->write(data, len);
152 }
153
155 virtual int availableForWrite() override {
156 return p_out==nullptr? 0 : p_out->availableForWrite();
157 }
158
160 virtual int available() override {
161 return p_in==nullptr? 0 : p_in->available();
162 }
163
165 void setAudioInfo(AudioInfo cfg) override {
166 TRACED();
167 // pass on notification
169
170 if (is_started){
173 // trigger resize of vectors
175 } else {
176 begin(cfg);
177 }
178 }
179
181 bool setVolume(float vol) override {
182 bool result = true;
183 // just to make sure that we have a valid start volume before begin
184 info.volume = vol;
185 for (int j=0;j<info.channels;j++){
186 result = setVolume(vol, j);
187 }
188 return result;
189 }
190
192 bool setVolume(float vol, int channel){
193 if ((vol > 1.0f && !info.allow_boost) || vol < 0.0f) {
194 LOGE("Invalid volume: %f", vol);
195 return false;
196 }
197 if (channel < info.channels){
198 setupVectors();
200 if (volume_values[channel] != volume_value){
201 LOGI("setVolume: %f at %d", volume_value, channel);
204 #if PREFER_FIXEDPOINT
205 //convert float to fixed point 2.6
206 //Fixedpoint-Math from https://github.com/earlephilhower/ESP8266Audio/blob/0abcf71012f6128d52a6bcd155ed1404d6cc6dcd/src/AudioOutput.h#L67
207 if(factor > 4.0) factor = 4.0;//factor can only be >1 if allow_boost == true TODO: should we update volume_values[channel] if factor got clipped to 4.0?
208 uint8_t factorF2P6 = (uint8_t) (factor*(1<<6));
210 #else
211 factor_for_channel[channel]=factor;
212 #endif
213 }
214 return true;
215 } else {
216 LOGE("Invalid channel %d - max: %d", channel, info.channels-1);
217 return false;
218 }
219 }
220
222 float volume() override {
223 // prevent npe
224 if (volume_values.size()==0) return 0;
225 // calculate avg
226 float total = 0;
227 int cnt = volume_values.size();
228 for (int j=0; j<cnt; j++){
229 total += volume_values[j];
230 }
231 return total / static_cast<float>(cnt);
232 }
233
235 float volume(int channel) {
236 return channel>=info.channels? 0 : volume_values[channel];
237 }
238
239 protected:
240 Print *p_out=nullptr;
241 Stream *p_in=nullptr;
247 #if PREFER_FIXEDPOINT
248 Vector<uint8_t> factor_for_channel; //Fixed point 2.6
249 #else
251 #endif
252 bool is_started = false;
253 float max_value = 32767; // max value for clipping
254
255 // checks if volume needs to be updated
257 if (!is_started) return false;
258 if (isAllChannelsFullVolume()) return false;
259 return true;
260 }
261
263 int channels = MIN(info.channels, volume_values.size());
264 for (int ch=0;ch < channels;ch++){
265 if (volume_values[ch]!=1.0) return false;
266 }
267 return true;
268 }
269
275
279 cfg1.channels = cfg.channels;
280 cfg1.sample_rate = cfg.sample_rate;
281 cfg1.bits_per_sample = cfg.bits_per_sample;
282 // keep volume which might habe been defined befor calling begin
283 cfg1.volume = info.volume;
284 cfg1.allow_boost = info.allow_boost;
285 return cfg1;
286 }
287
293
294 float volumeValue(float vol){
295 if (!info.allow_boost && vol>1.0f) vol = 1.0;
296 if (vol<0.0f) vol = 0.0;
297
298 // round to 2 digits
299 float value = (int)(vol * 100 + .5f);
300 float volume_value = (float)value / 100;
301 return volume_value;
302 }
303
307
308 #if PREFER_FIXEDPOINT
309 uint8_t factorForChannel(int channel){
310 #else
311 float factorForChannel(int channel){
312 #endif
313 if (factor_for_channel.size()==0) return 1.0;
314 if (channel >= factor_for_channel.size()) return 1.0;
315 return factor_for_channel[channel];
316 }
317
318 void applyVolume(const uint8_t *buffer, size_t size){
319 switch(info.bits_per_sample){
320 case 16:
321 applyVolume16((int16_t*)buffer, size/2);
322 break;
323 case 24:
324 applyVolume24((int24_t*)buffer, size/sizeof(int24_t));
325 break;
326 case 32:
327 applyVolume32((int32_t*)buffer, size/4);
328 break;
329 default:
330 LOGE("Unsupported bits_per_sample: %d", info.bits_per_sample);
331 }
332 }
333
334 void applyVolume16(int16_t* data, size_t size){
335 for (size_t j=0;j<size;j++){
336 #if PREFER_FIXEDPOINT
337 int32_t result = (data[j] * factorForChannel(j%info.channels)) >> 6; //Fixedpoint-Math from https://github.com/earlephilhower/ESP8266Audio/blob/0abcf71012f6128d52a6bcd155ed1404d6cc6dcd/src/AudioOutput.h#L67
338 #else
339 float result = factorForChannel(j%info.channels) * data[j];
340 #endif
341 if (!info.allow_boost){
342 if (result>max_value) result = max_value;
343 if (result<-max_value) result = -max_value;
344 }
345 data[j]= static_cast<int16_t>(result);
346 }
347 }
348
349 void applyVolume24(int24_t* data, size_t size) {
350 for (size_t j=0;j<size;j++){
351 #if PREFER_FIXEDPOINT
352 int32_t result = (data[j] * factorForChannel(j%info.channels)) >> 6; //8bits * 24bits = fits into 32
353 #else
354 float result = factorForChannel(j%info.channels) * data[j];
355 #endif
356 if (!info.allow_boost){
357 if (result>max_value) result = max_value;
358 if (result<-max_value) result = -max_value;
359 }
360 int32_t result1 = result;
361 data[j]= static_cast<int24_t>(result1);
362 }
363 }
364
365 void applyVolume32(int32_t* data, size_t size) {
366 for (size_t j=0;j<size;j++){
367 #if PREFER_FIXEDPOINT
368 int64_t result = (static_cast<int64_t>(data[j]) * static_cast<int64_t>(factorForChannel(j%info.channels))) >> 6;
369 #else
370 float result = factorForChannel(j%info.channels) * data[j];
371 #endif
372 if (!info.allow_boost){
373 if (result>max_value) result = max_value;
374 if (result<-max_value) result = -max_value;
375 }
376 data[j]= static_cast<int32_t>(result);
377 }
378 }
379};
380
381}
#define TRACED()
Definition AudioLoggerIDF.h:31
#define LOGI(...)
Definition AudioLoggerIDF.h:28
#define LOGD(...)
Definition AudioLoggerIDF.h:27
#define LOGE(...)
Definition AudioLoggerIDF.h:30
#define MIN(A, B)
Definition AudioTypes.h:17
void notifyAudioChange(AudioInfo info)
Definition AudioTypes.h:178
virtual void addNotifyAudioChange(AudioInfoSupport &bi)
Adds target to be notified about audio changes.
Definition AudioTypes.h:153
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:122
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:301
Definition NoArduino.h:62
virtual int availableForWrite()
Definition NoArduino.h:134
virtual size_t write(const uint8_t *data, size_t len)
Definition NoArduino.h:126
Simple simulated audio pot volume control inspired by https://eepower.com/resistor-guide/resistor-typ...
Definition VolumeControl.h:124
Definition NoArduino.h:142
virtual size_t readBytes(uint8_t *data, size_t len)
Definition NoArduino.h:147
virtual int available()
Definition NoArduino.h:146
Vector implementation which provides the most important methods as defined by std::vector....
Definition Vector.h:21
bool resize(int 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:34
float factorForChannel(int channel)
Definition VolumeStream.h:311
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:144
void setOutput(Print &out) override
Defines/Changes the output target.
Definition VolumeStream.h:70
VolumeControl & volumeControl()
Definition VolumeStream.h:304
Vector< float > volume_values
Definition VolumeStream.h:246
VolumeStreamConfig setupAudioInfo(AudioInfo cfg)
Provides a VolumeStreamConfig based on a AudioInfo.
Definition VolumeStream.h:277
void setOutput(Stream &in)
same as setStream
Definition VolumeStream.h:75
VolumeStreamConfig defaultConfig()
Definition VolumeStream.h:85
Stream * p_in
Definition VolumeStream.h:241
bool isVolumeUpdate()
Definition VolumeStream.h:256
bool isAllChannelsFullVolume()
Definition VolumeStream.h:262
bool is_started
Definition VolumeStream.h:252
void applyVolume16(int16_t *data, size_t size)
Definition VolumeStream.h:334
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:132
void end() override
Definition VolumeStream.h:116
void applyVolume(const uint8_t *buffer, size_t size)
Definition VolumeStream.h:318
void setStream(Print &out)
same as set Output
Definition VolumeStream.h:81
virtual int availableForWrite() override
Provides the nubmer of bytes we can write.
Definition VolumeStream.h:155
float volume() override
Provides the current (avg) volume accross all channels.
Definition VolumeStream.h:222
bool begin(VolumeStreamConfig cfg)
starts the processing
Definition VolumeStream.h:100
void setupVolumeStreamConfig(VolumeStreamConfig cfg)
Stores the local variable and calculates some max values.
Definition VolumeStream.h:289
VolumeStream()=default
Default Constructor.
VolumeStream(AudioStream &io)
Constructor which assigns Stream input or output.
Definition VolumeStream.h:57
void applyVolume24(int24_t *data, size_t size)
Definition VolumeStream.h:349
LinearVolumeControl linear_vc
Definition VolumeStream.h:243
CachedVolumeControl cached_volume
Definition VolumeStream.h:245
Print * p_out
Definition VolumeStream.h:240
void setupVectors()
Resizes the vectors.
Definition VolumeStream.h:271
float volumeValue(float vol)
Definition VolumeStream.h:294
Vector< float > factor_for_channel
Definition VolumeStream.h:250
float max_value
Definition VolumeStream.h:253
VolumeStreamConfig info
Definition VolumeStream.h:242
bool begin() override
Definition VolumeStream.h:90
VolumeStream(AudioOutput &out)
Constructor which assigns Print output.
Definition VolumeStream.h:50
void resetVolumeControl()
Resets the volume control to use the standard logic.
Definition VolumeStream.h:127
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:181
VolumeStream(Stream &in)
Constructor which assigns Stream input or output.
Definition VolumeStream.h:45
void applyVolume32(int32_t *data, size_t size)
Definition VolumeStream.h:365
void setStream(Stream &in) override
Defines/Changes the input & output.
Definition VolumeStream.h:64
void setAudioInfo(AudioInfo cfg) override
Detines the Audio info - The bits_per_sample are critical to work properly!
Definition VolumeStream.h:165
SimulatedAudioPot pot_vc
Definition VolumeStream.h:244
VolumeStream(Print &out)
Constructor which assigns Print output.
Definition VolumeStream.h:40
bool setVolume(float vol, int channel)
Sets the volume for one channel.
Definition VolumeStream.h:192
void setVolumeControl(VolumeControl &vc)
Defines the volume control logic.
Definition VolumeStream.h:122
float volume(int channel)
Provides the current volume setting for the indicated channel.
Definition VolumeStream.h:235
bool begin(AudioInfo cfg)
Definition VolumeStream.h:94
virtual int available() override
Provides the nubmer of bytes we can write.
Definition VolumeStream.h:160
Supports the setting and getting of the volume.
Definition AudioTypes.h:191
float volume_value
Definition AudioTypes.h:202
24bit integer which is used for I2S sound processing. The values are represented as int32_t,...
Definition Int24_4bytes_t.h:16
Generic Implementation of sound input and output for desktop environments using portaudio.
Definition AudioCodecsBase.h:10
size_t writeData(Print *p_out, T *data, int samples, int maxSamples=512)
Definition AudioTypes.h:512
Basic Audio information which drives e.g. I2S.
Definition AudioTypes.h:55
sample_rate_t sample_rate
Sample Rate: e.g 44100.
Definition AudioTypes.h:57
uint16_t channels
Number of channels: 2=stereo, 1=mono.
Definition AudioTypes.h:59
uint8_t bits_per_sample
Number of bits per sample (int16_t = 16 bits)
Definition AudioTypes.h:61
Config for VolumeStream.
Definition VolumeStream.h:15
float volume
Definition VolumeStream.h:21
bool allow_boost
Definition VolumeStream.h:20
VolumeStreamConfig()
Definition VolumeStream.h:16