Skip to content

Commit 2ad52c6

Browse files
committed
feat(i2s): Add I2S examples
- ES8388 loopback example using the LyraT board - ESP32-S3-EYE record WAV to SD card example - Simple tone example
1 parent f6e12eb commit 2ad52c6

File tree

9 files changed

+2240
-0
lines changed

9 files changed

+2240
-0
lines changed

libraries/ESP_I2S/examples/ES8388_loopback/ES8388.cpp

+905
Large diffs are not rendered by default.

libraries/ESP_I2S/examples/ES8388_loopback/ES8388.h

+1,091
Large diffs are not rendered by default.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
/*
2+
ESP32-LyraT I2S ES8388 loopback example
3+
This simple example demonstrates using the I2S library in combination
4+
with the ES8388 codec on the ESP32-LyraT board to record and play back
5+
audio data.
6+
7+
Don't forget to enable the PSRAM in the Tools menu!
8+
9+
Created for arduino-esp32 on 20 Dec, 2023
10+
by Lucas Saavedra Vaz (lucasssvaz)
11+
*/
12+
13+
#include "ESP_I2S.h"
14+
#include "Wire.h"
15+
16+
#include "ES8388.h"
17+
18+
/* Pin definitions */
19+
20+
/* I2C */
21+
const uint8_t I2C_SCL = 23;
22+
const uint8_t I2C_SDA = 18;
23+
const uint32_t I2C_FREQ = 400000;
24+
25+
/* I2S */
26+
const uint8_t I2S_MCLK = 0; /* Master clock */
27+
const uint8_t I2S_SCK = 5; /* Audio data bit clock */
28+
const uint8_t I2S_WS = 25; /* Audio data left and right clock */
29+
const uint8_t I2S_SDOUT = 26; /* ESP32 audio data output (to speakers) */
30+
const uint8_t I2S_SDIN = 35; /* ESP32 audio data input (from microphone) */
31+
32+
/* PA */
33+
const uint8_t PA_ENABLE = 21; /* Power amplifier enable */
34+
35+
void setup() {
36+
I2SClass i2s;
37+
ES8388 codec;
38+
uint8_t *wav_buffer;
39+
size_t wav_size;
40+
41+
// Initialize the serial port
42+
Serial.begin(115200);
43+
while (!Serial) { delay(10); }
44+
45+
pinMode(PA_ENABLE, OUTPUT);
46+
digitalWrite(PA_ENABLE, HIGH);
47+
48+
Serial.println("Initializing I2C bus...");
49+
50+
// Initialize the I2C bus
51+
Wire.begin(I2C_SDA, I2C_SCL, I2C_FREQ);
52+
53+
Serial.println("Initializing I2S bus...");
54+
55+
// Set up the pins used for audio input
56+
i2s.setPins(I2S_SCK, I2S_WS, I2S_SDOUT, I2S_SDIN, I2S_MCLK);
57+
58+
// Initialize the I2S bus in standard mode
59+
if (!i2s.begin(I2S_MODE_STD, 44100, I2S_DATA_BIT_WIDTH_16BIT, I2S_SLOT_MODE_STEREO, I2S_STD_SLOT_BOTH)) {
60+
Serial.println("Failed to initialize I2S bus!");
61+
return;
62+
}
63+
64+
Serial.println("Initializing ES8388...");
65+
66+
if (!codec.begin(i2s)) {
67+
Serial.println("Failed to initialize ES8388!");
68+
return;
69+
}
70+
71+
Serial.println("Recording 10 seconds of audio data...");
72+
73+
// Record 10 seconds of audio data
74+
wav_buffer = codec.recordWAV(10, &wav_size);
75+
76+
Serial.println("Recording complete. Playing audio data in 3 seconds.");
77+
delay(3000);
78+
79+
// Play the audio data
80+
Serial.println("Playing audio data...");
81+
codec.playWAV(wav_buffer, wav_size);
82+
83+
Serial.println("Application complete.");
84+
}
85+
86+
void loop() {}

libraries/ESP_I2S/examples/Record_to_WAV/.skip.esp32c3

Whitespace-only changes.

libraries/ESP_I2S/examples/Record_to_WAV/.skip.esp32c6

Whitespace-only changes.

libraries/ESP_I2S/examples/Record_to_WAV/.skip.esp32h2

Whitespace-only changes.

libraries/ESP_I2S/examples/Record_to_WAV/.skip.esp32s2

