@@ -201,22 +201,29 @@ def play_wav(cls, filename, result_callback=None):
201201 print (f"Playing { data_size } original bytes (vol { cls ._volume } %) ..." )
202202 f .seek (data_start )
203203
204- # ----- Viper volume scaler (16-bit only) -------------------------
205- @micropython .viper
204+ # Fallback to non-viper and non-functional code on desktop, as macOS/darwin throws "invalid micropython decorator"
206205 def scale_audio (buf : ptr8 , num_bytes : int , scale_fixed : int ):
207- for i in range (0 , num_bytes , 2 ):
208- lo = int (buf [i ])
209- hi = int (buf [i + 1 ])
210- sample = (hi << 8 ) | lo
211- if hi & 128 :
212- sample -= 65536
213- sample = (sample * scale_fixed ) // 32768
214- if sample > 32767 :
215- sample = 32767
216- elif sample < - 32768 :
217- sample = - 32768
218- buf [i ] = sample & 255
219- buf [i + 1 ] = (sample >> 8 ) & 255
206+ pass
207+
208+ try :
209+ # ----- Viper volume scaler (16-bit only) -------------------------
210+ @micropython .viper # throws "invalid micropython decorator" on macOS / darwin
211+ def scale_audio (buf : ptr8 , num_bytes : int , scale_fixed : int ):
212+ for i in range (0 , num_bytes , 2 ):
213+ lo = int (buf [i ])
214+ hi = int (buf [i + 1 ])
215+ sample = (hi << 8 ) | lo
216+ if hi & 128 :
217+ sample -= 65536
218+ sample = (sample * scale_fixed ) // 32768
219+ if sample > 32767 :
220+ sample = 32767
221+ elif sample < - 32768 :
222+ sample = - 32768
223+ buf [i ] = sample & 255
224+ buf [i + 1 ] = (sample >> 8 ) & 255
225+ except SyntaxError :
226+ print ("Viper not supported (e.g., on desktop)—using plain Python." )
220227
221228 chunk_size = 4096
222229 bytes_per_original_sample = (bits_per_sample // 8 ) * channels
@@ -276,3 +283,16 @@ def scale_audio(buf: ptr8, num_bytes: int, scale_fixed: int):
276283 if cls ._i2s :
277284 cls ._i2s .deinit ()
278285 cls ._i2s = None
286+
287+
288+
289+ def optional_viper (func ):
290+ """Decorator to apply @micropython.viper if possible."""
291+ try :
292+ @micropython .viper
293+ @func # Wait, no—see below for proper chaining
294+ def wrapped (* args , ** kwargs ):
295+ return func (* args , ** kwargs )
296+ return wrapped
297+ except SyntaxError :
298+ return func # Fallback to original
0 commit comments