Skip to content

Commit 216b271

Browse files
authored
Merge pull request #3103 from RetiredWizard/fruitjamappsound
Add audio output option file to Larsio and Chip's Challenge
2 parents b373170 + c1dafe7 commit 216b271

File tree

4 files changed

+83
-51
lines changed

4 files changed

+83
-51
lines changed

Fruit_Jam/Larsio_Paint_Music/sound_manager.py

Lines changed: 42 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -9,20 +9,20 @@
99
import math
1010
import time
1111
import array
12+
import json
1213
import gc
1314
import os
1415
import digitalio
15-
import busio
16-
1716

1817
import adafruit_midi
18+
import audiobusio
1919
import audiocore
2020
import audiopwmio
21-
import audiobusio
2221
import audiomixer
2322
import synthio
2423
import board
25-
import adafruit_tlv320
24+
import adafruit_pathlib as pathlib
25+
import adafruit_fruitjam
2626
from adafruit_midi.note_on import NoteOn
2727
from adafruit_midi.note_off import NoteOff
2828
import usb_midi
@@ -53,16 +53,20 @@ def __init__(self, audio_output="pwm", seconds_per_eighth=0.25):
5353
self.audio_output_type = audio_output
5454
self.tlv = None
5555

56-
# Initialize these variables to avoid use-before-assignment issues
57-
i2c = None
58-
bclck_pin = None
59-
wsel_pin = None
60-
din_pin = None
61-
6256
if self.audio_output_type == "pwm":
6357
# Setup PWM audio output on D10
6458
self.audio = audiopwmio.PWMAudioOut(board.D10)
6559
else: # i2s
60+
# optional configuration file for speaker/headphone setting
61+
launcher_config = {}
62+
for directory in ("/", "/sd/", "/saves/"):
63+
launcher_config_path = directory + "launcher.conf.json"
64+
if pathlib.Path(launcher_config_path).exists():
65+
with open(launcher_config_path, "r") as f:
66+
launcher_config = launcher_config | json.load(f)
67+
if "audio" not in launcher_config:
68+
launcher_config["audio"] = {}
69+
6670
try:
6771
# Import libraries needed for I2S
6872
#check for Metro RP2350 vs. Fruit Jam
@@ -76,11 +80,17 @@ def __init__(self, audio_output="pwm", seconds_per_eighth=0.25):
7680
time.sleep(0.1) # Pause 100ms
7781
reset_pin.value = True # Set high to release from reset
7882

79-
i2c = board.STEMMA_I2C() # initialize I2C
83+
# Initialize TLV320
84+
fjPeriphs = adafruit_fruitjam.Peripherals(
85+
audio_output=launcher_config["audio"].get("output", "headphone"),
86+
safe_volume_limit=launcher_config["audio"].get("volume_override_danger",12),
87+
sample_rate=11025,
88+
bit_depth=16,
89+
i2c=board.STEMMA_I2C()
90+
)
8091

81-
bclck_pin = board.D9
82-
wsel_pin = board.D10
83-
din_pin = board.D11
92+
self.tlv = fjPeriphs.dac
93+
fjPeriphs.audio = audiobusio.I2SOut(board.D9, board.D10, board.D11)
8494

8595
elif 'Fruit Jam' in board_type:
8696
print("Fruit Jam setup")
@@ -90,24 +100,27 @@ def __init__(self, audio_output="pwm", seconds_per_eighth=0.25):
90100
time.sleep(0.1)
91101
reset_pin.value = True
92102

93-
i2c = busio.I2C(board.SCL, board.SDA)
103+
# Initialize TLV320
104+
fjPeriphs = adafruit_fruitjam.Peripherals(
105+
audio_output=launcher_config["audio"].get("output", "headphone"),
106+
safe_volume_limit=launcher_config["audio"].get("volume_override_danger",12),
107+
sample_rate=11025,
108+
bit_depth=16,
109+
i2c=board.I2C()
110+
)
94111

95-
bclck_pin = board.I2S_BCLK
96-
wsel_pin = board.I2S_WS
97-
din_pin = board.I2S_DIN
112+
self.tlv = fjPeriphs.dac
98113

99-
# 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
114+
# If volume was specified use it, otherwise use the fruitjam library default
115+
if "volume_override_danger" in launcher_config["audio"]:
116+
fjPeriphs.volume = launcher_config["audio"]["volume_override_danger"]
117+
elif "volume" in launcher_config["audio"]:
118+
fjPeriphs.volume = launcher_config["audio"]["volume"] # FruitJam vol (1-20)
104119

105120
# 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-
)
121+
# Fruitjam library actually does this before we modify the configuration
122+
# but after the initial default configuration is performed
123+
self.audio = fjPeriphs.audio
111124

