Adding I2S template code.
This commit is contained in:
parent
00934d8c82
commit
8137a0ae4b
5 changed files with 107 additions and 0 deletions
28
CircuitPython_Templates/i2s_mp3_playback/code.py
Normal file
28
CircuitPython_Templates/i2s_mp3_playback/code.py
Normal file
|
|
@ -0,0 +1,28 @@
|
||||||
|
"""
|
||||||
|
CircuitPython I2S MP3 playback example.
|
||||||
|
Plays a single MP3 once.
|
||||||
|
|
||||||
|
Remove this line and all of the following docstring content before submitting to the Learn repo.
|
||||||
|
|
||||||
|
Update the three I2S pins to match the wiring chosen for the microcontroller. If you are unsure of
|
||||||
|
a proper I2S pin combination, run the pin combination script found here:
|
||||||
|
https://adafru.it/i2s-pin-combo-finder
|
||||||
|
|
||||||
|
Update the following pin names to a viable pin combination:
|
||||||
|
* BIT_CLOCK_PIN
|
||||||
|
* WORD_SELECT_PIN
|
||||||
|
* DATA_PIN
|
||||||
|
"""
|
||||||
|
import board
|
||||||
|
import audiomp3
|
||||||
|
import audiobusio
|
||||||
|
|
||||||
|
audio = audiobusio.I2SOut(board.BIT_CLOCK_PIN, board.WORD_SELECT_PIN, board.DATA_PIN)
|
||||||
|
|
||||||
|
mp3 = audiomp3.MP3Decoder(open("slow.mp3", "rb"))
|
||||||
|
|
||||||
|
audio.play(mp3)
|
||||||
|
while audio.playing:
|
||||||
|
pass
|
||||||
|
|
||||||
|
print("Done playing!")
|
||||||
BIN
CircuitPython_Templates/i2s_mp3_playback/slow.mp3
Executable file
BIN
CircuitPython_Templates/i2s_mp3_playback/slow.mp3
Executable file
Binary file not shown.
38
CircuitPython_Templates/i2s_tone_playback/code.py
Normal file
38
CircuitPython_Templates/i2s_tone_playback/code.py
Normal file
|
|
@ -0,0 +1,38 @@
|
||||||
|
"""
|
||||||
|
CircuitPython I2S Tone playback example.
|
||||||
|
Plays a tone for one second on, one
|
||||||
|
second off, in a loop.
|
||||||
|
|
||||||
|
Remove this line and all of the following docstring content before submitting to the Learn repo.
|
||||||
|
|
||||||
|
Update the three I2S pins to match the wiring chosen for the microcontroller. If you are unsure of
|
||||||
|
a proper I2S pin combination, run the pin combination script found here:
|
||||||
|
https://adafru.it/i2s-pin-combo-finder
|
||||||
|
|
||||||
|
Update the following pin names to a viable pin combination:
|
||||||
|
* BIT_CLOCK_PIN
|
||||||
|
* WORD_SELECT_PIN
|
||||||
|
* DATA_PIN
|
||||||
|
"""
|
||||||
|
import time
|
||||||
|
import array
|
||||||
|
import math
|
||||||
|
import audiocore
|
||||||
|
import board
|
||||||
|
import audiobusio
|
||||||
|
|
||||||
|
audio = audiobusio.I2SOut(board.BIT_CLOCK_PIN, board.WORD_SELECT_PIN, board.DATA_PIN)
|
||||||
|
|
||||||
|
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((math.sin(math.pi * 2 * i / length)) * tone_volume * (2 ** 15 - 1))
|
||||||
|
sine_wave_sample = audiocore.RawSample(sine_wave)
|
||||||
|
|
||||||
|
while True:
|
||||||
|
audio.play(sine_wave_sample, loop=True)
|
||||||
|
time.sleep(1)
|
||||||
|
audio.stop()
|
||||||
|
time.sleep(1)
|
||||||
BIN
CircuitPython_Templates/i2s_wav_playback/StreetChicken.wav
Executable file
BIN
CircuitPython_Templates/i2s_wav_playback/StreetChicken.wav
Executable file
Binary file not shown.
41
CircuitPython_Templates/i2s_wav_playback/code.py
Normal file
41
CircuitPython_Templates/i2s_wav_playback/code.py
Normal file
|
|
@ -0,0 +1,41 @@
|
||||||
|
"""
|
||||||
|
CircuitPython I2S WAV file playback.
|
||||||
|
Plays a WAV file for six seconds, pauses
|
||||||
|
for two seconds, then resumes and plays
|
||||||
|
the file to the end.
|
||||||
|
|
||||||
|
Remove this line and all of the following docstring content before submitting to the Learn repo.
|
||||||
|
|
||||||
|
Update the three I2S pins to match the wiring chosen for the microcontroller. If you are unsure of
|
||||||
|
a proper I2S pin combination, run the pin combination script found here:
|
||||||
|
https://adafru.it/i2s-pin-combo-finder
|
||||||
|
|
||||||
|
Update the following pin names to a viable pin combination:
|
||||||
|
* BIT_CLOCK_PIN
|
||||||
|
* WORD_SELECT_PIN
|
||||||
|
* DATA_PIN
|
||||||
|
"""
|
||||||
|
import time
|
||||||
|
import audiocore
|
||||||
|
import board
|
||||||
|
import audiobusio
|
||||||
|
|
||||||
|
audio = audiobusio.I2SOut(board.BIT_CLOCK_PIN, board.WORD_SELECT_PIN, board.DATA_PIN)
|
||||||
|
|
||||||
|
wave_file = open("StreetChicken.wav", "rb")
|
||||||
|
wav = audiocore.WaveFile(wave_file)
|
||||||
|
|
||||||
|
print("Playing wav file!")
|
||||||
|
audio.play(wav)
|
||||||
|
t = time.monotonic()
|
||||||
|
while time.monotonic() - t < 6:
|
||||||
|
pass
|
||||||
|
|
||||||
|
print("Pausing!")
|
||||||
|
audio.pause()
|
||||||
|
time.sleep(2)
|
||||||
|
print("Resuming!")
|
||||||
|
audio.resume()
|
||||||
|
while audio.playing:
|
||||||
|
pass
|
||||||
|
print("Done!")
|
||||||
Loading…
Reference in a new issue