From dd70e349447aef9b6c1d1d468aed17345d61b8c3 Mon Sep 17 00:00:00 2001 From: greenyleaf Date: Mon, 10 Feb 2025 21:33:54 +0800 Subject: [PATCH 1/4] made some fix to the ESP32 I2S simple tone example --- .../ESP_I2S/examples/Simple_tone/Simple_tone.ino | 13 ++++++++++--- 1 file changed, 10 insertions(+), 3 deletions(-) diff --git a/libraries/ESP_I2S/examples/Simple_tone/Simple_tone.ino b/libraries/ESP_I2S/examples/Simple_tone/Simple_tone.ino index 935aa4bc50f..6c8e444edcf 100644 --- a/libraries/ESP_I2S/examples/Simple_tone/Simple_tone.ino +++ b/libraries/ESP_I2S/examples/Simple_tone/Simple_tone.ino @@ -24,6 +24,8 @@ 2nd September 2021 Lucas Saavedra Vaz (lucasssvaz) 22nd December 2023 + anonZ + 10nd February 2025 */ #include @@ -36,10 +38,10 @@ i2s_data_bit_width_t bps = I2S_DATA_BIT_WIDTH_16BIT; i2s_mode_t mode = I2S_MODE_STD; i2s_slot_mode_t slot = I2S_SLOT_MODE_STEREO; -const int halfWavelength = (sampleRate / frequency); // half wavelength of square wave +const int halfWavelength = (sampleRate / frequency / 2); // half wavelength of square wave int32_t sample = amplitude; // current sample value -int count = 0; +unsigned int count = 0; I2SClass i2s; @@ -47,10 +49,13 @@ void setup() { Serial.begin(115200); Serial.println("I2S simple tone"); + i2s.setPins(5, 25, 26); + // start I2S at the sample rate with 16-bits per sample if (!i2s.begin(mode, sampleRate, bps, slot)) { Serial.println("Failed to initialize I2S!"); - while (1); // do nothing + while (1) + ; // do nothing } } @@ -61,7 +66,9 @@ void loop() { } i2s.write(sample); // Right channel + i2s.write(sample >> 8); i2s.write(sample); // Left channel + i2s.write(sample >> 8); // increment the counter for the next sample count++; From 6a9d40379cd2664246edaf3792be2dd00d90a193 Mon Sep 17 00:00:00 2001 From: greenyleaf Date: Sat, 8 Mar 2025 20:17:54 +0800 Subject: [PATCH 2/4] edit the I2S - simple tone example --- libraries/ESP_I2S/examples/Simple_tone/Simple_tone.ino | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/libraries/ESP_I2S/examples/Simple_tone/Simple_tone.ino b/libraries/ESP_I2S/examples/Simple_tone/Simple_tone.ino index 6c8e444edcf..e866c513286 100644 --- a/libraries/ESP_I2S/examples/Simple_tone/Simple_tone.ino +++ b/libraries/ESP_I2S/examples/Simple_tone/Simple_tone.ino @@ -30,6 +30,11 @@ #include +// In fact, the GPIO pins are not fixed, most other pins can be used for the I2S function. +#define LRC 25 +#define BCLK 5 +#define DIN 26 + const int frequency = 440; // frequency of square wave in Hz const int amplitude = 500; // amplitude of square wave const int sampleRate = 8000; // sample rate in Hz @@ -49,7 +54,7 @@ void setup() { Serial.begin(115200); Serial.println("I2S simple tone"); - i2s.setPins(5, 25, 26); + i2s.setPins(BCLK, LRC, DIN); // start I2S at the sample rate with 16-bits per sample if (!i2s.begin(mode, sampleRate, bps, slot)) { From b4f5688fefd0c179cc770aeca666191fae48b767 Mon Sep 17 00:00:00 2001 From: greenyleaf Date: Sat, 8 Mar 2025 20:29:05 +0800 Subject: [PATCH 3/4] edit the I2S - simple tone example --- libraries/ESP_I2S/examples/Simple_tone/Simple_tone.ino | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/libraries/ESP_I2S/examples/Simple_tone/Simple_tone.ino b/libraries/ESP_I2S/examples/Simple_tone/Simple_tone.ino index e866c513286..edc122750e9 100644 --- a/libraries/ESP_I2S/examples/Simple_tone/Simple_tone.ino +++ b/libraries/ESP_I2S/examples/Simple_tone/Simple_tone.ino @@ -31,9 +31,9 @@ #include // In fact, the GPIO pins are not fixed, most other pins can be used for the I2S function. -#define LRC 25 -#define BCLK 5 -#define DIN 26 +#define I2S_LRC 25 +#define I2S_BCLK 5 +#define I2S_DIN 26 const int frequency = 440; // frequency of square wave in Hz const int amplitude = 500; // amplitude of square wave @@ -54,7 +54,7 @@ void setup() { Serial.begin(115200); Serial.println("I2S simple tone"); - i2s.setPins(BCLK, LRC, DIN); + i2s.setPins(I2S_BCLK, I2S_LRC, I2S_DIN); // start I2S at the sample rate with 16-bits per sample if (!i2s.begin(mode, sampleRate, bps, slot)) { From fa899cde5208708dc6bb43d40ea3612d5527f23f Mon Sep 17 00:00:00 2001 From: greenyleaf Date: Sat, 22 Mar 2025 15:34:09 +0800 Subject: [PATCH 4/4] some edit --- .../ESP_I2S/examples/Simple_tone/Simple_tone.ino | 13 ++++++++----- 1 file changed, 8 insertions(+), 5 deletions(-) diff --git a/libraries/ESP_I2S/examples/Simple_tone/Simple_tone.ino b/libraries/ESP_I2S/examples/Simple_tone/Simple_tone.ino index edc122750e9..8ade700f647 100644 --- a/libraries/ESP_I2S/examples/Simple_tone/Simple_tone.ino +++ b/libraries/ESP_I2S/examples/Simple_tone/Simple_tone.ino @@ -24,13 +24,13 @@ 2nd September 2021 Lucas Saavedra Vaz (lucasssvaz) 22nd December 2023 - anonZ + anon 10nd February 2025 */ #include -// In fact, the GPIO pins are not fixed, most other pins can be used for the I2S function. +// The GPIO pins are not fixed, most other pins can be used for the I2S function. #define I2S_LRC 25 #define I2S_BCLK 5 #define I2S_DIN 26 @@ -43,7 +43,7 @@ i2s_data_bit_width_t bps = I2S_DATA_BIT_WIDTH_16BIT; i2s_mode_t mode = I2S_MODE_STD; i2s_slot_mode_t slot = I2S_SLOT_MODE_STEREO; -const int halfWavelength = (sampleRate / frequency / 2); // half wavelength of square wave +const int halfWavelength = sampleRate / frequency / 2; // half wavelength of square wave int32_t sample = amplitude; // current sample value unsigned int count = 0; @@ -70,9 +70,12 @@ void loop() { sample = -1 * sample; } - i2s.write(sample); // Right channel + // Right channel, low 8 bit then hight 8 bit + i2s.write(sample); i2s.write(sample >> 8); - i2s.write(sample); // Left channel + + // Left channel, low 8 bit then hight 8 bit + i2s.write(sample); i2s.write(sample >> 8); // increment the counter for the next sample