-
-
Notifications
You must be signed in to change notification settings - Fork 280
Audio Input and Output
Phil Schatzmann edited this page Dec 10, 2022
·
32 revisions
There are different mechanisms to output sound with Microcontrollers:
Audio Sinks
The stream of PCM audio data can be copied to an audio sink in order to render the sound to a output device:
- I2S: The best quality can be achieved with the help of I2S and an external DAC. I2S is as digital protocol which is supporting 2 channels only. You can then e.g. connect the analog output of the DAC to your Stereo HIFI Amplifier. (example). You can use I2S with the I2SStream class.
- SPDIF: If your audio equipment provides an digital input via SPDIF or Toslink you can feed the digital signal directly. You will need a corresponding connector however. (example)
- Analog/PCM: Some processors are providing an analog output, this is usually an easy and good approach: The number of pins (and herewith output channels) however is usually very limited. (example). This is currently only supported for regular ESP32 using the AnalogAudioStream
- PWM: The last possibility is to simulate an analog output with the help of PWM by using a frequency which is beyond the audible range of 20 KHz. This method is supported by all processors and usually supports a bigger number of output pins. In terms of audio quality this is usually the worst option and it is recommended to use a low pass filter to transform the digital signal into an analog one. (example)
Output Devices
The analog or pwm output is not powerful enough to drive any speakers, but it is sufficient for some
- Earphones
- Piezo Electric Elements
- Speakers
In order to drive some speakers you will need an Audio Amplifier Module (e.g. a class D Audio Amplifier)
If you do do have an amplifier at hand you could try to use a simple motor controller or build your own amplifier using some MOSFETs.
Audio Sources
Here are the most important classes, that can be used for reading audio data
- I2SStream
- AnalogAudioStream
The complete list of the supported Input/Output classes can be found in the class documentation.