From b499296867e855069cecc6566d54969f2da8854d Mon Sep 17 00:00:00 2001 From: thirdr Date: Wed, 27 Mar 2024 14:38:01 +0000 Subject: [PATCH] added amp enable to audio.py --- micropython/examples/cosmic_unicorn/audio/audio.py | 14 ++++++++++++-- .../cosmic_unicorn/audio/countdown_with_alarm.py | 7 ++----- .../audio/example_menu_with_sound.py | 6 +----- .../cosmic_unicorn/audio/simple_playback.py | 8 +------- .../examples/galactic_unicorn/audio/audio.py | 14 ++++++++++++-- .../galactic_unicorn/audio/countdown_with_alarm.py | 7 ++----- .../audio/example_menu_with_sound.py | 6 +----- .../galactic_unicorn/audio/simple_playback.py | 8 +------- .../examples/stellar_unicorn/audio/audio.py | 14 ++++++++++++-- .../stellar_unicorn/audio/countdown_with_alarm.py | 7 ++----- .../audio/example_menu_with_sound.py | 6 +----- .../stellar_unicorn/audio/simple_playback.py | 8 +------- 12 files changed, 48 insertions(+), 57 deletions(-) diff --git a/micropython/examples/cosmic_unicorn/audio/audio.py b/micropython/examples/cosmic_unicorn/audio/audio.py index 717a1659..10ce5858 100644 --- a/micropython/examples/cosmic_unicorn/audio/audio.py +++ b/micropython/examples/cosmic_unicorn/audio/audio.py @@ -5,7 +5,7 @@ import os import math import struct -from machine import I2S +from machine import I2S, Pin """ A class for playing Wav files out of an I2S audio amp. It can also play pure tones. @@ -34,12 +34,16 @@ class WavPlayer: TONE_BITS_PER_SAMPLE = 16 TONE_FULL_WAVES = 2 - def __init__(self, id, sck_pin, ws_pin, sd_pin, ibuf_len=INTERNAL_BUFFER_LENGTH, root="/"): + def __init__(self, id, sck_pin, ws_pin, sd_pin, amp_enable=None, ibuf_len=INTERNAL_BUFFER_LENGTH, root="/"): self.__id = id self.__sck_pin = sck_pin self.__ws_pin = ws_pin self.__sd_pin = sd_pin self.__ibuf_len = ibuf_len + self.__enable = None + + if amp_enable is not None: + self.__enable = Pin(amp_enable, Pin.OUT) # Set the directory to search for files in self.set_root(root) @@ -167,11 +171,17 @@ class WavPlayer: self.__audio_out.irq(self.__i2s_callback) self.__audio_out.write(self.__silence_samples) + if self.__enable is not None: + self.__enable.on() + def __stop_i2s(self): self.stop() # Stop any active playback while self.is_playing(): # and wait for it to complete pass + if self.__enable is not None: + self.__enable.off() + if self.__audio_out is not None: self.__audio_out.deinit() # Deinit any active I2S comms diff --git a/micropython/examples/cosmic_unicorn/audio/countdown_with_alarm.py b/micropython/examples/cosmic_unicorn/audio/countdown_with_alarm.py index da31b494..2828ae04 100644 --- a/micropython/examples/cosmic_unicorn/audio/countdown_with_alarm.py +++ b/micropython/examples/cosmic_unicorn/audio/countdown_with_alarm.py @@ -1,4 +1,4 @@ -from machine import Pin, Timer +from machine import Timer from audio import WavPlayer from cosmic import CosmicUnicorn from picographics import PicoGraphics, DISPLAY_COSMIC_UNICORN as DISPLAY @@ -7,9 +7,6 @@ import time cu = CosmicUnicorn() graphics = PicoGraphics(DISPLAY) -amp_enable = Pin(22, Pin.OUT) -amp_enable.on() - graphics.set_font("bitmap6") WHITE = graphics.create_pen(255, 255, 255) BLUE = graphics.create_pen(0, 0, 255) @@ -18,7 +15,7 @@ RED = graphics.create_pen(255, 0, 0) GREEN = graphics.create_pen(0, 255, 0) cu.set_brightness(0.7) -audio = WavPlayer(0, 10, 11, 9) +audio = WavPlayer(0, 10, 11, 9, amp_enable=22) class Countdown(object): diff --git a/micropython/examples/cosmic_unicorn/audio/example_menu_with_sound.py b/micropython/examples/cosmic_unicorn/audio/example_menu_with_sound.py index 30f70d71..8917daa8 100644 --- a/micropython/examples/cosmic_unicorn/audio/example_menu_with_sound.py +++ b/micropython/examples/cosmic_unicorn/audio/example_menu_with_sound.py @@ -1,4 +1,3 @@ -from machine import Pin from audio import WavPlayer from cosmic import CosmicUnicorn from picographics import PicoGraphics, DISPLAY_COSMIC_UNICORN as DISPLAY @@ -7,10 +6,7 @@ from time import sleep cu = CosmicUnicorn() graphics = PicoGraphics(DISPLAY) -amp_enable = Pin(22, Pin.OUT) -amp_enable.on() - -audio = WavPlayer(0, 10, 11, 9) +audio = WavPlayer(0, 10, 11, 9, amp_enable=22) WHITE = graphics.create_pen(255, 255, 255) RED = graphics.create_pen(255, 0, 0) diff --git a/micropython/examples/cosmic_unicorn/audio/simple_playback.py b/micropython/examples/cosmic_unicorn/audio/simple_playback.py index 1e531c49..61b19cdc 100644 --- a/micropython/examples/cosmic_unicorn/audio/simple_playback.py +++ b/micropython/examples/cosmic_unicorn/audio/simple_playback.py @@ -1,12 +1,6 @@ -from machine import Pin from audio import WavPlayer -from cosmic import CosmicUnicorn -cu = CosmicUnicorn() -amp_enable = Pin(22, Pin.OUT) -amp_enable.on() - -sound = WavPlayer(0, 10, 11, 9) +sound = WavPlayer(0, 10, 11, 9, amp_enable=22) sound.play_wav("beepboop.wav", False) diff --git a/micropython/examples/galactic_unicorn/audio/audio.py b/micropython/examples/galactic_unicorn/audio/audio.py index 717a1659..10ce5858 100644 --- a/micropython/examples/galactic_unicorn/audio/audio.py +++ b/micropython/examples/galactic_unicorn/audio/audio.py @@ -5,7 +5,7 @@ import os import math import struct -from machine import I2S +from machine import I2S, Pin """ A class for playing Wav files out of an I2S audio amp. It can also play pure tones. @@ -34,12 +34,16 @@ class WavPlayer: TONE_BITS_PER_SAMPLE = 16 TONE_FULL_WAVES = 2 - def __init__(self, id, sck_pin, ws_pin, sd_pin, ibuf_len=INTERNAL_BUFFER_LENGTH, root="/"): + def __init__(self, id, sck_pin, ws_pin, sd_pin, amp_enable=None, ibuf_len=INTERNAL_BUFFER_LENGTH, root="/"): self.__id = id self.__sck_pin = sck_pin self.__ws_pin = ws_pin self.__sd_pin = sd_pin self.__ibuf_len = ibuf_len + self.__enable = None + + if amp_enable is not None: + self.__enable = Pin(amp_enable, Pin.OUT) # Set the directory to search for files in self.set_root(root) @@ -167,11 +171,17 @@ class WavPlayer: self.__audio_out.irq(self.__i2s_callback) self.__audio_out.write(self.__silence_samples) + if self.__enable is not None: + self.__enable.on() + def __stop_i2s(self): self.stop() # Stop any active playback while self.is_playing(): # and wait for it to complete pass + if self.__enable is not None: + self.__enable.off() + if self.__audio_out is not None: self.__audio_out.deinit() # Deinit any active I2S comms diff --git a/micropython/examples/galactic_unicorn/audio/countdown_with_alarm.py b/micropython/examples/galactic_unicorn/audio/countdown_with_alarm.py index f1c7548e..a08ee8ba 100644 --- a/micropython/examples/galactic_unicorn/audio/countdown_with_alarm.py +++ b/micropython/examples/galactic_unicorn/audio/countdown_with_alarm.py @@ -1,4 +1,4 @@ -from machine import Pin, Timer +from machine import Timer from audio import WavPlayer from galactic import GalacticUnicorn from picographics import PicoGraphics, DISPLAY_GALACTIC_UNICORN as DISPLAY @@ -7,9 +7,6 @@ import time gu = GalacticUnicorn() graphics = PicoGraphics(DISPLAY) -amp_enable = Pin(22, Pin.OUT) -amp_enable.on() - graphics.set_font("bitmap6") WHITE = graphics.create_pen(255, 255, 255) BLUE = graphics.create_pen(0, 0, 255) @@ -18,7 +15,7 @@ RED = graphics.create_pen(255, 0, 0) GREEN = graphics.create_pen(0, 255, 0) gu.set_brightness(0.7) -audio = WavPlayer(0, 10, 11, 9) +audio = WavPlayer(0, 10, 11, 9, amp_enable=22) class Countdown(object): diff --git a/micropython/examples/galactic_unicorn/audio/example_menu_with_sound.py b/micropython/examples/galactic_unicorn/audio/example_menu_with_sound.py index 2b9dacd1..9e2dc23e 100644 --- a/micropython/examples/galactic_unicorn/audio/example_menu_with_sound.py +++ b/micropython/examples/galactic_unicorn/audio/example_menu_with_sound.py @@ -1,4 +1,3 @@ -from machine import Pin from audio import WavPlayer from galactic import GalacticUnicorn from picographics import PicoGraphics, DISPLAY_GALACTIC_UNICORN as DISPLAY @@ -7,10 +6,7 @@ from time import sleep gu = GalacticUnicorn() graphics = PicoGraphics(DISPLAY) -amp_enable = Pin(22, Pin.OUT) -amp_enable.on() - -audio = WavPlayer(0, 10, 11, 9) +audio = WavPlayer(0, 10, 11, 9, amp_enable=22) WHITE = graphics.create_pen(255, 255, 255) RED = graphics.create_pen(255, 0, 0) diff --git a/micropython/examples/galactic_unicorn/audio/simple_playback.py b/micropython/examples/galactic_unicorn/audio/simple_playback.py index fc27e044..61b19cdc 100644 --- a/micropython/examples/galactic_unicorn/audio/simple_playback.py +++ b/micropython/examples/galactic_unicorn/audio/simple_playback.py @@ -1,12 +1,6 @@ -from machine import Pin from audio import WavPlayer -from galactic import GalacticUnicorn -gu = GalacticUnicorn() -amp_enable = Pin(22, Pin.OUT) -amp_enable.on() - -sound = WavPlayer(0, 10, 11, 9) +sound = WavPlayer(0, 10, 11, 9, amp_enable=22) sound.play_wav("beepboop.wav", False) diff --git a/micropython/examples/stellar_unicorn/audio/audio.py b/micropython/examples/stellar_unicorn/audio/audio.py index 717a1659..10ce5858 100644 --- a/micropython/examples/stellar_unicorn/audio/audio.py +++ b/micropython/examples/stellar_unicorn/audio/audio.py @@ -5,7 +5,7 @@ import os import math import struct -from machine import I2S +from machine import I2S, Pin """ A class for playing Wav files out of an I2S audio amp. It can also play pure tones. @@ -34,12 +34,16 @@ class WavPlayer: TONE_BITS_PER_SAMPLE = 16 TONE_FULL_WAVES = 2 - def __init__(self, id, sck_pin, ws_pin, sd_pin, ibuf_len=INTERNAL_BUFFER_LENGTH, root="/"): + def __init__(self, id, sck_pin, ws_pin, sd_pin, amp_enable=None, ibuf_len=INTERNAL_BUFFER_LENGTH, root="/"): self.__id = id self.__sck_pin = sck_pin self.__ws_pin = ws_pin self.__sd_pin = sd_pin self.__ibuf_len = ibuf_len + self.__enable = None + + if amp_enable is not None: + self.__enable = Pin(amp_enable, Pin.OUT) # Set the directory to search for files in self.set_root(root) @@ -167,11 +171,17 @@ class WavPlayer: self.__audio_out.irq(self.__i2s_callback) self.__audio_out.write(self.__silence_samples) + if self.__enable is not None: + self.__enable.on() + def __stop_i2s(self): self.stop() # Stop any active playback while self.is_playing(): # and wait for it to complete pass + if self.__enable is not None: + self.__enable.off() + if self.__audio_out is not None: self.__audio_out.deinit() # Deinit any active I2S comms diff --git a/micropython/examples/stellar_unicorn/audio/countdown_with_alarm.py b/micropython/examples/stellar_unicorn/audio/countdown_with_alarm.py index 2af76258..08dbdc25 100644 --- a/micropython/examples/stellar_unicorn/audio/countdown_with_alarm.py +++ b/micropython/examples/stellar_unicorn/audio/countdown_with_alarm.py @@ -3,7 +3,7 @@ # Use VOL +/- to increase/decrease the amount of time # Use the Sleep ZZZ button to start the countdown -from machine import Pin, Timer +from machine import Timer from audio import WavPlayer from stellar import StellarUnicorn from picographics import PicoGraphics, DISPLAY_STELLAR_UNICORN as DISPLAY @@ -12,9 +12,6 @@ import time su = StellarUnicorn() graphics = PicoGraphics(DISPLAY) -amp_enable = Pin(22, Pin.OUT) -amp_enable.on() - graphics.set_font("bitmap6") WHITE = graphics.create_pen(255, 255, 255) BLUE = graphics.create_pen(0, 0, 255) @@ -23,7 +20,7 @@ RED = graphics.create_pen(255, 0, 0) GREEN = graphics.create_pen(0, 255, 0) su.set_brightness(0.5) -audio = WavPlayer(0, 10, 11, 9) +audio = WavPlayer(0, 10, 11, 9, amp_enable=22) class Countdown(object): diff --git a/micropython/examples/stellar_unicorn/audio/example_menu_with_sound.py b/micropython/examples/stellar_unicorn/audio/example_menu_with_sound.py index b54b6471..aef90e08 100644 --- a/micropython/examples/stellar_unicorn/audio/example_menu_with_sound.py +++ b/micropython/examples/stellar_unicorn/audio/example_menu_with_sound.py @@ -3,7 +3,6 @@ # Use Brightness +/- to move up and down # Press Sleep to play the selected sound -from machine import Pin from audio import WavPlayer from stellar import StellarUnicorn from picographics import PicoGraphics, DISPLAY_STELLAR_UNICORN as DISPLAY @@ -12,10 +11,7 @@ from time import sleep su = StellarUnicorn() graphics = PicoGraphics(DISPLAY) -amp_enable = Pin(22, Pin.OUT) -amp_enable.on() - -audio = WavPlayer(0, 10, 11, 9) +audio = WavPlayer(0, 10, 11, 9, amp_enable=22) WHITE = graphics.create_pen(255, 255, 255) RED = graphics.create_pen(255, 0, 0) diff --git a/micropython/examples/stellar_unicorn/audio/simple_playback.py b/micropython/examples/stellar_unicorn/audio/simple_playback.py index 1e531c49..61b19cdc 100644 --- a/micropython/examples/stellar_unicorn/audio/simple_playback.py +++ b/micropython/examples/stellar_unicorn/audio/simple_playback.py @@ -1,12 +1,6 @@ -from machine import Pin from audio import WavPlayer -from cosmic import CosmicUnicorn -cu = CosmicUnicorn() -amp_enable = Pin(22, Pin.OUT) -amp_enable.on() - -sound = WavPlayer(0, 10, 11, 9) +sound = WavPlayer(0, 10, 11, 9, amp_enable=22) sound.play_wav("beepboop.wav", False)