Skip to content

Commit ce4c56b

Browse files
committed
Add audio output option file to Larsio and Chip's Challenge
1 parent e804e24 commit ce4c56b

File tree

3 files changed

+107
-22
lines changed

3 files changed

+107
-22
lines changed

Fruit_Jam/Larsio_Paint_Music/sound_manager.py

Lines changed: 53 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
import math
1010
import time
1111
import array
12+
import json
1213
import gc
1314
import os
1415
import digitalio
@@ -22,6 +23,7 @@
2223
import audiomixer
2324
import synthio
2425
import board
26+
import adafruit_pathlib as pathlib
2527
import adafruit_tlv320
2628
from adafruit_midi.note_on import NoteOn
2729
from adafruit_midi.note_off import NoteOff
@@ -63,6 +65,15 @@ def __init__(self, audio_output="pwm", seconds_per_eighth=0.25):
6365
# Setup PWM audio output on D10
6466
self.audio = audiopwmio.PWMAudioOut(board.D10)
6567
else: # i2s
68+
# optional configuration file for speaker/headphone setting, check current and root directory
69+
launcher_config = {}
70+
if pathlib.Path("launcher.conf.json").exists():
71+
with open("launcher.conf.json", "r") as f:
72+
launcher_config = json.load(f)
73+
elif pathlib.Path("/launcher.conf.json").exists():
74+
with open("/launcher.conf.json", "r") as f:
75+
launcher_config = json.load(f)
76+
6677
try:
6778
# Import libraries needed for I2S
6879
#check for Metro RP2350 vs. Fruit Jam
@@ -96,20 +107,49 @@ def __init__(self, audio_output="pwm", seconds_per_eighth=0.25):
96107
wsel_pin = board.I2S_WS
97108
din_pin = board.I2S_DIN
98109

110+
# Check if DAC is connected
111+
while not i2c.try_lock():
112+
time.sleep(0.01)
113+
if 0x18 in i2c.scan():
114+
ltv320_present = True
115+
else:
116+
ltv320_present = False
117+
i2c.unlock()
118+
99119
# Initialize TLV320
100-
self.tlv = adafruit_tlv320.TLV320DAC3100(i2c)
101-
self.tlv.configure_clocks(sample_rate=11025, bit_depth=16)
102-
self.tlv.headphone_output = True
103-
self.tlv.headphone_volume = -15 # dB
104-
105-
# Setup I2S audio output - important to do this AFTER configuring the DAC
106-
self.audio = audiobusio.I2SOut(
107-
bit_clock=bclck_pin,
108-
word_select=wsel_pin,
109-
data=din_pin
110-
)
111-
112-
print("TLV320 I2S DAC initialized successfully")
120+
if ltv320_present:
121+
self.tlv = adafruit_tlv320.TLV320DAC3100(i2c)
122+
123+
# set sample rate & bit depth
124+
self.tlv.configure_clocks(sample_rate=11025, bit_depth=16)
125+
126+
if "sound" in launcher_config:
127+
if launcher_config["sound"] == "speaker":
128+
# use speaker
129+
self.tlv.speaker_output = True
130+
self.tlv.speaker_volume = -60
131+
else:
132+
# use headphones
133+
self.tlv.headphone_output = True
134+
self.tlv.headphone_volume = -15 # dB
135+
else:
136+
# default to headphones
137+
self.tlv.headphone_output = True
138+
self.tlv.headphone_volume = -15 # dB
139+
140+
# Setup I2S audio output - important to do this AFTER configuring the DAC
141+
self.audio = audiobusio.I2SOut(
142+
bit_clock=bclck_pin,
143+
word_select=wsel_pin,
144+
data=din_pin
145+
)
146+
147+
print("TLV320 I2S DAC initialized successfully")
148+
149+
else:
150+
print("TLV320 DAC not found, falling back to PWM audio output")
151+
self.audio = audiopwmio.PWMAudioOut(board.D10)
152+
113153
except Exception as e:
114154
print(f"Error initializing TLV320 DAC: {e}")
115155
print("Falling back to PWM audio output")

Metro/Metro_RP2350_Chips_Challenge/code.py

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@
77
import picodvi
88
import framebufferio
99
import displayio
10-
import adafruit_tlv320
1110
import audiobusio
1211
from audio import Audio
1312
from game import Game
@@ -39,16 +38,12 @@
3938

4039
displayio.release_displays()
4140

42-
i2c = board.I2C()
43-
dac = adafruit_tlv320.TLV320DAC3100(i2c)
44-
dac.configure_clocks(sample_rate=44100, bit_depth=16)
45-
dac.headphone_output = True
46-
dac.headphone_volume = -15 # dB
47-
4841
if hasattr(board, "I2S_BCLK"):
4942
audio_bus = audiobusio.I2SOut(board.I2S_BCLK, board.I2S_WS, board.I2S_DIN)
50-
else:
43+
elif hasattr(board, "D9") and hasattr(board, "D10") and hasattr(board, "D11"):
5144
audio_bus = audiobusio.I2SOut(board.D9, board.D10, board.D11)
45+
else:
46+
audio_bus = None
5247
audio = Audio(audio_bus, SOUND_EFFECTS)
5348

5449
fb = picodvi.Framebuffer(320, 240, clk_dp=board.CKP, clk_dn=board.CKN,

Metro/Metro_RP2350_Chips_Challenge/definitions.py

Lines changed: 51 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,60 @@
11
# SPDX-FileCopyrightText: 2025 Melissa LeBlanc-Williams
22
#
33
# SPDX-License-Identifier: MIT
4+
import json
5+
import board
46
from micropython import const
7+
import adafruit_pathlib as pathlib
8+
import adafruit_tlv320
9+
10+
# optional configuration file for speaker/headphone setting, check current and root directory
11+
launcher_config = {}
12+
if pathlib.Path("launcher.conf.json").exists():
13+
with open("launcher.conf.json", "r") as f:
14+
launcher_config = json.load(f)
15+
elif pathlib.Path("/launcher.conf.json").exists():
16+
with open("/launcher.conf.json", "r") as f:
17+
launcher_config = json.load(f)
18+
19+
# Check if DAC is connected
20+
i2c = board.I2C()
21+
while not i2c.try_lock():
22+
time.sleep(0.01)
23+
if 0x18 in i2c.scan():
24+
ltv320_present = True
25+
else:
26+
ltv320_present = False
27+
i2c.unlock()
28+
29+
if ltv320_present:
30+
dac = adafruit_tlv320.TLV320DAC3100(i2c)
31+
32+
# set sample rate & bit depth
33+
dac.configure_clocks(sample_rate=44100, bit_depth=16)
34+
35+
if "sound" in launcher_config:
36+
if launcher_config["sound"] == "speaker":
37+
# use speaker
38+
dac.speaker_output = True
39+
dac.speaker_volume = -40
40+
elif launcher_config["sound"] != "mute":
41+
# use headphones
42+
dac.headphone_output = True
43+
dac.headphone_volume = -15 # dB
44+
else:
45+
# default to headphones
46+
dac.headphone_output = True
47+
dac.headphone_volume = -15 # dB
548

649
# Settings
7-
PLAY_SOUNDS = True
50+
if ltv320_present:
51+
PLAY_SOUNDS = True
52+
else:
53+
PLAY_SOUNDS = False
54+
55+
if "sound" in launcher_config:
56+
if launcher_config["sound"] == "mute":
57+
PLAY_SOUNDS = False
858

959
# Timing Constants
1060
TICKS_PER_SECOND = const(20)

0 commit comments

Comments
 (0)