Skip to content

Commit

Permalink
Updated example code to utilize basic driver.
Browse files Browse the repository at this point in the history
  • Loading branch information
dcooperdalrymple committed Sep 2, 2024
1 parent 6299b2b commit 557d8e7
Show file tree
Hide file tree
Showing 5 changed files with 52 additions and 183 deletions.
16 changes: 7 additions & 9 deletions README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -88,15 +88,13 @@ Usage Example
.. code-block:: python
# Monitor Stereo Input: INPUT3 => Output Mixer => Headphones
import board, adafruit_wm8960
codec = adafruit_wm8960.WM8960(board.I2C())
codec.input = True
codec.input3_output = True
codec.input3_output_volume = 0.0
codec.output = True
codec.headphone = True
codec.mono_output = True
codec.headphone_volume = 0.0
import board
from adafruit_wm8960 import Input, WM8960
codec = WM8960(board.I2C())
codec.input = Input.LINE3
codec.gain = 0.5
codec.monitor = 1.0
codec.headphone = 0.5
Documentation
=============
Expand Down
57 changes: 7 additions & 50 deletions examples/wm8960_3d_enhance.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,60 +46,17 @@
"""

import board, time

Check failure on line 48 in examples/wm8960_3d_enhance.py

View workflow job for this annotation

GitHub Actions / test

Multiple imports on one line (board, time)

Check failure on line 48 in examples/wm8960_3d_enhance.py

View workflow job for this annotation

GitHub Actions / test

standard import "import board, time" should be placed before "import board, time"
import adafruit_wm8960
from adafruit_wm8960 import Input, WM8960

codec = adafruit_wm8960.WM8960(board.I2C())

# Setup signal flow to the ADC
codec.mic = True

# Connect from INPUT1 to "n" (aka inverting) inputs of PGAs.
codec.mic_inverting_input = True

# Disable mutes on PGA inputs (aka INTPUT1)
codec.mic_mute = False

# Set input boosts to get inputs 1 to the boost mixers
codec.mic_boost_gain = 0.0
codec.mic_boost = True

# Disconnect LB2LO (booster to output mixer (analog bypass)
# For this example, we are going to pass audio throught the ADC and DAC
codec.mic_output = False

# Connect from DAC outputs to output mixer
codec.dac_output = True

# Set gainstage between booster mixer and output mixer
# For this loopback example, we are going to keep these as low as they go
codec.mic_output_volume = adafruit_wm8960.OUTPUT_VOLUME_MIN

# Enable output mixers
codec.output = True

# Setup sample rate
codec.sample_rate = 44100
codec.master_mode = True
codec.gpio_output = True # Note, should not be changed while ADC is enabled.

# Enable ADCs and DACs
codec.adc = codec.dac = True

# Loopback sends ADC data directly into DAC
codec = WM8960(board.I2C())
codec.loopback = True
codec.input = Input.MIC1
codec.gain = 0.5
codec.volume = 1.0

# Default is "soft mute" on, so we must disable mute to make channels active
codec.dac_mute = False

# Enable Headphone Amp with OUT3 as capless buffer for headphone ground
codec.headphone = True
codec.mono_output = True
codec.headphone_volume = 0.0

# Set 3D enhance depth to max (0.0 - 1.0)
codec.enhance_depth = 1.0
codec.headphone = 0.5

# Toggle 3D enhance on and off
while True:
codec.enhance = not codec.enhance
codec.enhance = 0.0 if codec.enhance else 1.0
time.sleep(2.0)
91 changes: 23 additions & 68 deletions examples/wm8960_automatic_level_control.py
Original file line number Diff line number Diff line change
Expand Up @@ -57,81 +57,36 @@
import board, time
from analogio import AnalogIn
from adafruit_simplemath import map_range
import adafruit_wm8960
from adafruit_wm8960 import Input, WM8960

analog_in = AnalogIn(board.A0)

codec = adafruit_wm8960.WM8960(board.I2C())

# Setup signal flow to the ADC
codec.mic = True

# Connect from INPUT1 to "n" (aka inverting) inputs of PGAs
codec.mic_inverting_input = True

# Disable mutes on PGA inputs (aka INTPUT1)
codec.mic_mute = False

# Set input boosts to get inputs 1 to the boost mixers
codec.mic_boost_gain = adafruit_wm8960.MIC_BOOST_GAIN_0DB
codec.mic_boost = True
codec.input = True # Enable boost mixers

# Disconnect LB2LO (booster to output mixer (analog bypass)
# For this example, we are going to pass audio throught the ADC and DAC
codec.mic_output = False

# Connect from DAC outputs to output mixer
codec.dac_output = True

# Set gainstage between booster mixer and output mixer
# For this loopback example, we are going to keep these as low as they go
codec.mic_output_volume = adafruit_wm8960.OUTPUT_VOLUME_MIN

# Enable output mixers
codec.output = True

# Setup clock and mode
codec.sample_rate = 44100
codec.master_mode = True
codec.gpio_output = True # Note, should not be changed while ADC is enabled.

# Enable ADCs and DACs
codec.adc = codec.dac = True

# Loopback sends ADC data directly into DAC
codec = WM8960(board.I2C())
codec.loopback = True

# Default is "soft mute" on, so we must disable mute to make channels active
codec.dac_mute = False

# Enable headphone amp output
codec.headphone = True
codec.headphone_volume = 0.0

# Enables capless mode using the VMID as buffer for headphone ground on OUT3
codec.mono_output = True

# Automatic Level control configuration

# Only allows mic gain stages at a "zero crossover" point in audio stream.
# Minimizes "zipper" noise when chaning gains.
codec.mic_zero_cross = True
codec.input = Input.MIC1
codec.gain = 0.5
codec.volume = 1.0
codec.headphone = 0.5

codec.alc = True
codec.alc_target = -6.0
codec.alc_attack_time = 0.024
codec.alc_hold_time = 0.0
codec.alc_decay_time = 0.192
codec.alc_max_gain = adafruit_wm8960.ALC_MAX_GAIN_MAX
codec.alc_min_gain = adafruit_wm8960.ALC_MIN_GAIN_MIN

codec.alc_gain = (
0.75, # target
1.0, # max gain
0.0, # min gain
0.0, # noise gate
)
codec.alc_time = (
0.024, # attack
0.192, # decay
0.0, # hold
)

gain = codec.alc_gain
while True:
codec.alc_target = map_range(
gain[0] = map_range(
analog_in.value,
0,
65536,
adafruit_wm8960.ALC_TARGET_MIN,
adafruit_wm8960.ALC_TARGET_MAX,
0, 65536,
0.0, 1.0
)
codec.alc_gain = gain
time.sleep(1.0)
45 changes: 10 additions & 35 deletions examples/wm8960_input.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,43 +47,18 @@
"""

