-
Notifications
You must be signed in to change notification settings - Fork 675
/
i2s_stream.h
304 lines (287 loc) · 17.9 KB
/
i2s_stream.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
/*
* ESPRESSIF MIT License
*
* Copyright (c) 2018 <ESPRESSIF SYSTEMS (SHANGHAI) PTE LTD>
*
* Permission is hereby granted for use on all ESPRESSIF SYSTEMS products, in which case,
* it is free of charge, to any person obtaining a copy of this software and associated
* documentation files (the "Software"), to deal in the Software without restriction, including
* without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense,
* and/or sell copies of the Software, and to permit persons to whom the Software is furnished
* to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all copies or
* substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
* FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
* COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
* IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
* CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*
*/
#ifndef _I2S_STREAM_WRITER_H_
#define _I2S_STREAM_WRITER_H_
#include "driver/i2s.h"
#include "audio_common.h"
#include "audio_error.h"
#include "audio_idf_version.h"
#ifdef __cplusplus
extern "C" {
#endif
/**
* @brief I2S Stream configurations
* Default value will be used if any entry is zero
*/
typedef struct {
audio_stream_type_t type; /*!< Type of stream */
i2s_config_t i2s_config; /*!< I2S driver configurations */
i2s_port_t i2s_port; /*!< I2S driver hardware port */
bool use_alc; /*!< It is a flag for ALC. If use ALC, the value is true. Or the value is false */
int volume; /*!< The volume of audio input data will be set. */
int out_rb_size; /*!< Size of output ringbuffer */
int task_stack; /*!< Task stack size */
int task_core; /*!< Task running in core (0 or 1) */
int task_prio; /*!< Task priority (based on freeRTOS priority) */
bool stack_in_ext; /*!< Try to allocate stack in external memory */
int multi_out_num; /*!< The number of multiple output */
bool uninstall_drv; /*!< whether uninstall the i2s driver when stream destroyed*/
bool need_expand; /*!< whether to expand i2s data */
i2s_bits_per_sample_t expand_src_bits; /*!< The source bits per sample when data expand */
} i2s_stream_cfg_t;
#define I2S_STREAM_TASK_STACK (3072+512)
#define I2S_STREAM_BUF_SIZE (2048)
#define I2S_STREAM_TASK_PRIO (23)
#define I2S_STREAM_TASK_CORE (0)
#define I2S_STREAM_RINGBUFFER_SIZE (8 * 1024)
#if (ESP_IDF_VERSION < ESP_IDF_VERSION_VAL(4, 2, 0))
#define I2S_STREAM_CFG_DEFAULT() { \
.type = AUDIO_STREAM_WRITER, \
.i2s_config = { \
.mode = (i2s_mode_t)(I2S_MODE_MASTER | I2S_MODE_TX | I2S_MODE_RX), \
.sample_rate = 44100, \
.bits_per_sample = I2S_BITS_PER_SAMPLE_16BIT, \
.channel_format = I2S_CHANNEL_FMT_RIGHT_LEFT, \
.communication_format = I2S_COMM_FORMAT_I2S, \
.intr_alloc_flags = ESP_INTR_FLAG_LEVEL2 | ESP_INTR_FLAG_IRAM, \
.dma_buf_count = 3, \
.dma_buf_len = 300, \
.use_apll = true, \
.tx_desc_auto_clear = true, \
.fixed_mclk = 0 \
}, \
.i2s_port = I2S_NUM_0, \
.use_alc = false, \
.volume = 0, \
.out_rb_size = I2S_STREAM_RINGBUFFER_SIZE, \
.task_stack = I2S_STREAM_TASK_STACK, \
.task_core = I2S_STREAM_TASK_CORE, \
.task_prio = I2S_STREAM_TASK_PRIO, \
.stack_in_ext = false, \
.multi_out_num = 0, \
.uninstall_drv = true, \
.need_expand = false, \
.expand_src_bits = I2S_BITS_PER_SAMPLE_16BIT, \
}
#define I2S_STREAM_INTERNAL_DAC_CFG_DEFAULT() { \
.type = AUDIO_STREAM_WRITER, \
.i2s_config = { \
.mode = (i2s_mode_t)(I2S_MODE_MASTER | I2S_MODE_DAC_BUILT_IN | I2S_MODE_TX),\
.sample_rate = 44100, \
.bits_per_sample = I2S_BITS_PER_SAMPLE_16BIT, \
.channel_format = I2S_CHANNEL_FMT_RIGHT_LEFT, \
.communication_format = I2S_COMM_FORMAT_I2S_MSB, \
.intr_alloc_flags = ESP_INTR_FLAG_LEVEL2, \
.dma_buf_count = 3, \
.dma_buf_len = 300, \
.use_apll = false, \
.tx_desc_auto_clear = true, \
.fixed_mclk = 0 \
}, \
.i2s_port = I2S_NUM_0, \
.use_alc = false, \
.volume = 0, \
.out_rb_size = I2S_STREAM_RINGBUFFER_SIZE, \
.task_stack = I2S_STREAM_TASK_STACK, \
.task_core = I2S_STREAM_TASK_CORE, \
.task_prio = I2S_STREAM_TASK_PRIO, \
.stack_in_ext = false, \
.multi_out_num = 0, \
.uninstall_drv = false, \
.need_expand = false, \
.expand_src_bits = I2S_BITS_PER_SAMPLE_16BIT, \
}
#define I2S_STREAM_TX_PDM_CFG_DEFAULT() { \
.type = AUDIO_STREAM_WRITER, \
.i2s_config = { \
.mode = (i2s_mode_t)(I2S_MODE_MASTER | I2S_MODE_PDM | I2S_MODE_TX), \
.sample_rate = 44100, \
.bits_per_sample = I2S_BITS_PER_SAMPLE_16BIT, \
.channel_format = I2S_CHANNEL_FMT_RIGHT_LEFT, \
.communication_format = I2S_COMM_FORMAT_I2S_MSB, \
.dma_buf_count = 3, \
.dma_buf_len = 300, \
.use_apll = true, \
.tx_desc_auto_clear = true, \
.fixed_mclk = 0 \
}, \
.i2s_port = I2S_NUM_0, \
.use_alc = false, \
.volume = 0, \
.out_rb_size = I2S_STREAM_RINGBUFFER_SIZE, \
.task_stack = I2S_STREAM_TASK_STACK, \
.task_core = I2S_STREAM_TASK_CORE, \
.task_prio = I2S_STREAM_TASK_PRIO, \
.stack_in_ext = false, \
.multi_out_num = 0, \
.uninstall_drv = false, \
.need_expand = false, \
.expand_src_bits = I2S_BITS_PER_SAMPLE_16BIT, \
}
#else
#define I2S_STREAM_CFG_DEFAULT() { \
.type = AUDIO_STREAM_WRITER, \
.i2s_config = { \
.mode = (i2s_mode_t)(I2S_MODE_MASTER | I2S_MODE_TX | I2S_MODE_RX), \
.sample_rate = 44100, \
.bits_per_sample = I2S_BITS_PER_SAMPLE_16BIT, \
.channel_format = I2S_CHANNEL_FMT_RIGHT_LEFT, \
.communication_format = I2S_COMM_FORMAT_STAND_I2S, \
.intr_alloc_flags = ESP_INTR_FLAG_LEVEL2 | ESP_INTR_FLAG_IRAM, \
.dma_buf_count = 3, \
.dma_buf_len = 300, \
.use_apll = true, \
.tx_desc_auto_clear = true, \
.fixed_mclk = 0 \
}, \
.i2s_port = I2S_NUM_0, \
.use_alc = false, \
.volume = 0, \
.out_rb_size = I2S_STREAM_RINGBUFFER_SIZE, \
.task_stack = I2S_STREAM_TASK_STACK, \
.task_core = I2S_STREAM_TASK_CORE, \
.task_prio = I2S_STREAM_TASK_PRIO, \
.stack_in_ext = false, \
.multi_out_num = 0, \
.uninstall_drv = true, \
.need_expand = false, \
.expand_src_bits = I2S_BITS_PER_SAMPLE_16BIT, \
}
#define I2S_STREAM_INTERNAL_DAC_CFG_DEFAULT() { \
.type = AUDIO_STREAM_WRITER, \
.i2s_config = { \
.mode = (i2s_mode_t)(I2S_MODE_MASTER | I2S_MODE_DAC_BUILT_IN | I2S_MODE_TX),\
.sample_rate = 44100, \
.bits_per_sample = I2S_BITS_PER_SAMPLE_16BIT, \
.channel_format = I2S_CHANNEL_FMT_RIGHT_LEFT, \
.communication_format = I2S_COMM_FORMAT_STAND_MSB, \
.intr_alloc_flags = ESP_INTR_FLAG_LEVEL2, \
.dma_buf_count = 3, \
.dma_buf_len = 300, \
.use_apll = false, \
.tx_desc_auto_clear = true, \
.fixed_mclk = 0 \
}, \
.i2s_port = I2S_NUM_0, \
.use_alc = false, \
.volume = 0, \
.out_rb_size = I2S_STREAM_RINGBUFFER_SIZE, \
.task_stack = I2S_STREAM_TASK_STACK, \
.task_core = I2S_STREAM_TASK_CORE, \
.task_prio = I2S_STREAM_TASK_PRIO, \
.stack_in_ext = false, \
.multi_out_num = 0, \
.uninstall_drv = false, \
.need_expand = false, \
.expand_src_bits = I2S_BITS_PER_SAMPLE_16BIT, \
}
#define I2S_STREAM_TX_PDM_CFG_DEFAULT() { \
.type = AUDIO_STREAM_WRITER, \
.i2s_config = { \
.mode = (i2s_mode_t)(I2S_MODE_MASTER | I2S_MODE_PDM | I2S_MODE_TX), \
.sample_rate = 48000, \
.bits_per_sample = I2S_BITS_PER_SAMPLE_16BIT, \
.channel_format = I2S_CHANNEL_FMT_RIGHT_LEFT, \
.communication_format = I2S_COMM_FORMAT_STAND_MSB, \
.dma_buf_count = 3, \
.dma_buf_len = 300, \
.use_apll = true, \
.tx_desc_auto_clear = true, \
.fixed_mclk = 0 \
}, \
.i2s_port = I2S_NUM_0, \
.use_alc = false, \
.volume = 0, \
.out_rb_size = I2S_STREAM_RINGBUFFER_SIZE, \
.task_stack = I2S_STREAM_TASK_STACK, \
.task_core = I2S_STREAM_TASK_CORE, \
.task_prio = I2S_STREAM_TASK_PRIO, \
.stack_in_ext = false, \
.multi_out_num = 0, \
.uninstall_drv = false, \
.need_expand = false, \
.expand_src_bits = I2S_BITS_PER_SAMPLE_16BIT, \
}
#endif
/**
* @brief Create a handle to an Audio Element to stream data from I2S to another Element
* or get data from other elements sent to I2S, depending on the configuration of stream type
* is AUDIO_STREAM_READER or AUDIO_STREAM_WRITER.
* @note If I2S stream is enabled with built-in DAC mode, please don't use I2S_NUM_1. The built-in
* DAC functions are only supported on I2S0 for the current ESP32 chip.
* @param config The configuration
*
* @return The Audio Element handle
*/
audio_element_handle_t i2s_stream_init(i2s_stream_cfg_t *config);
/**
* @brief Setup clock for I2S Stream, this function is only used with handle created by `i2s_stream_init`
*
* @param[in] i2s_stream The i2s element handle
* @param[in] rate Clock rate (in Hz)
* @param[in] bits Audio bit width (8, 16, 24, 32)
* @param[in] ch Number of Audio channels (1: Mono, 2: Stereo)
*
* @return
* - ESP_OK
* - ESP_FAIL
*/
esp_err_t i2s_stream_set_clk(audio_element_handle_t i2s_stream, int rate, int bits, int ch);
/**
* @brief Setup volume of stream by using ALC
*
* @param[in] i2s_stream The i2s element handle
* @param[in] volume The volume of stream will be set.
*
* @return
* - ESP_OK
* - ESP_FAIL
*/
esp_err_t i2s_alc_volume_set(audio_element_handle_t i2s_stream, int volume);
/**
* @brief Get volume of stream
*
* @param[in] i2s_stream The i2s element handle
* @param[in] volume The volume of stream
*
* @return
* - ESP_OK
* - ESP_FAIL
*/
esp_err_t i2s_alc_volume_get(audio_element_handle_t i2s_stream, int *volume);
/**
* @brief Set sync delay of stream
*
* @param[in] i2s_stream The i2s element handle
* @param[in] delay_ms The delay of stream
*
* @return
* - ESP_OK
* - ESP_FAIL
*/
esp_err_t i2s_stream_sync_delay(audio_element_handle_t i2s_stream, int delay_ms);
#ifdef __cplusplus
}
#endif
#endif