8000 Move the Audio class to ugame.py · python-ugame/circuitpython-stage@f6d4ea3 · GitHub
[go: up one dir, main page]

Skip to content

Commit f6d4ea3

Browse files
committed
Move the Audio class to ugame.py
The Audio class is very different on different boards, so it should really live in the board-specific module. This also avoids having to check the existence of different audio libraries.
1 parent d0f1c46 commit f6d4ea3

File tree

4 files changed

+89
-62
lines changed

4 files changed

+89
-62
lines changed

pewpew_m4/ugame.py

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33
import supervisor
44
import time
55
import keypad
6+
import audioio
7+
import audiocore
68

79

810
K_X = 0x01
@@ -50,6 +52,28 @@ def get_pressed(self):
5052
return buttons
5153

5254

55+
class _Audio:
56+
last_audio = None
57+
58+
def __init__(self, speaker_pin):
59+
self.muted = True
60+
self.buffer = bytearray(128)
61+
self.audio = audioio.AudioOut(speaker_pin)
62+
63+
def play(self, audio_file, loop=False):
64+
if self.muted:
65+
return
66+
self.stop()
67+
wave = audiocore.WaveFile(audio_file, self.buffer)
68+
self.audio.play(wave, loop=loop)
69+
70+
def stop(self):
71+
self.audio.stop()
72+
73+
def mute(self, value=True):
74+
self.muted = value
75+
76+
5377
display = board.DISPLAY
5478
buttons = _Buttons()
55-
audio = stage.Audio(board.SPEAKER)
79+
audio = _Audio(board.SPEAKER)

pybadge/ugame.py

Lines changed: 32 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,11 @@
11
import board
2-
"""
3-
A helper module that initializes the display and buttons for the
4-
Adafruit PyBadge game console.
5-
"""
6-
72
import stage
83
import displayio
94
import busio
105
import time
116
import keypad
7+
import audioio
8+
import audiocore
129

1310

1411
K_X = 0x01
@@ -80,6 +77,35 @@ def get_pressed(self):
8077
return buttons
8178

8279

80+
class _Audio:
81+
last_audio = None
82+
83+
def __init__(self, speaker_pin, mute_pin=None):
84+
self.muted = True
85+
self.buffer = bytearray(128)
86+
if mute_pin:
87+
self.mute_pin = digitalio.DigitalInOut(mute_pin)
88+
self.mute_pin.switch_to_output(value=not self.muted)
89+
else:
90+
self.mute_pin = None
91+
self.audio = audioio.AudioOut(speaker_pin)
92+
93+
def play(self, audio_file, loop=False):
94+
if self.muted:
95+
return
96+
self.stop()
97+
wave = audiocore.WaveFile(audio_file, self.buffer)
98+
self.audio.play(wave, loop=loop)
99+
100+
def stop(self):
101+
self.audio.stop()
102+
103+
def mute(self, value=True):
104+
self.muted = value
105+
if self.mute_pin:
106+
self.mute_pin.value = not value
107+
108+
83109
displayio.release_displays()
84110
_tft_spi = busio.SPI(clock=board.TFT_SCK, MOSI=board.TFT_MOSI)
85111
_fourwire = displayio.FourWire(_tft_spi, command=board.TFT_DC,
@@ -89,4 +115,4 @@ def get_pressed(self):
89115
auto_refresh=False, auto_brightness=True)
90116
del _TFT_INIT
91117
buttons = _Buttons()
92-
audio = stage.Audio(board.SPEAKER, board.SPEAKER_ENABLE)
118+
audio = _Audio(board.SPEAKER, board.SPEAKER_ENABLE)

pygamer/ugame.py

Lines changed: 32 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,12 @@
1-
"""
2-
A helper module that initializes the display and buttons for the
3-
Adafruit PyGamer game console.
4-
"""
5-
61
import board
72
import analogio
83
import stage
94
import displayio
105
import busio
116
import time
127
import keypad
8+
import audioio
9+
import audiocore
1310

1411

1512
K_X = 0x01
@@ -94,6 +91,35 @@ def get_pressed(self):
9491
return buttons
9592

9693

94+
class _Audio:
95+
last_audio = None
96+
97+
def __init__(self, speaker_pin, mute_pin=None):
98+
self.muted = True
99+
self.buffer = bytearray(128)
100+
if mute_pin:
101+
self.mute_pin = digitalio.DigitalInOut(mute_pin)
102+
self.mute_pin.switch_to_output(value=not self.muted)
103+
else:
104+
self.mute_pin = None
105+
self.audio = audioio.AudioOut(speaker_pin)
106+
107+
def play(self, audio_file, loop=False):
108+
if self.muted:
109+
return
110+
self.stop()
111+
wave = audiocore.WaveFile(audio_file, self.buffer)
112+
self.audio.play(wave, loop=loop)
113+
114+
def stop(self):
115+
self.audio.stop()
116+
117+
def mute(self, value=True):
118+
self.muted = value
119+
if self.mute_pin:
120+
self.mute_pin.value = not value
121+
122+
97123
displayio.release_displays()
98124
_tft_spi = busio.SPI(clock=board.TFT_SCK, MOSI=board.TFT_MOSI)
99125
_fourwire = displayio.FourWire(_tft_spi, command=board.TFT_DC,
@@ -103,4 +129,4 @@ def get_pressed(self):
103129
auto_refresh=False, auto_brightness=True)
104130
del _TFT_INIT
105131
buttons = _Buttons()
106-
audio = stage.Audio(board.SPEAKER, board.SPEAKER_ENABLE)
132+
audio = _Audio(board.SPEAKER, board.SPEAKER_ENABLE)

stage.py

Lines changed: 0 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,6 @@
11
import time
22
import array
33
import digitalio
4-
try:
5-
import audioio
6-
except ImportError:
7-
pass
8-
else:
9-
try:
10-
import audiocore
11-
except ImportError:
12-
audiocore = audioio
134
import struct
145

156
import _stage
@@ -152,46 +143,6 @@ def collide(ax0, ay0, ax1, ay1, bx0, by0, bx1=None, by1=None):
152143
return not (ax1 < bx0 or ay1 < by0 or ax0 > bx1 or ay0 > by1)
153144

154145

155-
class Audio:
156-
"""Play sounds."""
157-
last_audio = None
158-
159-
def __init__(self, speaker_pin, mute_pin=None):
160-
self.muted = True
161-
self.buffer = bytearray(128)
162-
if mute_pin:
163-
self.mute_pin = digitalio.DigitalInOut(mute_pin)
164-
self.mute_pin.switch_to_output(value=not self.muted)
165-
else:
166-
self.mute_pin = None
167-
if audioio is None:
168-
self.audio = audiopwmio.PWMAudioOut(speaker_pin)
169-
else:
170-
self.audio = audioio.AudioOut(speaker_pin)
171-
172-
def play(self, audio_file, loop=False):
173-
"""
174-
Start playing an open file ``audio_file``. If ``loop`` is ``True``,
175-
repeat until stopped. This function doesn't block, the sound is
176-
played in the background.
177-
"""
178-
if self.muted:
179-
return
180-
self.stop()
181-
wave = audiocore.WaveFile(audio_file, self.buffer)
182-
self.audio.play(wave, loop=loop)
183-
184-
def stop(self):
185-
"""Stop playing whatever sound is playing."""
186-
self.audio.stop()
187-
188-
def mute(self, value=True):
189-
"""Enable or disable all sounds."""
190-
self.muted = value
191-
if self.mute_pin:
192-
self.mute_pin.value = not value
193-
194-
195146
class BMP16:
196147
"""Read 16-color BMP files."""
197148

0 commit comments

Comments
 (0)
0