From ccf7769979041cc61f6b9a511cb37a667f8e9028 Mon Sep 17 00:00:00 2001 From: Matteo Benedetto Date: Thu, 26 Dec 2024 01:14:26 +0100 Subject: [PATCH] =?UTF-8?q?Semplifica=20la=20gestione=20audio=20rimuovendo?= =?UTF-8?q?=20il=20supporto=20per=20pi=C3=B9=20dispositivi=20e=20centraliz?= =?UTF-8?q?zando=20l'uso=20di=20un=20singolo=20dispositivo=20audio?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- engine/sdl2.py | 22 ++++++---------------- rats.py | 4 ++++ 2 files changed, 10 insertions(+), 16 deletions(-) diff --git a/engine/sdl2.py b/engine/sdl2.py index 4f7e416..656b434 100644 --- a/engine/sdl2.py +++ b/engine/sdl2.py @@ -38,9 +38,10 @@ class GameWindow: self.key_down, self.key_up, self.axis_scroll = key_callback self.performance = 0 self.audio_devs = [] - 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))) + #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_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): bg_surface = sdl2.SDL_CreateRGBSurface(0, self.width, self.height, 32, 0, 0, 0, 0) for tile in tiles: @@ -240,20 +241,9 @@ class GameWindow: if sdl2.SDL_LoadWAV_RW(rw, 1, byref(spec), byref(_buf), byref(_length)) == None: raise RuntimeError("Failed to load WAV") - devid = None - 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) - self.audio_devs[id] = (False, devid) - break - if not devid: - - devid = self.audio_devs[id][1] - sdl2.SDL_ClearQueuedAudio(devid) - self.audio_devs[random.randint(0, 4)] = (True, devid) + devid = self.audio_dev + sdl2.SDL_ClearQueuedAudio(devid) + sdl2.SDL_ClearQueuedAudio(devid) # Start playing audio sdl2.SDL_QueueAudio(devid, _buf, _length) diff --git a/rats.py b/rats.py index d074e16..e1e0fdb 100644 --- a/rats.py +++ b/rats.py @@ -142,12 +142,16 @@ class MiceMaze(controls.KeyBindings): self.engine.scroll_view(self.pointer) def play_sound(self, sound_file,tag="main"): + self.engine.play_sound(sound_file) + return if self.audio: if len(self.sounds) > 5: self.sounds.pop(next(iter(self.sounds))).kill() self.sounds[f"{tag}_{random.random()}"] = subprocess.Popen(["aplay", f"sound/{sound_file}"]) def stop_sound(self, tag=None): + self.engine.stop_sound() + return for key, value in self.sounds.copy().items(): if tag is None or tag in key: value.kill()