forked from marcel-licence/esp32_basic_synth
-
Notifications
You must be signed in to change notification settings - Fork 0
/
esp32_audio_kit_module.ino
176 lines (149 loc) · 4.27 KB
/
esp32_audio_kit_module.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
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
/*
* this file contains basic stuff to work with the ESP32 Audio Kit V2.2 module
*
* Author: Marcel Licence
*/
#ifdef ESP32_AUDIO_KIT
/*
* Instructions: http://myosuploads3.banggood.com/products/20210306/20210306011116instruction.pdf
*
* Schmatic: https://docs.ai-thinker.com/_media/esp32-audio-kit_v2.2_sch.pdf
*/
#include "AC101.h" /* only compatible with forked repo: https://github.com/marcel-licence/AC101 */
/* AC101 pins */
#define IIS_SCLK 27
#define IIS_LCLK 26
#define IIS_DSIN 25
#define IIS_DSOUT 35
#define IIC_CLK 32
#define IIC_DATA 33
#define GPIO_PA_EN GPIO_NUM_21
#define GPIO_SEL_PA_EN GPIO_SEL_21
#define PIN_KEY_1 (36)
#define PIN_KEY_2 (13)
#define PIN_KEY_3 (19)
#define PIN_KEY_4 (23)
#define PIN_KEY_5 (18)
#define PIN_KEY_6 (5)
#define PIN_PLAY PIN_KEY_4
#define PIN_VOL_UP PIN_KEY_5
#define PIN_VOL_DOWN PIN_KEY_6
#define OUTPUT_PIN 0
#define MCLK_CH 0
#define PWM_BIT 1
static AC101 ac;
/* actually only supporting 16 bit */
//#define SAMPLE_SIZE_16BIT
//#define SAMPLE_SIZE_24BIT
//#define SAMPLE_SIZE_32BIT
#define CHANNEL_COUNT 2
#define WORD_SIZE 16
#define I2S1CLK (512*SAMPLE_RATE)
#define BCLK (SAMPLE_RATE*CHANNEL_COUNT*WORD_SIZE)
#define LRCK (SAMPLE_RATE*CHANNEL_COUNT)
/*
* this function could be used to set up the masterclock
* it is not necessary to use the ac101
*/
void ac101_mclk_setup()
{
// Put a signal out on pin
uint32_t freq = SAMPLE_RATE * 512; /* The maximal frequency is 80000000 / 2^bit_num */
Serial.printf("Output frequency: %d\n", freq);
ledcSetup(MCLK_CH, freq, PWM_BIT);
ledcAttachPin(OUTPUT_PIN, MCLK_CH);
ledcWrite(MCLK_CH, 1 << (PWM_BIT - 1)); /* 50% duty -> The available duty levels are (2^bit_num)-1, where bit_num can be 1-15. */
}
/*
* complete setup of the ac101 to enable in/output
*/
void ac101_setup()
{
Serial.printf("Connect to AC101 codec... ");
while (not ac.begin(IIC_DATA, IIC_CLK))
{
Serial.printf("Failed!\n");
delay(1000);
}
Serial.printf("OK\n");
#ifdef SAMPLE_SIZE_24BIT
ac.SetI2sWordSize(AC101::WORD_SIZE_24_BITS);
#endif
#ifdef SAMPLE_SIZE_16BIT
ac.SetI2sWordSize(AC101::WORD_SIZE_16_BITS);
#endif
#if (SAMPLE_RATE==44100)&&(defined(SAMPLE_SIZE_16BIT))
ac.SetI2sSampleRate(AC101::SAMPLE_RATE_44100);
/*
* BCLK: 44100 * 2 * 16 =
*
* I2S1CLK/BCLK1 -> 512 * 44100 / 44100*2*16
* BCLK1/LRCK -> 44100*2*16 / 44100 Obacht ... ein clock cycle goes high and low
* means 32 when 32 bits are in a LR word channel * word_size
*/
ac.SetI2sClock(AC101::BCLK_DIV_16, false, AC101::LRCK_DIV_32, false);
ac.SetI2sMode(AC101::MODE_SLAVE);
ac.SetI2sWordSize(AC101::WORD_SIZE_16_BITS);
ac.SetI2sFormat(AC101::DATA_FORMAT_I2S);
#endif
ac.SetVolumeSpeaker(3);
ac.SetVolumeHeadphone(99);
#if 1
ac.SetLineSource();
#else
ac.SetMicSource(); /* handle with care: mic is very sensitive and might cause feedback using amp!!! */
#endif
#if 0
ac.DumpRegisters();
#endif
// Enable amplifier
pinMode(GPIO_PA_EN, OUTPUT);
digitalWrite(GPIO_PA_EN, HIGH);
}
/*
* pullup required to enable reading the buttons (buttons will connect them to ground if pressed)
*/
void button_setup()
{
// Configure keys on ESP32 Audio Kit board
pinMode(PIN_PLAY, INPUT_PULLUP);
pinMode(PIN_VOL_UP, INPUT_PULLUP);
pinMode(PIN_VOL_DOWN, INPUT_PULLUP);
}
/*
* selects the microphone as audio source
* handle with care: mic is very sensitive and might cause feedback using amp!!!
*/
void ac101_setSourceMic(void)
{
ac.SetMicSource();
}
/*
* selects the line in as input
*/
void ac101_setSourceLine(void)
{
ac.SetLineSource();
}
/*
* very bad implementation checking the button state
* there is some work required for a better functionality
*/
void button_loop()
{
#if 0 /* wrong place */
if (digitalRead(PIN_PLAY) == LOW)
{
Serial.println("PIN_PLAY pressed");
}
if (digitalRead(PIN_VOL_UP) == LOW)
{
Serial.println("PIN_VOL_UP pressed");
}
if (digitalRead(PIN_VOL_DOWN) == LOW)
{
Serial.println("PIN_VOL_DOWN pressed");
}
#endif
}
#endif