arduino-audio-tools
Loading...
Searching...
No Matches
VolumeStream.h
1
2#pragma once
3#include "AudioTools/CoreAudio/AudioStreams.h"
4#include "AudioTools/CoreAudio/AudioOutput.h"
5#include "AudioTools/CoreAudio/VolumeControl.h"
6#include "AudioTools/CoreAudio/AudioTypes.h"
7
8namespace audio_tools {
9
17 bits_per_sample = 16;
18 channels = 2;
19 }
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
85 VolumeStreamConfig defaultConfig() {
87 return c;
88 }
89
90 bool begin() override {
91 return begin(info);
92 }
93
94 bool begin(AudioInfo cfg) {
95 VolumeStreamConfig cfg1 = setupAudioInfo(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){
105 setVolumeControl(linear_vc);
106 } else {
107 setVolumeControl(pot_vc);
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
123 cached_volume.setVolumeControl(vc);
124 }
125
128 cached_volume.setVolumeControl(pot_vc);
129 }
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
168 notifyAudioChange(cfg);
169
170 if (is_started){
173 // trigger resize of vectors
174 setVolume(info.volume);
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();
199 float volume_value = volumeValue(vol);
200 if (volume_values[channel] != volume_value){
201 LOGI("setVolume: %f at %d", volume_value, channel);
202 float factor = volumeControl().getVolumeFactor(volume_value);
203 volume_values[channel]=volume_value;
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));
209 factor_for_channel[channel] = factorF2P6;
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;
243 LinearVolumeControl linear_vc{true};
244 SimulatedAudioPot pot_vc;
245 CachedVolumeControl cached_volume{pot_vc};
246 Vector<float> volume_values;
247 #if PREFER_FIXEDPOINT
248 Vector<uint8_t> factor_for_channel; //Fixed point 2.6
249 #else
250 Vector<float> factor_for_channel;
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
256 bool isVolumeUpdate(){
257 if (!is_started) return false;
258 if (isAllChannelsFullVolume()) return false;
259 return true;
260 }
261
262 bool isAllChannelsFullVolume(){
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
272 factor_for_channel.resize(info.channels);
273 volume_values.resize(info.channels);
274 }
275
279 cfg1.channels = cfg.channels;
280 cfg1.sample_rate = cfg.sample_rate;
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
290 info = cfg;
292 }
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
304 VolumeControl &volumeControl(){
305 return cached_volume;
306 }
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}
virtual void addNotifyAudioChange(AudioInfoSupport &bi)
Adds target to be notified about audio changes.
Definition AudioTypes.h:151
Abstract Audio Ouptut class.
Definition AudioOutput.h:22
Base class for all Audio Streams. It support the boolean operator to test if the object is ready with...
Definition BaseStream.h:122
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:299
Definition NoArduino.h:62
Definition NoArduino.h:142
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
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
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
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 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 setupVectors()
Resizes the vectors.
Definition VolumeStream.h:271
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 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
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
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:189
Generic Implementation of sound input and output for desktop environments using portaudio.
Definition AudioCodecsBase.h:10
Basic Audio information which drives e.g. I2S.
Definition AudioTypes.h:53
sample_rate_t sample_rate
Sample Rate: e.g 44100.
Definition AudioTypes.h:55
uint16_t channels
Number of channels: 2=stereo, 1=mono.
Definition AudioTypes.h:57
uint8_t bits_per_sample
Number of bits per sample (int16_t = 16 bits)
Definition AudioTypes.h:59
Config for VolumeStream.
Definition VolumeStream.h:15