Skip to content

Commit

Permalink
Band-aid over wrong-order running
Browse files Browse the repository at this point in the history
  • Loading branch information
AstraLuma committed May 18, 2021
1 parent 55ebaab commit 7eca130
Showing 1 changed file with 25 additions and 14 deletions.
39 changes: 25 additions & 14 deletions ppb/systems/sound.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import ctypes
import io
import time

from sdl2 import (
AUDIO_S16SYS, rw_from_object,
Expand Down Expand Up @@ -27,10 +28,31 @@
__all__ = ('SoundController', 'Sound')


def query_spec():
"""
Helpful wrapper around Mix_QuerySpec()
"""
frequency = ctypes.c_int()
format = ctypes.c_uint16()
channels = ctypes.c_int()
count = mix_call(
Mix_QuerySpec,
ctypes.byref(frequency),
ctypes.byref(format),
ctypes.byref(channels),
_check_error=lambda rv: rv == 0 and Mix_GetError(),
)
return count, frequency, format, channels


class Sound(assetlib.Asset):
# This is wrapping a ctypes.POINTER(Mix_Chunk)

def background_parse(self, data):
# Band-aid over some synchronization issues
# https://github.com/ppb/pursuedpybear/issues/619
while not any(query_spec()):
time.sleep(0)
file = rw_from_object(io.BytesIO(data))
# ^^^^ is a pure-python emulation, does not need cleanup.
return mix_call(
Expand Down Expand Up @@ -66,20 +88,6 @@ def _filler_channel_finished(channel):
pass


def query_spec():
frequency = ctypes.c_int()
format = ctypes.c_uint16()
channels = ctypes.c_int()
count = mix_call(
Mix_QuerySpec,
ctypes.byref(frequency),
ctypes.byref(format),
ctypes.byref(channels),
_check_error=lambda rv: rv == 0 and Mix_GetError(),
)
return count, frequency, format, channels


class SoundController(SdlSubSystem, LoggingMixin):
_finished_callback = None

Expand Down Expand Up @@ -114,6 +122,9 @@ def __enter__(self):
_check_error=lambda rv: rv == -1
)
mix_call(Mix_Init, MIX_INIT_FLAC | MIX_INIT_MOD | MIX_INIT_MP3 | MIX_INIT_OGG)

print("SoundController", query_spec(), flush=True)

self.allocated_channels = 16

# Register callback, keeping reference for later cleanup
Expand Down

0 comments on commit 7eca130

Please sign in to comment.