-
Notifications
You must be signed in to change notification settings - Fork 11
/
AudioInI2S.h
117 lines (101 loc) · 3.4 KB
/
AudioInI2S.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
#ifndef AudioInI2S_H
#define AudioInI2S_H
#include "Arduino.h"
#include <driver/i2s.h>
/*
AudioInI2S.h
By Shea Ivey
https://github.com/sheaivey/ESP32-AudioInI2S
*/
class AudioInI2S
{
public:
AudioInI2S(int bck_pin, int ws_pin, int data_pin, int channel_pin = -1, i2s_channel_fmt_t channel_format = I2S_CHANNEL_FMT_ONLY_RIGHT);
void read(int32_t _samples[]);
// void readBuffered(int32_t _samples[], uint16_t len = 255); // Experimental stream samples buffer
void begin(int sample_size, int sample_rate = 44100, i2s_port_t i2s_port_number = I2S_NUM_0);
private:
int _bck_pin;
int _ws_pin;
int _data_pin;
int _channel_pin;
i2s_channel_fmt_t _channel_format;
int _sample_size;
int _sample_rate;
i2s_port_t _i2s_port_number;
i2s_config_t _i2s_config = {
.mode = (i2s_mode_t)(I2S_MODE_MASTER | I2S_MODE_RX),
.sample_rate = 0, // set in begin()
.bits_per_sample = I2S_BITS_PER_SAMPLE_32BIT, // TODO: let user decide what sample type to use (class type template)
.channel_format = I2S_CHANNEL_FMT_ONLY_RIGHT,
.communication_format = I2S_COMM_FORMAT_I2S,
.intr_alloc_flags = ESP_INTR_FLAG_LEVEL1,
.dma_buf_count = 4,
.dma_buf_len = 0, // set in begin()
.use_apll = false,
.tx_desc_auto_clear = false,
.fixed_mclk = 0
};
i2s_pin_config_t _i2s_mic_pins = {
.bck_io_num = I2S_PIN_NO_CHANGE, // set in begin()
.ws_io_num = I2S_PIN_NO_CHANGE, // set in begin()
.data_out_num = I2S_PIN_NO_CHANGE,
.data_in_num = I2S_PIN_NO_CHANGE // set in begin()
};
};
AudioInI2S::AudioInI2S(int bck_pin, int ws_pin, int data_pin, int channel_pin, i2s_channel_fmt_t channel_format)
{
_bck_pin = bck_pin;
_ws_pin = ws_pin;
_data_pin = data_pin;
_channel_pin = channel_pin;
_channel_format = channel_format;
}
void AudioInI2S::begin(int sample_size, int sample_rate, i2s_port_t i2s_port_number)
{
if (_channel_pin >= 0)
{
pinMode(_channel_pin, OUTPUT);
digitalWrite(_channel_pin, _channel_format == I2S_CHANNEL_FMT_ONLY_RIGHT ? LOW : HIGH);
}
_sample_rate = sample_rate;
_sample_size = sample_size;
_i2s_port_number = i2s_port_number;
_i2s_mic_pins.bck_io_num = _bck_pin;
_i2s_mic_pins.ws_io_num = _ws_pin;
_i2s_mic_pins.data_in_num = _data_pin;
_i2s_config.sample_rate = _sample_rate;
_i2s_config.dma_buf_len = _sample_size;
_i2s_config.channel_format = _channel_format;
// start up the I2S peripheral
i2s_driver_install(_i2s_port_number, &_i2s_config, 0, NULL);
i2s_set_pin(_i2s_port_number, &_i2s_mic_pins);
}
void AudioInI2S::read(int32_t _samples[])
{
// copy I2S data into the samples buffer
size_t bytes_read = 0;
i2s_read(_i2s_port_number, _samples, sizeof(int32_t) * _sample_size, &bytes_read, portMAX_DELAY);
int samples_read = bytes_read / sizeof(int32_t);
}
/* // Experimental stream samples buffer
void AudioInI2S::readBuffered(int32_t _samples[], uint16_t len)
{
// stream I2S data into the samples buffer
int32_t raw[len];
size_t bytes_read = 0;
i2s_read(_i2s_port_number, raw, sizeof(int32_t) * len, &bytes_read, portMAX_DELAY);
int samples_read = bytes_read / sizeof(int32_t);
// shift data to left
for (int j = 0; j < (_sample_size - samples_read); j++)
{
_samples[j] = _samples[j + samples_read];
}
// copy raw to end
for (int j = (_sample_size - samples_read), p = 0; j < _sample_size; j++, p++)
{
_samples[j] = raw[p];
}
}
*/
#endif // AudioInI2S_H