Whitespace-only changes.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
1+
/*
2+
ESP32-S2-EYE I2S record to WAV example
3+
This simple example demonstrates using the I2S library to record
4+
5 seconds of audio data and write it to a WAV file on the SD card.
5+
6+
Don't forget to select the OPI PSRAM, 8MB flash size and Enable USB CDC
7+
on boot in the Tools menu!
8+
9+
Created for arduino-esp32 on 18 Dec, 2023
10+
by Lucas Saavedra Vaz (lucasssvaz)
11+
*/
12+
13+
#include "ESP_I2S.h"
14+
#include "FS.h"
15+
#include "SD_MMC.h"
16+
17+
const uint8_t I2S_SCK = 41;
18+
const uint8_t I2S_WS = 42;
19+
const uint8_t I2S_DIN = 2;
20+
21+
const uint8_t SD_CMD = 38;
22+
const uint8_t SD_CLK = 39;
23+
const uint8_t SD_DATA0 = 40;
24+
25+
void setup() {
26+
// Create an instance of the I2SClass
27+
I2SClass i2s;
28+
29+
// Create variables to store the audio data
30+
uint8_t *wav_buffer;
31+
size_t wav_size;
32+
33+
// Initialize the serial port
34+
Serial.begin(115200);
35+
while (!Serial) { delay(10); }
36+
37+
Serial.println("Initializing I2S bus...");
38+
39+
// Set up the pins used for audio input
40+
i2s.setPins(I2S_SCK, I2S_WS, -1, I2S_DIN);
41+
42+
// Initialize the I2S bus in standard mode
43+
if (!i2s.begin(I2S_MODE_STD, 16000, I2S_DATA_BIT_WIDTH_32BIT, I2S_SLOT_MODE_MONO, I2S_STD_SLOT_LEFT)) {
44+
Serial.println("Failed to initialize I2S bus!");
45+
return;
46+
}
47+
48+
Serial.println("I2S bus initialized.");
49+
Serial.println("Initializing SD card...");
50+
51+
// Set up the pins used for SD card access
52+
if (!SD_MMC.setPins(SD_CLK, SD_CMD, SD_DATA0)) {
53+
Serial.println("Failed to set SD pins!");
54+
return;
55+
}
56+
57+
// Mount the SD card
58+
if(!SD_MMC.begin("/sdcard", true)){
59+
Serial.println("Failed to initialize SD card!");
60+
return;
61+
}
62+
63+
Serial.println("SD card initialized.");
64+
Serial.println("Recording 5 seconds of audio data...");
65+
66+
// Record 5 seconds of audio data
67+
wav_buffer = i2s.recordWAV(5, &wav_size);
68+
69+
// Create a file on the SD card
70+
File file = SD_MMC.open("/test.wav", FILE_WRITE);
71+
if (!file) {
72+
Serial.println("Failed to open file for writing!");
73+
return;
74+
}
75+
76+
Serial.println("Writing audio data to file...");
77+
78+
// Write the audio data to the file
79+
if (file.write(wav_buffer, wav_size) != wav_size) {
80+
Serial.println("Failed to write audio data to file!");
81+
return;
82+
}
83+
84+
// Close the file
85+
file.close();
86+
87+
Serial.println("Application complete.");
88+
}
89+
90+
void loop() {}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
/*
2+
This example generates a square wave based tone at a specified frequency
3+
and sample rate. Then outputs the data using the I2S interface to a
4+
MAX08357 I2S Amp Breakout board.
5+
I2S Circuit:
6+
* Arduino/Genuino Zero, MKR family and Nano 33 IoT
7+
* MAX08357:
8+
* GND connected GND
9+
* VIN connected 5V
10+
* LRC connected to pin 0 (Zero) or 3 (MKR), A2 (Nano) or 25 (ESP32)
11+
* BCLK connected to pin 1 (Zero) or 2 (MKR), A3 (Nano) or 5 (ESP32)
12+
* DIN connected to pin 9 (Zero) or A6 (MKR), 4 (Nano) or 26 (ESP32)
13+
DAC Circuit:
14+
* ESP32 or ESP32-S2
15+
* Audio amplifier
16+
- Note:
17+
- ESP32 has DAC on GPIO pins 25 and 26.
18+
- ESP32-S2 has DAC on GPIO pins 17 and 18.
19+
- Connect speaker(s) or headphones.
20+
created 17 November 2016
21+
by Sandeep Mistry
22+
For ESP extended
23+
Tomas Pilny
24+
2nd September 2021
25+
Lucas Saavedra Vaz (lucasssvaz)
26+
22nd December 2023
27+
*/
28+
29+
#include <ESP_I2S.h>
30+
31+
const int frequency = 440; // frequency of square wave in Hz
32+
const int amplitude = 500; // amplitude of square wave
33+
const int sampleRate = 8000; // sample rate in Hz
34+
35+
i2s_data_bit_width_t bps = I2S_DATA_BIT_WIDTH_16BIT;
36+
i2s_mode_t mode = I2S_MODE_STD;
37+
i2s_slot_mode_t slot = I2S_SLOT_MODE_STEREO;
38+
39+
const int halfWavelength = (sampleRate / frequency); // half wavelength of square wave
40+
41+
int32_t sample = amplitude; // current sample value
42+
int count = 0;
43+
44+
I2SClass i2s;
45+
46+
void setup() {
47+
Serial.begin(115200);
48+
Serial.println("I2S simple tone");
49+
50+
// start I2S at the sample rate with 16-bits per sample
51+
if (!i2s.begin(mode, sampleRate, bps, slot)) {
52+
Serial.println("Failed to initialize I2S!");
53+
while (1); // do nothing
54+
}
55+
}
56+
57+
void loop() {
58+
if (count % halfWavelength == 0 ) {
59+
// invert the sample every half wavelength count multiple to generate square wave
60+
sample = -1 * sample;
61+
}
62+
63+
i2s.write(sample); // Right channel
64+
i2s.write(sample); // Left channel
65+
66+
// increment the counter for the next sample
67+
count++;
68+
}

0 commit comments

Comments
 (0)