Skip to content

Commit

Permalink
Compatibility improvements for WSL: sound errors and makefile without…
Browse files Browse the repository at this point in the history
… zsh
  • Loading branch information
krs013 committed Oct 25, 2023
1 parent 0b2c632 commit 98e6628
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 27 deletions.
29 changes: 14 additions & 15 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ PYPY := pypy3
ROOT_DIR := $(shell git rev-parse --show-toplevel)
GITHUB_REF ?= "refs/tags/v0.0.0"
PYBOY_VERSION ?= $(shell echo ${GITHUB_REF} | cut -d'/' -f3)
SHELL:=zsh -o extended_glob +o nomatch -c

dist: clean build
${PY} setup.py sdist bdist_wheel
Expand All @@ -33,20 +32,20 @@ build:

clean:
@echo "Cleaning..."
${SHELL} 'rm -rf PyBoy.egg-info'
${SHELL} 'rm -rf build'
${SHELL} 'rm -rf dist'
${SHELL} 'rm -rf pyboy/**/__pycache__'
${SHELL} 'rm -rf pyboy/**/*.pyo'
${SHELL} 'rm -rf pyboy/**/*.pyc'
${SHELL} 'rm -rf pyboy/**/*.pyd'
${SHELL} 'rm -rf pyboy/**/*.so'
${SHELL} 'rm -rf pyboy/**/*.c'
${SHELL} 'rm -rf pyboy/**/*.h'
${SHELL} 'rm -rf pyboy/**/*.dll'
${SHELL} 'rm -rf pyboy/**/*.lib'
${SHELL} 'rm -rf pyboy/**/*.exp'
${SHELL} 'rm -rf pyboy/**/*.html'
rm -rf PyBoy.egg-info
rm -rf build
rm -rf dist
find pyboy/ -type f -name "*.pyo" -delete
find pyboy/ -type f -name "*.pyc" -delete
find pyboy/ -type f -name "*.pyd" -delete
find pyboy/ -type f -name "*.so" -delete
find pyboy/ -type f -name "*.c" -delete
find pyboy/ -type f -name "*.h" -delete
find pyboy/ -type f -name "*.dll" -delete
find pyboy/ -type f -name "*.lib" -delete
find pyboy/ -type f -name "*.exp" -delete
find pyboy/ -type f -name "*.html" -delete
find pyboy/ -type d -name "__pycache__" -delete

install: build
${PY} -m pip install .
Expand Down
36 changes: 24 additions & 12 deletions pyboy/core/sound.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
# http://gbdev.gg8.se/wiki/articles/Gameboy_sound_hardware
# http://www.devrs.com/gb/files/hosted/GBSOUND.txt

import logging
from array import array
from ctypes import c_void_p

Expand All @@ -15,6 +16,8 @@
except ImportError:
sdl2 = None

logger = logging.getLogger(__name__)

SOUND_DESYNC_THRESHOLD = 5
CPU_FREQ = 4213440 # hz

Expand All @@ -25,21 +28,30 @@ def __init__(self, enabled, emulate):
self.emulate = emulate or enabled # Just emulate registers etc.
if self.enabled:
# Initialization is handled in the windows, otherwise we'd need this
sdl2.SDL_Init(sdl2.SDL_INIT_AUDIO)

# Open audio device
spec_want = sdl2.SDL_AudioSpec(32768, sdl2.AUDIO_S8, 2, 64)
spec_have = sdl2.SDL_AudioSpec(0, 0, 0, 0)
self.device = sdl2.SDL_OpenAudioDevice(None, 0, spec_want, spec_have, 0)

# Start playback (move out of __init__ if needed, maybe for headless)
sdl2.SDL_PauseAudioDevice(self.device, 0)
if sdl2.SDL_Init(sdl2.SDL_INIT_AUDIO) >= 0:
# Open audio device
spec_want = sdl2.SDL_AudioSpec(32768, sdl2.AUDIO_S8, 2, 64)
spec_have = sdl2.SDL_AudioSpec(0, 0, 0, 0)
self.device = sdl2.SDL_OpenAudioDevice(None, 0, spec_want, spec_have, 0)

if self.device > 1:
# Start playback (move out of __init__ if needed, maybe for headless)
sdl2.SDL_PauseAudioDevice(self.device, 0)

self.sample_rate = spec_have.freq
self.sampleclocks = CPU_FREQ // self.sample_rate
else:
logger.error("SDL_OpenAudioDevice failed: ", sdl2.SDL_GetError().encode())
self.enabled = False
else:
logger.error("SDL_Init audio failed: ", sdl2.SDL_GetError().encode())
self.enabled = False
self.device = 0

self.sample_rate = spec_have.freq
self.sampleclocks = CPU_FREQ // self.sample_rate
else:
if not self.enabled:
self.sample_rate = 32768
self.sampleclocks = CPU_FREQ // self.sample_rate

self.audiobuffer = array("b", [0] * 4096) # Over 2 frames
self.audiobuffer_p = c_void_p(self.audiobuffer.buffer_info()[0])

Expand Down

0 comments on commit 98e6628

Please sign in to comment.