Skip to content

Commit 81345f5

Browse files
authored
Merge pull request #2465 from adafruit/pwm_metrom7
Adding PWM audio code
2 parents 6b04b76 + 0b4d0fe commit 81345f5

File tree

3 files changed

+54
-0
lines changed

3 files changed

+54
-0
lines changed

Metro_M7_Examples/PWM_Tone/code.py

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
# SPDX-FileCopyrightText: 2018 Kattni Rembor for Adafruit Industries
2+
#
3+
# SPDX-License-Identifier: MIT
4+
5+
"""
6+
CircuitPython PWM Audio Out tone example
7+
Plays a tone for one second on, one
8+
second off, in a loop.
9+
"""
10+
import time
11+
import array
12+
import math
13+
import board
14+
from audiocore import RawSample
15+
from audiopwmio import PWMAudioOut as AudioOut
16+
17+
audio = AudioOut(board.A1)
18+
19+
tone_volume = 0.1 # Increase this to increase the volume of the tone.
20+
frequency = 440 # Set this to the Hz of the tone you want to generate.
21+
length = 8000 // frequency
22+
sine_wave = array.array("H", [0] * length)
23+
for i in range(length):
24+
sine_wave[i] = int((1 + math.sin(math.pi * 2 * i / length)) * tone_volume * (2 ** 15 - 1))
25+
26+
sine_wave_sample = RawSample(sine_wave)
27+
28+
while True:
29+
audio.play(sine_wave_sample, loop=True)
30+
time.sleep(1)
31+
audio.stop()
32+
time.sleep(1)
413 KB
Binary file not shown.

Metro_M7_Examples/PWM_Wav/code.py

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
# SPDX-FileCopyrightText: 2018 Kattni Rembor for Adafruit Industries
2+
#
3+
# SPDX-License-Identifier: MIT
4+
5+
"""
6+
CircuitPython PWM Audio Out WAV example
7+
Play a WAV file once.
8+
"""
9+
import board
10+
from audiocore import WaveFile
11+
from audiopwmio import PWMAudioOut as AudioOut
12+
13+
audio = AudioOut(board.A1)
14+
15+
with open("StreetChicken.wav", "rb") as wave_file:
16+
wave = WaveFile(wave_file)
17+
print("Playing wav file!")
18+
audio.play(wave)
19+
while audio.playing:
20+
pass
21+
22+
print("Done!")

0 commit comments

Comments
 (0)