Skip to content

Commit 0f12bb8

Browse files
authored
Merge pull request #1827 from kattni/pico-i2s-mp3
Adding Pico I2S code.
2 parents 890f3ca + 450743b commit 0f12bb8

5 files changed

Lines changed: 74 additions & 0 deletions

File tree

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
"""
2+
CircuitPython I2S MP3 playback example.
3+
Plays a single MP3 once.
4+
"""
5+
import board
6+
import audiomp3
7+
import audiobusio
8+
9+
audio = audiobusio.I2SOut(board.GP0, board.GP1, board.GP2)
10+
11+
mp3 = audiomp3.MP3Decoder(open("slow.mp3", "rb"))
12+
13+
audio.play(mp3)
14+
while audio.playing:
15+
pass
16+
17+
print("Done playing!")
12.4 KB
Binary file not shown.
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
"""
2+
CircuitPython I2S Tone playback example.
3+
Plays a tone for one second on, one
4+
second off, in a loop.
5+
"""
6+
import time
7+
import array
8+
import math
9+
import audiocore
10+
import board
11+
import audiobusio
12+
13+
audio = audiobusio.I2SOut(board.GP0, board.GP1, board.GP2)
14+
15+
tone_volume = 0.1 # Increase this to increase the volume of the tone.
16+
frequency = 440 # Set this to the Hz of the tone you want to generate.
17+
length = 8000 // frequency
18+
sine_wave = array.array("h", [0] * length)
19+
for i in range(length):
20+
sine_wave[i] = int((math.sin(math.pi * 2 * i / length)) * tone_volume * (2 ** 15 - 1))
21+
sine_wave_sample = audiocore.RawSample(sine_wave)
22+
23+
while True:
24+
audio.play(sine_wave_sample, loop=True)
25+
time.sleep(1)
26+
audio.stop()
27+
time.sleep(1)
413 KB
Binary file not shown.
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
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+
import time
8+
import audiocore
9+
import board
10+
import audiobusio
11+
12+
audio = audiobusio.I2SOut(board.GP0, board.GP1, board.GP2)
13+
14+
wave_file = open("StreetChicken.wav", "rb")
15+
wav = audiocore.WaveFile(wave_file)
16+
17+
print("Playing wav file!")
18+
audio.play(wav)
19+
t = time.monotonic()
20+
while time.monotonic() - t < 6:
21+
pass
22+
23+
print("Pausing!")
24+
audio.pause()
25+
time.sleep(2)
26+
print("Resuming!")
27+
audio.resume()
28+
while audio.playing:
29+
pass
30+
print("Done!")

0 commit comments

Comments
 (0)