From f72b3e55afc7cbc13921ccacfa7b401006933093 Mon Sep 17 00:00:00 2001 From: jepler Date: Wed, 11 Sep 2019 17:29:13 -0500 Subject: [PATCH 1/7] tone: Work with 4.x, 5.0; AudioOut and PWMAudioOut .. by creating new private functions for getting the right AudioOut and RawSample objects. INCOMPATIBLE CHANGE: this removes support for CircuitPython versions before 3.x in tone() Testing performed: On CPB, `tone(board.SPEAKER, 440)`. On Metro M4 Express, `tone(board.A0, 440). Both tests were using CircuitPython 5.0.alpha versions. Closes: #45 --- simpleio.py | 58 ++++++++++++++++++++++++++++++++++++----------------- 1 file changed, 40 insertions(+), 18 deletions(-) diff --git a/simpleio.py b/simpleio.py index c9a1ade..fb44581 100644 --- a/simpleio.py +++ b/simpleio.py @@ -29,14 +29,44 @@ """ import time import sys -try: - import audioio -except ImportError: - pass # not always supported by every board! import array import digitalio import pulseio +def _AudioOut(pin): + try: + import audioio + except ImportError: + pass + else: + return audioio.AudioOut(pin) + + try: + import audiopwmio + except ImportError: + pass + else: + return audiopwmio.PWMAudioOut(pin) + + raise RuntimeError("AudioOut not available") + +def _RawSample(buffer): + try: + import audiocore + except ImportError: + pass + else: + return audiocore.RawSample(buffer) + + try: + import audioio + except ImportError: + pass + else: + return audioio.RawSample(buffer) + + raise RuntimeError("AudioOut not available") + def tone(pin, frequency, duration=1, length=100): """ Generates a square wave of the specified frequency on a pin @@ -61,21 +91,13 @@ def tone(pin, frequency, duration=1, length=100): square_wave = array.array("H", [0] * sample_length) for i in range(sample_length / 2): square_wave[i] = 0xFFFF - if sys.implementation.version[0] >= 3: - square_wave_sample = audioio.RawSample(square_wave) - square_wave_sample.sample_rate = int(len(square_wave) * frequency) - with audioio.AudioOut(pin) as dac: - if not dac.playing: - dac.play(square_wave_sample, loop=True) - time.sleep(duration) - dac.stop() - else: - sample_tone = audioio.AudioOut(pin, square_wave) - sample_tone.frequency = int(len(square_wave) * frequency) - if not sample_tone.playing: - sample_tone.play(loop=True) + square_wave_sample = _RawSample(square_wave) + square_wave_sample.sample_rate = int(len(square_wave) * frequency) + with _AudioOut(pin) as dac: + if not dac.playing: + dac.play(square_wave_sample, loop=True) time.sleep(duration) - sample_tone.stop() + dac.stop() From 18807c1acc089bb7bb579da4199da8cdb573c466 Mon Sep 17 00:00:00 2001 From: jepler Date: Wed, 11 Sep 2019 17:36:53 -0500 Subject: [PATCH 2/7] pylint: Silence some expected pylint errors These functions are deliberately named with a leading underscore to indicate that they are private functions to the simpleio module. --- simpleio.py | 2 ++ 1 file changed, 2 insertions(+) diff --git a/simpleio.py b/simpleio.py index fb44581..f8fe8cd 100644 --- a/simpleio.py +++ b/simpleio.py @@ -33,6 +33,7 @@ import digitalio import pulseio +#pylint: disable=invalid-name def _AudioOut(pin): try: import audioio @@ -66,6 +67,7 @@ def _RawSample(buffer): return audioio.RawSample(buffer) raise RuntimeError("AudioOut not available") +#pylint: enable=invalid-name def tone(pin, frequency, duration=1, length=100): """ From 76b86b4277db6d77e460e65c2bcf63a455d57b1a Mon Sep 17 00:00:00 2001 From: jepler Date: Wed, 11 Sep 2019 17:37:09 -0500 Subject: [PATCH 3/7] pylint: remove unneeded import According to pylint, this import is not needed, as `sys` is never referenced --- simpleio.py | 1 - 1 file changed, 1 deletion(-) diff --git a/simpleio.py b/simpleio.py index f8fe8cd..09bf8a4 100644 --- a/simpleio.py +++ b/simpleio.py @@ -28,7 +28,6 @@ * Author(s): Scott Shawcroft """ import time -import sys import array import digitalio import pulseio From 992f6b152ca39783ce87400ea4dd3ea613337f19 Mon Sep 17 00:00:00 2001 From: jepler Date: Wed, 11 Sep 2019 19:19:56 -0500 Subject: [PATCH 4/7] revisions based on review comments from @ladyada --- simpleio.py | 45 +++++++++++---------------------------------- 1 file changed, 11 insertions(+), 34 deletions(-) diff --git a/simpleio.py b/simpleio.py index 09bf8a4..397f8b8 100644 --- a/simpleio.py +++ b/simpleio.py @@ -32,41 +32,18 @@ import digitalio import pulseio -#pylint: disable=invalid-name -def _AudioOut(pin): +try: + from audioio import AudioOut +except ImportError: try: - import audioio + from audiopwmio import PWMAudioOut as AudioOut except ImportError: - pass - else: - return audioio.AudioOut(pin) - - try: - import audiopwmio - except ImportError: - pass - else: - return audiopwmio.PWMAudioOut(pin) - - raise RuntimeError("AudioOut not available") - -def _RawSample(buffer): - try: - import audiocore - except ImportError: - pass - else: - return audiocore.RawSample(buffer) - - try: - import audioio - except ImportError: - pass - else: - return audioio.RawSample(buffer) + pass # not always supported by every board! - raise RuntimeError("AudioOut not available") -#pylint: enable=invalid-name +try: + import audiocore +except ImportError: + import audioio as audiocore def tone(pin, frequency, duration=1, length=100): """ @@ -92,9 +69,9 @@ def tone(pin, frequency, duration=1, length=100): square_wave = array.array("H", [0] * sample_length) for i in range(sample_length / 2): square_wave[i] = 0xFFFF - square_wave_sample = _RawSample(square_wave) + square_wave_sample = audiocore.RawSample(square_wave) square_wave_sample.sample_rate = int(len(square_wave) * frequency) - with _AudioOut(pin) as dac: + with AudioOut(pin) as dac: if not dac.playing: dac.play(square_wave_sample, loop=True) time.sleep(duration) From 9a580b57213a261550ee53fb988b4d1e5f93d583 Mon Sep 17 00:00:00 2001 From: jepler Date: Wed, 11 Sep 2019 20:18:48 -0500 Subject: [PATCH 5/7] Rearrange imports to satisfy linter error `ungrouped-imports` --- simpleio.py | 10 ++++------ 1 file changed, 4 insertions(+), 6 deletions(-) diff --git a/simpleio.py b/simpleio.py index 397f8b8..c5c2ef2 100644 --- a/simpleio.py +++ b/simpleio.py @@ -31,7 +31,10 @@ import array import digitalio import pulseio - +try: + import audiocore +except ImportError: + import audioio as audiocore try: from audioio import AudioOut except ImportError: @@ -40,11 +43,6 @@ except ImportError: pass # not always supported by every board! -try: - import audiocore -except ImportError: - import audioio as audiocore - def tone(pin, frequency, duration=1, length=100): """ Generates a square wave of the specified frequency on a pin From 4bc5cfc5135854557c30bf1a1be328385842bf76 Mon Sep 17 00:00:00 2001 From: jepler Date: Wed, 11 Sep 2019 20:22:30 -0500 Subject: [PATCH 6/7] fix audioio import for boards without it (and docs) --- simpleio.py | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/simpleio.py b/simpleio.py index c5c2ef2..4b66202 100644 --- a/simpleio.py +++ b/simpleio.py @@ -34,7 +34,10 @@ try: import audiocore except ImportError: - import audioio as audiocore + try: + import audioio as audiocore + except ImportError: + pass # not always supported by every board! try: from audioio import AudioOut except ImportError: From 429426bf28102d1b2a1aa46b39ff582fc28b83f8 Mon Sep 17 00:00:00 2001 From: jepler Date: Fri, 13 Sep 2019 20:39:51 -0500 Subject: [PATCH 7/7] Further rearrange audio imports * where possible, check with sys.implementation.version, instead of try/except * reduce overall number of try/except clauses --- simpleio.py | 20 ++++++++++---------- 1 file changed, 10 insertions(+), 10 deletions(-) diff --git a/simpleio.py b/simpleio.py index 4b66202..8437dd9 100644 --- a/simpleio.py +++ b/simpleio.py @@ -28,23 +28,23 @@ * Author(s): Scott Shawcroft """ import time +import sys import array import digitalio import pulseio try: - import audiocore -except ImportError: - try: + # RawSample was moved in CircuitPython 5.x. + if sys.implementation.version[0] >= 5: + import audiocore + else: import audioio as audiocore - except ImportError: - pass # not always supported by every board! -try: - from audioio import AudioOut -except ImportError: + # Some boards have AudioOut (true DAC), others have PWMAudioOut. try: - from audiopwmio import PWMAudioOut as AudioOut + from audioio import AudioOut except ImportError: - pass # not always supported by every board! + from audiopwmio import PWMAudioOut as AudioOut +except ImportError: + pass # not always supported by every board! def tone(pin, frequency, duration=1, length=100): """