99import board
1010import keypad
1111import audiobusio
12+ import audiocore
1213import audiomp3
1314import audiomixer
1415
3839# without "opening" a "file"!
3940EMPTY_MP3_BYTES = b"\xff \xe3 "
4041
41- # THis is actually a valid but very short mp3 file, use it in case the core
42- # changes and becomes more picky
43- # EMPTY_MP3_BYTES = b'\xff\xe3\x18\xc4\x00\x00\x00\x03H\x00\x00\x00\x00CIRCUITPYUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUU\xff\xe3\x18\xc4;\x00\x00\x03H\x00\x00\x00\x00UUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUU\xff\xe3\x18\xc4v\x00\x00\x03H\x00\x00\x00\x00UUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUU'
44-
45-
4642def exists (p ):
4743 try :
4844 os .stat (p )
@@ -60,19 +56,19 @@ def random_choice(seq):
6056# stop any audio (it's already stopped) but it DOES mark the voice & decoder as
6157# available. Otherwise, we might needlessly stop some other sample.
6258def free_stopped_channels ():
63- for trigger in triggers :
64- if trigger . _voice and not trigger .playing :
59+ for i in triggers :
60+ if i . voice and not i .playing :
6561 print ("fst" )
66- trigger .force_off ()
62+ i .force_off ()
6763
6864
6965# iterating on reversed triggers gives priority to **lower** numbered triggers
7066def ensure_available_decoder ():
7167 if available_decoders :
7268 return available_decoders .popleft ()
7369
74- for trigger in reversed_triggers :
75- trigger .force_off ()
70+ for i in reversed_triggers :
71+ i .force_off ()
7672 if available_decoders :
7773 break
7874
@@ -83,8 +79,8 @@ def ensure_available_voice():
8379 if available_voices :
8480 return available_voices .popleft ()
8581
86- for trigger in reversed_triggers :
87- trigger .force_off ()
82+ for i in reversed_triggers :
83+ i .force_off ()
8884 if available_voices :
8985 break
9086
@@ -94,8 +90,8 @@ def ensure_available_voice():
9490class TriggerBase :
9591 def __init__ (self , prefix ):
9692 self ._decoder = None
97- self ._voice = None
98- self ._filenames = list (self ._gather_filenames (prefix ))
93+ self .voice = None
94+ self .filenames = list (self ._gather_filenames (prefix ))
9995
10096 def _gather_filenames (self , prefix ):
10197 for stem in self .stems :
@@ -108,7 +104,7 @@ def _gather_filenames(self, prefix):
108104 yield name_wav
109105 continue
110106
111- def _get_sample (self , path ):
107+ def get_sample (self , path ):
112108 if path .endswith (".mp3" ):
113109 self ._decoder = ensure_available_decoder ()
114110 self ._decoder .open (path )
@@ -119,16 +115,16 @@ def _get_sample(self, path):
119115 def play (self , path , loop = False ):
120116 self .force_off ()
121117 free_stopped_channels ()
122- sample = self ._get_sample (path )
123- self ._voice = ensure_available_voice ()
124- self ._voice .play (sample , loop = loop )
118+ sample = self .get_sample (path )
119+ self .voice = ensure_available_voice ()
120+ self .voice .play (sample , loop = loop )
125121
126122 def force_off (self ):
127123 print ("force off" , self )
128- voice = self ._voice
124+ voice = self .voice
129125 if voice is not None :
130126 print (f"return voice { id (voice )} " )
131- self ._voice = None
127+ self .voice = None
132128 voice .stop ()
133129 available_voices .append (voice )
134130 decoder = self ._decoder
@@ -141,7 +137,7 @@ def force_off(self):
141137
142138 @property
143139 def playing (self ):
144- return False if self ._voice is None else self ._voice .playing
140+ return False if self .voice is None else self .voice .playing
145141
146142 @classmethod
147143 def matches (cls , prefix ):
@@ -151,7 +147,7 @@ def matches(cls, prefix):
151147 return exists (name_wav ) or exists (name_mp3 )
152148
153149 def __repr__ (self ):
154- return f"<{ self .__class__ .__name__ } { self ._filenames } { ' playing' if self .playing else '' } >"
150+ return f"<{ self .__class__ .__name__ } { self .filenames } { ' playing' if self .playing else '' } >"
155151
156152
157153class NopTrigger (TriggerBase ):
@@ -172,7 +168,7 @@ class BasicTrigger(TriggerBase):
172168 stems = ["" ]
173169
174170 def on_press (self ):
175- self .play (self ._filenames [0 ])
171+ self .play (self .filenames [0 ])
176172
177173 def on_release (self ):
178174 pass
@@ -184,7 +180,7 @@ class HoldLoopingTrigger(TriggerBase):
184180 stems = ["HOLDL" ]
185181
186182 def on_press (self ):
187- self .play (self ._filenames [0 ], loop = True )
183+ self .play (self .filenames [0 ], loop = True )
188184
189185 def on_release (self ):
190186 self .force_off ()
@@ -199,7 +195,7 @@ def on_press(self):
199195 if self .playing :
200196 self .force_off ()
201197 else :
202- self .play (self ._filenames [0 ], loop = True )
198+ self .play (self .filenames [0 ], loop = True )
203199
204200 def on_release (self ):
205201 pass
@@ -213,8 +209,8 @@ def __init__(self, prefix):
213209 self ._phase = 0
214210
215211 def on_press (self ):
216- self .play (self ._filenames [self ._phase ])
217- self ._phase = (self ._phase + 1 ) % len (self ._filenames )
212+ self .play (self .filenames [self ._phase ])
213+ self ._phase = (self ._phase + 1 ) % len (self .filenames )
218214
219215 def on_release (self ):
220216 pass
@@ -223,11 +219,8 @@ def on_release(self):
223219class PlayRandomTrigger (TriggerBase ):
224220 stems = [f"RAND{ i } " for i in range (10 )]
225221
226- def __init__ (self , prefix ):
227- super ().__init__ (prefix )
228-
229222 def on_press (self ):
230- self .play (random_choice (self ._filenames ))
223+ self .play (random_choice (self .filenames ))
231224
232225 def on_release (self ):
233226 pass
@@ -275,24 +268,24 @@ def playback_specs(sample):
275268 )
276269
277270
278- def check_match_make_mixer (audiodev ):
271+ def check_match_make_mixer (dev ):
279272 all_filenames = []
280- for trigger in triggers :
281- all_filenames .extend (trigger . _filenames )
273+ for i in triggers :
274+ all_filenames .extend (i . filenames )
282275
283276 if not all_filenames :
284277 raise RuntimeError ("*** NO AUDIO FILES FOUND ***" )
285278
286279 if max_simultaneous_voices == 1 :
287- return [audiodev ]
280+ return [dev ]
288281
289282 first_trigger = triggers [0 ]
290283
291284 mixer_buffer_size = (1152 * 4 ) * 4
292285
293286 specs = None
294287 for filename in all_filenames :
295- sample = first_trigger ._get_sample (filename )
288+ sample = first_trigger .get_sample (filename )
296289 new_specs = playback_specs (sample )
297290 if specs is None :
298291 specs = new_specs
@@ -312,7 +305,7 @@ def check_match_make_mixer(audiodev):
312305 samples_signed = samples_signed ,
313306 ** specs ,
314307 )
315- audiodev .play (mixer )
308+ dev .play (mixer )
316309
317310 return list (mixer .voice )
318311
0 commit comments