Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

streams-i2s-i2s does not work with 16 bits #186

Answered by pschatzmann
wimverlindensa asked this question in Q&A
Discussion options

Hi, I tried
streams-I2S > serial plotter : OK
streams-generator-I2s : OK

Without changing pins I then tried streams-i2s-i2s example found in Arduino IDE and adjusted the pins, but it seems not to work, the mic is not forwarded to the DAC PCM102, I hear only noise at the speakers.
Please confirm the sketch is correct and was tested. What can be wrong> Thank for help!

/**

#include "AudioTools.h"

uint16_t sample_rate=44100;
uint16_t channels = 2;
I2SStream in;
I2SStream out;
StreamCopy copier(out, in); // copies sound into i2s

// Arduino Setup
void setup(void) {
// Open Serial
Serial.begin(115200);
// change to Warning to improve the quality
//AudioLogger::instance().begin(Serial, AudioLogger::Info);

// start I2S in
Serial.println("starting I2S...");
auto config_in = in.defaultConfig(RX_MODE);
config_in.sample_rate = sample_rate;
config_in.bits_per_sample = 16;
config_in.i2s_format = I2S_STD_FORMAT;
config_in.is_master = true;
config_in.port_no = 0;
config_in.pin_ws = 27; //14;
config_in.pin_bck = 12; //15;
config_in.pin_data = 32; //13;
// config_in.fixed_mclk = sample_rate * 256
// config_in.pin_mck = 2
in.begin(config_in);

// start I2S out
auto config_out = out.defaultConfig(TX_MODE);
config_out.sample_rate = sample_rate;
config_out.bits_per_sample = 16;
config_out.i2s_format = I2S_STD_FORMAT;
config_out.is_master = true;
config_out.port_no = 1;
config_out.pin_ws = 15; // 17; // LR_CLK
config_out.pin_bck = 14; //18; //
config_out.pin_data = 22; //19;
out.begin(config_out);

Serial.println("I2S started...");
}

// Arduino loop - copy sound to out
void loop() {
copier.copy();
}

You must be logged in to vote

I noticed that this microphone does not always provide proper data with 16 bits, so I suggest that you try to change the bits per sample to 32 both on the input and output side and I am surprised that you did not catch this when you tested the input.

You can reproduce the issue if you display the 16 bit data as CSV. I am getting proper values on channel 0, but invalid values (instead of 0) on channel 1: this is creating the noise!

11:52:54.164 -> -4, 0
11:52:54.164 -> -4, 0
11:52:54.164 -> -4, 0
11:52:54.164 -> -9, -32768
11:52:54.164 -> -9, -32768
11:52:54.164 -> -20, 0
11:52:54.164 -> -20, 0
11:52:54.164 -> -24, 0
11:52:54.164 -> -24, 0
11:52:54.164 -> -21, -16384
11:52:54.164 -> -21, -...

Replies: 5 comments 1 reply

Comment options

This should work - though I and most others usually use the 1 port approach with sharing the ws and bck pins (see https://www.youtube.com/watch?v=D0EUDRo4Mx0)

I suggest to split your problem up and that you test the input and output separately with these settings to figure out where the issue is using

  • output to CsvStream to test input
  • output of sine tone to test output
You must be logged in to vote
1 reply
Comment options

That test I made and that worked :
streams-I2S > serial plotter : OK
streams-generator-I2s : OK
Then I kept the same connections and changed the pins in the sketch i2s to i2s with config_in.pin and config_out.pin, but no sound of mic was forwarded to the dac.

Comment options

Did you really use the exact same I2S definitions in both of your tests like in the copy example ?

Yes I checked it and tried it again, I have only noise no sound of the mic and a little noise every time the Serial.print is sended.

I can use any pins for I2S right?

starting I2S...
[I] I2SConfig.h : 89 - rx/tx mode: RX
[I] I2SConfig.h : 90 - port_no: 0
[I] I2SConfig.h : 91 - is_master: Master
[I] I2SConfig.h : 92 - sample rate: 44100
[I] I2SConfig.h : 93 - bits per sample: 16
[I] I2SConfig.h : 94 - number of channels: 2
[I] I2SConfig.h : 95 - i2s_format: I2S_STD_FORMAT
[I] I2SConfig.h : 97 - auto_clear:0
[I] I2SConfig.h : 104 - buffer_count:10
[I] I2SConfig.h : 105 - buffer_size:512
[I] I2SConfig.h : 109 - pin_bck: 12
[I] I2SConfig.h : 110 - pin_ws: 27
[I] I2SConfig.h : 111 - pin_data: 32
[I] I2SConfig.h : 89 - rx/tx mode: TX
[I] I2SConfig.h : 90 - port_no: 1
[I] I2SConfig.h : 91 - is_master: Master
[I] I2SConfig.h : 92 - sample rate: 44100
[I] I2SConfig.h : 93 - bits per sample: 16
[I] I2SConfig.h : 94 - number of channels: 2
[I] I2SConfig.h : 95 - i2s_format: I2S_STD_FORMAT
[I] I2SConfig.h : 97 - auto_clear:1
[I] I2SConfig.h : 104 - buffer_count:10
[I] I2SConfig.h : 105 - buffer_size:512
[I] I2SConfig.h : 109 - pin_bck: 14
[I] I2SConfig.h : 110 - pin_ws: 15
[I] I2SConfig.h : 111 - pin_data: 22
I2S started...

You must be logged in to vote
0 replies
Comment options

Hi Phil,

After more than 1 month I still not get it working with your library, with micropython it took me 1h to do it. With your library the speaker produces some noise but not the sound captured by the I2S mic.
It is frustrating because I have no idea where to start looking and what to do to get it working.
I use I2C INMP441 MIC to capture the sound and forward it to the I2S DAC MAX98357A, all is connected as printed below and pin SD of MAX is open which should mix L and R.

What can I do to solve this?

My project's purpose is to forward I2C sound captured by the INMP441 mic to the DAC MAX98357A try to avoid oscillation and add a digital volume control and a led indicator that indicates when someone is speaking.

Is it normal that in both configurations config_in.is_master = true;? I would expect both devices are slaves and ESP32 is master?

Can you help to solve this please?

In micropython this works
samples = bytearray(4096)

while True:

read_bytes = audio_in.readinto(samples)
# amplify the signal to make it more audible
I2S.shift(buf=samples, bits=16, shift=3)
#time.sleep(0.1)
audio_out.write(samples[:read_bytes])

Below is what is printed in the serial montior

Thanks for your help in advance!

starting I2S...
[I] I2SConfig.h : 89 - rx/tx mode: RX
[I] I2SConfig.h : 90 - port_no: 0
[I] I2SConfig.h : 91 - is_master: Master
[I] I2SConfig.h : 92 - sample rate: 44100
[I] I2SConfig.h : 93 - bits per sample: 16
[I] I2SConfig.h : 94 - number of channels: 2
[I] I2SConfig.h : 95 - i2s_format: I2S_STD_FORMAT
[I] I2SConfig.h : 97 - auto_clear:0
[I] I2SConfig.h : 104 - buffer_count:10
[I] I2SConfig.h : 105 - buffer_size:512
[I] I2SConfig.h : 109 - pin_bck: 15
[I] I2SConfig.h : 110 - pin_ws: 14
[I] I2SConfig.h : 111 - pin_data: 16
[I] I2SConfig.h : 89 - rx/tx mode: TX
[I] I2SConfig.h : 90 - port_no: 1
[I] I2SConfig.h : 91 - is_master: Master
[I] I2SConfig.h : 92 - sample rate: 44100
[I] I2SConfig.h : 93 - bits per sample: 16
[I] I2SConfig.h : 94 - number of channels: 2
[I] I2SConfig.h : 95 - i2s_format: I2S_STD_FORMAT
[I] I2SConfig.h : 97 - auto_clear:1
[I] I2SConfig.h : 104 - buffer_count:10
[I] I2SConfig.h : 105 - buffer_size:512
[I] I2SConfig.h : 109 - pin_bck: 18
[I] I2SConfig.h : 110 - pin_ws: 17
[I] I2SConfig.h : 111 - pin_data: 19
I2S started...
[I] AudioCopy.h : 121 - StreamCopy::copy 1024 -> 1024 -> 1024 bytes - in 1 hops
[I] AudioCopy.h : 121 - StreamCopy::copy 1024 -> 1024 -> 1024 bytes - in 1 hops
[I] AudioCopy.h : 121 - StreamCopy::copy 1024 -> 1024 -> 1024 bytes - in 1 hops
[I] AudioCopy.h : 121 - StreamCopy::copy 1024 -> 1024 -> 1024 bytes - in 1 hops

You must be logged in to vote
0 replies
Comment options

I noticed that this microphone does not always provide proper data with 16 bits, so I suggest that you try to change the bits per sample to 32 both on the input and output side and I am surprised that you did not catch this when you tested the input.

You can reproduce the issue if you display the 16 bit data as CSV. I am getting proper values on channel 0, but invalid values (instead of 0) on channel 1: this is creating the noise!

11:52:54.164 -> -4, 0
11:52:54.164 -> -4, 0
11:52:54.164 -> -4, 0
11:52:54.164 -> -9, -32768
11:52:54.164 -> -9, -32768
11:52:54.164 -> -20, 0
11:52:54.164 -> -20, 0
11:52:54.164 -> -24, 0
11:52:54.164 -> -24, 0
11:52:54.164 -> -21, -16384
11:52:54.164 -> -21, -16384
11:52:54.164 -> -17, -32768
11:52:54.164 -> -17, -32768
11:52:54.164 -> -13, -16384
11:52:54.164 -> -13, -16384
11:52:54.164 -> -8, 0

If you want to stick with 16bits you can use the ConverterFillLeftAndRight as follows:

ConverterFillLeftAndRight<int16_t> filler(RightIsEmpty);
...
copier.copy(filler);

This copies the values from channel 0 to channel1, so the invalid values are overwritten. But please double check that with your pin assignments the same channel is filled with the proper data!

It is also a good idea to reduce the sample rate as you did in the Python example.
And do not forget to change the log level to Warning if you see that the copy is working!

ps. your microphone test to CSV was most likely also done with 32 bits....

You must be logged in to vote
0 replies
Answer selected by pschatzmann
Comment options

Good news, when I change from sample_rate = 44100 to sample_rate = 16000 it works fine, with bits_per_sample 16 or 32, but the sound is very quiet. Will try to increase the volume now. Thanks! Van: Phil Schatzmann ***@***.***> Verzonden: vrijdag 27 mei 2022 10:13 Aan: pschatzmann/arduino-audio-tools ***@***.***> CC: wimverlindensa ***@***.***>; Author ***@***.***> Onderwerp: Re: [pschatzmann/arduino-audio-tools] streams-i2s-i2s not works (Discussion #186) I noticed that this microphone does not always provide proper data with 16 bits, so I suggest that you try to change the bits per sample to 32 both on the input and output side. You can reproduce the issue if you display the 16 bit data as CSV. It is also a good idea to reduce the sample rate as you did in the Python example. And do not forget to change the log level to Warning if you see that the copy is working... — Reply to this email directly, view it on GitHub <#186 (comment)> , or unsubscribe <https://github.com/notifications/unsubscribe-auth/AHHQX7AFSM3HOA4FA7JNVNLVMB7YTANCNFSM5UY4CMPQ> . You are receiving this because you authored the thread.Message ID: ***@***.***>
You must be logged in to vote
0 replies
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
invalid This doesn't seem right

AltStyle によって変換されたページ (->オリジナル) /