1919import adafruit_midi
2020import audiocore
2121import audiopwmio
22- import audiobusio
2322import audiomixer
2423import synthio
2524import board
2625import adafruit_pathlib as pathlib
27- import adafruit_tlv320
26+ import adafruit_fruitjam
2827from adafruit_midi .note_on import NoteOn
2928from adafruit_midi .note_off import NoteOff
3029import usb_midi
@@ -67,9 +66,13 @@ def __init__(self, audio_output="pwm", seconds_per_eighth=0.25):
6766 else : # i2s
6867 # optional configuration file for speaker/headphone setting
6968 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 )
69+ for directory in ("/" , "/sd/" , "/saves/" ):
70+ launcher_config_path = directory + "launcher.conf.json"
71+ if pathlib .Path (launcher_config_path ).exists ():
72+ with open (launcher_config_path , "r" ) as f :
73+ launcher_config = launcher_config | json .load (f )
74+ if "audio" not in launcher_config :
75+ launcher_config ["audio" ] = {}
7376
7477 try :
7578 # Import libraries needed for I2S
@@ -84,11 +87,17 @@ def __init__(self, audio_output="pwm", seconds_per_eighth=0.25):
8487 time .sleep (0.1 ) # Pause 100ms
8588 reset_pin .value = True # Set high to release from reset
8689
87- i2c = board .STEMMA_I2C () # initialize I2C
90+ # Initialize TLV320
91+ fjPeriphs = adafruit_fruitjam .Peripherals (
92+ audio_output = launcher_config ["audio" ].get ("output" , "headphone" ),
93+ safe_volume_limit = launcher_config ["audio" ].get ("volume_override_danger" ,12 ),
94+ sample_rate = 11025 ,
95+ bit_depth = 16 ,
96+ i2c = board .STEMMA_I2C ()
97+ )
8898
89- bclck_pin = board .D9
90- wsel_pin = board .D10
91- din_pin = board .D11
99+ self .tlv = fjPeriphs .dac
100+ fjPeriphs .audio = audiobusio .I2SOut (board .D9 , board .D10 , board .D11 )
92101
93102 elif 'Fruit Jam' in board_type :
94103 print ("Fruit Jam setup" )
@@ -98,38 +107,27 @@ def __init__(self, audio_output="pwm", seconds_per_eighth=0.25):
98107 time .sleep (0.1 )
99108 reset_pin .value = True
100109
101- i2c = busio .I2C (board .SCL , board .SDA )
110+ # Initialize TLV320
111+ fjPeriphs = adafruit_fruitjam .Peripherals (
112+ audio_output = launcher_config ["audio" ].get ("output" , "headphone" ),
113+ safe_volume_limit = launcher_config ["audio" ].get ("volume_override_danger" ,12 ),
114+ sample_rate = 11025 ,
115+ bit_depth = 16 ,
116+ i2c = board .I2C ()
117+ )
102118
103- bclck_pin = board .I2S_BCLK
104- wsel_pin = board .I2S_WS
105- din_pin = board .I2S_DIN
119+ self .tlv = fjPeriphs .dac
106120
107- # Initialize TLV320
108- self .tlv = adafruit_tlv320 .TLV320DAC3100 (i2c )
109-
110- # set sample rate & bit depth
111- self .tlv .configure_clocks (sample_rate = 11025 , bit_depth = 16 )
112-
113- if "tlv320" in launcher_config :
114- if launcher_config ["tlv320" ].get ("output" ) == "speaker" :
115- # use speaker
116- self .tlv .speaker_output = True
117- self .tlv .dac_volume = launcher_config ["tlv320" ].get ("volume" ,5 ) # dB
118- else :
119- # use headphones
120- self .tlv .headphone_output = True
121- self .tlv .dac_volume = launcher_config ["tlv320" ].get ("volume" ,0 ) # dB
122- else :
123- # default to headphones
124- self .tlv .headphone_output = True
125- self .tlv .dac_volume = 0 # dB
121+ # If volume was specified use it, otherwise use the fruitjam library default
122+ if "volume_override_danger" in launcher_config ["audio" ]:
123+ fjPeriphs .volume = launcher_config ["audio" ]["volume_override_danger" ]
124+ elif "volume" in launcher_config ["audio" ]:
125+ fjPeriphs .volume = launcher_config ["audio" ]["volume" ] # FruitJam vol (1-20)
126126
127127 # Setup I2S audio output - important to do this AFTER configuring the DAC
128- self .audio = audiobusio .I2SOut (
129- bit_clock = bclck_pin ,
130- word_select = wsel_pin ,
131- data = din_pin
132- )
128+ # Fruitjam library actually does this before we modify the configuration
129+ # but after the initial default configuration is performed
130+ self .audio = fjPeriphs .audio
133131
134132 print ("TLV320 I2S DAC initialized successfully" )
135133 except Exception as e :
@@ -617,6 +615,7 @@ def deinit(self):
617615 try :
618616 # For TLV320DAC3100, headphone_output = False will power down the output
619617 self .tlv .headphone_output = False
618+ self .tlv .speaker_output = False
620619 except Exception :
621620 pass
622621
0 commit comments