3 #include "AudioTools/CoreAudio/AudioLogger.h"
4 #include "AudioTools/CoreAudio/Buffers.h"
5 #include "AudioTools/CoreAudio/AudioTimer/AudioTimer.h"
6 #include "AudioTools/CoreAudio/AudioOutput.h"
11 class OversamplingDAC;
12 volatile uint32_t output_frame_count = 0;
40 const int bits =
sizeof(int32_t) * 8;
70 void write(
int pin,
bool value){
71 digitalWrite(pin, value);
92 virtual DACInfo defaultConfig() {
111 LOGE(
"Only 16 Bits per sample are supported - you requested %d", info.
bits_per_sample);
122 current_values =
new int32_t[channels];
123 last_values =
new int32_t[channels];
124 cummulated_error =
new int32_t[channels];
130 virtual uint32_t outputRate() = 0;
143 write_buffer[write_buffer_pos++] = c;
144 if (write_buffer_pos==4){
145 result =
write(write_buffer, 4);
157 virtual size_t write(
const uint8_t *data,
size_t len){
159 if (len==0)
return 0;
160 int16_t *ptr = (int16_t *)data;
164 while(is_blocking && frames==0){
168 int samples = frames * info.
channels;
169 for (
int j=0; j<samples; j++){
178 return output_frame_count;
186 void setBlocking(
bool blocking){
187 is_blocking = blocking;
195 TimerAlarmRepeating timer_object;
198 int32_t *current_values =
nullptr;
199 int32_t *last_values =
nullptr;
200 int32_t *cummulated_error =
nullptr;
201 uint8_t write_buffer[4];
203 uint8_t bytes_per_sample = 2;
204 int write_buffer_pos = 0;
205 int current_bit = -1;
206 const int fixedPosValue=0x007fff00;
208 bool is_blocking =
true;
211 virtual void quantize(int16_t newSamp,
int left_right_idx) = 0;
218 if (current_values!=
nullptr){
219 delete current_values;
220 current_values =
nullptr;
222 if (last_values!=
nullptr){
224 last_values =
nullptr;
226 if (cummulated_error!=
nullptr){
227 delete cummulated_error;
228 cummulated_error =
nullptr;
232 virtual void startTimer() = 0;
247 friend void dacTimerCallback(
void *ptr);
255 if (active_count!=
nullptr){
256 delete[]active_count;
264 return OversamplingDAC::begin(cfg);
265 if (active_count==
nullptr){
266 active_count =
new uint8_t[cfg.
channels];
270 uint32_t outputRate()
override {
275 virtual void startTimer() {
280 auto dacTimerCallback = [] (
void *ptr)->
void IRAM_ATTR {
281 output_frame_count++;
282 ((SimpleDAC*) ptr)->writePins();
284 timer_object.setCallbackParameter(
this);
285 timer_object.begin(dacTimerCallback, timeUs, US);
286 LOGI(
"Timer started");
288 LOGW(
"No output because output Rate <=0");
294 RingBuffer<uint8_t> buffer = RingBuffer<uint8_t>(DEFAULT_BUFFER_SIZE);
296 uint8_t *active_count=
nullptr;
301 int32_t current_values[channels];
302 if (active && buffer.available()>=2){
305 active_count[0]=buffer.read();
306 active_count[1]=buffer.read();
310 for (
int j=0;j<channels;j++){
311 out.write(info.
start_pin+j, active_count[j]<=bit_counter);
323 return buffer.availableForWrite() / info.
channels;
327 void quantize(int16_t audioValue,
int left_right_idx)
override {
329 uint16_t sample =
map(audioValue, -32768, 32767, 0, info.
output_bits );
330 buffer.write(sample);
355 return OversamplingDAC::begin(cfg);
358 virtual uint32_t outputRate()
override {
362 virtual void startTimer() {
367 auto dacTimerCallback = [] (
void *ptr)->
void IRAM_ATTR {
368 output_frame_count++;
369 ((OversamplingDAC32*) ptr)->writePins();
371 timer_object.setCallbackParameter(
this);
372 timer_object.begin(dacTimerCallback, timeUs, US);
373 LOGI(
"Timer started");
375 LOGW(
"No output because output Rate <=0");
381 RingBuffer<int32_t> buffer = RingBuffer<int32_t>(DEFAULT_BUFFER_SIZE);
387 return buffer.availableForWrite() / (
sizeof(int32_t) * info.
channels);
393 int32_t current_values[channels];
394 if (active && buffer.available()>=2){
396 if (current_bit < 0){
397 for (
int j=0;j<channels;j++){
398 current_values[j] = buffer.read();
404 for (
int j=0;j<channels;j++){
405 out.write(info.
start_pin+j, current_values[j] >> (current_bit) & 1);
413 virtual void quantize(int16_t newSamp,
int left_right_idx)
override{
415 uint16_t sample = newSamp + (0xFFFF / 2);
417 last_values[left_right_idx] = sample;
420 int32_t diffPerStep = (sample - last_values[left_right_idx]) >> (4 + info.
oversample_factor);
423 uint32_t bits = 0xFFFFFFFF;
424 bits = bits >> (32 - (sample/2048));
449 DACInfo defaultConfig()
override {
461 LOGI(
"Setting Baudrate: %d", rate);
465 LOGE(
"sample_rate not defined");
468 return OversamplingDAC::begin(info);
472 virtual size_t write(
const uint8_t *data,
size_t len)
override {
474 return serial->write(data, len);
477 virtual uint32_t outputRate()
override {
483 HardwareSerial *serial =
nullptr;
484 int16_t totalSamples;
488 virtual void quantize(int16_t newSamp,
int left_right_idx)
override {
489 if (left_right_idx==0){
490 totalSamples = newSamp;
492 totalSamples += newSamp;
495 if (left_right_idx==cfg->channels-1) {
497 uint16_t sample = (totalSamples / cfg->channels) + (0xFFFF / 2);
499 int32_t diffPerStep = (sample - last_values[left_right_idx]) >> (4 + info.
oversample_factor);
502 uint32_t bits = 0xFFFFFFFF;
503 bits = bits >> (32 - (sample/2048));
510 #ifdef USE_DELTASIGMA
521 class DeltaSigmaDAC :
public OversamplingDAC32 {
523 DeltaSigmaDAC() : OversamplingDAC32() {}
525 DACInfo defaultConfig()
override {
527 result.oversample_factor = 2;
531 bool begin(DACInfo cfg)
override {
535 return OversamplingDAC::begin(cfg);
538 virtual uint32_t outputRate()
override {
539 return info.outputBitRate();
545 virtual void quantize(int16_t newSamp,
int left_right_idx)
override {
547 last_values[left_right_idx] = newSamp;
550 int32_t diffPerStep = (newSamp - last_values[left_right_idx]) >> (4 + info.oversample_factor);
551 for (
int j = 0; j < info.oversample_factor; j++) {
554 for (
int i = 32; i > 0; i--) {
556 if (cummulated_error[left_right_idx] < 0) {
558 cummulated_error[left_right_idx] += fixedPosValue - newSamp;
561 cummulated_error[left_right_idx] -= fixedPosValue + newSamp;
563 newSamp += diffPerStep;
582 pmw_frequency = pwmFrequency;
589 virtual DACInfo defaultConfig() {
600 bool result = OversamplingDAC::begin(cfg);
615 uint32_t max_pwm_value;
616 uint32_t pmw_frequency;
624 virtual void quantize(int16_t audioValue,
int left_right_idx)
override {
626 uint16_t sample =
map(audioValue, -32768, 32767, 0, max_pwm_value );
627 buffer.
write(sample);
632 LOGI(
"pmw_frequency: %u", pmw_frequency)
633 LOGI(
"max_pwm_value: %u", max_pwm_value)
634 for (
int ch=0;ch<info.
channels;ch++){
644 for (
int ch=0;ch<info.
channels;ch++){
645 ledcWrite(ch, buffer.
read());
650 virtual void startTimer()
override {
655 auto dacTimerCallback = [] (
void *ptr)->
void IRAM_ATTR {
656 output_frame_count++;
657 ((
PWMDAC*) ptr)->writePins();
659 timer_object.setCallbackParameter(
this);
660 timer_object.begin(dacTimerCallback, timeUs, US);
661 LOGI(
"Timer started");
663 LOGW(
"No output because output Rate <=0");