#419 ESP32 Audio Tutorial with lots of examples

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
a well-kept secret of the esp32 is its extended audio capabilities because it is hard to use luckily i found a library and a toolset enabling us to easily use these capabilities we now can connect microphones loudspeakers bluetooth loudspeakers sd cards and even browsers to an esp32 to record and play sound very handy for all sorts of projects and we will build an internet radio with a single line of code in the loop gritzy youtubers here is the guy with the swiss accent with a new episode and fresh ideas around sensors and microcontrollers remember if you subscribe you will always sit in the first row i wanted to use the esp32 to build digital filters when i discovered this beauty the audio tools library from p schatzman it covers nearly all thinkable combinations of input and output devices and as we will see he uses an extremely simple way of dealing with them streams so in this video we will cover a bit of theory to understand how things work particularly i square s connect an i square s digital to analog converter with a loudspeaker and play sounds generated by the esp32 connect to an mp3 file on the internet and play it through the loudspeaker connect a bluetooth loudspeaker to the esp32 show the waveform of signals in serial plotter connect an isquares analog to digital converter to the esp32 stream the sound recorded by a tiny microphone to a browser somewhere in the internet let's start with the basics the isquare s or inter ic sound interface it is similar to spi and was already introduced in 1986 by philips but unfortunately they created a mess with the naming of the signals each device needs at least three signals the continuous serial clock sck often is named bitclock bclk the word select officially is called word select ws too but you often also find it named left right clock lrclk or framesync fs the third signal officially is called serial data sd but it can be called sddata sdn sd out dac d80 or adc d80 a fourth line sometime is needed the master clock it is usually 256 times the word clock and is needed inside some i square s chips and now we get into shallow waters each device can be act as a master or as whatever name you prefer here you see a few proposals fortunately i can avoid the second name in this video because we use the esp32 always in the master mode in this mode it provides the clocks by the way let's have a first look at phil schatzman's work as you might have guessed he is also a guy with a swiss accent the library's readme gives a good overview it offers audio sources and audio syncs to get sound into the esp or out of it next it offers encoders and decoders to deal with different audio formats like aac mp3 or wav for testing and experimenting it offers a sign generator for processing audio it offers effects like echo and lastly it offers converters and filters converters sometimes are needed if an input and an output device do not support the same format and filters will be covered in another video either on this or my second channel the fast esp32 enables the creation of good digital filters let's try it out i'm curious if it works as described after downloading the zip file and installing the library you find plenty of examples in the arduino ide we will mainly use examples from the streams category for this video and start with the simplest one the stream generator csv you see everything is well organized the first name is where the signal comes from and the second is where it goes csv means that it can be displayed in serial plotter by the way when we look at the sketch we see that there is only one line of code in the loop copier dot copy astonishing this is possible because all definitions are done before all needed objects are defined and in setup the audio locker is installed to provide debugging information warning is the right level for operation and info for a start or if you encounter a problem info provides you with more data on serial but maybe the sketch will no more work properly because serial is too slow next we configure and start the output stream this definition has lots of default values to change however most of them are okay and do not have to be declared here the third step is to start the sine wave generator as an input now the loop only has to copy the data from the input to the output this is probably the shortest loop i have ever seen much shorter than the blink sketch let's check it out yes we see a sine wave with a peak amplitude of 32 000 cool but i promised audio and we do not hear anything so i replace the serial plotter with a built-in dac now we see the sine wave on pin 25 and 26 with a proper amplifier we could drive loudspeakers but we also see that the built-in dac is not useful for audio it only has 8 bits or 256 levels this would not sound very good a better solution is this max 98 357 a amplifier with i square s input it needs three signal lines as described before the default config is described here pins 14 15 and 22. we could change these pins in the configuration if we want this chip does not need a master clock now we hear the sound loud and clear to reduce complexity i used color coding for the wires and created an excel sheet to map the different pin names you find a link in the description the next example uses a url from a swiss radio station it creates an internet radio with only one line of code in the loop incredible of course we could play an mp3 file from our raspberry pi then we would have to place the sound file in the home directory and start this command this command starts a server that can be connected from the esp32 just change the url and it plays the song from the pi i also replaced the max 98357a with this pcm 5102 a board and it worked without changing the sketch here you see that this library also works for the pi pico by the way of course without wi-fi we can now play sounds from several sources but how about recording let's start with the built-in adc i connect a sine wave to gpio 34 and use the streams analog serial example and really we see a sine wave pay attention make sure your input signal is between 0 and 3.3 volts if you do not want to fry your esp32 we could connect an analog microphone to the adc but we would have to add an amplifier to create the needed voltages and we know that the esp32 adcs are not the best a much better solution is to use a small inmp-441 or its successor this microphone has a built-in adc which creates an i square s signal let's try it with a sketch stream i square s csv yes it also works and we see the signal so small and already a microphone cool if we needed a better quality input we could add an adc board that creates an i-square s signal or we could use this combined sound card it contains an adc as well as a dac i leave you a link to a video where this sound card is used to create an analog filter the latter two boards need a master clock which can create some difficulties the default pin for this master clock is gpio zero unfortunately not all esp32 boards expose this pin on the headers fortunately espressif allows the master clock also on gpio1 or gpio3 but pay attention these pins are used for serial gpio1 is a no go in the arduino environment gpio3 can be used as a replacement for gpio0 but you lose the capability to send data to the esp when the sketch runs not a big issue but you have to know it i leave you a link to this sport which has an accessible gpio0 by the way you need at least version 2 of the esp32 framework to change the master clock pin another possibility is to buy such an audio kit it uses a particular esp32 module the esp32-a1s which has a built-in isquare s adc and dac plus the needed peripherals to experiment with audio phil also added a lot of examples for this sport you find many more examples in the library the last one i will show in this video is this one it is the simplest baby monitor i ever saw one line of code in the loop and you can listen to the microphone's input via wi-fi in a browser here is the guy with a swiss accent here is the guy with the swiss accent i tried it with chrome an explorer on a pc and it worked unfortunately safari on the iphone did not work did i forget something of course the bluetooth speaker let's try to connect this small backer and try it with the i square s microphone i add its name to the sketch and try to connect yesterday it worked but today i have no luck and it only connects and disconnects so phil has some work to do hopefully he will be successful because this would be a useful functionality summarized i square s is the primary protocol for audio applications it works similar to spi and is supported by the esp-32 it needs three or four signals a clock a left-right signal a data signal and sometimes also a master clock the esp32 is always the master in the examples used in this video the audio tools library makes it extremely easy to experiment with audio and i-square-s it has plenty of examples to try all examples have a sound input and the sound output they either are connected by a direct copy function or in the case of filters or effects a manipulation is inserted on the way from the source to the output as sources we tried an internal sign generator the built-in adc a tiny i-square-s microphone and a sound card we also were able to play mp3 from a web server and the coolest by connecting to a stream of a radio station we were able to create an internet radio as outputs we used the serial plotter an i-square s amplifier with a loudspeaker and a dac for a headphone in addition i tried to connect a bluetooth speaker but unfortunately it did not work anymore and entirely unexpected for me we created a baby monitor using an i-square s microphone and streamed its sound to a browser now it's up to you to create your project there are no more excuses like audio is complicated this was all for today as always you find the relevant links in the description i hope this video was useful or at least interesting for you if true please consider supporting the channel to secure its future existence thank you bye
Info
Channel: Andreas Spiess
Views: 214,841
Rating: undefined out of 5
Keywords:
Id: a936wNgtcRA
Channel Id: undefined
Length: 13min 48sec (828 seconds)
Published: Sun Mar 06 2022
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.