Browse Source

Semplifica la gestione audio rimuovendo il supporto per più dispositivi e centralizzando l'uso di un singolo dispositivo audio

master
Matteo Benedetto 12 months ago
parent
commit
ccf7769979
  1. 18
      engine/sdl2.py
  2. 4
      rats.py

18
engine/sdl2.py

@ -38,9 +38,10 @@ class GameWindow:
self.key_down, self.key_up, self.axis_scroll = key_callback self.key_down, self.key_up, self.axis_scroll = key_callback
self.performance = 0 self.performance = 0
self.audio_devs = [] self.audio_devs = []
for i in range(5): #for i in range(5):
self.audio_devs.append((True, sdl2.SDL_OpenAudioDevice(None, 0, SDL_AudioSpec(freq=22050, aformat=sdl2.AUDIO_U8, channels=1, samples=2048), None, 0))) # self.audio_devs.append((True, sdl2.SDL_OpenAudioDevice(None, 0, SDL_AudioSpec(freq=22050, aformat=sdl2.AUDIO_U8, channels=1, samples=2048), None, 0)))
self.audio_dev = sdl2.SDL_OpenAudioDevice(None, 0, SDL_AudioSpec(freq=22050, aformat=sdl2.AUDIO_U8, channels=1, samples=2048), None, 0)
def create_texture(self, tiles: list): def create_texture(self, tiles: list):
bg_surface = sdl2.SDL_CreateRGBSurface(0, self.width, self.height, 32, 0, 0, 0, 0) bg_surface = sdl2.SDL_CreateRGBSurface(0, self.width, self.height, 32, 0, 0, 0, 0)
for tile in tiles: for tile in tiles:
@ -240,20 +241,9 @@ class GameWindow:
if sdl2.SDL_LoadWAV_RW(rw, 1, byref(spec), byref(_buf), byref(_length)) == None: if sdl2.SDL_LoadWAV_RW(rw, 1, byref(spec), byref(_buf), byref(_length)) == None:
raise RuntimeError("Failed to load WAV") raise RuntimeError("Failed to load WAV")
devid = None devid = self.audio_dev
while not devid:
id = random.randint(0, 4)
for idx, dev in enumerate(self.audio_devs):
if dev[0]:
devid = dev[1]
sdl2.SDL_ClearQueuedAudio(devid) sdl2.SDL_ClearQueuedAudio(devid)
self.audio_devs[id] = (False, devid)
break
if not devid:
devid = self.audio_devs[id][1]
sdl2.SDL_ClearQueuedAudio(devid) sdl2.SDL_ClearQueuedAudio(devid)
self.audio_devs[random.randint(0, 4)] = (True, devid)
# Start playing audio # Start playing audio
sdl2.SDL_QueueAudio(devid, _buf, _length) sdl2.SDL_QueueAudio(devid, _buf, _length)

4
rats.py

@ -142,12 +142,16 @@ class MiceMaze(controls.KeyBindings):
self.engine.scroll_view(self.pointer) self.engine.scroll_view(self.pointer)
def play_sound(self, sound_file,tag="main"): def play_sound(self, sound_file,tag="main"):
self.engine.play_sound(sound_file)
return
if self.audio: if self.audio:
if len(self.sounds) > 5: if len(self.sounds) > 5:
self.sounds.pop(next(iter(self.sounds))).kill() self.sounds.pop(next(iter(self.sounds))).kill()
self.sounds[f"{tag}_{random.random()}"] = subprocess.Popen(["aplay", f"sound/{sound_file}"]) self.sounds[f"{tag}_{random.random()}"] = subprocess.Popen(["aplay", f"sound/{sound_file}"])
def stop_sound(self, tag=None): def stop_sound(self, tag=None):
self.engine.stop_sound()
return
for key, value in self.sounds.copy().items(): for key, value in self.sounds.copy().items():
if tag is None or tag in key: if tag is None or tag in key:
value.kill() value.kill()

Loading…
Cancel
Save