From d69983ce435d042aae5f186be7e4dd9cb30e5010 Mon Sep 17 00:00:00 2001 From: Volodymyr Shymanskyy Date: Thu, 1 Apr 2021 17:09:40 +0300 Subject: [PATCH] pygame audio example: exit gracefully --- platforms/python/examples/pygame-audio.py | 35 ++++++++++++++++------- 1 file changed, 25 insertions(+), 10 deletions(-) diff --git a/platforms/python/examples/pygame-audio.py b/platforms/python/examples/pygame-audio.py index c4deb6c..8263d97 100644 --- a/platforms/python/examples/pygame-audio.py +++ b/platforms/python/examples/pygame-audio.py @@ -7,17 +7,21 @@ import numpy os.environ['PYGAME_HIDE_SUPPORT_PROMPT'] = "true" -def consumer(conn): +def player(q): 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()) + 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) + while channel.get_queue() != None: time.sleep(0.01) @@ -27,12 +31,13 @@ def consumer(conn): 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,)) + q = mp.Queue() + p = mp.Process(target=player, args=(q,)) p.start() scriptpath = os.path.dirname(os.path.realpath(__file__)) @@ -47,23 +52,27 @@ if __name__ == '__main__': rt.load(mod) buff = b'' - buff_sz = 256 + buff_sz = 512 print("Pre-buffering...") def fd_write(fd, wasi_iovs, iows_len, nwritten): global buff, buff_sz mem = rt.get_memory(0) - # decode + # get data (off, size) = struct.unpack(" buff_sz*1024: - conn.send(buff) + q.put(buff) buff = b'' - buff_sz = 64 + buff_sz = 128 return size @@ -72,4 +81,10 @@ if __name__ == '__main__': wasm_start = rt.find_function("_start") wasm_start() + q.put(b'') + p.join() + + print() + print("Finished") +