Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix sound #625

Merged
merged 2 commits into from
May 19, 2021
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 29 additions & 1 deletion ppb/systems/sound.py
Original file line number Diff line number Diff line change
@@ -1,14 +1,18 @@
import ctypes
import io
import time

from sdl2 import (
AUDIO_S16SYS, rw_from_object,
)

from sdl2.sdlmixer import (
# Errors, https://www.libsdl.org/projects/SDL_mixer/docs/SDL_mixer_7.html#SEC7
Mix_GetError,
# Support library loading https://www.libsdl.org/projects/SDL_mixer/docs/SDL_mixer_7.html#SEC7
Mix_Init, Mix_Quit, MIX_INIT_FLAC, MIX_INIT_MOD, MIX_INIT_MP3, MIX_INIT_OGG,
# Mixer init https://www.libsdl.org/projects/SDL_mixer/docs/SDL_mixer_7.html#SEC7
Mix_OpenAudio, Mix_CloseAudio,
Mix_OpenAudio, Mix_CloseAudio, Mix_QuerySpec,
# Samples https://www.libsdl.org/projects/SDL_mixer/docs/SDL_mixer_16.html#SEC16
Mix_LoadWAV_RW, Mix_FreeChunk, Mix_VolumeChunk,
# Channels https://www.libsdl.org/projects/SDL_mixer/docs/SDL_mixer_25.html#SEC25
Expand All @@ -24,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 @@ -97,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