3 #include "AudioConfig.h"
5 # include <type_traits>
7 #include "AudioTools/AudioLogger.h"
8 #include "AudioBasic/Collections/Vector.h"
15 #define MIN(A, B) ((A) < (B) ? (A) : (B))
20 using sample_rate_t = uint32_t;
26 enum RxTxMode {UNDEFINED_MODE=0, TX_MODE=1, RX_MODE=2, RXTX_MODE=3 };
38 static const char*
RxTxModeNames[]={
"UNDEFINED_MODE",
"TX_MODE",
"RX_MODE",
"RXTX_MODE" };
44 static const char* TimeUnitStr[] {
"MS",
"US",
"HZ"};
63 AudioInfo(sample_rate_t sampleRate, uint16_t channelCount, uint8_t bitsPerSample) {
79 return !(*
this == alt);
120 virtual void clear(){
126 virtual void logInfo(
const char* source=
"") {
146 using AudioBaseInfo = AudioInfo;
147 using AudioBaseInfoDependent = AudioInfoSupport;
148 using AudioInfoDependent = AudioInfoSupport;
159 if (!notify_vector.contains(&bi)) notify_vector.push_back(&bi);
164 int pos = notify_vector.indexOf(&bi);
165 if (pos<0)
return false;
166 notify_vector.erase(pos);
172 notify_vector.clear();
184 is_notify_active = flag;
189 return is_notify_active;
194 bool is_notify_active =
true;
198 for(
auto n : notify_vector){
199 n->setAudioInfo(info);
213 virtual float volume() {
return volume_value; }
220 float volume_value = 1.0f;
229 virtual size_t write(
const void *in_ptr,
size_t in_size) = 0;
231 virtual void setOutput(
Print &out_stream) = 0;
232 virtual operator bool() = 0;
233 virtual bool begin() = 0;
238 virtual void end() = 0;
240 void writeBlocking(
Print *out, uint8_t* data,
size_t len){
245 int result = out->write(data+written, open);
259 static uint32_t
toTimeUs(uint32_t samplingRate, uint8_t limit=10){
260 uint32_t result = 1000000l / samplingRate;
261 if (1000000l % samplingRate!=0){
264 if (result <= limit){
265 LOGW(
"Time for samplingRate %u -> %u is < %u μs - we rounded up", (
unsigned int)samplingRate, (
unsigned int)result, (
unsigned int)limit);
278 static uint32_t toTimeMs(uint32_t samplingRate, uint8_t limit=1){
279 uint32_t result = 1000l / samplingRate;
280 if (1000000l % samplingRate!=0){
283 if (result <= limit){
284 LOGW(
"Time for samplingRate %u -> %u is < %u μs - we rounded up", (
unsigned int)samplingRate, (
unsigned int)result, (
unsigned int)limit);
290 static float toRateUs(uint32_t time_us){
291 return 1000000.0 / time_us;
294 static float toRateMs(uint32_t time_ms){
295 return 1000.0 / time_ms;
305 static int32_t convertFrom24To32(
int24_t value) {
309 static int16_t convertFrom24To16(
int24_t value) {
313 static float convertFrom24ToFloat(
int24_t value) {
317 static int16_t convertFrom32To16(int32_t value) {
318 return static_cast<float>(value) / INT32_MAX * INT16_MAX;
321 static int16_t convert16(
int value,
int value_bits_per_sample){
325 static int16_t convert8(
int value,
int value_bits_per_sample){
330 static int64_t
maxValue(
int value_bits_per_sample){
331 switch(value_bits_per_sample){
345 template <
typename T>
347 #ifdef USE_TYPETRAITS
349 return (std::is_same<T, int24_t>::value ) ? 8388607 :
maxValue(
sizeof(T)*8);
356 template <
typename T>
361 }
else if (value < -mv){
368 template <
typename FromT,
typename ToT>
370 int64_t value1 = value;
371 return clip<ToT>(value1 * maxValueT<ToT>() / maxValueT<FromT>());
374 template <
typename FromT,
typename ToT>
375 static void convertArray(FromT* from, ToT*to,
int samples,
float vol=1.0f){
376 float factor =
static_cast<float>(maxValueT<ToT>()) / maxValueT<FromT>();
377 float vol_factor = factor * vol;
378 for (
int j=0;j<samples;j++){
379 to[j] = clip<ToT>(vol_factor * from[j]);
395 I2S_RIGHT_JUSTIFIED_FORMAT,
396 I2S_LEFT_JUSTIFIED_FORMAT,
400 static const char* i2s_formats[] = {
"I2S_STD_FORMAT",
"I2S_LSB_FORMAT",
"I2S_MSB_FORMAT",
"I2S_PHILIPS_FORMAT",
"I2S_RIGHT_JUSTIFIED_FORMAT",
"I2S_LEFT_JUSTIFIED_FORMAT",
"I2S_PCM"};
412 static const char* i2s_signal_types[] = {
"Digital",
"Analog",
"PDM",
"TDM"};
420 uint8_t *p_result = (uint8_t*) &result;
421 int open =
sizeof(T);
425 int read = p_stream->readBytes(p_result+total, open);
435 uint8_t *p_result = (uint8_t*) data;
436 int open = samples*
sizeof(T);
441 int read = p_stream->readBytes(p_result+total, open);
451 if (retryCount >= 0 && count0 > retryCount)
455 return total /
sizeof(T);
461 uint8_t *p_result = (uint8_t*) data;
462 int open = samples*
sizeof(T);
466 int to_write = min(open,
static_cast<int>(maxSamples*
sizeof(T)));
467 int written = p_out->write(p_result+total, to_write );
472 return total /
sizeof(T);
483 inline float mapFloat(
float x,
float in_min,
float in_max,
float out_min,
float out_max) {
484 return (x - in_min) * (out_max - out_min) / (in_max - in_min) + out_min;
MemoryType
Memory types.
Definition: AudioTypes.h:33
void waitFor(bool &flag)
wait for flag to be active
Definition: AudioTypes.h:499
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:43