Skip to content

Commit 13cc2c7

Browse files
authored
Merge pull request #2461 from adafruit/m7_i2s
Adding I2S examples
2 parents eea188d + eb2f428 commit 13cc2c7

3 files changed

Lines changed: 69 additions & 0 deletions

File tree

Metro_M7_Examples/I2S_Tone/code.py

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
# SPDX-FileCopyrightText: 2021 Kattni Rembor for Adafruit Industries
2+
# SPDX-License-Identifier: MIT
3+
"""
4+
CircuitPython I2S Tone playback example.
5+
Plays a tone for one second on, one
6+
second off, in a loop.
7+
Remove this line and all of the following docstring content before submitting to the Learn repo.
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+
Update the following pin names to a viable pin combination:
12+
* BIT_CLOCK_PIN
13+
* WORD_SELECT_PIN
14+
* DATA_PIN
15+
"""
16+
import time
17+
import array
18+
import math
19+
import audiocore
20+
import board
21+
import audiobusio
22+
23+
audio = audiobusio.I2SOut(board.D10, board.D9, board.D12)
24+
25+
tone_volume = 0.1 # Increase this to increase the volume of the tone.
26+
frequency = 440 # Set this to the Hz of the tone you want to generate.
27+
length = 8000 // frequency
28+
sine_wave = array.array("h", [0] * length)
29+
for i in range(length):
30+
sine_wave[i] = int((math.sin(math.pi * 2 * i / length)) * tone_volume * (2 ** 15 - 1))
31+
sine_wave_sample = audiocore.RawSample(sine_wave)
32+
33+
while True:
34+
audio.play(sine_wave_sample, loop=True)
35+
time.sleep(1)
36+
audio.stop()
37+
time.sleep(1)
413 KB
Binary file not shown.

Metro_M7_Examples/I2S_Wav/code.py

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
# SPDX-FileCopyrightText: 2021 Kattni Rembor for Adafruit Industries
2+
# SPDX-License-Identifier: MIT
3+
"""
4+
CircuitPython I2S WAV file playback.
5+
Plays a WAV file once.
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 audiocore
19+
import board
20+
import audiobusio
21+
22+
audio = audiobusio.I2SOut(board.D10, board.D9, board.D12)
23+
24+
with open("StreetChicken.wav", "rb") as wave_file:
25+
wav = audiocore.WaveFile(wave_file)
26+
27+
print("Playing wav file!")
28+
audio.play(wav)
29+
while audio.playing:
30+
pass
31+
32+
print("Done!")

0 commit comments

Comments
 (0)