From 199f6e6f5ad348df8c4848bbe7e39e818e3d42b7 Mon Sep 17 00:00:00 2001 From: Joe Baird Date: Tue, 19 Oct 2021 08:45:57 -0700 Subject: [PATCH 1/5] add ability to stop a non-blocking audio file being played and turn off the audio --- adafruit_pyportal/peripherals.py | 15 ++++++++++++--- 1 file changed, 12 insertions(+), 3 deletions(-) diff --git a/adafruit_pyportal/peripherals.py b/adafruit_pyportal/peripherals.py index eb8b297..69646c0 100755 --- a/adafruit_pyportal/peripherals.py +++ b/adafruit_pyportal/peripherals.py @@ -62,6 +62,7 @@ def __init__(self, spi, display, splash_group, debug=False): self.audio = audioio.AudioOut(board.SPEAKER) else: raise AttributeError("Board does not have a builtin speaker!") + self.wavfile = None if debug: print("Init SD Card") @@ -144,17 +145,25 @@ def play_file(self, file_name, wait_to_finish=True): """Play a wav file. :param str file_name: The name of the wav file to play on the speaker. + :param bool wait_to_finish: flag to determine if this is a blocking call """ - wavfile = open(file_name, "rb") - wavedata = audiocore.WaveFile(wavfile) + self.wavfile = open(file_name, "rb") + wavedata = audiocore.WaveFile(self.wavfile) self._speaker_enable.value = True self.audio.play(wavedata) if not wait_to_finish: return while self.audio.playing: pass - wavfile.close() + self.wavfile.close() + self._speaker_enable.value = False + + def stop_play(self): + """Stops playing a wav file. + """ + if self.wavfile is not None: + self.wavfile.close() self._speaker_enable.value = False def sd_check(self): From f0fc73466b132a9345465db97bf395a7793977c9 Mon Sep 17 00:00:00 2001 From: Joe Baird Date: Tue, 19 Oct 2021 09:36:11 -0700 Subject: [PATCH 2/5] add missing passthrough function all to the stop_play --- adafruit_pyportal/__init__.py | 1 + 1 file changed, 1 insertion(+) diff --git a/adafruit_pyportal/__init__.py b/adafruit_pyportal/__init__.py index 6f4e3e5..3a1f750 100755 --- a/adafruit_pyportal/__init__.py +++ b/adafruit_pyportal/__init__.py @@ -185,6 +185,7 @@ def __init__( self.set_backlight = self.peripherals.set_backlight self.sd_check = self.peripherals.sd_check self.play_file = self.peripherals.play_file + self.stop_play = self.peripherals.stop_play self.image_converter_url = self.network.image_converter_url self.wget = self.network.wget From 4aeab9896ecfa5c0cd91889337af4965bfcacf50 Mon Sep 17 00:00:00 2001 From: Joe Baird Date: Tue, 19 Oct 2021 09:52:36 -0700 Subject: [PATCH 3/5] fix white space on comment caught by pre-commit --- adafruit_pyportal/peripherals.py | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/adafruit_pyportal/peripherals.py b/adafruit_pyportal/peripherals.py index 69646c0..08bb590 100755 --- a/adafruit_pyportal/peripherals.py +++ b/adafruit_pyportal/peripherals.py @@ -160,8 +160,7 @@ def play_file(self, file_name, wait_to_finish=True): self._speaker_enable.value = False def stop_play(self): - """Stops playing a wav file. - """ + """Stops playing a wav file.""" if self.wavfile is not None: self.wavfile.close() self._speaker_enable.value = False From 986e4d65d9cb825ddd54fb98d48015ce00b2b02c Mon Sep 17 00:00:00 2001 From: Joe Baird Date: Tue, 19 Oct 2021 11:55:41 -0700 Subject: [PATCH 4/5] calling stop on the audio before shutting it down --- adafruit_pyportal/peripherals.py | 1 + 1 file changed, 1 insertion(+) diff --git a/adafruit_pyportal/peripherals.py b/adafruit_pyportal/peripherals.py index 08bb590..fa3d87f 100755 --- a/adafruit_pyportal/peripherals.py +++ b/adafruit_pyportal/peripherals.py @@ -161,6 +161,7 @@ def play_file(self, file_name, wait_to_finish=True): def stop_play(self): """Stops playing a wav file.""" + self.audio.stop() if self.wavfile is not None: self.wavfile.close() self._speaker_enable.value = False From 02a7b8648fb0f3f7b5b197ac140be1a977da302a Mon Sep 17 00:00:00 2001 From: foamyguy Date: Sun, 12 Dec 2021 14:30:14 -0600 Subject: [PATCH 5/5] pylint exception --- adafruit_pyportal/peripherals.py | 3 +++ 1 file changed, 3 insertions(+) diff --git a/adafruit_pyportal/peripherals.py b/adafruit_pyportal/peripherals.py index fa3d87f..3076cfc 100755 --- a/adafruit_pyportal/peripherals.py +++ b/adafruit_pyportal/peripherals.py @@ -148,6 +148,9 @@ def play_file(self, file_name, wait_to_finish=True): :param bool wait_to_finish: flag to determine if this is a blocking call """ + + # pylint: disable=consider-using-with + # can't use `with` because we need wavefile to remain open after return self.wavfile = open(file_name, "rb") wavedata = audiocore.WaveFile(self.wavfile) self._speaker_enable.value = True