Skip to content

Commit b499296

Browse files
committed
added amp enable to audio.py
1 parent 193adac commit b499296

File tree

12 files changed

+48
-57
lines changed

12 files changed

+48
-57
lines changed

micropython/examples/cosmic_unicorn/audio/audio.py

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
import os
66
import math
77
import struct
8-
from machine import I2S
8+
from machine import I2S, Pin
99

1010
"""
1111
A class for playing Wav files out of an I2S audio amp. It can also play pure tones.
@@ -34,12 +34,16 @@ class WavPlayer:
3434
TONE_BITS_PER_SAMPLE = 16
3535
TONE_FULL_WAVES = 2
3636

37-
def __init__(self, id, sck_pin, ws_pin, sd_pin, ibuf_len=INTERNAL_BUFFER_LENGTH, root="/"):
37+
def __init__(self, id, sck_pin, ws_pin, sd_pin, amp_enable=None, ibuf_len=INTERNAL_BUFFER_LENGTH, root="/"):
3838
self.__id = id
3939
self.__sck_pin = sck_pin
4040
self.__ws_pin = ws_pin
4141
self.__sd_pin = sd_pin
4242
self.__ibuf_len = ibuf_len
43+
self.__enable = None
44+
45+
if amp_enable is not None:
46+
self.__enable = Pin(amp_enable, Pin.OUT)
4347

4448
# Set the directory to search for files in
4549
self.set_root(root)
@@ -167,11 +171,17 @@ def __start_i2s(self, bits=16, format=I2S.MONO, rate=44_100, state=STOP, mode=MO
167171
self.__audio_out.irq(self.__i2s_callback)
168172
self.__audio_out.write(self.__silence_samples)
169173

174+
if self.__enable is not None:
175+
self.__enable.on()
176+
170177
def __stop_i2s(self):
171178
self.stop() # Stop any active playback
172179
while self.is_playing(): # and wait for it to complete
173180
pass
174181

182+
if self.__enable is not None:
183+
self.__enable.off()
184+
175185
if self.__audio_out is not None:
176186
self.__audio_out.deinit() # Deinit any active I2S comms
177187

micropython/examples/cosmic_unicorn/audio/countdown_with_alarm.py

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
from machine import Pin, Timer
1+
from machine import Timer
22
from audio import WavPlayer
33
from cosmic import CosmicUnicorn
44
from picographics import PicoGraphics, DISPLAY_COSMIC_UNICORN as DISPLAY
@@ -7,9 +7,6 @@
77
cu = CosmicUnicorn()
88
graphics = PicoGraphics(DISPLAY)
99

10-
amp_enable = Pin(22, Pin.OUT)
11-
amp_enable.on()
12-
1310
graphics.set_font("bitmap6")
1411
WHITE = graphics.create_pen(255, 255, 255)
1512
BLUE = graphics.create_pen(0, 0, 255)
@@ -18,7 +15,7 @@
1815
GREEN = graphics.create_pen(0, 255, 0)
1916
cu.set_brightness(0.7)
2017

21-
audio = WavPlayer(0, 10, 11, 9)
18+
audio = WavPlayer(0, 10, 11, 9, amp_enable=22)
2219

2320

2421
class Countdown(object):

micropython/examples/cosmic_unicorn/audio/example_menu_with_sound.py

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
from machine import Pin
21
from audio import WavPlayer
32
from cosmic import CosmicUnicorn
43
from picographics import PicoGraphics, DISPLAY_COSMIC_UNICORN as DISPLAY
@@ -7,10 +6,7 @@
76
cu = CosmicUnicorn()
87
graphics = PicoGraphics(DISPLAY)
98

10-
amp_enable = Pin(22, Pin.OUT)
11-
amp_enable.on()
12-
13-
audio = WavPlayer(0, 10, 11, 9)
9+
audio = WavPlayer(0, 10, 11, 9, amp_enable=22)
1410

1511
WHITE = graphics.create_pen(255, 255, 255)
1612
RED = graphics.create_pen(255, 0, 0)

micropython/examples/cosmic_unicorn/audio/simple_playback.py

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,6 @@
1-
from machine import Pin
21
from audio import WavPlayer
3-
from cosmic import CosmicUnicorn
42

5-
cu = CosmicUnicorn()
6-
amp_enable = Pin(22, Pin.OUT)
7-
amp_enable.on()
8-
9-
sound = WavPlayer(0, 10, 11, 9)
3+
sound = WavPlayer(0, 10, 11, 9, amp_enable=22)
104

115
sound.play_wav("beepboop.wav", False)
126

micropython/examples/galactic_unicorn/audio/audio.py

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
import os
66
import math
77
import struct
8-
from machine import I2S
8+
from machine import I2S, Pin
99

1010
"""
1111
A class for playing Wav files out of an I2S audio amp. It can also play pure tones.
@@ -34,12 +34,16 @@ class WavPlayer:
3434
TONE_BITS_PER_SAMPLE = 16
3535
TONE_FULL_WAVES = 2
3636

