4 #include "AudioConfig.h"
5 #if defined(IS_MBED) && defined(USE_ANALOG) || defined(DOXYGEN)
28 LOGE(
"Only 16 bits_per_sample supported");
32 LOGE(
"max channels: 2");
40 auto_center.begin(config.channels, config.bits_per.sample);
42 ring_buffer.resize(n_samples);
45 dac1.begin(AN_RESOLUTION_12, info.
sample_rate, n_samples,
49 dac1.begin(AN_RESOLUTION_12, info.
sample_rate, n_samples / 2,
51 dac2.begin(AN_RESOLUTION_12, info.
sample_rate, n_samples / 2,
67 int availableForWrite() {
68 return dac1.available() ? info.buffer_size : 0;
72 size_t write(
const uint8_t *src,
size_t size_bytes)
override {
74 if (!dac1.available())
79 int sample_count = size_bytes / 2;
80 Sample *data = (Sample *)src;
81 for (
int j = 0; j < sample_count; j++) {
82 ring_buffer.
write(data[j]);
84 if (ring_buffer.
isFull()) {
92 const int size = info.buffer_size;
93 const uint8_t data[size] = {0};
98 size_t readBytes(uint8_t *dest,
size_t size_bytes)
override {
101 int16_t *data = (int16_t *)dest;
102 size_t samples = size_bytes / 2;
105 for (
int j = 0; j < samples; j++) {
106 data[j] = adc1.read();
111 for (
int j = 0; j < samples; j += 2) {
112 data[j] = adc1.read();
113 data[j + 1] = adc2.read();
120 if (adc_config.is_auto_center_read){
121 auto_center.convert(dest, result);
127 virtual int available()
override {
return info.buffer_size; }
132 ConverterAutoCenter auto_center;
133 AdvancedDAC dac1{PIN_DAC_1};
134 AdvancedDAC dac2{PIN_DAC_2};
135 AdvancedADC adc1{PIN_ANALOG_START};
136 AdvancedADC adc2{PIN_ANALOG_START + 1};
142 switch (info.channels) {
144 SampleBuffer buf = dac1.dequeue();
145 for (
size_t i = 0; i < buf.size(); i++) {
146 buf[i] = ring_buffer.
read();
152 SampleBuffer buf1 = dac1.dequeue();
153 SampleBuffer buf2 = dac2.dequeue();
154 for (
size_t i = 0; i < buf1.size(); i += 2) {
155 buf1[i] = ring_buffer.
read();
156 buf2[i] = ring_buffer.
read();
163 assert(ring_buffer.isEmpty());