-
Notifications
You must be signed in to change notification settings - Fork 7.1k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
A2DP audio glitches due to zero packets in output (IDFGH-1086) #3407
Comments
Hi @steenou, I found this line in your log that indicates you are using the PLL_D2 clock and the i2s real rate is 44642Hz which is higher than the expected 44100Hz:
As far as I know, when i2s output clock is faster than the a2dp audio output speed, there will be some glitches after sometime, because the i2s tx buffer is underflow and the new data is not ready. My issue #3380 is just the opposite, I am using the APLL clock and the i2s real rate is lower than the expected one:
That causes |
Hi @redchenjs, the workaround you proposed eliminates indeed the packet drops that happen in the middle of the streaming. However, the zeroes described by @steenou appear only during the first few seconds after hitting play. From that moment on, sound works as expected. Is this happening to you or do you hear no glitches at any time? In case it's helpful, we are using 32 bit samples (the APLL has been configured accordingly), |
@crespum I logged my i2s output by a logic analyzer, and yeah this problem did happen to me everytime at the first few seconds. So the inaccuracy of i2s clock only causes problems during the streaming, not at the beginning. |
Good to know you have the same issue as me and @crespum. So all of us still have an issue that is tied to the DMA buffer when beginning a BT stream. @redchenjs I've been digging in the IDF a bit and I think I've found the code related to the I2S DMA buffer there. Do you know print the logs with I2S_TAG log level? We might see the glitching coincide with a log with the I2S tag... |
@steenou We can set the Log output level in |
I've tested IDF 3.0, 3.0.2 and current master (df61612) and there are cut outs at the beginning of the streaming in all of them. |
I confirm I have the same issue |
The same glitch pattern occurs for me later on in the stream as well. It sounds like glitching, and can be triggered by increasing the distance between the phone and the ESP or when a different phone nearby goes in and out of airplane mode. There is no |
@pad52 @redchenjs do you have glitching sounds occurring during playback? Not only in the beginning? |
@steenou yeah, it's very similar to what I've been through before, it's caused by the inaccurate i2s clock. |
But I've applied your I2S patch and am still getting the glitches |
@steenou Which I2S clock are you using now, PLL_D2 or APLL? This patch only works with APLL@44100Hz, 16bit, 2ch, chip Rev.1 |
@steenou Yes, I still have some glitches sometimes,
With the spectrum anyway I can see some glitches sometimes, but not audibile. |
Thanks @pad52. Our environment is a bit different, but we've modified the workaround accordingly. We use APLL@44100Hz, 16 data bits over 32 bits clock, 2ch, chip Rev.1. if (sample_rate == 44100) {
// see https://github.com/espressif/esp-idf/issues/3380
rtc_clk_apll_enable(1, 28, 8, 5, 2);
ESP_LOGW(
BT_AV_TAG,
"Workaround for I2S APLL clock at 44100Hz 32-bit 2ch");
} DMA buffers are: .dma_buf_count = 128;
.dma_buf_len = 64; |
Well it happens with anything - of course with a tone it's easier to listen to it. As @redchenjs wrote you need to tune the I2S Clock according to what are your needs.. My enviroment is: tune this settings according to your clock generator (in case your esp is slave like mine) |
Just out of curiosity, where does your MCLK come from? According to the technical reference, the ESP32 does not support architectures where MCLK comes from an external XTAL (quite a constrain IMHO):
|
Thanks @crespum that's really an interesting note! |
Hey @pad52, what values do you use in rtc_clk_apll_enable()? |
APLL: Slave MCLK: More relevantly then: I'm willing to bet though if you ran two ESP32's BT tx/rx in slave mode from the same I2S BCLK source or one master and one slave (no MCLK connection just bit clock LR clock and your data) you'd have perfect sync - and if not you are dropping BT packets. |
Thanks @fknrdcls. Have you also experienced glitches the first few seconds after the playback starts? |
@crespum from memory when I tried the A2DP sink code example it behaved exactly as you described. I remember tweaking the sample-rate manually to attempt to match the data input rate (we're talking 10s of Hz) and it made it better not perfect, as I said above I decided the problem was just the mismatch in the audio sampling rate on the source device and playback rate. If the ESP32 had AAC or aptx I likely would have stuck it out and fixed it, but it's just not worth it for A2DP. Also as I alluded to above, the bluetooth stack must all be complete and therefore should handle the playback as expected, there might be something missing or broken in the sample code or the drivers. I'm not sure how we're supposed to take the ADF seriously if (and I haven't checked recently so I may be mistaken) if the I2S driver still can't set the APLL with correct values. Frankly the ESP32 shouldn't be considered for bluetooth audio, A2DP is just not good enough, and AAC decode should be possible and I might be willing to bother porting if we're given proper ISA documentation. Decode would be fine in 32bit float with the tight loops rolled out into optimised assembly to take advantage of the multiply-add multiply-subtract and fine tune the float coprocessor pipelining. |
Hi guys Firstly, fixed inaccurate i2s MCLK modification will be available soon. |
Hello, how do you implement the connection to the Bluetooth headset? I use ESP32 to connect to the Bluetooth headset and always stuck in "esp_a2d_source_connect". I just modified the official example a2dp_source. But connecting a computer can be, that is, connecting a Bluetooth headset does not work. Sometimes it suddenly crashes and then restarts and suddenly connects. |
Environment
Problem Description
Using A2DP sink code examples results in glitches in the output. When putting a logic analyser on the I2S outputs of my ESP32-WROVER, I can see the glitches occur due to zeroes in the I2S data line. There is a pattern of zero packets on Left and Right data, always followed by 5 data packets (always Left Right Left Right Left), followed by zeroes again. These zeroes and data packets reoccur a couple times:
Sometimes there is a period of only zeroes after
The amount of clock cycles of the LRCLK (WS) during zeroes and during data is always the same as
i2s_config.dma_buf_len
which is declared in I2S.cpp. For example if I doi2s_config.dma_buf_len = 8
, I see 5½ samples of zeroes, 2½ samples of data:This is true for any value of
i2s_config.dma_buf_len
. n- 2½ samples of zeroes, 2½ samples of data, reoccurring.The moments these glitches occur seem to occur when a Bluetooth A2DP stream has just started and randomly throughout the stream too. The glitches seem to occur both when the ESP32 is the I2S master and when the ESP32 is the I2S slave, although it seems to occur less during the stream when the ESP32 is the I2S master.
When the stream is paused, and resumed quickly after (maybe within a window of 3 seconds), there will be no glitches in the output. When the stream is paused, and you wait until
I (80520) BT_AV: A2DP audio state: Suspended
has been written in the log, and then resume the stream (e.g. by pressing play again), you will hear glitches in the output.Expected Behavior
Smooth audio when running ESP example code.
Actual Behavior
Glitchy audio.
Code to reproduce this issue
The a2dp_gatts_coex example code in the IDF.
Debug Logs
The text was updated successfully, but these errors were encountered: