added amp enable to audio.py

pull/916/head
thirdr 2024-03-27 14:38:01 +00:00
rodzic 193adaca72
commit b499296867
12 zmienionych plików z 48 dodań i 57 usunięć

Wyświetl plik

@ -5,7 +5,7 @@
import os import os
import math import math
import struct 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. 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_BITS_PER_SAMPLE = 16
TONE_FULL_WAVES = 2 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.__id = id
self.__sck_pin = sck_pin self.__sck_pin = sck_pin
self.__ws_pin = ws_pin self.__ws_pin = ws_pin
self.__sd_pin = sd_pin self.__sd_pin = sd_pin
self.__ibuf_len = ibuf_len 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 # Set the directory to search for files in
self.set_root(root) self.set_root(root)
@ -167,11 +171,17 @@ class WavPlayer:
self.__audio_out.irq(self.__i2s_callback) self.__audio_out.irq(self.__i2s_callback)
self.__audio_out.write(self.__silence_samples) self.__audio_out.write(self.__silence_samples)
if self.__enable is not None:
self.__enable.on()
def __stop_i2s(self): def __stop_i2s(self):
self.stop() # Stop any active playback self.stop() # Stop any active playback
while self.is_playing(): # and wait for it to complete while self.is_playing(): # and wait for it to complete
pass pass
if self.__enable is not None:
self.__enable.off()
if self.__audio_out is not None: if self.__audio_out is not None:
self.__audio_out.deinit() # Deinit any active I2S comms self.__audio_out.deinit() # Deinit any active I2S comms

Wyświetl plik

@ -1,4 +1,4 @@
from machine import Pin, Timer from machine import Timer
from audio import WavPlayer from audio import WavPlayer
from cosmic import CosmicUnicorn from cosmic import CosmicUnicorn
from picographics import PicoGraphics, DISPLAY_COSMIC_UNICORN as DISPLAY from picographics import PicoGraphics, DISPLAY_COSMIC_UNICORN as DISPLAY
@ -7,9 +7,6 @@ import time
cu = CosmicUnicorn() cu = CosmicUnicorn()
graphics = PicoGraphics(DISPLAY) graphics = PicoGraphics(DISPLAY)
amp_enable = Pin(22, Pin.OUT)
amp_enable.on()
graphics.set_font("bitmap6") graphics.set_font("bitmap6")
WHITE = graphics.create_pen(255, 255, 255) WHITE = graphics.create_pen(255, 255, 255)
BLUE = graphics.create_pen(0, 0, 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) GREEN = graphics.create_pen(0, 255, 0)
cu.set_brightness(0.7) cu.set_brightness(0.7)
audio = WavPlayer(0, 10, 11, 9) audio = WavPlayer(0, 10, 11, 9, amp_enable=22)
class Countdown(object): class Countdown(object):

Wyświetl plik

@ -1,4 +1,3 @@
from machine import Pin
from audio import WavPlayer from audio import WavPlayer
from cosmic import CosmicUnicorn from cosmic import CosmicUnicorn
from picographics import PicoGraphics, DISPLAY_COSMIC_UNICORN as DISPLAY from picographics import PicoGraphics, DISPLAY_COSMIC_UNICORN as DISPLAY
@ -7,10 +6,7 @@ from time import sleep
cu = CosmicUnicorn() cu = CosmicUnicorn()
graphics = PicoGraphics(DISPLAY) graphics = PicoGraphics(DISPLAY)
amp_enable = Pin(22, Pin.OUT) audio = WavPlayer(0, 10, 11, 9, amp_enable=22)
amp_enable.on()
audio = WavPlayer(0, 10, 11, 9)
WHITE = graphics.create_pen(255, 255, 255) WHITE = graphics.create_pen(255, 255, 255)
RED = graphics.create_pen(255, 0, 0) RED = graphics.create_pen(255, 0, 0)

Wyświetl plik

@ -1,12 +1,6 @@
from machine import Pin
from audio import WavPlayer from audio import WavPlayer
from cosmic import CosmicUnicorn
cu = CosmicUnicorn() sound = WavPlayer(0, 10, 11, 9, amp_enable=22)
amp_enable = Pin(22, Pin.OUT)
amp_enable.on()
sound = WavPlayer(0, 10, 11, 9)
sound.play_wav("beepboop.wav", False) sound.play_wav("beepboop.wav", False)

Wyświetl plik

@ -5,7 +5,7 @@
import os import os
import math import math
import struct 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. 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_BITS_PER_SAMPLE = 16
TONE_FULL_WAVES = 2 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.__id = id
self.__sck_pin = sck_pin self.__sck_pin = sck_pin
self.__ws_pin = ws_pin self.__ws_pin = ws_pin
self.__sd_pin = sd_pin self.__sd_pin = sd_pin
self.__ibuf_len = ibuf_len 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 # Set the directory to search for files in
self.set_root(root) self.set_root(root)
@ -167,11 +171,17 @@ class WavPlayer:
self.__audio_out.irq(self.__i2s_callback) self.__audio_out.irq(self.__i2s_callback)
self.__audio_out.write(self.__silence_samples) self.__audio_out.write(self.__silence_samples)
if self.__enable is not None:
self.__enable.on()
def __stop_i2s(self): def __stop_i2s(self):
self.stop() # Stop any active playback self.stop() # Stop any active playback
while self.is_playing(): # and wait for it to complete while self.is_playing(): # and wait for it to complete
pass pass
if self.__enable is not None:
self.__enable.off()
if self.__audio_out is not None: if self.__audio_out is not None:
self.__audio_out.deinit() # Deinit any active I2S comms self.__audio_out.deinit() # Deinit any active I2S comms

Wyświetl plik

@ -1,4 +1,4 @@
from machine import Pin, Timer from machine import Timer
from audio import WavPlayer from audio import WavPlayer
from galactic import GalacticUnicorn from galactic import GalacticUnicorn
from picographics import PicoGraphics, DISPLAY_GALACTIC_UNICORN as DISPLAY from picographics import PicoGraphics, DISPLAY_GALACTIC_UNICORN as DISPLAY
@ -7,9 +7,6 @@ import time
gu = GalacticUnicorn() gu = GalacticUnicorn()
graphics = PicoGraphics(DISPLAY) graphics = PicoGraphics(DISPLAY)
amp_enable = Pin(22, Pin.OUT)
amp_enable.on()
graphics.set_font("bitmap6") graphics.set_font("bitmap6")
WHITE = graphics.create_pen(255, 255, 255) WHITE = graphics.create_pen(255, 255, 255)
BLUE = graphics.create_pen(0, 0, 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) GREEN = graphics.create_pen(0, 255, 0)
gu.set_brightness(0.7) gu.set_brightness(0.7)
audio = WavPlayer(0, 10, 11, 9) audio = WavPlayer(0, 10, 11, 9, amp_enable=22)
class Countdown(object): class Countdown(object):

Wyświetl plik

@ -1,4 +1,3 @@
from machine import Pin
from audio import WavPlayer from audio import WavPlayer
from galactic import GalacticUnicorn from galactic import GalacticUnicorn
from picographics import PicoGraphics, DISPLAY_GALACTIC_UNICORN as DISPLAY from picographics import PicoGraphics, DISPLAY_GALACTIC_UNICORN as DISPLAY
@ -7,10 +6,7 @@ from time import sleep
gu = GalacticUnicorn() gu = GalacticUnicorn()
graphics = PicoGraphics(DISPLAY) graphics = PicoGraphics(DISPLAY)
amp_enable = Pin(22, Pin.OUT) audio = WavPlayer(0, 10, 11, 9, amp_enable=22)
amp_enable.on()
audio = WavPlayer(0, 10, 11, 9)
WHITE = graphics.create_pen(255, 255, 255) WHITE = graphics.create_pen(255, 255, 255)
RED = graphics.create_pen(255, 0, 0) RED = graphics.create_pen(255, 0, 0)

Wyświetl plik

@ -1,12 +1,6 @@
from machine import Pin
from audio import WavPlayer from audio import WavPlayer
from galactic import GalacticUnicorn
gu = GalacticUnicorn() sound = WavPlayer(0, 10, 11, 9, amp_enable=22)
amp_enable = Pin(22, Pin.OUT)
amp_enable.on()
sound = WavPlayer(0, 10, 11, 9)
sound.play_wav("beepboop.wav", False) sound.play_wav("beepboop.wav", False)

Wyświetl plik

@ -5,7 +5,7 @@
import os import os
import math import math
import struct 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. 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_BITS_PER_SAMPLE = 16
TONE_FULL_WAVES = 2 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.__id = id
self.__sck_pin = sck_pin self.__sck_pin = sck_pin
self.__ws_pin = ws_pin self.__ws_pin = ws_pin
self.__sd_pin = sd_pin self.__sd_pin = sd_pin
self.__ibuf_len = ibuf_len 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 # Set the directory to search for files in
self.set_root(root) self.set_root(root)
@ -167,11 +171,17 @@ class WavPlayer:
self.__audio_out.irq(self.__i2s_callback) self.__audio_out.irq(self.__i2s_callback)
self.__audio_out.write(self.__silence_samples) self.__audio_out.write(self.__silence_samples)
if self.__enable is not None:
self.__enable.on()
def __stop_i2s(self): def __stop_i2s(self):
self.stop() # Stop any active playback self.stop() # Stop any active playback
while self.is_playing(): # and wait for it to complete while self.is_playing(): # and wait for it to complete
pass pass
if self.__enable is not None:
self.__enable.off()
if self.__audio_out is not None: if self.__audio_out is not None:
self.__audio_out.deinit() # Deinit any active I2S comms self.__audio_out.deinit() # Deinit any active I2S comms

Wyświetl plik

@ -3,7 +3,7 @@
# Use VOL +/- to increase/decrease the amount of time # Use VOL +/- to increase/decrease the amount of time
# Use the Sleep ZZZ button to start the countdown # Use the Sleep ZZZ button to start the countdown
from machine import Pin, Timer from machine import Timer
from audio import WavPlayer from audio import WavPlayer
from stellar import StellarUnicorn from stellar import StellarUnicorn
from picographics import PicoGraphics, DISPLAY_STELLAR_UNICORN as DISPLAY from picographics import PicoGraphics, DISPLAY_STELLAR_UNICORN as DISPLAY
@ -12,9 +12,6 @@ import time
su = StellarUnicorn() su = StellarUnicorn()
graphics = PicoGraphics(DISPLAY) graphics = PicoGraphics(DISPLAY)
amp_enable = Pin(22, Pin.OUT)
amp_enable.on()
graphics.set_font("bitmap6") graphics.set_font("bitmap6")
WHITE = graphics.create_pen(255, 255, 255) WHITE = graphics.create_pen(255, 255, 255)
BLUE = graphics.create_pen(0, 0, 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) GREEN = graphics.create_pen(0, 255, 0)
su.set_brightness(0.5) su.set_brightness(0.5)
audio = WavPlayer(0, 10, 11, 9) audio = WavPlayer(0, 10, 11, 9, amp_enable=22)
class Countdown(object): class Countdown(object):

Wyświetl plik

@ -3,7 +3,6 @@
# Use Brightness +/- to move up and down # Use Brightness +/- to move up and down
# Press Sleep to play the selected sound # Press Sleep to play the selected sound
from machine import Pin
from audio import WavPlayer from audio import WavPlayer
from stellar import StellarUnicorn from stellar import StellarUnicorn
from picographics import PicoGraphics, DISPLAY_STELLAR_UNICORN as DISPLAY from picographics import PicoGraphics, DISPLAY_STELLAR_UNICORN as DISPLAY
@ -12,10 +11,7 @@ from time import sleep
su = StellarUnicorn() su = StellarUnicorn()
graphics = PicoGraphics(DISPLAY) graphics = PicoGraphics(DISPLAY)
amp_enable = Pin(22, Pin.OUT) audio = WavPlayer(0, 10, 11, 9, amp_enable=22)
amp_enable.on()
audio = WavPlayer(0, 10, 11, 9)
WHITE = graphics.create_pen(255, 255, 255) WHITE = graphics.create_pen(255, 255, 255)
RED = graphics.create_pen(255, 0, 0) RED = graphics.create_pen(255, 0, 0)

Wyświetl plik

@ -1,12 +1,6 @@
from machine import Pin
from audio import WavPlayer from audio import WavPlayer
from cosmic import CosmicUnicorn
cu = CosmicUnicorn() sound = WavPlayer(0, 10, 11, 9, amp_enable=22)
amp_enable = Pin(22, Pin.OUT)
amp_enable.on()
sound = WavPlayer(0, 10, 11, 9)
sound.play_wav("beepboop.wav", False) sound.play_wav("beepboop.wav", False)