3 #include "AudioConfig.h"
5 # include <type_traits>
9 #include "AudioTools/CoreAudio/AudioLogger.h"
10 #include "AudioTools/CoreAudio/AudioBasic/Collections/Vector.h"
17 #define MIN(A, B) ((A) < (B) ? (A) : (B))
22 using sample_rate_t = uint32_t;
28 enum RxTxMode {UNDEFINED_MODE=0, TX_MODE=1, RX_MODE=2, RXTX_MODE=3 };
40 static const char*
RxTxModeNames[4]={
"UNDEFINED_MODE",
"TX_MODE",
"RX_MODE",
"RXTX_MODE" };
46 static const char* TimeUnitStr[3] {
"MS",
"US",
"HZ"};
65 AudioInfo(sample_rate_t sampleRate, uint16_t channelCount, uint8_t bitsPerSample) {
81 return !(*
this == alt);
123 virtual void clear(){
129 virtual void logInfo(
const char* source=
"") {
150 using AudioBaseInfo = AudioInfo;
151 using AudioBaseInfoDependent = AudioInfoSupport;
152 using AudioInfoDependent = AudioInfoSupport;
163 if (!notify_vector.contains(&bi)) notify_vector.push_back(&bi);
168 int pos = notify_vector.indexOf(&bi);
169 if (pos<0)
return false;
170 notify_vector.erase(pos);
176 notify_vector.clear();
181 is_notify_active = flag;
186 return is_notify_active;
191 bool is_notify_active =
true;
195 for(
auto n : notify_vector){
196 n->setAudioInfo(info);
210 virtual float volume() {
return volume_value; }
217 float volume_value = 1.0f;
226 virtual size_t write(
const uint8_t *data,
size_t len) = 0;
228 virtual void setOutput(
Print &out_stream) = 0;
229 virtual operator bool() = 0;
230 virtual bool begin() = 0;
235 virtual void end() = 0;
237 void writeBlocking(
Print *out, uint8_t* data,
size_t len){
242 int result = out->write(data+written, open);
256 static uint32_t
toTimeUs(uint32_t samplingRate, uint8_t limit=10){
257 uint32_t result = 1000000l / samplingRate;
258 if (1000000l % samplingRate!=0){
261 if (result <= limit){
262 LOGW(
"Time for samplingRate %u -> %u is < %u μs - we rounded up", (
unsigned int)samplingRate, (
unsigned int)result, (
unsigned int)limit);
275 static uint32_t toTimeMs(uint32_t samplingRate, uint8_t limit=1){
276 uint32_t result = 1000l / samplingRate;
277 if (1000000l % samplingRate!=0){
280 if (result <= limit){
281 LOGW(
"Time for samplingRate %u -> %u is < %u μs - we rounded up", (
unsigned int)samplingRate, (
unsigned int)result, (
unsigned int)limit);
287 static float toRateUs(uint32_t time_us){
288 return 1000000.0 / time_us;
291 static float toRateMs(uint32_t time_ms){
292 return 1000.0 / time_ms;
303 template <
typename T>
304 inline T
mapT(T x, T in_min, T in_max, T out_min, T out_max) {
305 return (x - in_min) * (out_max - out_min) / (in_max - in_min) + out_min;
317 static int64_t
maxValue(
int value_bits_per_sample){
318 switch(value_bits_per_sample){
332 template <
typename T>
334 #ifdef USE_TYPETRAITS
337 return std::numeric_limits<T>::max();
343 template <
typename T>
344 static float minValueT(){
345 #ifdef USE_TYPETRAITS
348 return std::numeric_limits<T>::min();
357 template <
typename T>
359 float mv = maxValueT<T>();
362 }
else if (value < -mv){
369 inline static int32_t
clip(
float value,
int bits){
373 }
else if (value < -mv){
380 template <
typename T>
382 return static_cast<float>(value) / maxValueT<T>();
386 template <
typename T>
388 return value * maxValueT<T>();
392 inline static float toFloat(int32_t value,
int bits) {
393 return static_cast<float>(value) /
maxValue(bits);
402 template <
typename FromT,
typename ToT>
404 float value1 = value;
405 float minTo = minValueT<ToT>();
406 float maxTo = maxValueT<ToT>();
407 float maxFrom = maxValueT<FromT>();
408 float minFrom = minValueT<FromT>();
410 if (maxTo - minTo > 1.0f
411 || maxFrom - minFrom > 1.0f) {
412 return mapT<float>(value1, minFrom, maxFrom, minTo, maxTo);
416 return value1 * maxValueT<ToT>() / maxValueT<FromT>();
420 template <
typename FromT,
typename ToT>
421 static void convertArray(FromT* from, ToT*to,
int samples,
float vol=1.0f){
422 float factor =
static_cast<float>(maxValueT<ToT>()) / maxValueT<FromT>();
423 float vol_factor = factor * vol;
424 for (
int j=0;j<samples;j++){
425 to[j] = clipT<ToT>(vol * convert<FromT, ToT>(from[j]));
441 I2S_RIGHT_JUSTIFIED_FORMAT,
442 I2S_LEFT_JUSTIFIED_FORMAT,
446 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"};
458 static const char* i2s_signal_types[] = {
"Digital",
"Analog",
"PDM",
"TDM"};
466 uint8_t *p_result = (uint8_t*) &result;
467 int open =
sizeof(T);
471 int read = p_stream->readBytes(p_result+total, open);
481 uint8_t *p_result = (uint8_t*) data;
482 int open = samples*
sizeof(T);
487 int read = p_stream->readBytes(p_result+total, open);
497 if (retryCount >= 0 && count0 > retryCount)
501 return total /
sizeof(T);
507 uint8_t *p_result = (uint8_t*) data;
508 int open = samples*
sizeof(T);
512 int to_write = min(open,
static_cast<int>(maxSamples*
sizeof(T)));
513 int written = p_out->write(p_result+total, to_write );
518 return total /
sizeof(T);
MemoryType
Memory types.
Definition: AudioTypes.h:35
void waitFor(bool &flag)
wait for flag to be active
Definition: AudioTypes.h:534
RxTxMode
The Microcontroller is the Audio Source (TX_MODE) or Audio Sink (RX_MODE). RXTX_MODE is Source and Si...
Definition: AudioTypes.h:28
TimeUnit
Time Units.
Definition: AudioTypes.h:45