Skip to content

Commit 8137a0a

Browse files
committed
Adding I2S template code.
1 parent 00934d8 commit 8137a0a

5 files changed

Lines changed: 107 additions & 0 deletions

File tree

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
"""
2+
CircuitPython I2S MP3 playback example.
3+
Plays a single MP3 once.
4+
5+
Remove this line and all of the following docstring content before submitting to the Learn repo.
6+
7+
Update the three I2S pins to match the wiring chosen for the microcontroller. If you are unsure of
8+
a proper I2S pin combination, run the pin combination script found here:
9+
https://adafru.it/i2s-pin-combo-finder
10+
11+
Update the following pin names to a viable pin combination:
12+
* BIT_CLOCK_PIN
13+
* WORD_SELECT_PIN
14+
* DATA_PIN
15+
"""
16+
import board
17+
import audiomp3
18+
import audiobusio
19+
20+
audio = audiobusio.I2SOut(board.BIT_CLOCK_PIN, board.WORD_SELECT_PIN, board.DATA_PIN)
21+
22+
mp3 = audiomp3.MP3Decoder(open("slow.mp3", "rb"))
23+
24+
audio.play(mp3)
25+
while audio.playing:
26+
pass
27+
28+
print("Done playing!")
12.4 KB
Binary file not shown.
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
"""
2+
CircuitPython I2S Tone playback example.
3+
Plays a tone for one second on, one
4+
second off, in a loop.
5+
6+
Remove this line and all of the following docstring content before submitting to the Learn repo.
7+
8+
Update the three I2S pins to match the wiring chosen for the microcontroller. If you are unsure of
9+
a proper I2S pin combination, run the pin combination script found here:
10+
https://adafru.it/i2s-pin-combo-finder
11+
12+
Update the following pin names to a viable pin combination:
13+
* BIT_CLOCK_PIN
14+
* WORD_SELECT_PIN
15+
* DATA_PIN
16+
"""
17+
import time
18+
import array
19+
import math
20+
import audiocore
21+
import board
22+
import audiobusio
23+
24+
audio = audiobusio.I2SOut(board.BIT_CLOCK_PIN, board.WORD_SELECT_PIN, board.DATA_PIN)
25+
26+
tone_volume = 0.1 # Increase this to increase the volume of the tone.
27+
frequency = 440 # Set this to the Hz of the tone you want to generate.
28+
length = 8000 // frequency
29+
sine_wave = array.array("h", [0] * length)
30+
for i in range(length):
31+
sine_wave[i] = int((math.sin(math.pi * 2 * i / length)) * tone_volume * (2 ** 15 - 1))
32+
sine_wave_sample = audiocore.RawSample(sine_wave)
33+
34+
while True:
35+
audio.play(sine_wave_sample, loop=True)
36+
time.sleep(1)
37+
audio.stop()
38+
time.sleep(1)
413 KB
Binary file not shown.
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
"""
2+
CircuitPython I2S WAV file playback.
3+
Plays a WAV file for six seconds, pauses
4+
for two seconds, then resumes and plays
5+
the file to the end.
6+
7+
Remove this line and all of the following docstring content before submitting to the Learn repo.
8+
9+
Update the three I2S pins to match the wiring chosen for the microcontroller. If you are unsure of
10+
a proper I2S pin combination, run the pin combination script found here:
11+
https://adafru.it/i2s-pin-combo-finder
12+
13+
Update the following pin names to a viable pin combination:
14+
* BIT_CLOCK_PIN
15+
* WORD_SELECT_PIN
16+
* DATA_PIN
17+
"""
18+
import time
19+
import audiocore
20+
import board
21+
import audiobusio
22+
23+
audio = audiobusio.I2SOut(board.BIT_CLOCK_PIN, board.WORD_SELECT_PIN, board.DATA_PIN)
24+
25+
wave_file = open("StreetChicken.wav", "rb")
26+
wav = audiocore.WaveFile(wave_file)
27+
28+
print("Playing wav file!")
29+
audio.play(wav)
30+
t = time.monotonic()
31+
while time.monotonic() - t < 6:
32+
pass
33+
34+
print("Pausing!")
35+
audio.pause()
36+
time.sleep(2)
37+
print("Resuming!")
38+
audio.resume()
39+
while audio.playing:
40+
pass
41+
print("Done!")

0 commit comments

Comments
 (0)