diff --git a/platforms/python/examples/pygame-audio.py b/platforms/python/examples/pygame-audio.py new file mode 100644 index 0000000..c4deb6c --- /dev/null +++ b/platforms/python/examples/pygame-audio.py @@ -0,0 +1,75 @@ +#!/usr/bin/env python3 + +import os, struct, time +import multiprocessing as mp +import wasm3 +import numpy + +os.environ['PYGAME_HIDE_SUPPORT_PROMPT'] = "true" + +def consumer(conn): + import pygame + pygame.mixer.pre_init(frequency=44100, size=-16, channels=2) + pygame.init() + + channel = pygame.mixer.Channel(0) + while True: + chunk = pygame.mixer.Sound(buffer=conn.recv()) + + indicator = '|' if channel.get_queue() else '.' + print(indicator, end='', flush=True) + while channel.get_queue() != None: + time.sleep(0.01) + + channel.queue(chunk) + + pygame.quit() + + +if __name__ == '__main__': + print("Hondarribia - Intro song WebAssembly Summit 2020 by Peter Salomonsen") + print("Source: https://petersalomonsen.com/webassemblymusic/livecodev2/?gist=5b795090ead4f192e7f5ee5dcdd17392") + print("Synthesized: https://soundcloud.com/psalomo/hondarribia") + + conn, child_conn = mp.Pipe() + p = mp.Process(target=consumer, args=(child_conn,)) + p.start() + + scriptpath = os.path.dirname(os.path.realpath(__file__)) + wasm_fn = os.path.join(scriptpath, "./wasm/hondarribia.wasm") + + # Prepare Wasm3 engine + + env = wasm3.Environment() + rt = env.new_runtime(1024) + with open(wasm_fn, "rb") as f: + mod = env.parse_module(f.read()) + rt.load(mod) + + buff = b'' + buff_sz = 256 + print("Pre-buffering...") + + def fd_write(fd, wasi_iovs, iows_len, nwritten): + global buff, buff_sz + mem = rt.get_memory(0) + + # decode + (off, size) = struct.unpack(" buff_sz*1024: + conn.send(buff) + buff = b'' + buff_sz = 64 + + return size + + mod.link_function("wasi_unstable", "fd_write", "i(i*i*)", fd_write) + + wasm_start = rt.find_function("_start") + wasm_start() + + p.join() diff --git a/platforms/python/examples/wasm/hondarribia.wasm b/platforms/python/examples/wasm/hondarribia.wasm new file mode 100644 index 0000000..e58fbd7 Binary files /dev/null and b/platforms/python/examples/wasm/hondarribia.wasm differ