37-
def __init__(self, id, sck_pin, ws_pin, sd_pin, ibuf_len=INTERNAL_BUFFER_LENGTH, root="/"):
37+
def __init__(self, id, sck_pin, ws_pin, sd_pin, amp_enable=None, ibuf_len=INTERNAL_BUFFER_LENGTH, root="/"):
3838
self.__id = id
3939
self.__sck_pin = sck_pin
4040
self.__ws_pin = ws_pin
4141
self.__sd_pin = sd_pin
4242
self.__ibuf_len = ibuf_len
43+
self.__enable = None
44+
45+
if amp_enable is not None:
46+
self.__enable = Pin(amp_enable, Pin.OUT)
4347

4448
# Set the directory to search for files in
4549
self.set_root(root)
@@ -167,11 +171,17 @@ def __start_i2s(self, bits=16, format=I2S.MONO, rate=44_100, state=STOP, mode=MO
167171
self.__audio_out.irq(self.__i2s_callback)
168172
self.__audio_out.write(self.__silence_samples)
169173

174+
if self.__enable is not None:
175+
self.__enable.on()
176+
170177
def __stop_i2s(self):
171178
self.stop() # Stop any active playback
172179
while self.is_playing(): # and wait for it to complete
173180
pass
174181

182+
if self.__enable is not None:
183+
self.__enable.off()
184+
175185
if self.__audio_out is not None:
176186
self.__audio_out.deinit() # Deinit any active I2S comms
177187

micropython/examples/galactic_unicorn/audio/countdown_with_alarm.py

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
from machine import Pin, Timer
1+
from machine import Timer
22
from audio import WavPlayer
33
from galactic import GalacticUnicorn
44
from picographics import PicoGraphics, DISPLAY_GALACTIC_UNICORN as DISPLAY
@@ -7,9 +7,6 @@
77
gu = GalacticUnicorn()
88
graphics = PicoGraphics(DISPLAY)
99

10-
amp_enable = Pin(22, Pin.OUT)
11-
amp_enable.on()
12-
1310
graphics.set_font("bitmap6")
1411
WHITE = graphics.create_pen(255, 255, 255)
1512
BLUE = graphics.create_pen(0, 0, 255)
@@ -18,7 +15,7 @@
1815
GREEN = graphics.create_pen(0, 255, 0)
1916
gu.set_brightness(0.7)
2017

21-
audio = WavPlayer(0, 10, 11, 9)
18+
audio = WavPlayer(0, 10, 11, 9, amp_enable=22)
2219

2320

2421
class Countdown(object):

micropython/examples/galactic_unicorn/audio/example_menu_with_sound.py

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
from machine import Pin
21
from audio import WavPlayer
32
from galactic import GalacticUnicorn
43
from picographics import PicoGraphics, DISPLAY_GALACTIC_UNICORN as DISPLAY
@@ -7,10 +6,7 @@
76
gu = GalacticUnicorn()
87
graphics = PicoGraphics(DISPLAY)
98

10-
amp_enable = Pin(22, Pin.OUT)
11-
amp_enable.on()
12-
13-
audio = WavPlayer(0, 10, 11, 9)
9+
audio = WavPlayer(0, 10, 11, 9, amp_enable=22)
1410

1511
WHITE = graphics.create_pen(255, 255, 255)
1612
RED = graphics.create_pen(255, 0, 0)

micropython/examples/galactic_unicorn/audio/simple_playback.py

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,6 @@
1-
from machine import Pin
21
from audio import WavPlayer
3-
from galactic import GalacticUnicorn
42

5-
gu = GalacticUnicorn()
6-
amp_enable = Pin(22, Pin.OUT)
7-
amp_enable.on()
8-
9-
sound = WavPlayer(0, 10, 11, 9)
3+
sound = WavPlayer(0, 10, 11, 9, amp_enable=22)
104

115
sound.play_wav("beepboop.wav", False)
126

micropython/examples/stellar_unicorn/audio/audio.py

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
import os
66
import math
77
import struct
8-
from machine import I2S
8+
from machine import I2S, Pin
99

1010
"""
1111
A class for playing Wav files out of an I2S audio amp. It can also play pure tones.
@@ -34,12 +34,16 @@ class WavPlayer:
3434
TONE_BITS_PER_SAMPLE = 16
3535
TONE_FULL_WAVES = 2
3636

37-
def __init__(self, id, sck_pin, ws_pin, sd_pin, ibuf_len=INTERNAL_BUFFER_LENGTH, root="/"):
37+
def __init__(self, id, sck_pin, ws_pin, sd_pin, amp_enable=None, ibuf_len=INTERNAL_BUFFER_LENGTH, root="/"):
3838
self.__id = id
3939
self.__sck_pin = sck_pin
4040
self.__ws_pin = ws_pin
4141
self.__sd_pin = sd_pin
4242
self.__ibuf_len = ibuf_len
43+
self.__enable = None
44+
45+
if amp_enable is not None:
46+
self.__enable = Pin(amp_enable, Pin.OUT)
4347

4448
# Set the directory to search for files in
4549
self.set_root(root)
@@ -167,11 +171,17 @@ def __start_i2s(self, bits=16, format=I2S.MONO, rate=44_100, state=STOP, mode=MO
167171
self.__audio_out.irq(self.__i2s_callback)
168172
self.__audio_out.write(self.__silence_samples)
169173

174+
if self.__enable is not None:
175+
self.__enable.on()
176+
170177
def __stop_i2s(self):
171178
self.stop() # Stop any active playback
172179
while self.is_playing(): # and wait for it to complete
173180
pass
174181

182+
if self.__enable is not None:
183+
self.__enable.off()
184+
175185
if self.__audio_out is not None:
176186
self.__audio_out.deinit() # Deinit any active I2S comms
177187

micropython/examples/stellar_unicorn/audio/countdown_with_alarm.py

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
# Use VOL +/- to increase/decrease the amount of time
44
# Use the Sleep ZZZ button to start the countdown
55

6-
from machine import Pin, Timer
6+
from machine import Timer
77
from audio import WavPlayer
88
from stellar import StellarUnicorn
99
from picographics import PicoGraphics, DISPLAY_STELLAR_UNICORN as DISPLAY
@@ -12,9 +12,6 @@
1212
su = StellarUnicorn()
1313
graphics = PicoGraphics(DISPLAY)
1414

15-
amp_enable = Pin(22, Pin.OUT)
16-
amp_enable.on()
17-
1815
graphics.set_font("bitmap6")
1916
WHITE = graphics.create_pen(255, 255, 255)
2017
BLUE = graphics.create_pen(0, 0, 255)
@@ -23,7 +20,7 @@
2320
GREEN = graphics.create_pen(0, 255, 0)
2421
su.set_brightness(0.5)
2522

26-
audio = WavPlayer(0, 10, 11, 9)
23+
audio = WavPlayer(0, 10, 11, 9, amp_enable=22)
2724

2825

2926
class Countdown(object):

0 commit comments

Comments
 (0)