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.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)
|
||||
|
||||
@@ -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()
|
||||
|
||||
Reference in New Issue
Block a user