-
Notifications
You must be signed in to change notification settings - Fork 29
/
underTheHood.ino
96 lines (73 loc) · 2.69 KB
/
underTheHood.ino
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
// cricial audio bits taken from https://github.com/espressif/esp-idf/blob/master/examples/bluetooth/bluedroid/classic_bt/a2dp_sink/main/main.c
// bluetooth, config, discover and audio
#include "esp_bt_main.h"
#include "esp_bt_device.h"
#include "esp_gap_bt_api.h"
#include "esp_a2dp_api.h"
// the audio DAC and amp configuration.
#include "driver/i2s.h"
// the callback(processes bluetooth data).
// this is the most important function.
void bt_data_cb(const uint8_t *data, uint32_t len){
// number of 16 bit samples
int n = len/2;
// point to a 16bit sample
int16_t* data16=(int16_t*)data;
// create a variable (potentially processed) that we'll pass via I2S
int16_t fy;
// Records number of bytes written via I2S
size_t i2s_bytes_write = 0;
for(int i=0;i<n;i++){
// put the current sample in fy
fy=*data16;
//making this value larger will decrease the volume(Very simple DSP!).
fy/=1;
// write data to I2S buffer
i2s_write(I2S_NUM_0, &fy, 2, &i2s_bytes_write, 10 );
//move to next memory address housing 16 bit data
data16++;
}
}
void setup() {
// i2s configuration
static const i2s_config_t i2s_config = {
.mode = static_cast<i2s_mode_t>(I2S_MODE_MASTER | I2S_MODE_TX),
.sample_rate = 44100,
.bits_per_sample = I2S_BITS_PER_SAMPLE_16BIT,
.channel_format = I2S_CHANNEL_FMT_RIGHT_LEFT,
.communication_format = static_cast<i2s_comm_format_t>(I2S_COMM_FORMAT_I2S|I2S_COMM_FORMAT_I2S_MSB),
.intr_alloc_flags = ESP_INTR_FLAG_LEVEL1, // default interrupt priority
.dma_buf_count = 8,
.dma_buf_len = 1000,
.use_apll = false,
.tx_desc_auto_clear = true
};
// i2s pinout
static const i2s_pin_config_t pin_config = {
.bck_io_num = 26,//26
.ws_io_num = 27,
.data_out_num = 25, //
.data_in_num = I2S_PIN_NO_CHANGE
};
// now configure i2s with constructed pinout and config
i2s_driver_install(I2S_NUM_0, &i2s_config, 0, NULL);
i2s_set_pin(I2S_NUM_0, &pin_config);
i2s_set_clk(I2S_NUM_0, 44100, I2S_BITS_PER_SAMPLE_16BIT, I2S_CHANNEL_STEREO);
i2s_set_sample_rates(I2S_NUM_0, 44100);
// set up bluetooth classic via bluedroid
btStart();
esp_bluedroid_init();
esp_bluedroid_enable();
// set up device name
const char *dev_name = "ESP_SPEAKER";
esp_bt_dev_set_device_name(dev_name);
// initialize A2DP sink and set the data callback(A2DP is bluetooth audio)
esp_a2d_sink_register_data_callback(bt_data_cb);
esp_a2d_sink_init();
// set discoverable and connectable mode, wait to be connected
esp_bt_gap_set_scan_mode(ESP_BT_SCAN_MODE_CONNECTABLE_DISCOVERABLE);
}
void loop() {
// put your main code here, to run repeatedly:
delay(1000);
}