arduino-audio-tools
Loading...
Searching...
No Matches
I2SDriverESP8266.h
Go to the documentation of this file.
1#pragma once
2
3#ifdef ESP8266
4#include <I2S.h>
5
10
11#define IS_I2S_IMPLEMENTED
12
13namespace audio_tools {
14
23 friend class I2SStream;
24
25 public:
28 I2SConfigStd c(mode);
29 return c;
30 }
31
33 bool setAudioInfo(AudioInfo) { return false; }
34
36 bool begin(RxTxMode mode = TX_MODE) { return begin(defaultConfig(mode)); }
37
40 this->cfg = cfg;
44 LOGE("i2s_rxtx_begin failed");
45 return false;
46 }
47 return true;
48 }
49
51 void end() { i2s_end(); }
52
55
58
60 I2SConfigStd config() { return cfg; }
61
62 protected:
64
66 size_t writeBytes(const void *src, size_t size_bytes) {
67 size_t result = 0;
68 size_t frame_size = cfg.channels * (cfg.bits_per_sample / 8);
69 uint16_t frame_count = size_bytes / frame_size;
70 if (cfg.channels == 2 && cfg.bits_per_sample == 16) {
71 result = i2s_write_buffer((int16_t *)src, frame_count) * frame_size;
72 } else {
73 result = writeExt(src, size_bytes);
74 }
75 return result;
76 }
77
79 size_t writeExt(const void *src, size_t size_bytes) {
80 size_t result = 0;
81 int j;
82
83 switch (cfg.bits_per_sample) {
84 case 8:
85 for (j = 0; j < size_bytes; j += cfg.channels) {
86 int8_t *data = (int8_t *)src;
87 int16_t frame[2];
88 frame[0] = data[j] * 256;
89 if (cfg.channels == 1) {
90 frame[1] = data[j] * 256;
91 } else {
92 frame[1] = data[j + 1] * 256;
93 }
94 uint32_t *frame_ptr = (uint32_t *)frame;
96 result += 2;
97 }
98 }
99 break;
100
101 case 16:
102 for (j = 0; j < size_bytes / 2; j += cfg.channels) {
103 int16_t *data = (int16_t *)src;
104 int16_t frame[2];
105 frame[0] = data[j];
106 if (cfg.channels == 1) {
107 frame[1] = data[j];
108 } else {
109 frame[1] = data[j + 1];
110 }
111 uint32_t *frame_ptr = (uint32_t *)frame;
113 result += 2;
114 }
115 }
116 break;
117
118 case 24:
119 for (j = 0; j < size_bytes / 3; j += cfg.channels) {
120 int24_t *data = (int24_t *)src;
121 int24_t frame[2];
122 int32_t value = data[j];
123 frame[0] = value;
124 if (cfg.channels == 1) {
125 frame[1] = value;
126 } else {
127 value = data[j + 1];
128 frame[1] = value / 256;
129 }
130 uint32_t *frame_ptr = (uint32_t *)frame;
132 result += 2;
133 }
134 }
135 break;
136
137 case 32:
138 for (j = 0; j < size_bytes / 4; j += cfg.channels) {
139 int32_t *data = (int32_t *)src;
140 int32_t frame[2];
141 frame[0] = data[j] / 65538;
142 if (cfg.channels == 1) {
143 frame[1] = data[j] / 65538;
144 } else {
145 frame[1] = data[j + 1] / 65538;
146 }
147 uint32_t *frame_ptr = (uint32_t *)frame;
149 result += 2;
150 }
151 }
152 break;
153 }
154 return result;
155 }
156
158 size_t readBytes(void *dest, size_t size_bytes) {
159 size_t result_bytes = 0;
160 uint16_t frame_size = cfg.channels * (cfg.bits_per_sample / 8);
161 size_t frames = size_bytes / frame_size;
162 int16_t *ptr = (int16_t *)dest;
163
164 for (int j = 0; j < frames; j++) {
165 int16_t *left = ptr;
166 int16_t *right = ptr + 1;
167 bool ok = i2s_read_sample(
168 left, right, false); // RX data returned in both 16-bit outputs.
169 if (!ok) {
170 break;
171 }
172 ptr += 2;
173 result_bytes += frame_size;
174 }
175 return result_bytes;
176 }
177};
178
179using I2SDriver = I2SDriverESP8266;
180
181} // namespace audio_tools
182
183#endif
#define LOGE(...)
Definition AudioLoggerIDF.h:30
#define I2S_BUFFER_SIZE
Definition AudioToolsConfig.h:112
#define I2S_BUFFER_COUNT
Definition AudioToolsConfig.h:116
Configuration for i2s.
Definition I2SConfigSTD.h:33
RxTxMode rx_tx_mode
public settings
Definition I2SConfigSTD.h:64
Definition I2SDriverBase.h:7
Basic I2S API - for the ESP8266 Only 16 bits are supported !
Definition I2SDriverESP8266.h:22
bool setAudioInfo(AudioInfo)
Potentially updates the sample rate (if supported)
Definition I2SDriverESP8266.h:33
I2SConfigStd config()
provides the actual configuration
Definition I2SDriverESP8266.h:60
int available()
we assume the data is already available in the buffer
Definition I2SDriverESP8266.h:54
I2SConfigStd defaultConfig(RxTxMode mode)
Provides the default configuration.
Definition I2SDriverESP8266.h:27
int availableForWrite()
We limit the write size to the buffer size.
Definition I2SDriverESP8266.h:57
size_t writeExt(const void *src, size_t size_bytes)
writes the data by making shure that we send 2 channels 16 bit data
Definition I2SDriverESP8266.h:79
bool begin(I2SConfigStd cfg)
starts the DAC
Definition I2SDriverESP8266.h:39
void end()
stops the I2C and unistalls the driver
Definition I2SDriverESP8266.h:51
size_t writeBytes(const void *src, size_t size_bytes)
writes the data to the I2S interface
Definition I2SDriverESP8266.h:66
size_t readBytes(void *dest, size_t size_bytes)
reads the data from the I2S interface
Definition I2SDriverESP8266.h:158
bool begin(RxTxMode mode=TX_MODE)
starts the DAC with the default config
Definition I2SDriverESP8266.h:36
I2SConfigStd cfg
Definition I2SDriverESP8266.h:63
We support the Stream interface for the I2S access. In addition we allow a separate mute pin which mi...
Definition I2SStream.h:40
24bit integer which is used for I2S sound processing. The values are represented as int32_t,...
Definition Int24_4bytes_t.h:22
RxTxMode
The Microcontroller is the Audio Source (TX_MODE) or Audio Sink (RX_MODE). RXTX_MODE is Source and Si...
Definition AudioTypes.h:26
@ TX_MODE
Definition AudioTypes.h:26
@ RX_MODE
Definition AudioTypes.h:26
Generic Implementation of sound input and output for desktop environments using portaudio.
Definition AudioCodecsBase.h:10
I2SDriverESP32 I2SDriver
Definition I2SDriverESP32.h:403
size_t writeData(Print *p_out, T *data, int samples, int maxSamples=512)
Definition AudioTypes.h:508
Basic Audio information which drives e.g. I2S.
Definition AudioTypes.h:51
sample_rate_t sample_rate
Sample Rate: e.g 44100.
Definition AudioTypes.h:53
uint16_t channels
Number of channels: 2=stereo, 1=mono.
Definition AudioTypes.h:55
uint8_t bits_per_sample
Number of bits per sample (int16_t = 16 bits)
Definition AudioTypes.h:57