Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 24 additions & 2 deletions Fruit_Jam/Larsio_Paint_Music/sound_manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import math
import time
import array
import json
import gc
import os
import digitalio
Expand All @@ -22,6 +23,7 @@
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
Expand Down Expand Up @@ -63,6 +65,12 @@ def __init__(self, audio_output="pwm", seconds_per_eighth=0.25):
# Setup PWM audio output on D10
self.audio = audiopwmio.PWMAudioOut(board.D10)
else: # i2s
# optional configuration file for speaker/headphone setting
launcher_config = {}
if 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
Expand Down Expand Up @@ -98,9 +106,23 @@ def __init__(self, audio_output="pwm", seconds_per_eighth=0.25):

# Initialize TLV320
self.tlv = adafruit_tlv320.TLV320DAC3100(i2c)

# set sample rate & bit depth
self.tlv.configure_clocks(sample_rate=11025, bit_depth=16)
self.tlv.headphone_output = True
self.tlv.headphone_volume = -15 # dB

if "tlv320" in launcher_config:
if launcher_config["tlv320"].get("output") == "speaker":
# use speaker
self.tlv.speaker_output = True
self.tlv.dac_volume = launcher_config["tlv320"].get("volume",5) # dB
else:
# use headphones
self.tlv.headphone_output = True
self.tlv.dac_volume = launcher_config["tlv320"].get("volume",0) # dB
else:
# default to headphones
self.tlv.headphone_output = True
self.tlv.dac_volume = 0 # dB
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

does this end up playing louder than the previous sound level with -15 on headphone_volume?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's hard for me to say because when I was using the headphone_volume & speaker_volume and switching back and forth between the outputs the volume levels seemed to jump around randomly (probably not but in a way I didn't understand). The headphone level at dac_volume=0 seems good on the headphones I've been using.


# Setup I2S audio output - important to do this AFTER configuring the DAC
self.audio = audiobusio.I2SOut(
Expand Down
11 changes: 3 additions & 8 deletions Metro/Metro_RP2350_Chips_Challenge/code.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
import picodvi
import framebufferio
import displayio
import adafruit_tlv320
import audiobusio
from audio import Audio
from game import Game
Expand Down Expand Up @@ -39,16 +38,12 @@

displayio.release_displays()

i2c = board.I2C()
dac = adafruit_tlv320.TLV320DAC3100(i2c)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I lean towards leaving the hardware init and configuration in code.py rather than move it to definitions.py. I would defer to @makermelissa if she has an opinion on it.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It would be easy enough to move the init back to code.py. The reason I moved it to definitions.py was that I thought it was useful for setting a default value for PLAY_SOUNDS that would avoid crashes on hardware that didn't have the DAC hardware. The sound in Chip's Challenge is awesome, but the game is still lots of fun without it.

The default isn't necessary though and after looking at definitions.py again I can see that my code made it less clear that PLAY_SOUND was a definition that could be customized. I've added an update that I think helps with that issue but I'm happy to switch the default back to True and move the initialization back to code.py if that's the way you and Melissa want to go 😁

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Another reason for moving the sound config to definitions.py was that although it's probably unlikely, if @makermelissa ever wanted to make some of the parameters in definitions.py user settable, the launcher_config dictionary would already be available without having to have it opened and read in two different modules.

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,
Expand Down
57 changes: 56 additions & 1 deletion Metro/Metro_RP2350_Chips_Challenge/definitions.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,65 @@
# SPDX-FileCopyrightText: 2025 Melissa LeBlanc-Williams
#
# SPDX-License-Identifier: MIT
import json
import time
import board
from micropython import const
import adafruit_pathlib as pathlib
import adafruit_tlv320

# optional configuration file for speaker/headphone setting
launcher_config = {}
if pathlib.Path("/launcher.conf.json").exists():
with open("/launcher.conf.json", "r") as f:
launcher_config = json.load(f)

# Check if DAC is connected
if "I2C" in dir(board):
i2c = board.I2C()
for i in range(500): # try for 5 seconds
if i2c.try_lock():
break
time.sleep(0.01)
if 0x18 in i2c.scan():
tlv320_present = True
else:
tlv320_present = False
i2c.unlock()
else:
tlv320_present = False

if tlv320_present:
dac = adafruit_tlv320.TLV320DAC3100(i2c)

# set sample rate & bit depth
dac.configure_clocks(sample_rate=44100, bit_depth=16)

if "tlv320" in launcher_config:
if launcher_config["tlv320"].get("output") == "speaker":
# use speaker
dac.speaker_output = True
dac.dac_volume = launcher_config["tlv320"].get("volume",5) # dB
else:
# use headphones
dac.headphone_output = True
dac.dac_volume = launcher_config["tlv320"].get("volume",0) # dB
else:
# default to headphones
dac.headphone_output = True
dac.dac_volume = 0 # dB

if tlv320_present:
_default_play_sounds = True
else:
_default_play_sounds = False

if "sound" in launcher_config:
if launcher_config["tlv320"].get("output") == "mute":
_default_play_sounds = False

# Settings
PLAY_SOUNDS = True
PLAY_SOUNDS = _default_play_sounds

# Timing Constants
TICKS_PER_SECOND = const(20)
Expand Down