arduino-audio-tools
Loading...
Searching...
No Matches
AudioTypes.h
Go to the documentation of this file.
1#pragma once
2
3#include "AudioToolsConfig.h"
4#ifdef USE_TYPETRAITS
5#include <limits>
6#include <type_traits>
7#endif
8
12// Some top level functions: stop(), checkMemory()
14
15namespace audio_tools {
16
20
26enum RxTxMode { UNDEFINED_MODE = 0, TX_MODE = 1, RX_MODE = 2, RXTX_MODE = 3 };
27
34
38static const char* RxTxModeNames[4] = {"UNDEFINED_MODE", "TX_MODE", "RX_MODE",
39 "RXTX_MODE"};
44// Some libc headers (e.g. sys/param.h, pulled in transitively by headers
45// included above) #define HZ - the #undef in AudioToolsConfig.h only guards
46// against pollution before that file's own includes run, not pollution
47// reintroduced by headers included since. Undef again right before use.
48#undef HZ
49enum TimeUnit { MS, US, HZ };
50static const char* TimeUnitStr[3]{"MS", "US", "HZ"};
51
56struct AudioInfo {
63
65 AudioInfo() = default;
66
68 AudioInfo(sample_rate_t sampleRate, uint16_t channelCount,
69 uint8_t bitsPerSample) {
70 sample_rate = sampleRate;
71 channels = channelCount;
72 bits_per_sample = bitsPerSample;
73 };
74
76 AudioInfo(const AudioInfo&) = default;
77
80 return sample_rate == alt.sample_rate && channels == alt.channels &&
82 }
83
85 bool operator!=(AudioInfo alt) { return !(*this == alt); }
86
88 bool equals(AudioInfo alt) { return *this == alt; }
89
92 return channels == alt.channels && bits_per_sample == alt.bits_per_sample;
93 }
94
96 void set(AudioInfo info) {
98 channels = info.channels;
100 }
101
103 void setAudioInfo(AudioInfo info) { set(info); }
104
106 void copyFrom(AudioInfo info) { setAudioInfo(info); }
107
108#ifndef SWIG
111 setAudioInfo(info);
112 return *this;
113 }
114#endif
116 operator bool() {
117 return sample_rate > 0 && sample_rate <= 192000 && channels > 0 &&
118 channels < 20 && bits_per_sample > 0 && bits_per_sample <= 64;
119 }
120
121 virtual void clear() {
122 sample_rate = 0;
123 channels = 0;
124 bits_per_sample = 0;
125 }
126
127 virtual void logInfo(const char* source = "") {
128 LOGI("%s sample_rate: %d / channels: %d / bits_per_sample: %d", source,
129 (int)sample_rate, (int)channels, (int)bits_per_sample);
130 }
131};
132
138 public:
140 virtual void setAudioInfo(AudioInfo info) = 0;
142 virtual AudioInfo audioInfo() = 0;
145 virtual AudioInfo audioInfoOut() { return audioInfo(); }
146};
147
153 public:
156 if (!notify_vector.contains(&bi)) notify_vector.push_back(&bi);
157 }
158
161 int pos = notify_vector.indexOf(&bi);
162 if (pos < 0) return false;
163 notify_vector.erase(pos);
164 return true;
165 }
166
168 virtual void clearNotifyAudioChange() { notify_vector.clear(); }
169
171 void setNotifyActive(bool flag) { is_notify_active = flag; }
172
175
176 protected:
178 bool is_notify_active = true;
179
181 if (isNotifyActive()) {
182 for (auto n : notify_vector) {
183 n->setAudioInfo(info);
184 }
185 }
186 }
187};
188
194 public:
196 virtual float volume() { return volume_value; }
198 virtual bool setVolume(float volume) {
200 return true;
201 }
202
203 protected:
204 float volume_value = 1.0f;
205};
206
212 public:
213 virtual size_t write(const uint8_t* data, size_t len) = 0;
214 virtual void setAudioInfo(AudioInfo from) = 0;
215 virtual void setOutput(Print& out_stream) = 0;
216 virtual operator bool() = 0;
217 virtual bool begin() = 0;
218 virtual bool begin(AudioInfo info) {
219 setAudioInfo(info);
220 return begin();
221 }
222 virtual void end() = 0;
223
224 protected:
225 void writeBlocking(Print* out, uint8_t* data, size_t len) {
226 TRACED();
227 int open = len;
228 int written = 0;
229 while (open > 0) {
230 int result = out->write(data + written, open);
231 open -= result;
232 written += result;
233 }
234 }
235};
236
242 public:
244 static uint32_t toTimeUs(uint32_t samplingRate, uint8_t limit = 10) {
245 uint32_t result = 1000000l / samplingRate;
246 if (1000000l % samplingRate != 0) {
247 result++;
248 }
249 if (result <= limit) {
250 LOGW("Time for samplingRate %u -> %u is < %u μs - we rounded up",
251 (unsigned int)samplingRate, (unsigned int)result,
252 (unsigned int)limit);
253 result = limit;
254 }
255 return result;
256 }
257
260 size_t samples = info.sample_rate * millis / 1000;
261 size_t bytes = samples * info.channels * info.bits_per_sample / 8;
262 return bytes;
263 }
264
265 static uint32_t toTimeMs(uint32_t samplingRate, uint8_t limit = 1) {
266 uint32_t result = 1000l / samplingRate;
267 if (1000000l % samplingRate != 0) {
268 result++;
269 }
270 if (result <= limit) {
271 LOGW("Time for samplingRate %u -> %u is < %u μs - we rounded up",
272 (unsigned int)samplingRate, (unsigned int)result,
273 (unsigned int)limit);
274 result = limit;
275 }
276 return result;
277 }
278
279 static float toRateUs(uint32_t time_us) { return 1000000.0 / time_us; }
280
281 static float toRateMs(uint32_t time_ms) { return 1000.0 / time_ms; }
282};
283
291template <typename T>
292inline T mapT(T x, T in_min, T in_max, T out_min, T out_max) {
293 return (x - in_min) * (out_max - out_min) / (in_max - in_min) + out_min;
294}
295
301 public:
303 static int64_t maxValue(int value_bits_per_sample) {
304 switch (value_bits_per_sample) {
305 case 8:
306 return 127;
307 case 16:
308 return 32767;
309 case 24:
310 return 8388607;
311 case 32:
312 return 2147483647;
313 }
314 return 32767;
315 }
316
318 template <typename T>
319 static float maxValueT() {
320#ifdef USE_TYPETRAITS
321 // int24_t uses 4 bytes instead of 3!
322 // return (std::is_same<T, int24_t>::value ) ? 8388607 :
323 // maxValue(sizeof(T)*8);
324 return std::numeric_limits<T>::max();
325#else
326 return maxValue(sizeof(T) * 8);
327#endif
328 }
329
332 template <typename T>
333 static T maxValueExactT() {
334#ifdef USE_TYPETRAITS
335 return std::numeric_limits<T>::max();
336#else
337 return maxValue(sizeof(T) * 8);
338#endif
339 }
340
341 template <typename T>
342 static float minValueT() {
343#ifdef USE_TYPETRAITS
344 // int24_t uses 4 bytes instead of 3!
345 // return (std::is_same<T, int24_t>::value ) ? 8388607 :
346 // maxValue(sizeof(T)*8);
347 return std::numeric_limits<T>::min();
348#else
349 return -maxValue(sizeof(T) * 8);
350#endif
351 }
352
354 template <typename T>
355 static T clipT(float value) {
356 float mv = maxValueT<T>();
357 if (value >= mv) {
358 return maxValueExactT<T>();
359 } else if (value < -mv) {
360 return -mv;
361 }
362 return value;
363 }
364
366 inline static int32_t clip(float value, int bits) {
367 float mv = maxValue(bits);
368 if (value >= mv) {
369 return maxValue(bits);
370 } else if (value < -mv) {
371 return -mv;
372 }
373 return value;
374 }
375
377 template <typename T>
378 static float toFloatT(T value) {
379 return static_cast<float>(value) / maxValueT<T>();
380 }
381
383 template <typename T>
384 static T fromFloatT(float value) {
385 return clipT<T>(value * maxValueT<T>());
386 }
387
389 inline static float toFloat(int32_t value, int bits) {
390 return static_cast<float>(value) / maxValue(bits);
391 }
392
394 inline static int32_t fromFloat(float value, int bits) {
395 return clip(value * maxValue(bits), bits);
396 }
397
399 template <typename FromT, typename ToT>
400 static ToT convert(FromT value) {
401 float value1 = value;
402 float minTo = minValueT<ToT>();
403 float maxTo = maxValueT<ToT>();
404 float maxFrom = maxValueT<FromT>();
405 float minFrom = minValueT<FromT>();
406
407 float result;
408 if (maxTo - minTo > 1.0f || maxFrom - minFrom > 1.0f) {
409 result = mapT<float>(value1, minFrom, maxFrom, minTo, maxTo);
410 } else {
411 result = value1 * maxValueT<ToT>() / maxValueT<FromT>();
412 }
413 // maxTo is rounded up for 32 bit types, so the maximum would not fit
414 if (result >= maxTo) {
415 return maxValueExactT<ToT>();
416 }
417 // Clamp to the two's complement minimum of ToT (-maxTo - 1). Without type
418 // traits minValueT() is -maxTo, so the mapped minimum of FromT can land
419 // one step below it, which is still a valid ToT value.
420 if (result < -maxTo - 1.0f) {
421 return -maxTo - 1.0f;
422 }
423 return result;
424 }
425
427 template <typename FromT, typename ToT>
428 static void convertArray(FromT* from, ToT* to, int samples,
429 float vol = 1.0f) {
430 // convert() already maps FromT range to ToT range. Apply only the gain
431 // and clip to the valid ToT range to avoid double scaling.
432 for (int j = 0; j < samples; j++) {
433 ToT mapped = convert<FromT, ToT>(from[j]);
434 float scaled = static_cast<float>(mapped) * vol;
435 to[j] = clipT<ToT>(scaled);
436 }
437 }
438};
439
440#if defined(USE_I2S)
441
454
455static const char* i2s_formats[] = {"I2S_STD_FORMAT",
456 "I2S_LSB_FORMAT",
457 "I2S_MSB_FORMAT",
458 "I2S_PHILIPS_FORMAT",
459 "I2S_RIGHT_JUSTIFIED_FORMAT",
460 "I2S_LEFT_JUSTIFIED_FORMAT",
461 "I2S_PCM"};
462
472
473static const char* i2s_signal_types[] = {"Digital", "Analog", "PDM", "TDM"};
474
475#endif
476
478template <typename T>
479T readSample(Stream* p_stream) {
480 T result = 0;
481 uint8_t* p_result = (uint8_t*)&result;
482 int open = sizeof(T);
483 int total = 0;
484 // copy missing data
485 while (open > 0) {
486 int read = p_stream->readBytes(p_result + total, open);
487 open -= read;
488 total += read;
489 }
490 return result;
491}
492
494template <typename T>
495size_t readSamples(Stream* p_stream, T* data, int samples,
496 int retryCount = -1) {
497 uint8_t* p_result = (uint8_t*)data;
498 int open = samples * sizeof(T);
499 int total = 0;
500 int count0 = 0;
501 // copy missing data - we abort after 5 x read returned 0 bytes
502 while (open > 0) {
503 int read = p_stream->readBytes(p_result + total, open);
504 open -= read;
505 total += read;
506 if (read == 0) {
507 count0++;
508 delay(1);
509 } else {
510 count0 = 0;
511 }
512 // abort loop
513 if (retryCount >= 0 && count0 > retryCount) break;
514 }
515 // convert bytes to samples
516 return total / sizeof(T);
517}
518
520template <typename T, class P>
521size_t writeDataT(P* p_out, T* data, int samples, int maxSamples = 512) {
522 uint8_t* p_result = (uint8_t*)data;
523 int open = samples * sizeof(T);
524 int total = 0;
525 // copy missing data
526 while (open > 0) {
527 int to_write = min(open, static_cast<int>(maxSamples * sizeof(T)));
528 int written = p_out->write(p_result + total, to_write);
529 open -= written;
530 total += written;
531 }
532 // convert bytes to samples
533 return total / sizeof(T);
534}
535
536template <typename T>
537size_t writeData(Print* p_out, T* data, int samples, int maxSamples = 512) {
538 return writeDataT<T, Print>(p_out, data, samples, maxSamples);
539}
540
542static const char* mime_pcm = "audio/pcm";
543
544#ifndef IS_DESKTOP
546inline void waitFor(HardwareSerial& out) { while (!out); }
547#endif
548
550inline void waitFor(bool& flag) { while (!flag); }
551
555
576 public:
590 virtual const char* mime() = 0;
591};
592
609 public:
615 virtual uint32_t millis() { return ::millis(); }
616
626 virtual uint32_t playbackTime() { return millis(); }
627};
628
629} // namespace audio_tools
#define LOGW(...)
Definition AudioLoggerIDF.h:29
#define TRACED()
Definition AudioLoggerIDF.h:31
#define LOGI(...)
Definition AudioLoggerIDF.h:28
#define DEFAULT_BITS_PER_SAMPLE
Definition AudioToolsConfig.h:99
#define DEFAULT_CHANNELS
Definition AudioToolsConfig.h:95
#define DEFAULT_SAMPLE_RATE
Definition AudioToolsConfig.h:91
Definition Arduino.h:172
Definition Arduino.h:56
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
Supports the subscription to audio change notifications.
Definition AudioTypes.h:152
bool isNotifyActive()
Checks if the automatic AudioInfo update is active.
Definition AudioTypes.h:174
void notifyAudioChange(AudioInfo info)
Definition AudioTypes.h:180
virtual void addNotifyAudioChange(AudioInfoSupport &bi)
Adds target to be notified about audio changes.
Definition AudioTypes.h:155
void setNotifyActive(bool flag)
Deactivate/Reactivate automatic AudioInfo updates: (default is active)
Definition AudioTypes.h:171
virtual bool removeNotifyAudioChange(AudioInfoSupport &bi)
Removes a target in order not to be notified about audio changes.
Definition AudioTypes.h:160
Vector< AudioInfoSupport * > notify_vector
Definition AudioTypes.h:177
bool is_notify_active
Definition AudioTypes.h:178
virtual void clearNotifyAudioChange()
Deletes all change notify subscriptions.
Definition AudioTypes.h:168
Supports changes to the sampling rate, bits and channels.
Definition AudioTypes.h:137
virtual AudioInfo audioInfoOut()
Definition AudioTypes.h:145
virtual AudioInfo audioInfo()=0
provides the actual input AudioInfo
virtual void setAudioInfo(AudioInfo info)=0
Defines the input AudioInfo.
Tools for calculating timer values.
Definition AudioTypes.h:241
static uint32_t toBytes(uint32_t millis, AudioInfo info)
converts milliseconds to bytes
Definition AudioTypes.h:259
static float toRateUs(uint32_t time_us)
Definition AudioTypes.h:279
static uint32_t toTimeMs(uint32_t samplingRate, uint8_t limit=1)
Definition AudioTypes.h:265
static uint32_t toTimeUs(uint32_t samplingRate, uint8_t limit=10)
converts sampling rate to delay in microseconds (μs)
Definition AudioTypes.h:244
static float toRateMs(uint32_t time_ms)
Definition AudioTypes.h:281
E.g. used by Encoders and Decoders.
Definition AudioTypes.h:211
void writeBlocking(Print *out, uint8_t *data, size_t len)
Definition AudioTypes.h:225
virtual void setAudioInfo(AudioInfo from)=0
Defines the input AudioInfo.
virtual bool begin(AudioInfo info)
Definition AudioTypes.h:218
virtual size_t write(const uint8_t *data, size_t len)=0
virtual void setOutput(Print &out_stream)=0
virtual void end()=0
virtual bool begin()=0
Abstract interface for classes that can provide MIME type information.
Definition AudioTypes.h:575
virtual const char * mime()=0
Get the MIME type string.
Converts from a source to a target number with a different type.
Definition AudioTypes.h:300
static float toFloatT(T value)
Convert an integer integer autio type to a float (with max 1.0)
Definition AudioTypes.h:378
static T fromFloatT(float value)
Convert a float (with max 1.0) to an integer autio type.
Definition AudioTypes.h:384
static void convertArray(FromT *from, ToT *to, int samples, float vol=1.0f)
Convert an array of int types.
Definition AudioTypes.h:428
static float toFloat(int32_t value, int bits)
Convert an integer integer autio type to a float (with max 1.0)
Definition AudioTypes.h:389
static T clipT(float value)
Clips the value to avoid any over or underflows.
Definition AudioTypes.h:355
static int32_t fromFloat(float value, int bits)
Convert a float (with max 1.0) to an integer autio type.
Definition AudioTypes.h:394
static int64_t maxValue(int value_bits_per_sample)
provides the biggest number for the indicated number of bits
Definition AudioTypes.h:303
static T maxValueExactT()
Definition AudioTypes.h:333
static float minValueT()
Definition AudioTypes.h:342
static ToT convert(FromT value)
Convert an int number from one type to another.
Definition AudioTypes.h:400
static int32_t clip(float value, int bits)
clips a value
Definition AudioTypes.h:366
static float maxValueT()
provides the biggest number for the indicated type
Definition AudioTypes.h:319
Interface for classes that can provide time information - two distinct notions of "now",...
Definition AudioTypes.h:608
virtual uint32_t playbackTime()
Definition AudioTypes.h:626
virtual uint32_t millis()
Definition AudioTypes.h:615
Vector implementation which provides the most important methods as defined by std::vector....
Definition Vector.h:21
Supports the setting and getting of the volume.
Definition AudioTypes.h:193
float volume_value
Definition AudioTypes.h:204
virtual float volume()
provides the actual volume in the range of 0.0f to 1.0f
Definition AudioTypes.h:196
virtual bool setVolume(float volume)
define the actual volume in the range of 0.0f to 1.0f
Definition AudioTypes.h:198
MemoryType
Memory types.
Definition AudioTypes.h:33
void waitFor(bool &flag)
wait for flag to be active
Definition AudioTypes.h:550
RxTxMode
The Microcontroller is the Audio Source (TX_MODE) or Audio Sink (RX_MODE). RXTX_MODE is Source and Si...
Definition AudioTypes.h:26
TimeUnit
Time Units.
Definition AudioTypes.h:49
@ RAM
Definition AudioTypes.h:33
@ PS_RAM
Definition AudioTypes.h:33
@ FLASH_RAM
Definition AudioTypes.h:33
@ RXTX_MODE
Definition AudioTypes.h:26
@ TX_MODE
Definition AudioTypes.h:26
@ UNDEFINED_MODE
Definition AudioTypes.h:26
@ RX_MODE
Definition AudioTypes.h:26
@ US
Definition AudioTypes.h:49
@ HZ
Definition AudioTypes.h:49
@ MS
Definition AudioTypes.h:49
Generic Implementation of sound input and output for desktop environments using portaudio.
Definition LMSEchoCancellationStream.h:6
static const char * i2s_signal_types[]
Definition AudioTypes.h:473
I2SSignalType
I2S Signal Types: Digital, Analog, PDM.
Definition AudioTypes.h:466
@ Digital
Definition AudioTypes.h:467
@ PDM
Definition AudioTypes.h:469
@ TDM
Definition AudioTypes.h:470
@ Analog
Definition AudioTypes.h:468
size_t readSamples(Stream *p_stream, T *data, int samples, int retryCount=-1)
guaranteed to return the requested data
Definition AudioTypes.h:495
static const char * mime_pcm
Mime type for PCM.
Definition AudioTypes.h:542
static const char * TimeUnitStr[3]
Definition AudioTypes.h:50
static const char * RxTxModeNames[4]
Text string (description) for RxTxMode.
Definition AudioTypes.h:38
T mapT(T x, T in_min, T in_max, T out_min, T out_max)
Similar to Arduino map function but using floats.
Definition AudioTypes.h:292
I2SFormat
I2S Formats.
Definition AudioTypes.h:445
@ I2S_STD_FORMAT
Definition AudioTypes.h:446
@ I2S_PHILIPS_FORMAT
Definition AudioTypes.h:449
@ I2S_LSB_FORMAT
Definition AudioTypes.h:447
@ I2S_LEFT_JUSTIFIED_FORMAT
Definition AudioTypes.h:451
@ I2S_RIGHT_JUSTIFIED_FORMAT
Definition AudioTypes.h:450
@ I2S_MSB_FORMAT
Definition AudioTypes.h:448
@ I2S_PCM
Definition AudioTypes.h:452
void delay(uint32_t ms)
Definition Arduino.h:259
T readSample(Stream *p_stream)
guaranteed to return the requested data
Definition AudioTypes.h:479
static const char * i2s_formats[]
Definition AudioTypes.h:455
size_t writeDataT(P *p_out, T *data, int samples, int maxSamples=512)
guaranteed to return the requested data
Definition AudioTypes.h:521
uint32_t millis()
Returns the milliseconds since the start.
Definition Arduino.h:260
size_t writeData(Print *p_out, T *data, int samples, int maxSamples=512)
Definition AudioTypes.h:537
Basic Audio information which drives e.g. I2S.
Definition AudioTypes.h:56
void copyFrom(AudioInfo info)
Same as set.
Definition AudioTypes.h:106
sample_rate_t sample_rate
Sample Rate: e.g 44100.
Definition AudioTypes.h:58
bool equals(AudioInfo alt)
Returns true if alt values are the same like the current values.
Definition AudioTypes.h:88
uint16_t channels
Number of channels: 2=stereo, 1=mono.
Definition AudioTypes.h:60
void set(AudioInfo info)
Copies the values from info.
Definition AudioTypes.h:96
AudioInfo(const AudioInfo &)=default
Copy constructor.
AudioInfo & operator=(const AudioInfo &info)
Same as set.
Definition AudioTypes.h:110
bool operator==(AudioInfo alt)
Returns true if alt values are the same like the current values.
Definition AudioTypes.h:79
void setAudioInfo(AudioInfo info)
Same as set.
Definition AudioTypes.h:103
bool equalsExSampleRate(AudioInfo alt)
Checks if only the sample rate is different.
Definition AudioTypes.h:91
bool operator!=(AudioInfo alt)
Returns true if alt values are the different from the current values.
Definition AudioTypes.h:85
uint8_t bits_per_sample
Number of bits per sample (int16_t = 16 bits)
Definition AudioTypes.h:62
virtual void clear()
Definition AudioTypes.h:121
virtual void logInfo(const char *source="")
Definition AudioTypes.h:127
AudioInfo(sample_rate_t sampleRate, uint16_t channelCount, uint8_t bitsPerSample)
Constructor which supports all attribures as parameters.
Definition AudioTypes.h:68
AudioInfo()=default
Default constructor.