Skip to content

Commit

Permalink
Compatibility improvements for WSL: sound errors
Browse files Browse the repository at this point in the history
  • Loading branch information
krs013 authored and Baekalfen committed Oct 25, 2023
1 parent 169191e commit 8e27df9
Showing 1 changed file with 23 additions and 12 deletions.
35 changes: 23 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,29 @@ 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: %s", sdl2.SDL_GetError().decode())
self.enabled = False # We will continue with emulation
else:
logger.error("SDL_Init audio failed: %s", sdl2.SDL_GetError().decode())
self.enabled = False # We will continue with emulation

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 8e27df9

Please sign in to comment.