Skip to main content

Introduction

Documentation Status Discord Build Status Code Style: Ruff

CircuitPython driver library for Cirrus CS42L42 audio codec

Dependencies

This driver depends on:

Please ensure all dependencies are available on the CircuitPython filesystem. This is easily achieved by downloading the Adafruit library and driver bundle or individual libraries can be installed using circup.

Installing from PyPI

On supported GNU/Linux systems like the Raspberry Pi, you can install the driver locally from PyPI. To install for current user:

pip3 install adafruit-circuitpython-cs42l42

To install system-wide (this may be required in some cases):

sudo pip3 install adafruit-circuitpython-cs42l42

To install in a virtual environment in your current project:

mkdir project-name && cd project-name
python3 -m venv .venv
source .env/bin/activate
pip3 install adafruit-circuitpython-cs42l42

Installing to a Connected CircuitPython Device with Circup

Make sure that you have circup installed in your Python environment. Install it with the following command if necessary:

pip3 install circup

With circup installed and your CircuitPython device connected use the following command to install:

circup install adafruit_cs42l42

Or the following command to update an existing version:

circup update

Usage Example

import time

import audiobusio
import board
import digitalio
import synthio

import adafruit_cs42l42

RATE = 48000
# CircuitPython sends 32 bit clocks per stereo frame, so the bit clock the
# codec sees -- and the number configure_clocks wants -- is 32 x the rate.
SCLK = RATE * 32

# Mixer attenuation, dB. 0 is full scale; the quickstart starts at -20.
DAC_VOLUME = -20

# MIDI note numbers for a C-major scale, C4 up to C5.
SCALE = (60, 62, 64, 65, 67, 69, 71, 72)

# CAUTION: this drives a headphone amplifier. Take the headphones off before
# the first run and confirm the level is comfortable before wearing them.

oscillator = digitalio.DigitalInOut(board.OSC_EN)
oscillator.direction = digitalio.Direction.OUTPUT
oscillator.value = True

reset = digitalio.DigitalInOut(board.CS42_RESET)
reset.direction = digitalio.Direction.OUTPUT
reset.value = False  # active low: hold the codec in reset while clocks start

i2s = audiobusio.I2SOut(board.I2S_BIT_CLOCK, board.I2S_WORD_SELECT, board.I2S_DOUT)

synth = synthio.Synthesizer(sample_rate=RATE)

# Playing the synth starts the bit clock, which the codec needs before it can
# switch off its own oscillator. The synth streams silence until a note is
# pressed, so nothing is audible yet.
i2s.play(synth)

# Constructing the driver releases the reset
codec = adafruit_cs42l42.CS42L42(board.I2C(), reset_pin=reset)
codec.configure_clocks(sclk_hz=SCLK)
codec.configure_asp(sample_rate=RATE, bit_depth=16)

codec.headphone_output = True
codec.dac_volume = DAC_VOLUME

print("playing scale")
try:
    while True:
        for note in SCALE:
            synth.press(note)
            time.sleep(0.25)
            synth.release(note)
            time.sleep(0.125)
finally:
    codec.muted = True
    i2s.stop()
    i2s.deinit()
    reset.value = False
    reset.deinit()
    oscillator.value = False
    oscillator.deinit()

Documentation

API documentation for this library can be found on Read the Docs.

For information on building library documentation, please check out this guide.

Contributing

Contributions are welcome! Please read our Code of Conduct before contributing to help this project stay welcoming.

Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

adafruit_circuitpython_cs42l42-1.0.0.tar.gz (41.4 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

adafruit_circuitpython_cs42l42-1.0.0-py3-none-any.whl (21.6 kB view details)

Uploaded Python 3

File details

Details for the file adafruit_circuitpython_cs42l42-1.0.0.tar.gz.

File metadata

File hashes

Hashes for adafruit_circuitpython_cs42l42-1.0.0.tar.gz
Algorithm Hash digest
SHA256 a2760ece9dc2fafbd5b73af6f44692b756de42af9ba8d2b596be101cf48c1fb9
MD5 4e18697abc8f8643cd81337da4534545
BLAKE2b-256 061ab935f72768e775f3e1bac0f45fc0d9596c7ecaf3e4a9058cd2f2b0956421

See more details on using hashes here.

File details

Details for the file adafruit_circuitpython_cs42l42-1.0.0-py3-none-any.whl.

File metadata

File hashes

Hashes for adafruit_circuitpython_cs42l42-1.0.0-py3-none-any.whl
Algorithm Hash digest
SHA256 71952e8fb3d238b80da1133ed5d13fe2944f16ba65f1edbff01c6ba5db073248
MD5 892739effdf47a865015b5a8ac8a7c58
BLAKE2b-256 77714279b95f2d185ef89010ab40b3993b05dbc9efee402198d222d469cf8035

See more details on using hashes here.

Release history Release notifications | RSS feed

This release

1.0.0 This release

2 files

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page