You've already forked MicroPythonOS
mirror of
https://github.com/m5stack/MicroPythonOS.git
synced 2026-05-20 11:51:27 -07:00
Try to fix @micropython.viper issues on macOS/darwin
This commit is contained in:
@@ -61,7 +61,7 @@ class Draw(Activity):
|
||||
lv.draw_line(self.layer,dsc)
|
||||
self.canvas.finish_layer(self.layer)
|
||||
|
||||
@micropython.viper # make it with native compilation
|
||||
# @micropython.viper # "invalid micropython decorator" on macOS
|
||||
def draw_rect(self, x: int, y: int):
|
||||
draw_dsc = lv.draw_rect_dsc_t()
|
||||
lv.draw_rect_dsc_t.init(draw_dsc)
|
||||
|
||||
@@ -201,22 +201,29 @@ class AudioPlayer:
|
||||
print(f"Playing {data_size} original bytes (vol {cls._volume}%) ...")
|
||||
f.seek(data_start)
|
||||
|
||||
# ----- Viper volume scaler (16-bit only) -------------------------
|
||||
@micropython.viper
|
||||
# Fallback to non-viper and non-functional code on desktop, as macOS/darwin throws "invalid micropython decorator"
|
||||
def scale_audio(buf: ptr8, num_bytes: int, scale_fixed: int):
|
||||
for i in range(0, num_bytes, 2):
|
||||
lo = int(buf[i])
|
||||
hi = int(buf[i+1])
|
||||
sample = (hi << 8) | lo
|
||||
if hi & 128:
|
||||
sample -= 65536
|
||||
sample = (sample * scale_fixed) // 32768
|
||||
if sample > 32767:
|
||||
sample = 32767
|
||||
elif sample < -32768:
|
||||
sample = -32768
|
||||
buf[i] = sample & 255
|
||||
buf[i+1] = (sample >> 8) & 255
|
||||
pass
|
||||
|
||||
try:
|
||||
# ----- Viper volume scaler (16-bit only) -------------------------
|
||||
@micropython.viper # throws "invalid micropython decorator" on macOS / darwin
|
||||
def scale_audio(buf: ptr8, num_bytes: int, scale_fixed: int):
|
||||
for i in range(0, num_bytes, 2):
|
||||
lo = int(buf[i])
|
||||
hi = int(buf[i+1])
|
||||
sample = (hi << 8) | lo
|
||||
if hi & 128:
|
||||
sample -= 65536
|
||||
sample = (sample * scale_fixed) // 32768
|
||||
if sample > 32767:
|
||||
sample = 32767
|
||||
elif sample < -32768:
|
||||
sample = -32768
|
||||
buf[i] = sample & 255
|
||||
buf[i+1] = (sample >> 8) & 255
|
||||
except SyntaxError:
|
||||
print("Viper not supported (e.g., on desktop)—using plain Python.")
|
||||
|
||||
chunk_size = 4096
|
||||
bytes_per_original_sample = (bits_per_sample // 8) * channels
|
||||
@@ -276,3 +283,16 @@ class AudioPlayer:
|
||||
if cls._i2s:
|
||||
cls._i2s.deinit()
|
||||
cls._i2s = None
|
||||
|
||||
|
||||
|
||||
def optional_viper(func):
|
||||
"""Decorator to apply @micropython.viper if possible."""
|
||||
try:
|
||||
@micropython.viper
|
||||
@func # Wait, no—see below for proper chaining
|
||||
def wrapped(*args, **kwargs):
|
||||
return func(*args, **kwargs)
|
||||
return wrapped
|
||||
except SyntaxError:
|
||||
return func # Fallback to original
|
||||
|
||||
Reference in New Issue
Block a user