Skip to content

Audio Input and Output

Phil Schatzmann edited this page Sep 10, 2024 · 31 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. . You can use I2S with the I2SStream class. (See example)
  • I2S with an Audio Chip: Some I2S Audio chips need to be configured dynamically to work. The I2SCodecStream or AudioBoardStream classes are used for this scenario.
  • TDM: TDM is similar to I2S with the difference that it supports more then 2 channels.
  • SPDIF: If your audio equipment provides a digital input via SPDIF or Toslink you can feed the digital signal directly. You will need a corresponding connector however. Use the SPDIFStream:(See 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
  • PDM: Pulse Density Modulation is an alternative way to "simulate" an analog output. Since the newer ESP32 chips do not provide an Analog output via I2S this is the perfect replacement. On the ESP32 you can use PDM with the I2SStream class, by setting PDM to the signal_type in the configuration.
  • R-2R: You can build your own DAC with some simple resistors. The DAC is driven by n-digital pins: so with e.g. 4 pins you get a 4bit DAC. This functionality is provided by the R2ROutput class. (example)
  • 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. This functionality is provided by the PWMAudioOutput (example)

Output Devices

The analog or pwm output is not powerful enough to drive any speakers, but it is sufficient for some

  • Earphones

Earphones

  • Piezo Electric Elements

Piezo Element

  • 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: Reading the data from a DAC or Microphone that has an I2S interface
  • AnalogAudioStream: Reading analog data.
  • URLStream: Reading the data from the internet
  • File: Reading the data from a file

The complete list of the supported Input/Output classes can be found in the class documentation.

Clone this wiki locally