Add audio output option file to Larsio and Chip's Challenge
This commit is contained in:
parent
e804e240a6
commit
ce4c56b6c8
3 changed files with 105 additions and 20 deletions
|
|
@ -9,6 +9,7 @@
|
|||
import math
|
||||
import time
|
||||
import array
|
||||
import json
|
||||
import gc
|
||||
import os
|
||||
import digitalio
|
||||
|
|
@ -22,6 +23,7 @@ import audiobusio
|
|||
import audiomixer
|
||||
import synthio
|
||||
import board
|
||||
import adafruit_pathlib as pathlib
|
||||
import adafruit_tlv320
|
||||
from adafruit_midi.note_on import NoteOn
|
||||
from adafruit_midi.note_off import NoteOff
|
||||
|
|
@ -63,6 +65,15 @@ class SoundManager:
|
|||
# Setup PWM audio output on D10
|
||||
self.audio = audiopwmio.PWMAudioOut(board.D10)
|
||||
else: # i2s
|
||||
# optional configuration file for speaker/headphone setting, check current and root directory
|
||||
launcher_config = {}
|
||||
if pathlib.Path("launcher.conf.json").exists():
|
||||
with open("launcher.conf.json", "r") as f:
|
||||
launcher_config = json.load(f)
|
||||
elif pathlib.Path("/launcher.conf.json").exists():
|
||||
with open("/launcher.conf.json", "r") as f:
|
||||
launcher_config = json.load(f)
|
||||
|
||||
try:
|
||||
# Import libraries needed for I2S
|
||||
#check for Metro RP2350 vs. Fruit Jam
|
||||
|
|
@ -96,20 +107,49 @@ class SoundManager:
|
|||
wsel_pin = board.I2S_WS
|
||||
din_pin = board.I2S_DIN
|
||||
|
||||
# Check if DAC is connected
|
||||
while not i2c.try_lock():
|
||||
time.sleep(0.01)
|
||||
if 0x18 in i2c.scan():
|
||||
ltv320_present = True
|
||||
else:
|
||||
ltv320_present = False
|
||||
i2c.unlock()
|
||||
|
||||
# Initialize TLV320
|
||||
self.tlv = adafruit_tlv320.TLV320DAC3100(i2c)
|
||||
self.tlv.configure_clocks(sample_rate=11025, bit_depth=16)
|
||||
self.tlv.headphone_output = True
|
||||
self.tlv.headphone_volume = -15 # dB
|
||||
if ltv320_present:
|
||||
self.tlv = adafruit_tlv320.TLV320DAC3100(i2c)
|
||||
|
||||
# Setup I2S audio output - important to do this AFTER configuring the DAC
|
||||
self.audio = audiobusio.I2SOut(
|
||||
bit_clock=bclck_pin,
|
||||
word_select=wsel_pin,
|
||||
data=din_pin
|
||||
)
|
||||
# set sample rate & bit depth
|
||||
self.tlv.configure_clocks(sample_rate=11025, bit_depth=16)
|
||||
|
||||
if "sound" in launcher_config:
|
||||
if launcher_config["sound"] == "speaker":
|
||||
# use speaker
|
||||
self.tlv.speaker_output = True
|
||||
self.tlv.speaker_volume = -60
|
||||
else:
|
||||
# use headphones
|
||||
self.tlv.headphone_output = True
|
||||
self.tlv.headphone_volume = -15 # dB
|
||||
else:
|
||||
# default to headphones
|
||||
self.tlv.headphone_output = True
|
||||
self.tlv.headphone_volume = -15 # dB
|
||||
|
||||
# Setup I2S audio output - important to do this AFTER configuring the DAC
|
||||
self.audio = audiobusio.I2SOut(
|
||||
bit_clock=bclck_pin,
|
||||
word_select=wsel_pin,
|
||||
data=din_pin
|
||||
)
|
||||
|
||||
print("TLV320 I2S DAC initialized successfully")
|
||||
|
||||
else:
|
||||
print("TLV320 DAC not found, falling back to PWM audio output")
|
||||
self.audio = audiopwmio.PWMAudioOut(board.D10)
|
||||
|
||||
print("TLV320 I2S DAC initialized successfully")
|
||||
except Exception as e:
|
||||
print(f"Error initializing TLV320 DAC: {e}")
|
||||
print("Falling back to PWM audio output")
|
||||
|
|
|
|||
|
|
@ -7,7 +7,6 @@ import board
|
|||
import picodvi
|
||||
import framebufferio
|
||||
import displayio
|
||||
import adafruit_tlv320
|
||||
import audiobusio
|
||||
from audio import Audio
|
||||
from game import Game
|
||||
|
|
@ -39,16 +38,12 @@ SOUND_EFFECTS = {
|
|||
|
||||
displayio.release_displays()
|
||||
|
||||
i2c = board.I2C()
|
||||
dac = adafruit_tlv320.TLV320DAC3100(i2c)
|
||||
dac.configure_clocks(sample_rate=44100, bit_depth=16)
|
||||
dac.headphone_output = True
|
||||
dac.headphone_volume = -15 # dB
|
||||
|
||||
if hasattr(board, "I2S_BCLK"):
|
||||
audio_bus = audiobusio.I2SOut(board.I2S_BCLK, board.I2S_WS, board.I2S_DIN)
|
||||
else:
|
||||
elif hasattr(board, "D9") and hasattr(board, "D10") and hasattr(board, "D11"):
|
||||
audio_bus = audiobusio.I2SOut(board.D9, board.D10, board.D11)
|
||||
else:
|
||||
audio_bus = None
|
||||
audio = Audio(audio_bus, SOUND_EFFECTS)
|
||||
|
||||
fb = picodvi.Framebuffer(320, 240, clk_dp=board.CKP, clk_dn=board.CKN,
|
||||
|
|
|
|||
|
|
@ -1,10 +1,60 @@
|
|||
# SPDX-FileCopyrightText: 2025 Melissa LeBlanc-Williams
|
||||
#
|
||||
# SPDX-License-Identifier: MIT
|
||||
import json
|
||||
import board
|
||||
from micropython import const
|
||||
import adafruit_pathlib as pathlib
|
||||
import adafruit_tlv320
|
||||
|
||||
# optional configuration file for speaker/headphone setting, check current and root directory
|
||||
launcher_config = {}
|
||||
if pathlib.Path("launcher.conf.json").exists():
|
||||
with open("launcher.conf.json", "r") as f:
|
||||
launcher_config = json.load(f)
|
||||
elif pathlib.Path("/launcher.conf.json").exists():
|
||||
with open("/launcher.conf.json", "r") as f:
|
||||
launcher_config = json.load(f)
|
||||
|
||||
# Check if DAC is connected
|
||||
i2c = board.I2C()
|
||||
while not i2c.try_lock():
|
||||
time.sleep(0.01)
|
||||
if 0x18 in i2c.scan():
|
||||
ltv320_present = True
|
||||
else:
|
||||
ltv320_present = False
|
||||
i2c.unlock()
|
||||
|
||||
if ltv320_present:
|
||||
dac = adafruit_tlv320.TLV320DAC3100(i2c)
|
||||
|
||||
# set sample rate & bit depth
|
||||
dac.configure_clocks(sample_rate=44100, bit_depth=16)
|
||||
|
||||
if "sound" in launcher_config:
|
||||
if launcher_config["sound"] == "speaker":
|
||||
# use speaker
|
||||
dac.speaker_output = True
|
||||
dac.speaker_volume = -40
|
||||
elif launcher_config["sound"] != "mute":
|
||||
# use headphones
|
||||
dac.headphone_output = True
|
||||
dac.headphone_volume = -15 # dB
|
||||
else:
|
||||
# default to headphones
|
||||
dac.headphone_output = True
|
||||
dac.headphone_volume = -15 # dB
|
||||
|
||||
# Settings
|
||||
PLAY_SOUNDS = True
|
||||
if ltv320_present:
|
||||
PLAY_SOUNDS = True
|
||||
else:
|
||||
PLAY_SOUNDS = False
|
||||
|
||||
if "sound" in launcher_config:
|
||||
if launcher_config["sound"] == "mute":
|
||||
PLAY_SOUNDS = False
|
||||
|
||||
# Timing Constants
|
||||
TICKS_PER_SECOND = const(20)
|
||||
|
|
|
|||
Loading…
Reference in a new issue