-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathM5Module_Audio.cpp
More file actions
899 lines (785 loc) · 24.2 KB
/
Copy pathM5Module_Audio.cpp
File metadata and controls
899 lines (785 loc) · 24.2 KB
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
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
/*
* SPDX-FileCopyrightText: 2024 M5Stack Technology CO LTD
*
* SPDX-License-Identifier: MIT
*/
#include "M5Module_Audio.h"
#define TAG "EchoBase"
namespace {
uint32_t sample_rate_to_hz(es_sample_rate_t rate)
{
switch (rate) {
case SAMPLE_RATE_8K:
return 8000;
case SAMPLE_RATE_11K:
return 11025;
case SAMPLE_RATE_16K:
return 16000;
case SAMPLE_RATE_24K:
return 24000;
case SAMPLE_RATE_32K:
return 32000;
case SAMPLE_RATE_44K:
return 44100;
case SAMPLE_RATE_48K:
return 48000;
default:
return 0;
}
}
} // namespace
#if USE_NEW_I2S_API
M5ModuleAudio::M5ModuleAudio()
: _wire(nullptr), _m5_i2c(nullptr), _i2c_speed(400000), _sda(0), _scl(0), _addr(I2C_ADDR), es8388(nullptr)
{
}
#else
M5ModuleAudio::M5ModuleAudio(i2s_port_t i2s_num)
: _wire(nullptr),
_m5_i2c(nullptr),
_i2c_speed(400000),
_sda(0),
_scl(0),
_addr(I2C_ADDR),
es8388(nullptr),
i2s_num(i2s_num)
{
}
#endif
M5ModuleAudio::~M5ModuleAudio()
{
if (es8388 != nullptr) {
delete es8388;
es8388 = nullptr;
}
}
bool M5ModuleAudio::begin(TwoWire &wire, uint8_t addr)
{
uint8_t sda = M5.getPin(m5::pin_name_t::in_i2c_sda);
uint8_t scl = M5.getPin(m5::pin_name_t::in_i2c_scl);
#if defined(CONFIG_IDF_TARGET_ESP32S3)
uint8_t bck = M5.getPin(m5::pin_name_t::mbus_pin24); // SCLK
uint8_t mck = M5.getPin(m5::pin_name_t::mbus_pin22); // MCLK
#else
uint8_t bck = M5.getPin(m5::pin_name_t::mbus_pin22); // SCLK
uint8_t mck = M5.getPin(m5::pin_name_t::mbus_pin24); // MCLK
#endif
uint8_t di = M5.getPin(m5::pin_name_t::mbus_pin26);
uint8_t ws = M5.getPin(m5::pin_name_t::mbus_pin21); // LRCK
uint8_t do_ = M5.getPin(m5::pin_name_t::mbus_pin23);
printf("I2C SDA: %d, SCL: %d\n", sda, scl);
printf("I2S MCK: %d, DI: %d, WS: %d, DO: %d, BCK: %d\n", mck, di, ws, do_, bck);
return begin(wire, sda, scl, addr, 400000, 44100, mck, di, ws, do_, bck);
}
bool M5ModuleAudio::begin(m5::I2C_Class &i2c, uint8_t addr, uint32_t speed)
{
#if defined(CONFIG_IDF_TARGET_ESP32S3)
uint8_t bck = M5.getPin(m5::pin_name_t::mbus_pin24); // SCLK
uint8_t mck = M5.getPin(m5::pin_name_t::mbus_pin22); // MCLK
#else
uint8_t bck = M5.getPin(m5::pin_name_t::mbus_pin22); // SCLK
uint8_t mck = M5.getPin(m5::pin_name_t::mbus_pin24); // MCLK
#endif
uint8_t di = M5.getPin(m5::pin_name_t::mbus_pin26);
uint8_t ws = M5.getPin(m5::pin_name_t::mbus_pin21); // LRCK
uint8_t do_ = M5.getPin(m5::pin_name_t::mbus_pin23);
printf("I2C port: %d, SDA: %d, SCL: %d\n", i2c.getPort(), i2c.getSDA(), i2c.getSCL());
printf("I2S MCK: %d, DI: %d, WS: %d, DO: %d, BCK: %d\n", mck, di, ws, do_, bck);
return begin(i2c, addr, speed, 44100, mck, di, ws, do_, bck);
}
bool M5ModuleAudio::begin(TwoWire &wire, uint8_t sda, uint8_t scl, uint8_t addr, uint32_t speed)
{
_i2s_initialized = false;
_wire = &wire;
_m5_i2c = nullptr;
_i2c_speed = speed;
_sda = sda;
_scl = scl;
_addr = addr;
ESP_LOGI(TAG, "I2C initialized");
_wire->begin(_sda, _scl, speed);
delay(10);
_wire->beginTransmission(addr);
uint8_t error = _wire->endTransmission();
if (error != 0) {
return false;
}
if (!es8388_codec_init()) {
ESP_LOGI(TAG, "ES8388 codec initialization failed");
return false;
}
return setMicGain(MIC_GAIN_0DB);
}
bool M5ModuleAudio::begin(TwoWire &wire, uint8_t sda, uint8_t scl, uint8_t addr, uint32_t speed, int sample_rate,
int i2s_mck, int i2s_di, int i2s_ws, int i2s_do, int i2s_bck)
{
_wire = &wire;
_m5_i2c = nullptr;
_i2c_speed = speed;
_sda = sda;
_scl = scl;
_addr = addr;
// Store IO pins
_i2s_mck = i2s_mck;
_i2s_bck = i2s_bck;
_i2s_ws = i2s_ws;
_i2s_do = i2s_do;
_i2s_di = i2s_di;
// Initialize I2C
ESP_LOGI(TAG, "I2C initialized");
_wire->begin(_sda, _scl, speed);
delay(10);
_wire->beginTransmission(addr);
uint8_t error = _wire->endTransmission();
if (error != 0) {
return false;
}
// Initialize I2S driver
if (!i2s_driver_init(sample_rate)) {
ESP_LOGI(TAG, "I2S driver initialization failed");
return false;
}
// Initialize ES8388 codec
if (!es8388_codec_init()) {
ESP_LOGI(TAG, "ES8388 codec initialization failed");
return false;
}
setMicGain(MIC_GAIN_0DB);
return true;
}
bool M5ModuleAudio::begin(m5::I2C_Class &i2c, uint8_t addr, uint32_t speed, int sample_rate, int i2s_mck, int i2s_di,
int i2s_ws, int i2s_do, int i2s_bck)
{
_wire = nullptr;
_m5_i2c = &i2c;
_i2c_speed = speed;
_sda = i2c.getSDA();
_scl = i2c.getSCL();
_addr = addr;
_i2s_mck = i2s_mck;
_i2s_bck = i2s_bck;
_i2s_ws = i2s_ws;
_i2s_do = i2s_do;
_i2s_di = i2s_di;
if (!i2c.isEnabled() || !i2c.scanID(addr, speed)) {
ESP_LOGI(TAG, "Module Audio controller not found on M5Unified I2C bus");
return false;
}
if (!i2s_driver_init(sample_rate)) {
ESP_LOGI(TAG, "I2S driver initialization failed");
return false;
}
if (!es8388_codec_init()) {
ESP_LOGI(TAG, "ES8388 codec initialization failed");
return false;
}
return setMicGain(MIC_GAIN_0DB);
}
void M5ModuleAudio::setMICStatus(audio_mic_t status)
{
uint8_t reg = MICROPHONE_STATUS;
writeBytes(_addr, reg, (uint8_t *)&status, 1);
}
void M5ModuleAudio::setHPMode(audio_hpmode_t mode)
{
uint8_t reg = HEADPHONE_MODE;
writeBytes(_addr, reg, (uint8_t *)&mode, 1);
}
void M5ModuleAudio::setHPMICStatus(audio_mic_t status)
{
uint8_t reg = HEADPHONE_MIC_STATUS;
writeBytes(_addr, reg, (uint8_t *)&status, 1);
}
void M5ModuleAudio::setRGBBrightness(uint8_t brightness)
{
if (brightness > 100) brightness = 100;
uint8_t reg = RGB_LED_BRIGHTNESS;
writeBytes(_addr, reg, (uint8_t *)&brightness, 1);
}
void M5ModuleAudio::setRGBLED(uint8_t num, uint32_t color)
{
if (num > 2) num = 2;
uint8_t rgb[3] = {
static_cast<uint8_t>((color >> 16) & 0xFF),
static_cast<uint8_t>((color >> 8) & 0xFF),
static_cast<uint8_t>(color & 0xFF),
};
uint8_t reg = RGB_LED + num * sizeof(rgb);
writeBytes(_addr, reg, rgb, sizeof(rgb));
}
void M5ModuleAudio::setAllRGBLED(uint32_t color)
{
const uint8_t red = static_cast<uint8_t>((color >> 16) & 0xFF);
const uint8_t green = static_cast<uint8_t>((color >> 8) & 0xFF);
const uint8_t blue = static_cast<uint8_t>(color & 0xFF);
uint8_t rgb[9];
for (size_t index = 0; index < sizeof(rgb); index += 3) {
rgb[index] = red;
rgb[index + 1] = green;
rgb[index + 2] = blue;
}
writeBytes(_addr, RGB_LED, rgb, sizeof(rgb));
}
uint8_t M5ModuleAudio::setI2CAddress(uint8_t newAddr)
{
if (newAddr < I2C_ADDR_MIN || newAddr > I2C_ADDR_MAX || newAddr == ES8388_ADDR) {
return _addr;
}
uint8_t reg = I2C_ADDRESS;
writeBytes(_addr, reg, (uint8_t *)&newAddr, 1);
_addr = newAddr;
delay(20);
return _addr;
}
void M5ModuleAudio::setFlashWriteBack()
{
uint8_t data = 1;
uint8_t reg = FLASH_WRITE;
writeBytes(_addr, reg, (uint8_t *)&data, 1);
delay(20);
}
audio_mic_t M5ModuleAudio::getMICStatus()
{
uint8_t data;
uint8_t reg = MICROPHONE_STATUS;
readBytes(_addr, reg, (uint8_t *)&data, 1);
return (audio_mic_t)data;
}
audio_hpmode_t M5ModuleAudio::getHPMode()
{
uint8_t data;
uint8_t reg = HEADPHONE_MODE;
readBytes(_addr, reg, (uint8_t *)&data, 1);
return (audio_hpmode_t)data;
}
audio_mic_t M5ModuleAudio::getHPMICStatus()
{
uint8_t data;
uint8_t reg = HEADPHONE_MIC_STATUS;
readBytes(_addr, reg, (uint8_t *)&data, 1);
return (audio_mic_t)data;
}
uint8_t M5ModuleAudio::getHPInsertStatus()
{
uint8_t data;
uint8_t reg = HEADPHONE_INSERT_STATUS;
readBytes(_addr, reg, (uint8_t *)&data, 1);
return data;
}
uint8_t M5ModuleAudio::getRGBBrightness()
{
uint8_t data;
uint8_t reg = RGB_LED_BRIGHTNESS;
readBytes(_addr, reg, (uint8_t *)&data, 1);
return data;
}
uint32_t M5ModuleAudio::getRGBLED(uint8_t num)
{
uint8_t rgb[3] = {0};
uint8_t reg = RGB_LED + num * 3;
readBytes(_addr, reg, rgb, 3);
return (rgb[0] << 16) | (rgb[1] << 8) | rgb[2];
}
bool M5ModuleAudio::getRGBLEDs(uint32_t colors[3])
{
if (colors == nullptr) {
return false;
}
uint8_t rgb[9] = {};
if (!readBytes(_addr, RGB_LED, rgb, sizeof(rgb))) {
colors[0] = colors[1] = colors[2] = 0;
return false;
}
for (size_t index = 0; index < 3; ++index) {
const size_t offset = index * 3;
colors[index] = (static_cast<uint32_t>(rgb[offset]) << 16) | (static_cast<uint32_t>(rgb[offset + 1]) << 8) |
static_cast<uint32_t>(rgb[offset + 2]);
}
return true;
}
uint8_t M5ModuleAudio::getFirmwareVersion()
{
uint8_t version;
uint8_t reg = FIRMWARE_VERSION;
readBytes(_addr, reg, (uint8_t *)&version, 1);
return version;
}
uint8_t M5ModuleAudio::getI2CAddress()
{
uint8_t I2CAddress;
uint8_t reg = I2C_ADDRESS;
readBytes(_addr, reg, (uint8_t *)&I2CAddress, 1);
return I2CAddress;
}
bool M5ModuleAudio::getDeviceUID(uint8_t uid[DEVICE_UID_LENGTH])
{
if (uid == nullptr) {
return false;
}
return readBytes(_addr, DEVICE_UID, uid, DEVICE_UID_LENGTH);
}
bool M5ModuleAudio::es8388_codec_init()
{
if (es8388 != nullptr) {
delete es8388;
es8388 = nullptr;
}
if (_m5_i2c != nullptr) {
es8388 = new ES8388(_m5_i2c, _i2c_speed);
} else {
es8388 = new ES8388(_wire, _sda, _scl, _i2c_speed);
}
if (!es8388->init()) {
ESP_LOGI(TAG, "Failed to initialize ES8388 codec");
return false;
}
ESP_LOGI(TAG, "ES8388 codec initialized successfully");
return true;
}
bool M5ModuleAudio::i2s_driver_init(int sample_rate)
{
_i2s_initialized = false;
#if USE_NEW_I2S_API
I2S.setPins(_i2s_bck, _i2s_ws, _i2s_do, _i2s_di, _i2s_mck); // SCK, WS, SDOUT, SDIN, MCLK
if (!I2S.begin(I2S_MODE_STD, sample_rate, I2S_DATA_BIT_WIDTH_16BIT, I2S_SLOT_MODE_STEREO)) {
ESP_LOGI(TAG, "Failed to initialize I2S (new API)");
return false;
}
_i2s_initialized = true;
return true;
#else
// Initialize I2S driver
i2s_cfg.mode = (i2s_mode_t)(I2S_MODE_MASTER | I2S_MODE_TX | I2S_MODE_RX);
i2s_cfg.sample_rate = (uint32_t)sample_rate;
i2s_cfg.bits_per_sample = I2S_BITS_PER_SAMPLE_16BIT;
i2s_cfg.channel_format = I2S_CHANNEL_FMT_RIGHT_LEFT;
i2s_cfg.communication_format = I2S_COMM_FORMAT_STAND_I2S;
i2s_cfg.intr_alloc_flags = ESP_INTR_FLAG_LEVEL1;
i2s_cfg.dma_buf_count = 8;
i2s_cfg.dma_buf_len = 512;
i2s_cfg.use_apll = false;
i2s_cfg.tx_desc_auto_clear = true;
i2s_cfg.fixed_mclk = 0;
#if defined(CONFIG_IDF_TARGET_ESP32)
// ESP-IDF 4.x on ESP32 still needs CLK_OUT1 routed explicitly for MCLK.
PIN_FUNC_SELECT(PERIPHS_IO_MUX_GPIO0_U, FUNC_GPIO0_CLK_OUT1);
WRITE_PERI_REG(PIN_CTRL, 0xFFF0);
#endif
// I2S pin configuration
i2s_pin_cfg.mck_io_num = _i2s_mck;
i2s_pin_cfg.bck_io_num = _i2s_bck;
i2s_pin_cfg.ws_io_num = _i2s_ws;
i2s_pin_cfg.data_out_num = _i2s_do;
i2s_pin_cfg.data_in_num = _i2s_di;
if (i2s_driver_install(i2s_num, &i2s_cfg, 0, NULL) != ESP_OK) {
ESP_LOGI(TAG, "Failed to install I2S driver");
return false;
}
if (i2s_set_pin(i2s_num, &i2s_pin_cfg) != ESP_OK) {
ESP_LOGI(TAG, "Failed to set I2S pins");
return false;
}
if (i2s_set_clk(i2s_num, i2s_cfg.sample_rate, i2s_cfg.bits_per_sample, I2S_CHANNEL_STEREO) != ESP_OK) {
ESP_LOGI(TAG, "Failed to set I2S clock");
return false;
}
i2s_zero_dma_buffer(i2s_num);
// idf 4.x
i2s_start(i2s_num);
_i2s_initialized = true;
return true;
#endif
}
bool M5ModuleAudio::setSpeakerVolume(int volume)
{
if (volume < 0 || volume > 100) {
ESP_LOGI(TAG, "Volume out of range (0-100)");
return false;
}
if (es8388 == nullptr) {
ESP_LOGI(TAG, "ES8388 codec is not initialized");
return false;
}
if (!es8388->setDACVolume(volume)) {
ESP_LOGI(TAG, "Failed to set speaker volume");
return false;
}
return true;
}
bool M5ModuleAudio::setMicGain(es_mic_gain_t gain)
{
if (es8388 == nullptr) {
ESP_LOGI(TAG, "ES8388 codec is not initialized");
return false;
}
if (!es8388->setMicGain(gain)) {
ESP_LOGI(TAG, "Failed to set microphone gain");
return false;
}
return true;
}
bool M5ModuleAudio::setMicInputLine(es_adc_input_t input)
{
if (es8388 == nullptr) {
ESP_LOGI(TAG, "ES8388 codec is not initialized");
return false;
}
if (!es8388->setADCInput(input)) {
ESP_LOGI(TAG, "Failed to set ADC input");
return false;
}
return true;
}
bool M5ModuleAudio::setMicPGAGain(es_mic_gain_t gain)
{
if (es8388 == nullptr) {
ESP_LOGI(TAG, "ES8388 codec is not initialized");
return false;
}
if (!es8388->setMicGain(gain)) {
ESP_LOGI(TAG, "Failed to configure ES8388 PGA gain");
return false;
}
return true;
}
bool M5ModuleAudio::setMicAdcVolume(uint8_t volume)
{
if (volume > 100) {
ESP_LOGI(TAG, "ADC Volume out of range (0-100)");
return false;
}
if (es8388 == nullptr) {
ESP_LOGI(TAG, "ES8388 codec is not initialized");
return false;
}
if (!es8388->setADCVolume(volume)) {
ESP_LOGI(TAG, "Failed to set ADC volume");
return false;
}
return true;
}
bool M5ModuleAudio::setMute(bool mute)
{
if (es8388 == nullptr) {
ESP_LOGI(TAG, "ES8388 codec is not initialized");
return false;
}
if (!es8388->setDACmute(mute)) {
ESP_LOGI(TAG, "Failed to set mute state");
return false;
}
return true;
}
bool M5ModuleAudio::setSpeakerOutput(es_dac_output_t output)
{
if (es8388 == nullptr) {
ESP_LOGI(TAG, "ES8388 codec is not initialized");
return false;
}
if (!es8388->setDACOutput(output)) {
ESP_LOGI(TAG, "Failed to set DAC output");
return false;
}
return true;
}
bool M5ModuleAudio::setMixSourceSelect(es_mixsel_t lmixsel, es_mixsel_t rmixsel)
{
if (es8388 == nullptr) {
ESP_LOGI(TAG, "ES8388 codec is not initialized");
return false;
}
if (!es8388->setMixSourceSelect(lmixsel, rmixsel)) {
ESP_LOGI(TAG, "Failed to set mix source");
return false;
}
return true;
}
bool M5ModuleAudio::setLineBypass(bool enabled)
{
if (es8388 == nullptr) {
ESP_LOGI(TAG, "ES8388 codec is not initialized");
return false;
}
if (!es8388->setLineBypass(enabled)) {
ESP_LOGI(TAG, "Failed to configure ES8388 line bypass");
return false;
}
return true;
}
bool M5ModuleAudio::setBitsSample(es_module_t mode, es_bits_length_t bits_len)
{
if (es8388 == nullptr) {
ESP_LOGI(TAG, "ES8388 codec is not initialized");
return false;
}
if (!es8388->setBitsSample(mode, bits_len)) {
ESP_LOGI(TAG, "Failed to set sample bits");
return false;
}
return true;
}
bool M5ModuleAudio::setSampleRate(es_sample_rate_t rate)
{
if (es8388 == nullptr) {
ESP_LOGI(TAG, "ES8388 codec is not initialized");
return false;
}
uint32_t sample_rate_hz = sample_rate_to_hz(rate);
if (sample_rate_hz == 0) {
ESP_LOGI(TAG, "Invalid sample rate");
return false;
}
if (_i2s_initialized) {
#if USE_NEW_I2S_API
if (!I2S.configureRX(sample_rate_hz, I2S_DATA_BIT_WIDTH_16BIT, I2S_SLOT_MODE_STEREO)) {
ESP_LOGI(TAG, "Failed to set I2S RX sample rate");
return false;
}
if (!I2S.configureTX(sample_rate_hz, I2S_DATA_BIT_WIDTH_16BIT, I2S_SLOT_MODE_STEREO)) {
ESP_LOGI(TAG, "Failed to set I2S TX sample rate");
return false;
}
#else
if (i2s_set_clk(i2s_num, sample_rate_hz, i2s_cfg.bits_per_sample, I2S_CHANNEL_STEREO) != ESP_OK) {
ESP_LOGI(TAG, "Failed to set I2S sample rate");
return false;
}
i2s_cfg.sample_rate = sample_rate_hz;
#endif
}
if (!es8388->setSampleRate(rate)) {
ESP_LOGI(TAG, "Failed to set sample rate");
return false;
}
return true;
}
uint8_t M5ModuleAudio::getMicGain()
{
if (es8388 == nullptr) {
ESP_LOGI(TAG, "ES8388 codec is not initialized");
return 0;
}
return es8388->getMicGain();
}
uint8_t *M5ModuleAudio::readAllReg()
{
if (es8388 == nullptr) {
ESP_LOGI(TAG, "ES8388 codec is not initialized");
return nullptr;
}
return es8388->readAllReg();
}
int M5ModuleAudio::getBufferSize(int duration, int sample_rate)
{
if (!sample_rate) {
#if USE_NEW_I2S_API
sample_rate = I2S.txSampleRate();
#else
sample_rate = i2s_cfg.sample_rate;
#endif
}
return duration * sample_rate * 2 * 2; // sample_rate * bytes_per_sample * channels
}
int M5ModuleAudio::getDuration(int size, int sample_rate)
{
if (!sample_rate) {
#if USE_NEW_I2S_API
sample_rate = I2S.txSampleRate();
#else
sample_rate = i2s_cfg.sample_rate;
#endif
}
return size / (sample_rate * 2 * 2); // sample_rate * bytes_per_sample * channels
}
bool M5ModuleAudio::record(FS &fs, const char *filename, int size)
{
// Open file for writing
File file = fs.open(filename, FILE_WRITE);
if (!file) {
ESP_LOGI(TAG, "Failed to open file for recording");
return false;
}
// Define chunk size (e.g., 1024 bytes)
const size_t CHUNK_SIZE = 1024;
uint8_t buffer[CHUNK_SIZE];
size_t total_bytes_to_record = size; // sample_rate * bytes_per_sample * channels
size_t bytes_recorded = 0;
while (bytes_recorded < total_bytes_to_record) {
size_t bytes_to_read = CHUNK_SIZE;
if (bytes_recorded + CHUNK_SIZE > total_bytes_to_record) {
bytes_to_read = total_bytes_to_record - bytes_recorded;
}
size_t bytes_read = 0;
#if USE_NEW_I2S_API
bytes_read = I2S.readBytes((char *)buffer, bytes_to_read);
if (bytes_read != bytes_to_read) {
ESP_LOGI(TAG, "Recording failed during I2S read");
file.close();
return false;
}
#else
esp_err_t err = i2s_read(i2s_num, buffer, bytes_to_read, &bytes_read, portMAX_DELAY);
if (err != ESP_OK || bytes_read != bytes_to_read) {
ESP_LOGI(TAG, "Recording failed during I2S read");
file.close();
return false;
}
#endif
file.write(buffer, bytes_read);
bytes_recorded += bytes_read;
}
file.close();
return true;
}
bool M5ModuleAudio::record(uint8_t *buffer, int size)
{
// Calculate the number of bytes to record
size_t bytes_to_record = size; // sample_rate * bytes_per_sample * channels
memset(buffer, 0, bytes_to_record);
size_t bytes_read = 0;
#if USE_NEW_I2S_API
bytes_read = I2S.readBytes((char *)buffer, bytes_to_record);
if (bytes_read != bytes_to_record) {
ESP_LOGI(TAG, "Recording failed during I2S read");
return false;
}
#else
esp_err_t err = i2s_read(i2s_num, buffer, bytes_to_record, &bytes_read, getDuration(size) * 1000 + 1000);
if (err != ESP_OK || bytes_read != bytes_to_record) {
ESP_LOGI(TAG, "Recording failed");
return false;
}
#endif
return true;
}
bool M5ModuleAudio::play(FS &fs, const char *filename)
{
// Open file for reading
File file = fs.open(filename, FILE_READ);
if (!file) {
ESP_LOGI(TAG, "Failed to open file for playback");
return false;
}
// Define chunk size (e.g., 1024 bytes)
const size_t CHUNK_SIZE = 1024;
uint8_t buffer[CHUNK_SIZE];
while (file.available()) {
size_t bytes_to_read = CHUNK_SIZE;
if (file.available() < CHUNK_SIZE) {
bytes_to_read = file.available();
}
size_t bytes_read = file.read(buffer, bytes_to_read);
if (bytes_read > 0) {
size_t bytes_written = 0;
#if USE_NEW_I2S_API
bytes_written = I2S.write(buffer, bytes_read);
if (bytes_written < 0) {
ESP_LOGI(TAG, "Playback failed during I2S write");
file.close();
return false;
}
#else
esp_err_t err = i2s_write(i2s_num, buffer, bytes_read, &bytes_written, portMAX_DELAY);
if (err != ESP_OK) {
ESP_LOGI(TAG, "Playback failed during I2S write");
file.close();
return false;
}
#endif
}
}
file.close();
return true;
}
bool M5ModuleAudio::play(const uint8_t *buffer, int size)
{
size_t bytes_written = 0;
#if USE_NEW_I2S_API
bytes_written = I2S.write(buffer, size);
if (bytes_written != static_cast<size_t>(size)) {
ESP_LOGI(TAG, "Playback failed");
return false;
}
#else
esp_err_t err = i2s_write(i2s_num, buffer, size, &bytes_written, portMAX_DELAY);
if (err != ESP_OK || bytes_written != static_cast<size_t>(size)) {
ESP_LOGI(TAG, "Playback failed");
return false;
}
#endif
return true;
}
void M5ModuleAudio::writeBytes(uint8_t addr, uint8_t reg, uint8_t *buffer, uint8_t length)
{
if (_m5_i2c != nullptr) {
_m5_i2c->writeRegister(addr, reg, buffer, length, _i2c_speed);
return;
}
if (_wire == nullptr) {
return;
}
_wire->beginTransmission(addr);
_wire->write(reg);
for (int i = 0; i < length; i++) {
_wire->write(*(buffer + i));
}
_wire->endTransmission();
#if AUDIO_I2C_DEBUG
Serial.print("Write bytes: [");
Serial.print(addr);
Serial.print(",");
Serial.print(reg);
Serial.print(",");
for (int i = 0; i < length; i++) {
Serial.print(buffer[i]);
if (i < length - 1) {
Serial.print(",");
}
}
Serial.println("]");
#else
#endif
}
bool M5ModuleAudio::readBytes(uint8_t addr, uint8_t reg, uint8_t *buffer, uint8_t length)
{
if (buffer == nullptr || length == 0) {
return false;
}
if (_m5_i2c != nullptr) {
if (!_m5_i2c->readRegister(addr, reg, buffer, length, _i2c_speed)) {
memset(buffer, 0, length);
return false;
}
return true;
}
if (_wire == nullptr) {
memset(buffer, 0, length);
return false;
}
uint8_t index = 0;
_wire->beginTransmission(addr);
_wire->write(reg);
if (_wire->endTransmission(false) != 0 || _wire->requestFrom(addr, length) != length) {
memset(buffer, 0, length);
return false;
}
for (int i = 0; i < length; i++) {
buffer[index++] = _wire->read();
}
#if AUDIO_I2C_DEBUG
Serial.print("Read bytes: [");
Serial.print(addr);
Serial.print(",");
Serial.print(reg);
Serial.print(",");
for (int i = 0; i < length; i++) {
Serial.print(buffer[i]);
if (i < length - 1) {
Serial.print(",");
}
}
Serial.println("]");
#else
#endif
return true;
}