-
Notifications
You must be signed in to change notification settings - Fork 664
/
i2s_stream.h
159 lines (145 loc) · 7.66 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
/*
* 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"
#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) */
int multi_out_num; /*!< The number of multiple output */
} 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)
#define I2S_STREAM_CFG_DEFAULT() { \
.type = AUDIO_STREAM_WRITER, \
.task_prio = I2S_STREAM_TASK_PRIO, \
.task_core = I2S_STREAM_TASK_CORE, \
.task_stack = I2S_STREAM_TASK_STACK, \
.out_rb_size = I2S_STREAM_RINGBUFFER_SIZE, \
.i2s_config = { \
.mode = I2S_MODE_MASTER | I2S_MODE_TX | I2S_MODE_RX, \
.sample_rate = 44100, \
.bits_per_sample = 16, \
.channel_format = I2S_CHANNEL_FMT_RIGHT_LEFT, \
.communication_format = I2S_COMM_FORMAT_I2S, \
.dma_buf_count = 3, \
.dma_buf_len = 300, \
.use_apll = 1, \
.intr_alloc_flags = ESP_INTR_FLAG_LEVEL2, \
}, \
.i2s_port = 0, \
.use_alc = false, \
.volume = 0, \
.multi_out_num = 0, \
}
#define I2S_STREAM_INTERNAL_DAC_CFG_DEFAULT() { \
.type = AUDIO_STREAM_WRITER, \
.task_prio = I2S_STREAM_TASK_PRIO, \
.task_core = I2S_STREAM_TASK_CORE, \
.task_stack = I2S_STREAM_TASK_STACK, \
.out_rb_size = I2S_STREAM_RINGBUFFER_SIZE, \
.i2s_config = { \
.mode = I2S_MODE_MASTER | I2S_MODE_DAC_BUILT_IN | I2S_MODE_TX, \
.sample_rate = 44100, \
.bits_per_sample = 16, \
.channel_format = I2S_CHANNEL_FMT_RIGHT_LEFT, \
.communication_format = I2S_COMM_FORMAT_I2S_MSB, \
.dma_buf_count = 3, \
.dma_buf_len = 300, \
.intr_alloc_flags = ESP_INTR_FLAG_LEVEL2, \
}, \
.i2s_port = 0, \
.use_alc = false, \
.volume = 0, \
.multi_out_num = 0, \
}
/**
* @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);
#ifdef __cplusplus
}
#endif
#endif