Skip to content

Commit 10e1d37

Browse files
authored
Merge pull request #837 from kattni/cpb-cpx-audio-out-update
Update Audio Out for CPB and 5.x
2 parents 86b67e4 + 16e859a commit 10e1d37

2 files changed

Lines changed: 35 additions & 11 deletions

File tree

Introducing_CircuitPlaygroundExpress/CircuitPlaygroundExpress_AudioFiles.py

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,25 @@
1-
import audioio
21
import board
32
import digitalio
43

5-
# enable the speaker
4+
try:
5+
from audiocore import WaveFile
6+
except ImportError:
7+
from audioio import WaveFile
8+
9+
try:
10+
from audioio import AudioOut
11+
except ImportError:
12+
try:
13+
from audiopwmio import PWMAudioOut as AudioOut
14+
except ImportError:
15+
pass # not always supported by every board!
16+
17+
# Enable the speaker
618
spkrenable = digitalio.DigitalInOut(board.SPEAKER_ENABLE)
719
spkrenable.direction = digitalio.Direction.OUTPUT
820
spkrenable.value = True
921

10-
# make the 2 input buttons
22+
# Make the 2 input buttons
1123
buttonA = digitalio.DigitalInOut(board.BUTTON_A)
1224
buttonA.direction = digitalio.Direction.INPUT
1325
buttonA.pull = digitalio.Pull.DOWN
@@ -23,8 +35,8 @@
2335
def play_file(filename):
2436
print("Playing file: " + filename)
2537
wave_file = open(filename, "rb")
26-
with audioio.WaveFile(wave_file) as wave:
27-
with audioio.AudioOut(board.A0) as audio:
38+
with WaveFile(wave_file) as wave:
39+
with AudioOut(board.SPEAKER) as audio:
2840
audio.play(wave)
2941
while audio.playing:
3042
pass
Lines changed: 18 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,22 @@
11
import time
22
import array
33
import math
4-
import audioio
54
import board
65
import digitalio
76

7+
try:
8+
from audiocore import RawSample
9+
except ImportError:
10+
from audioio import RawSample
11+
12+
try:
13+
from audioio import AudioOut
14+
except ImportError:
15+
try:
16+
from audiopwmio import PWMAudioOut as AudioOut
17+
except ImportError:
18+
pass # not always supported by every board!
19+
820
FREQUENCY = 440 # 440 Hz middle 'A'
921
SAMPLERATE = 8000 # 8000 samples/second, recommended!
1022

@@ -14,14 +26,14 @@
1426
for i in range(length):
1527
sine_wave[i] = int(math.sin(math.pi * 2 * i / 18) * (2 ** 15) + 2 ** 15)
1628

17-
# enable the speaker
29+
# Enable the speaker
1830
speaker_enable = digitalio.DigitalInOut(board.SPEAKER_ENABLE)
1931
speaker_enable.direction = digitalio.Direction.OUTPUT
2032
speaker_enable.value = True
2133

22-
audio = audioio.AudioOut(board.A0)
23-
sine_wave_sample = audioio.RawSample(sine_wave)
34+
audio = AudioOut(board.SPEAKER)
35+
sine_wave_sample = RawSample(sine_wave)
2436

25-
audio.play(sine_wave_sample, loop=True) # keep playing the sample over and over
37+
audio.play(sine_wave_sample, loop=True) # Keep playing the sample over and over
2638
time.sleep(1) # until...
27-
audio.stop() # we tell the board to stop
39+
audio.stop() # We tell the board to stop

0 commit comments

Comments
 (0)