From 77ea029e98c235f7bed6b5c205da7ae9c3d07ff6 Mon Sep 17 00:00:00 2001 From: Volodymyr Shymanskyy Date: Fri, 2 Apr 2021 00:10:49 +0300 Subject: [PATCH] Update Hondarribia sample --- platforms/python/examples/pygame-audio.py | 55 ++++++++++-------- ...ibia_22050.wasm => hondarribia-22050.wasm} | Bin ...ondarribia.wasm => hondarribia-44100.wasm} | Bin 3 files changed, 31 insertions(+), 24 deletions(-) rename platforms/python/examples/wasm/{hondarribia_22050.wasm => hondarribia-22050.wasm} (100%) rename platforms/python/examples/wasm/{hondarribia.wasm => hondarribia-44100.wasm} (100%) diff --git a/platforms/python/examples/pygame-audio.py b/platforms/python/examples/pygame-audio.py index 35653a0..9261b7f 100644 --- a/platforms/python/examples/pygame-audio.py +++ b/platforms/python/examples/pygame-audio.py @@ -7,32 +7,34 @@ import numpy os.environ['PYGAME_HIDE_SUPPORT_PROMPT'] = "true" +sample_rate = 22050 # or 44100 + def player(q): import pygame - pygame.mixer.pre_init(frequency=22050, size=-16, channels=2) + pygame.mixer.pre_init(frequency=sample_rate, size=-16, channels=2) pygame.init() channel = pygame.mixer.Channel(0) - while True: - buff = q.get() - if not len(buff): - break - chunk = pygame.mixer.Sound(buffer=buff) - - indicator = '|' if channel.get_queue() else '.' - print(indicator, end='', flush=True) + try: + while True: + chunk = pygame.mixer.Sound(buffer=q.get()) - while channel.get_queue() is not None: - time.sleep(0.01) + indicator = '|' if channel.get_queue() else '.' + print(indicator, end='', flush=True) - channel.queue(chunk) + while channel.get_queue() is not None: + time.sleep(0.01) - pygame.quit() + channel.queue(chunk) + except: + pass + finally: + pygame.quit() if __name__ == '__main__': - print("Hondarribia - Intro song WebAssembly Summit 2020 by Peter Salomonsen") + print("Hondarribia - intro song for WebAssembly Summit 2020 by Peter Salomonsen") print("Source: https://petersalomonsen.com/webassemblymusic/livecodev2/?gist=5b795090ead4f192e7f5ee5dcdd17392") print("Synthesized: https://soundcloud.com/psalomo/hondarribia") @@ -41,7 +43,7 @@ if __name__ == '__main__': p.start() scriptpath = os.path.dirname(os.path.realpath(__file__)) - wasm_fn = os.path.join(scriptpath, "./wasm/hondarribia_22050.wasm") + wasm_fn = os.path.join(scriptpath, f"./wasm/hondarribia-{sample_rate}.wasm") # Prepare Wasm3 engine @@ -51,9 +53,9 @@ if __name__ == '__main__': mod = env.parse_module(f.read()) rt.load(mod) - buff = b'' - buff_sz = 512 print("Pre-buffering...") + buff = b'' + buff_sz = 1024 def fd_write(fd, wasi_iovs, iows_len, nwritten): global buff, buff_sz @@ -70,20 +72,25 @@ if __name__ == '__main__': # buffer buff += data if len(buff) > buff_sz*1024: + #print('+', end='', flush=True) q.put(buff) buff = b'' - buff_sz = 128 + buff_sz = 64 return size - mod.link_function("wasi_snapshot_preview1", "fd_write", "i(i*i*)", fd_write) + for modname in ["wasi_unstable", "wasi_snapshot_preview1"]: + mod.link_function(modname, "fd_write", "i(i*i*)", fd_write) wasm_start = rt.find_function("_start") - wasm_start() - - q.put(b'') - - p.join() + try: + wasm_start() + q.put(buff) + except: + pass + finally: + q.put(None) + p.join() print() print("Finished") diff --git a/platforms/python/examples/wasm/hondarribia_22050.wasm b/platforms/python/examples/wasm/hondarribia-22050.wasm similarity index 100% rename from platforms/python/examples/wasm/hondarribia_22050.wasm rename to platforms/python/examples/wasm/hondarribia-22050.wasm diff --git a/platforms/python/examples/wasm/hondarribia.wasm b/platforms/python/examples/wasm/hondarribia-44100.wasm similarity index 100% rename from platforms/python/examples/wasm/hondarribia.wasm rename to platforms/python/examples/wasm/hondarribia-44100.wasm