codec – CodeX audio CODEC driver library

Audio driver functions for CodeX. This provides a Python interface to the CodeX ES8311 CODEC (enCOder-DECoder) chip

See soundlib module for a higher-level audio interface.

class codec.Codec(io_expander, power, sample_freq=16000)[source]

Access to the audio system of the CodeX (play and record)

Initialize ES8311 codec, defaults to DAC mode

enable_amp(do_enable)[source]
Enable the audio amplifier for the onboard speaker.

Allows you to turn OFF the speaker if desired, with headphones jack still active.

Parameters

do_enable (bool) – True to enable or False to disable

enable_dac_equalizer(do_enable)[source]

Enable the output-side (DAC) equalizer in the ES8311 CODEC.

Parameters

do_enable (bool) – True to enable or False to disable

get_chip_id()[source]

Get the chip ID of ES8311

get_volume()[source]

Get the output audio volume

Returns

volume from 0 to 100 percent

Return type

int

initialize()[source]

Enable the audio system and the speaker.

You can perform these steps manually for fine tuning, but this is a quick way to get audio going.

is_amp_enabled()[source]

Return the status of the audio amplifier.

Returns

True if the amplifier is enabled

Return type

bool

is_phones()[source]

Are headphones plugged in?

Returns

True if headphones are detected

Return type

bool

make_pitch_func(func_type=3)[source]

Create and set sample function for basic ‘pitch()’ tone generation. Default to triangle waveform, with lovely harmonics so it’s louder on CodeX little speaker than a pure sine wave.

mic_gain(pga=16, adc=191)[source]

Set the microphone gain

Parameters
  • pga (int) – PGA gain

  • adc (int) – ADC volume

mp3(filename)[source]

Play given MP3 file.

Parameters

filename (str) – Path and name of file (.mp3 extension is optional)

off()[source]

Stop ALL output to speaker and headphones.

Note: For “beeping” on and off it will sound better to use the FuncSample returned

from the ‘pitch()’ function. Or see ‘soundlib’ module for more advanced features.

pitch(freq, dur)[source]

Play a tone on the speaker, with the specified frequency.

Parameters
  • freq (int) – Frequency in Hertz

  • dur (float) – Duration in seconds

Returns

a sample you can optionally control with env_attack(), env_release(), etc.

Return type

FuncSample

playbuf(buf, sample_freq=16000, loop=False)[source]

Play ‘buf’ via I2SOut. Works with array returned from record(), or any 16-bit array you create using array.array(“h”) and fill with samples.

record(duration=2.0, sample_freq=16000, pga_gain=18, adc_gain=208)[source]

Record from ADC (microphone), returning a 16-bit array of samples.

Note: All samples must fit in a single RAM buffer, so be careful with RAM management.

(ESP32S2 WROVER module has 2M bytes of RAM available total)

set_bass_level(gain)[source]

Adjust the low-frequency (bass) audio gain for the audio output.

Parameters

gain (integer) – 0 to -26 dB

set_volume(volume)[source]

Set the output audio volume

Parameters

volume (int) – volume from 0 to 100 percent

wav(filename)[source]

Play given WAV file

Parameters

filename (str) – Path and name of file (.wav extension is optional)

codec.FT_ORGAN = 5
codec.FT_RANDOM = 4
codec.FT_SAWTOOTH = 2
codec.FT_SINE = 0
codec.FT_SQUARE = 1
codec.FT_SYNTH = 6
codec.FT_TRIANGLE = 3
codec.MOD_AMPLITUDE = 1
codec.MOD_FREQUENCY = 0

Download Source