Adding PWM audio code

Adding PWM audio code for the metro m7 template pages
This commit is contained in:
Liz 2023-03-30 10:40:20 -04:00
parent 6b04b76f73
commit 0b4d0feb4f
3 changed files with 54 additions and 0 deletions

View file

@ -0,0 +1,32 @@
# SPDX-FileCopyrightText: 2018 Kattni Rembor for Adafruit Industries
#
# SPDX-License-Identifier: MIT
"""
CircuitPython PWM Audio Out tone example
Plays a tone for one second on, one
second off, in a loop.
"""
import time
import array
import math
import board
from audiocore import RawSample
from audiopwmio import PWMAudioOut as AudioOut
audio = AudioOut(board.A1)
tone_volume = 0.1 # Increase this to increase the volume of the tone.
frequency = 440 # Set this to the Hz of the tone you want to generate.
length = 8000 // frequency
sine_wave = array.array("H", [0] * length)
for i in range(length):
sine_wave[i] = int((1 + math.sin(math.pi * 2 * i / length)) * tone_volume * (2 ** 15 - 1))
sine_wave_sample = RawSample(sine_wave)
while True:
audio.play(sine_wave_sample, loop=True)
time.sleep(1)
audio.stop()
time.sleep(1)

Binary file not shown.

View file

@ -0,0 +1,22 @@
# SPDX-FileCopyrightText: 2018 Kattni Rembor for Adafruit Industries
#
# SPDX-License-Identifier: MIT
"""
CircuitPython PWM Audio Out WAV example
Play a WAV file once.
"""
import board
from audiocore import WaveFile
from audiopwmio import PWMAudioOut as AudioOut
audio = AudioOut(board.A1)
with open("StreetChicken.wav", "rb") as wave_file:
wave = WaveFile(wave_file)
print("Playing wav file!")
audio.play(wave)
while audio.playing:
pass
print("Done!")