Semplifica la gestione audio rimuovendo il supporto per più dispositivi e centralizzando l'uso di un singolo dispositivo audio
This commit is contained in:
+6
-16
@@ -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:
|
sdl2.SDL_ClearQueuedAudio(devid)
|
||||||
id = random.randint(0, 4)
|
sdl2.SDL_ClearQueuedAudio(devid)
|
||||||
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)
|
|
||||||
|
|
||||||
# Start playing audio
|
# Start playing audio
|
||||||
sdl2.SDL_QueueAudio(devid, _buf, _length)
|
sdl2.SDL_QueueAudio(devid, _buf, _length)
|
||||||
|
|||||||
@@ -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()
|
||||||
|
|||||||
Reference in New Issue
Block a user