soundd: use with statement for wave.open to ensure proper resource cleanup (#34815)

use with statement for wave.open to ensure proper resource cleanup
pull/34720/merge
Dean Lee 2 months ago committed by GitHub
parent 3e629acf79
commit fa79b29cba
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
  1. 13
      selfdrive/ui/soundd.py

@ -69,14 +69,13 @@ class Soundd:
for sound in sound_list: for sound in sound_list:
filename, play_count, volume = sound_list[sound] filename, play_count, volume = sound_list[sound]
wavefile = wave.open(BASEDIR + "/selfdrive/assets/sounds/" + filename, 'r') with wave.open(BASEDIR + "/selfdrive/assets/sounds/" + filename, 'r') as wavefile:
assert wavefile.getnchannels() == 1
assert wavefile.getsampwidth() == 2
assert wavefile.getframerate() == SAMPLE_RATE
assert wavefile.getnchannels() == 1 length = wavefile.getnframes()
assert wavefile.getsampwidth() == 2 self.loaded_sounds[sound] = np.frombuffer(wavefile.readframes(length), dtype=np.int16).astype(np.float32) / (2**16/2)
assert wavefile.getframerate() == SAMPLE_RATE
length = wavefile.getnframes()
self.loaded_sounds[sound] = np.frombuffer(wavefile.readframes(length), dtype=np.int16).astype(np.float32) / (2**16/2)
def get_sound_data(self, frames): # get "frames" worth of data from the current alert sound, looping when required def get_sound_data(self, frames): # get "frames" worth of data from the current alert sound, looping when required

Loading…
Cancel
Save