forked from pschatzmann/arduino-audio-tools
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAudioTypes.h
365 lines (312 loc) · 9.65 KB
/
AudioTypes.h
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
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
#pragma once
#include "AudioConfig.h"
#ifdef USE_TYPETRAITS
# include <type_traits>
#endif
#include "AudioTools/AudioLogger.h"
#include "AudioBasic/Collections/Vector.h"
// fix compile error for ESP32 C3
#undef HZ
namespace audio_tools {
/**
* @brief Audio Source (TX_MODE) or Audio Sink (RX_MODE). RXTX_MODE is Source and Sink at the same time!
* @ingroup basic
*/
enum RxTxMode {UNDEFINED_MODE=0, TX_MODE=1, RX_MODE=2, RXTX_MODE=3 };
/**
* @brief Memory types
* @ingroup basic
*/
enum MemoryType {RAM, PS_RAM, FLASH_RAM};
/**
* @brief Text string (description) for RxTxMode
*/
INLINE_VAR const char* RxTxModeNames[]={"UNDEFINED_MODE","TX_MODE","RX_MODE","RXTX_MODE" };
/**
* @brief Time Units
* @ingroup basic
*/
enum TimeUnit {MS, US, HZ};
INLINE_VAR const char* TimeUnitStr[] {"MS","US","HZ"};
/**
* @brief Basic Audio information which drives e.g. I2S
* @ingroup basic
*/
struct AudioInfo {
/// Default constructor
AudioInfo() = default;
/// Constructor which supports all attribures as parameters
AudioInfo(int sampleRate, int channelCount, int bitsPerSample) {
sample_rate = sampleRate;
channels = channelCount;
bits_per_sample = bitsPerSample;
};
/// Copy constructor
AudioInfo(const AudioInfo &) = default;
bool operator==(AudioInfo alt){
return sample_rate==alt.sample_rate && channels == alt.channels && bits_per_sample == alt.bits_per_sample;
}
bool operator!=(AudioInfo alt){
return !(*this == alt);
}
/// Copies the values from info
void set(AudioInfo info) {
sample_rate = info.sample_rate;
channels = info.channels;
bits_per_sample = info.bits_per_sample;
}
/// Same as set
void setAudioInfo(AudioInfo info) {
set(info);
}
/// Same as set
void copyFrom(AudioInfo info){
setAudioInfo(info);
}
AudioInfo& operator= (const AudioInfo& info){
setAudioInfo(info);
return *this;
}
virtual void logInfo(const char* source=nullptr) {
//static AudioInfo old;
//if (*this!=old){
if(source!=nullptr) LOGI("Info from %s:", source);
LOGI("sample_rate: %d", sample_rate);
LOGI("channels: %d", channels);
LOGI("bits_per_sample: %d", bits_per_sample);
//old = *this;
//}
}
// public attributes
int sample_rate = 0; // undefined
int channels = 0; // undefined
int bits_per_sample=16; // we assume int16_t
};
/**
* @brief Supports changes to the sampling rate, bits and channels
* @ingroup basic
*/
class AudioInfoSupport {
public:
virtual void setAudioInfo(AudioInfo info)=0;
virtual AudioInfo audioInfo() = 0;
virtual bool validate(AudioInfo &info){
return true;
}
};
// Support legacy name
using AudioBaseInfo = AudioInfo;
using AudioBaseInfoDependent = AudioInfoSupport;
using AudioInfoDependent = AudioInfoSupport;
/**
* @brief Supports the subscription to audio change notifications
* @ingroup basic
*/
class AudioInfoSource {
public:
virtual void setNotifyAudioChange(AudioInfoSupport &bi) = 0;
};
/**
* @brief E.g. used by Encoders and Decoders
* @ingroup basic
*/
class AudioWriter {
public:
virtual size_t write(const void *in_ptr, size_t in_size) = 0;
virtual void setAudioInfo(AudioInfo from) = 0;
virtual void setOutput(Print &out_stream) = 0;
virtual operator bool() = 0;
virtual void begin() = 0;
virtual void begin(AudioInfo info) {
setAudioInfo(info);
begin();
}
virtual void end() = 0;
protected:
void writeBlocking(Print *out, uint8_t* data, size_t len){
TRACED();
int open = len;
int written = 0;
while(open>0){
int result = out->write(data+written, open);
open -= result;
written += result;
}
}
};
/**
* @brief Tools for calculating timer values
* @ingroup timer
*/
class AudioTime {
public:
/// converts sampling rate to delay in microseconds (μs)
static uint32_t toTimeUs(uint32_t samplingRate, uint8_t limit=10){
uint32_t result = 1000000l / samplingRate;
if (1000000l % samplingRate!=0){
result++;
}
if (result <= limit){
LOGW("Time for samplingRate %u -> %u is < %u μs - we rounded up", (unsigned int)samplingRate, (unsigned int)result, (unsigned int)limit);
result = limit;
}
return result;
}
/// converts milliseconds to bytes
static uint32_t toBytes(uint32_t millis, AudioInfo info){
size_t samples = info.sample_rate * millis / 1000;
size_t bytes = samples * info.channels * info.bits_per_sample / 8;
return bytes;
}
static uint32_t toTimeMs(uint32_t samplingRate, uint8_t limit=1){
uint32_t result = 1000l / samplingRate;
if (1000000l % samplingRate!=0){
result++;
}
if (result <= limit){
LOGW("Time for samplingRate %u -> %u is < %u μs - we rounded up", (unsigned int)samplingRate, (unsigned int)result, (unsigned int)limit);
result = limit;
}
return result;
}
static float toRateUs(uint32_t time_us){
return 1000000.0 / time_us;
}
static float toRateMs(uint32_t time_ms){
return 1000.0 / time_ms;
}
};
/**
* @brief Converts from a source to a target number with a different type
* @ingroup basic
*/
class NumberConverter {
public:
static int32_t convertFrom24To32(int24_t value) {
return value.scale32();
}
static int16_t convertFrom24To16(int24_t value) {
return value.scale16();
}
static float convertFrom24ToFloat(int24_t value) {
return value.scaleFloat();
}
static int16_t convertFrom32To16(int32_t value) {
return static_cast<float>(value) / INT32_MAX * INT16_MAX;
}
static int16_t convert16(int value, int value_bits_per_sample){
return value * NumberConverter::maxValue(16) / NumberConverter::maxValue(value_bits_per_sample);
}
static int16_t convert8(int value, int value_bits_per_sample){
return value * NumberConverter::maxValue(8) / NumberConverter::maxValue(value_bits_per_sample);
}
/// provides the biggest number for the indicated number of bits
static int64_t maxValue(int value_bits_per_sample){
switch(value_bits_per_sample){
case 8:
return 127;
case 16:
return 32767;
case 24:
return 8388607;
case 32:
return 2147483647;
}
return 32767;
}
/// provides the biggest number for the indicated type
template <typename T>
static int64_t maxValueT(){
#ifdef USE_TYPETRAITS
// int24_t uses 4 bytes instead of 3!
return (std::is_same<T, int24_t>::value ) ? 8388607 : maxValue(sizeof(T)*8);
#else
return maxValue(sizeof(T)*8);
#endif
}
/// Clips the value to avoid any over or underflows
template <typename T>
static T clip(int64_t value){
T mv = maxValue(sizeof(T)*8);
if (value > mv){
return mv;
} else if (value < -mv){
return -mv;
}
return value;
}
/// Convert a number from one type to another
template <typename FromT, typename ToT>
static ToT convert(FromT value){
int64_t value1 = value;
return clip<ToT>(value1 * maxValueT<ToT>() / maxValueT<FromT>());
}
};
/// guaranteed to return the requested data
template<typename T>
T readSample(Stream* p_stream){
T result=0;
uint8_t *p_result = (uint8_t*) &result;
int open = sizeof(T);
int total = 0;
// copy missing data
while (open>0){
int read = p_stream->readBytes(p_result+total, open);
open -= read;
total += read;
}
return result;
}
/// guaranteed to return the requested data
template<typename T>
size_t readSamples(Stream* p_stream, T* data, int samples){
uint8_t *p_result = (uint8_t*) data;
int open = samples*sizeof(T);
int total = 0;
// copy missing data
while (open>0){
int read = p_stream->readBytes(p_result+total, open);
open -= read;
total += read;
}
return samples;
}
/// guaranteed to return the requested data
template<typename T>
size_t writeSamples(Print* p_out, T* data, int samples){
uint8_t *p_result = (uint8_t*) data;
int open = samples*sizeof(T);
int total = 0;
// copy missing data
while (open>0){
int written = p_out->write(p_result+total, open);
open -= written;
total += written;
}
return samples;
}
/// @brief Similar to Arduino map function but using floats
/// @param x
/// @param in_min
/// @param in_max
/// @param out_min
/// @param out_max
/// @return
inline float mapFloat(float x, float in_min, float in_max, float out_min, float out_max) {
return (x - in_min) * (out_max - out_min) / (in_max - in_min) + out_min;
}
/// @brief Mime type for PCM
static const char* mime_pcm = "audio/pcm";
#ifndef IS_DESKTOP
/// wait for the Output to be ready
inline void waitFor(HardwareSerial &out){
while(!out);
}
#endif
/// wait for flag to be active @ingroup basic
inline void waitFor(bool &flag){
while(!flag);
}
/// Pins @ingroup basic
using Pins = Vector<int>;
}