112125
print("TLV320 I2S DAC initialized successfully")
113126
except Exception as e:
@@ -595,6 +608,7 @@ def deinit(self):
595608
try:
596609
# For TLV320DAC3100, headphone_output = False will power down the output
597610
self.tlv.headphone_output = False
611+
self.tlv.speaker_output = False
598612
except Exception:
599613
pass
600614

Metro/Metro_RP2350_Chips_Challenge/audio.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ def __init__(self, audio_bus, sounds):
1414
self.play(tuple(self._wav_files.keys())[0], wait=True)
1515

1616
def play(self, sound_name, wait=False):
17-
if not PLAY_SOUNDS:
17+
if not PLAY_SOUNDS or self._audio is None:
1818
return
1919
if sound_name in self._wav_files:
2020
with open(self._wav_files[sound_name], "rb") as wave_file:

Metro/Metro_RP2350_Chips_Challenge/code.py

Lines changed: 36 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,14 @@
22
#
33
# SPDX-License-Identifier: MIT
44

5+
import json
56
import time
67
import board
7-
import picodvi
8-
import framebufferio
9-
import displayio
10-
import adafruit_tlv320
8+
import supervisor
119
import audiobusio
1210
from audio import Audio
11+
import adafruit_pathlib as pathlib
12+
import adafruit_fruitjam.Peripherals
1313
from game import Game
1414
from definitions import SECOND_LENGTH, TICKS_PER_SECOND
1515

@@ -37,28 +37,43 @@
3737
"TIME_UP": "sounds/bell.wav"
3838
}
3939

40-
displayio.release_displays()
40+
# optional configuration file for speaker/headphone setting
41+
launcher_config = {}
42+
for directory in ("/", "/sd/", "/saves/"):
43+
launcher_config_path = directory + "launcher.conf.json"
44+
if pathlib.Path(launcher_config_path).exists():
45+
with open(launcher_config_path, "r") as f:
46+
launcher_config = launcher_config | json.load(f)
47+
if "audio" not in launcher_config:
48+
launcher_config["audio"] = {}
4149

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
4750

48-
if hasattr(board, "I2S_BCLK"):
49-
audio_bus = audiobusio.I2SOut(board.I2S_BCLK, board.I2S_WS, board.I2S_DIN)
51+
fjPeriphs = adafruit_fruitjam.Peripherals.Peripherals(
52+
audio_output=launcher_config["audio"].get("output", "headphone"),
53+
safe_volume_limit=launcher_config["audio"].get("volume_override_danger",12),
54+
sample_rate=44100,
55+
bit_depth=16,
56+
i2c=board.I2C()
57+
)
58+
if not hasattr(board, "I2S_BCLK") and \
59+
hasattr(board, "D9") and hasattr(board, "D10") and hasattr(board, "D11"):
60+
61+
fjPeriphs.audio = audiobusio.I2SOut(board.D9, board.D10, board.D11)
62+
63+
# If volume was specified use it, otherwise use the fruitjam library default
64+
if "volume_override_danger" in launcher_config["audio"]:
65+
fjPeriphs.volume = launcher_config["audio"]["volume_override_danger"]
66+
elif "volume" in launcher_config["audio"]:
67+
fjPeriphs.volume = launcher_config["audio"]["volume"] # FruitJam vol (1-20)
68+
69+
if fjPeriphs.audio is not None:
70+
audio = Audio(fjPeriphs.audio, SOUND_EFFECTS)
5071
else:
51-
audio_bus = audiobusio.I2SOut(board.D9, board.D10, board.D11)
52-
audio = Audio(audio_bus, SOUND_EFFECTS)
72+
audio = None
5373

54-
fb = picodvi.Framebuffer(320, 240, clk_dp=board.CKP, clk_dn=board.CKN,
55-
red_dp=board.D0P, red_dn=board.D0N,
56-
green_dp=board.D1P, green_dn=board.D1N,
57-
blue_dp=board.D2P, blue_dn=board.D2N,
58-
color_depth=8)
59-
display = framebufferio.FramebufferDisplay(fb)
74+
adafruit_fruitjam.Peripherals.request_display_config(320, 240, 8)
6075

61-
game = Game(display, DATA_FILE, audio)
76+
game = Game(supervisor.runtime.display, DATA_FILE, audio)
6277
tick_length = SECOND_LENGTH / 1000 / TICKS_PER_SECOND
6378
while True:
6479
start = time.monotonic()

Metro/Metro_RP2350_Chips_Challenge/savestate.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,10 @@ def _mount_sd_card(self):
3232
except OSError:
3333
pass
3434

35-
self._card_detect = DigitalInOut(board.SD_CARD_DETECT)
35+
try:
36+
self._card_detect = DigitalInOut(board.SD_CARD_DETECT)
37+
except ValueError:
38+
return False
3639
self._card_detect.switch_to_input(pull=Pull.UP)
3740
if self._card_detect.value:
3841
return False

0 commit comments

Comments
 (0)