99import math
1010import time
1111import array
12+ import json
1213import gc
1314import os
1415import digitalio
15- import busio
16-
1716
1817import adafruit_midi
18+ import audiobusio
1919import audiocore
2020import audiopwmio
21- import audiobusio
2221import audiomixer
2322import synthio
2423import board
25- import adafruit_tlv320
24+ import adafruit_pathlib as pathlib
25+ import adafruit_fruitjam
2626from adafruit_midi .note_on import NoteOn
2727from adafruit_midi .note_off import NoteOff
2828import 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
0 commit comments