Mozzi  version v2.0
sound synthesis library for Arduino
Mozzi on ESP32-based boards.

port by Dieter Vandoren, Thomas Friedrichsmeier and Thomas Combriat

Port status and notes

  • Since flash memory is not built into the ESP32, but connected, externally, it is much too slow for keeping wave tables, audio samples, etc. Instead, these are kept in RAM on this platform.
  • Asynchronous analog reads are not implemented. mozziAnalogRead() relays to analogRead(). MOZZI_AUDIO_INPUT is not implemented
  • twi_nonblock is not ported
  • WIFI-activity not yet tested, but likely the same notes as for ESP8266 apply, i.e. any Wifi activity is likely to introdcue considerable nose. Consider turning off WIFI.
  • The implementation of audioTicks() may be slightly inaccurate on this platform.

Output modes

The following audio modes (see MOZZI_AUDIO_MODE) are currently supported on this hardware:

  • MOZZI_OUTPUT_EXTERNAL_TIMED
  • MOZZI_OUTPUT_EXTERNAL_CUSTOM
  • MOZZI_OUTPUT_PDM_VIA_I2S
  • MOZZI_OUTPUT_I2S_DAC
  • MOZZI_OUTPUT_INTERNAL_DAC
  • MOZZI_OUTPUT_PWM

The default mode is MOZZI_OUTPUT_INTERNAL_DAC for the ESP32 (that has a built-in DAC) and MOZZI_OUTPUT_PWM for all other ESP32 flavours.

MOZZI_OUTPUT_INTERNAL_DAC

The internal DAC has 8 bit resolution, and outputs to GPIO pins 25 and 26 (non-configurable). For simplicity of code, both pins are always used. In a mono configuration, both pins output the same sample.

TODO: We could really use this to hack in a 2 PIN mode!

Note
The number 25 refers to "GPIO 25" or sometimes labelled "D25". Confusingly, many boards come with an additional, totally different numbering scheme on top of that. Check a detailed pinout diagram, if in any doubt.

Internally, the inbuilt DAC is connected via an I2S interface. Which interface number to use can be configured using:

#define MOZZI_I2S_PORT ... // (default: I2S_NUM_0)

MOZZI_OUTPUT_I2S_DAC

This mode outputs to a PT8211 (or compatible) I2S DAC, which allows for very high quality (mono or stereo) output. By default this mode outputs a plain I2S signal, with BCK, WS and DATA lines defined (but configurable, see after), which is equivalent to defining:

#define MOZZI_I2S_FORMAT MOZZI_I2S_FORMAT_PLAIN

The mode can be changed to LSBJ format (the PT8211 DAC for instance uses this format) by using:

#define MOZZI_I2S_FORMAT MOZZI_I2S_FORMAT_LSBJ

In addition to the three pins referred above, MCLK pin, which is of use in some DAC can be defined. Presumably, any pins qualify, and you can configure this using:

#define MOZZI_I2S_PIN_BCK ... // (default: 26)
#define MOZZI_I2S_PIN_WS ... // (default: 15)
#define MOZZI_I2S_PIN_DATA ... // (default: 33)
#define MOZZI_I2S_PIN_MCLK ... // (default: UNDEFINED)
#define MOZZI_I2S_PORT ... // (default: I2S_NUM_0)

For further customization, the signals outputted on some of these pins can be inverted, to accomodate DAC that would not fall into the standard, by using:

#define MOZZI_I2S_BCK_INV ... // (default: 0)
#define MOZZI_I2S_WS_INV ... // (default: 0 for PLAIN format, 1 for LSBJ)
#define MOZZI_I2S_MBCK_INV ... // (default: 0)

MOZZI_OUTPUT_PDM_VIA_I2S

This mode uses the same setup as MOZZI_OUTPUT_I2S_DAC, but rather than using an external DAC, the communication signal itself is modulated in PDM (pulse density modulation) encoded form. Thus not extra hardware is needed, and the signal is output on the DATA pin (see above). The BCK and WS pins are also claimed, but should be left non-connected, and do not produce anything meaningful. This can only be used in mono mode.

Output resolution may be adjusted by defining MOZZI_PDM_RESOLUTION , where the default value of 4 means that each audio sample is encoded into four 32 bit blocks of ones and zeros. Obviously, more is potentially better, but at the cost of considerable computation power.

MOZZI_OUTPUT_PWM

This mode outputs a standard PWM signal (10 bits) cadenced at MOZZI_AUDIO_RATE. Allegedly, any pin should qualify but by default it is set to GPIO 15 (left/mono) and GPIO 16 (right). They can be customized by using:

#define MOZZI_AUDIO_PIN_1 ... // (default: 15)
#define MOZZI_AUDIO_PIN_2 ... // (default: 16)

MOZZI_OUTPUT_EXTERNAL_TIMED and MOZZI_OUTPUT_EXTERNAL_CUSTOM

See External audio output