3#include "AudioToolsConfig.h"
9#include "AudioTools/CoreAudio/AudioBasic/Collections/Vector.h"
10#include "AudioTools/CoreAudio/AudioLogger.h"
17#define MIN(A, B) ((A) < (B) ? (A) : (B))
30enum RxTxMode { UNDEFINED_MODE = 0, TX_MODE = 1, RX_MODE = 2, RXTX_MODE = 3 };
42static const char*
RxTxModeNames[4] = {
"UNDEFINED_MODE",
"TX_MODE",
"RX_MODE",
49static const char* TimeUnitStr[3]{
"MS",
"US",
"HZ"};
68 uint8_t bitsPerSample) {
116 return sample_rate > 0 && sample_rate <= 100000 && channels > 0 && channels < 20 && bits_per_sample > 0 &&
bits_per_sample <= 64;
119 virtual void clear() {
125 virtual void logInfo(
const char* source =
"") {
126 LOGI(
"%s sample_rate: %d / channels: %d / bits_per_sample: %d", source,
154 if (!notify_vector.contains(&bi)) notify_vector.push_back(&bi);
159 int pos = notify_vector.indexOf(&bi);
160 if (pos < 0)
return false;
161 notify_vector.erase(pos);
176 bool is_notify_active =
true;
180 for (
auto n : notify_vector) {
181 n->setAudioInfo(info);
194 virtual float volume() {
return volume_value; }
202 float volume_value = 1.0f;
211 virtual size_t write(
const uint8_t* data,
size_t len) = 0;
213 virtual void setOutput(
Print& out_stream) = 0;
214 virtual operator bool() = 0;
215 virtual bool begin() = 0;
220 virtual void end() = 0;
223 void writeBlocking(
Print* out, uint8_t* data,
size_t len) {
228 int result = out->write(data + written, open);
242 static uint32_t
toTimeUs(uint32_t samplingRate, uint8_t limit = 10) {
243 uint32_t result = 1000000l / samplingRate;
244 if (1000000l % samplingRate != 0) {
247 if (result <= limit) {
248 LOGW(
"Time for samplingRate %u -> %u is < %u μs - we rounded up",
249 (
unsigned int)samplingRate, (
unsigned int)result,
250 (
unsigned int)limit);
263 static uint32_t toTimeMs(uint32_t samplingRate, uint8_t limit = 1) {
264 uint32_t result = 1000l / samplingRate;
265 if (1000000l % samplingRate != 0) {
268 if (result <= limit) {
269 LOGW(
"Time for samplingRate %u -> %u is < %u μs - we rounded up",
270 (
unsigned int)samplingRate, (
unsigned int)result,
271 (
unsigned int)limit);
277 static float toRateUs(uint32_t time_us) {
return 1000000.0 / time_us; }
279 static float toRateMs(uint32_t time_ms) {
return 1000.0 / time_ms; }
290inline T
mapT(T x, T in_min, T in_max, T out_min, T out_max) {
291 return (x - in_min) * (out_max - out_min) / (in_max - in_min) + out_min;
301 static int64_t
maxValue(
int value_bits_per_sample) {
302 switch (value_bits_per_sample) {
316 template <
typename T>
322 return std::numeric_limits<T>::max();
328 template <
typename T>
329 static float minValueT() {
334 return std::numeric_limits<T>::min();
341 template <
typename T>
343 float mv = maxValueT<T>();
346 }
else if (value < -mv) {
353 inline static int32_t
clip(
float value,
int bits) {
357 }
else if (value < -mv) {
364 template <
typename T>
366 return static_cast<float>(value) / maxValueT<T>();
370 template <
typename T>
372 return value * maxValueT<T>();
376 inline static float toFloat(int32_t value,
int bits) {
377 return static_cast<float>(value) /
maxValue(bits);
381 inline static int32_t
fromFloat(
float value,
int bits) {
386 template <
typename FromT,
typename ToT>
388 float value1 = value;
389 float minTo = minValueT<ToT>();
390 float maxTo = maxValueT<ToT>();
391 float maxFrom = maxValueT<FromT>();
392 float minFrom = minValueT<FromT>();
394 if (maxTo - minTo > 1.0f || maxFrom - minFrom > 1.0f) {
395 return mapT<float>(value1, minFrom, maxFrom, minTo, maxTo);
398 return value1 * maxValueT<ToT>() / maxValueT<FromT>();
402 template <
typename FromT,
typename ToT>
405 float factor =
static_cast<float>(maxValueT<ToT>()) / maxValueT<FromT>();
406 float vol_factor = factor * vol;
407 for (
int j = 0; j < samples; j++) {
408 to[j] = clipT<ToT>(vol_factor * convert<FromT, ToT>(from[j]));
423 I2S_RIGHT_JUSTIFIED_FORMAT,
424 I2S_LEFT_JUSTIFIED_FORMAT,
428static const char* i2s_formats[] = {
"I2S_STD_FORMAT",
431 "I2S_PHILIPS_FORMAT",
432 "I2S_RIGHT_JUSTIFIED_FORMAT",
433 "I2S_LEFT_JUSTIFIED_FORMAT",
446static const char* i2s_signal_types[] = {
"Digital",
"Analog",
"PDM",
"TDM"};
454 uint8_t* p_result = (uint8_t*)&result;
455 int open =
sizeof(T);
459 int read = p_stream->readBytes(p_result + total, open);
469 int retryCount = -1) {
470 uint8_t* p_result = (uint8_t*)data;
471 int open = samples *
sizeof(T);
476 int read = p_stream->readBytes(p_result + total, open);
486 if (retryCount >= 0 && count0 > retryCount)
break;
489 return total /
sizeof(T);
493template <
typename T,
class P>
494size_t writeDataT(P* p_out, T* data,
int samples,
int maxSamples = 512) {
495 uint8_t* p_result = (uint8_t*)data;
496 int open = samples *
sizeof(T);
500 int to_write = min(open,
static_cast<int>(maxSamples *
sizeof(T)));
501 int written = p_out->write(p_result + total, to_write);
506 return total /
sizeof(T);
510size_t writeData(Print* p_out, T* data,
int samples,
int maxSamples = 512) {
511 return writeDataT<T, Print>(p_out, data, samples, maxSamples);
523inline void waitFor(
bool& flag) {
while (!flag); }
MemoryType
Memory types.
Definition AudioTypes.h:37
uint32_t sample_rate_t
Type alias for sample rate values.
Definition AudioTypes.h:23
void waitFor(bool &flag)
wait for flag to be active
Definition AudioTypes.h:523
RxTxMode
The Microcontroller is the Audio Source (TX_MODE) or Audio Sink (RX_MODE). RXTX_MODE is Source and Si...
Definition AudioTypes.h:30
TimeUnit
Time Units.
Definition AudioTypes.h:48