miketeachman / micropython-esp32-i2s-examples

Usage and examples for I2S support on the ESP32 microcontroller
MIT License
156 stars 29 forks source link

Code examples #10

Closed TomaszBarc closed 4 years ago

TomaszBarc commented 4 years ago

Hi Mike,

Thanks to you and all involved for a great job.

I just started programming and I have a question, as I am not sure if it is a mistake or I don't understand something.

In the example code: [(https://github.com/miketeachman/micropython-esp32-i2s-examples/blob/master/examples/play-stereo-wav-from-internal-flash.py)]

In lines: 46 and 64 shouldn't be: wav.seek(44), instead of pos = wav.seek(44), in order to work properly ?

Would you be able to produce the code how to play a *.wav file exactly once ?

Is there any way to immediately stop playing a *.wav file ? When I play and leaving the loop, then I can hear buffering. I would like to stop cleanly playback.

I use Adafruit I2S 3W Class D Amplifier Breakout - MAX98357A

Kind regards,

Tom

miketeachman commented 4 years ago

Hi Tom, @TomaszBarc Congratulations on getting started with MicroPython programming and also for asking questions !

The pos is added only to stop the text "44" from being displayed when testing this code using the REPL -- there should be no affect on the seek() operation.

To play the file once, I recommend replacing line 63 with a break statement. That should cause the while loop to exit when the first end-of-file has been detected (if num_read == 0:).

For stopping playback immediately: First some background: When the write() method is stopped, the I2S hardware on the ESP32 will continue to play audio samples that remain in the DMA buffer. One strategy is to reduce the time it will continue playing by making the DMA buffers smaller. Amount of buffering = dmacount * dmalen. You will need to experiment. If the buffering is reduced too much the risk of gaps in the playback increases.

Here is another idea on stopping playback. You might try adding audio_out.deinit() at the point in the code where you want the audio playback to stop. I have not tried this, but it might immediately stop the I2S feature from sending out the clock signal and data to the amplifier? If you try this method please let me know if it works or not. thanks :)

miketeachman commented 4 years ago

No response from user after implementation suggestions were given. Assuming that these suggestions solved the issue.