Skip to content

Commit d0f1c46

Browse files
committed
Allow use of audiopwmio in place of audioio
1 parent 3fa977c commit d0f1c46

File tree

3 files changed

+32
-25
lines changed

3 files changed

+32
-25
lines changed

meowbit/audioio.py

Lines changed: 0 additions & 13 deletions
This file was deleted.

meowbit/ugame.py

Lines changed: 20 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import busio
44
import time
55
import keypad
6+
import audiocore
67

78

89
K_X = 0x01
@@ -51,15 +52,27 @@ def get_pressed(self):
5152
return buttons
5253

5354

54-
class DummyAudio:
55-
def play(self, f, loop=False):
56-
pass
55+
class _Audio:
56+
last_audio = None
57+
58+
def __init__(self):
59+
self.muted = True
60+
self.buffer = bytearray(128)
61+
self.audio = board.BUZZ
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)
5769

5870
def stop(self):
59-
pass
71+
self.audio.stop()
72+
73+
def mute(self, value=True):
74+
self.muted = value
6075

61-
def mute(self, mute):
62-
pass
6376

64-
audio = DummyAudio()
77+
audio = _Audio()
6578
buttons = _Buttons()

stage.py

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,16 @@
11
import time
22
import array
33
import digitalio
4-
import audioio
5-
import struct
64
try:
7-
import audiocore
5+
import audioio
86
except ImportError:
9-
audiocore = audioio
7+
pass
8+
else:
9+
try:
10+
import audiocore
11+
except ImportError:
12+
audiocore = audioio
13+
import struct
1014

1115
import _stage
1216

@@ -160,7 +164,10 @@ def __init__(self, speaker_pin, mute_pin=None):
160164
self.mute_pin.switch_to_output(value=not self.muted)
161165
else:
162166
self.mute_pin = None
163-
self.audio = audioio.AudioOut(speaker_pin)
167+
if audioio is None:
168+
self.audio = audiopwmio.PWMAudioOut(speaker_pin)
169+
else:
170+
self.audio = audioio.AudioOut(speaker_pin)
164171

165172
def play(self, audio_file, loop=False):
166173
"""

0 commit comments

Comments
 (0)