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 codex.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_volume()[source]
Get the output audio volume
- Returns:
volume from 0 to 100 percent
- Return type:
- 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:
- is_phones()[source]
Are headphones plugged in?
- Returns:
True if headphones are detected
- Return type:
- 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.
- 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.
- 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
- codex.codec.FT_ORGAN = 5
- codex.codec.FT_RANDOM = 4
- codex.codec.FT_SAWTOOTH = 2
- codex.codec.FT_SINE = 0
- codex.codec.FT_SQUARE = 1
- codex.codec.FT_SYNTH = 6
- codex.codec.FT_TRIANGLE = 3
- codex.codec.MOD_AMPLITUDE = 1
- codex.codec.MOD_FREQUENCY = 0