import board
import adafruit_wm8960
from adafruit_wm8960 import Input, WM8960

codec = adafruit_wm8960.WM8960(board.I2C())
codec = WM8960(board.I2C())

# Setup signal flow through the analog audio bypass connections
# Select the desired input. Available options are MIC1 (single-ended), MIC2 (differential), MIC3 (differential), LINE2, or LINE3.
codec.input = Input.MIC1

# INPUT1 must pass through the Mic Boost
codec.mic = True
codec.mic_inverting_input = True
codec.mic_input = adafruit_wm8960.MIC_VMID # Non-inverting input
codec.mic_mute = False
codec.mic_boost_gain = 0.0
codec.mic_boost = True
codec.mic_volume = 0.0
# Configure the microphone boost gain
codec.gain = 0.5

# In order to use INPUT2 or INPUT3 with the Mic Boost (PGA)
# codec.mic_input = adafruit_wm8960.MIC_INPUT2
# codec.mic_input = adafruit_wm8960.MIC_INPUT3
# If codec.mic_inverting_input is enabled, mic boost uses input as balanced input between INPUT1 & INPUT2/3
# Bypass analog signal to analog output
codec.monitor = 1.0

# Set input boosts to get INPUT2 or INPUT3 (both left and right) to the boost mixers and bypass the Mic Boost (PGA)
codec.input2_boost = 0.0
codec.input3_boost = 0.0

# Enable input boost mixers
codec.input = True

# Connect LB2LO (booster to output mixer [aka analog bypass])
codec.mic_output = True

# Set gainstage between boost mixer and output mixers (analog bypass)
codec.mic_output_volume = 0.0

# Enable output mixers
codec.output = True

# Enable Headphone Amp with OUT3 as capless buffer for headphone ground
codec.headphone = True
codec.mono_output = True
codec.headphone_volume = 0.0
# Enable the amplifier and set the output volume
codec.headphone = 0.5
26 changes: 5 additions & 21 deletions examples/wm8960_simpletest.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,30 +40,14 @@
import audiobusio
import board
import synthio
import adafruit_wm8960
from adafruit_wm8960 import WM8960
import time
import digitalio

codec = adafruit_wm8960.WM8960(board.I2C())

# Setup Digital Interface
codec.sample_rate = 44100
codec.bit_depth = 16

# Enable DAC
codec.dac = True
codec.dac_output = True
codec.output = True
codec.dac_mute = False

# Enable Headphone Amp with OUT3 as capless buffer for headphone ground
codec.headphone = True
codec.mono_output = True
codec.headphone_volume = 0.0

# Enable Speaker Amp
codec.speaker = True
codec.speaker_volume = 0.0
codec = WM8960(board.I2C(), 44100, 16)
codec.volume = 1.0
codec.headphone = 0.5
codec.speaker = 0.5

# Configure I2S Output
audio = audiobusio.I2SOut(board.AUDIO_BCLK, board.AUDIO_SYNC, board.AUDIO_TXD)
Expand Down

0 comments on commit 557d8e7

Please sign in to comment.