Though the following example uses the Circuit Playground Express to demonstrate, the lawmaking works exactly the aforementioned way with the Excursion Playground Bluefruit. Just copy the code and follow forth with your Circuit Playground Bluefruit!

The Circuit Playground Express has some squeamish congenital in audio output capabilities.

There are ii means to get audio output, one is via the minor built in speaker. The other is by using alligator clips to connect a headphone or powered speaker to the A0/Sound pin.

  • circuit_playground_spkr.jpg

The speaker is over here, its pocket-size simply tin brand some loud sounds! Yous can ENABLE or disable the speaker. If you disable the speaker, audio will only come up out the A0/Sound pin. If you enable the speaker, audio will come up out from both!

If you want to connect a speaker or headphones, employ 2 alligator clips and connect GND to the sleeve of the headphone, and A0/AUDIO to the tip.

The A0/AUDIO pin cannot drive a speaker straight, please but connect headphones, or powered speakers!

circuit_playground_hp.png

Basic Tones

Nosotros can kickoff by making elementary tones. We will play sine waves. Nosotros get-go generate a unmarried period of a sine moving ridge in python, with the math.sin function, and stick information technology into sine_wave.

Then we enable the speaker by setting the SPEAKER_ENABLE pin to exist an output and True.

We can create the sound object with this line that sets the output pin and the sine wave sample object and requite information technology the sample array

audio = AudioOut(board.SPEAKER)
sine_wave_sample = RawSample(sine_wave)

Finally you can run audio.play() - if you only want to play the sample in one case, telephone call every bit is. If you want it to loop the sample, which nosotros definitely exercise so its one long tone, pass in loop=True

Yous tin can and then do whatever you like, the tone will play in the background until y'all call sound.terminate()

# SPDX-FileCopyrightText: 2017 Limor Fried for Adafruit Industries # # SPDX-License-Identifier: MIT  import fourth dimension import array import math import board import digitalio  try:     from audiocore import RawSample except ImportError:     from audioio import RawSample  try:     from audioio import AudioOut except ImportError:     endeavor:         from audiopwmio import PWMAudioOut equally AudioOut     except ImportError:         laissez passer  # not always supported by every board!  FREQUENCY = 440  # 440 Hz middle 'A' SAMPLERATE = 8000  # 8000 samples/second, recommended!  # Generate one period of sine wav. length = SAMPLERATE // FREQUENCY sine_wave = array.array("H", [0] * length) for i in range(length):     sine_wave[i] = int(math.sin(math.pi * 2 * i / length) * (2 ** fifteen) + two ** 15)  # Enable the speaker speaker_enable = digitalio.DigitalInOut(board.SPEAKER_ENABLE) speaker_enable.direction = digitalio.Management.OUTPUT speaker_enable.value = True  audio = AudioOut(board.SPEAKER) sine_wave_sample = RawSample(sine_wave)  # A single sine moving ridge sample is hundredths of a second long. If yous set loop=False, information technology will play # a single instance of the sample (a quick flare-up of sound) and so silence for the remainder of the # elapsing of the time.sleep(). If loop=True, information technology volition play the single instance of the sample # continuously for the duration of the fourth dimension.sleep(). audio.play(sine_wave_sample, loop=True)  # Play the single sine_wave sample continuously... time.sleep(1)  # for the duration of the sleep (in seconds) audio.stop()  # and so stop.                
# SPDX-FileCopyrightText: 2017 Limor Fried for Adafruit Industries # # SPDX-License-Identifier: MIT  import time import assortment import math import board import digitalio  endeavor:     from audiocore import RawSample except ImportError:     from audioio import RawSample  endeavour:     from audioio import AudioOut except ImportError:     try:         from audiopwmio import PWMAudioOut as AudioOut     except ImportError:         pass  # non always supported past every board!  FREQUENCY = 440  # 440 Hz heart 'A' SAMPLERATE = 8000  # 8000 samples/2d, recommended!  # Generate one period of sine wav. length = SAMPLERATE // FREQUENCY sine_wave = array.array("H", [0] * length) for i in range(length):     sine_wave[i] = int(math.sin(math.pi * 2 * i / length) * (ii ** 15) + two ** 15)  # Enable the speaker speaker_enable = digitalio.DigitalInOut(board.SPEAKER_ENABLE) speaker_enable.direction = digitalio.Direction.OUTPUT speaker_enable.value = True  audio = AudioOut(lath.SPEAKER) sine_wave_sample = RawSample(sine_wave)  # A unmarried sine moving ridge sample is hundredths of a second long. If you set up loop=Fake, it will play # a single instance of the sample (a quick burst of audio) and so silence for the rest of the # duration of the time.sleep(). If loop=True, it will play the single instance of the sample # continuously for the duration of the time.sleep(). audio.play(sine_wave_sample, loop=Truthful)  # Play the single sine_wave sample continuously... fourth dimension.sleep(1)  # for the duration of the sleep (in seconds) audio.cease()  # and then stop.                

Playing Audio Files

Tones are lovely simply lets play some music! You tin can drag-and-drop sound files onto the CIRCUITPY drive and and so play them with a Python command

Here's the ii files nosotros're going to play:

Click the green buttons to download the wave files, and save them onto your CIRCUITPY drive, aslope your lawmaking.py and lib files

circuit_playground_waves.png

This is the case code nosotros'll be using

# SPDX-FileCopyrightText: 2017 Limor Fried for Adafruit Industries # # SPDX-License-Identifier: MIT  import board import digitalio  endeavor:     from audiocore import WaveFile except ImportError:     from audioio import WaveFile  try:     from audioio import AudioOut except ImportError:     try:         from audiopwmio import PWMAudioOut as AudioOut     except ImportError:         laissez passer  # not e'er supported by every board!  # Enable the speaker spkrenable = digitalio.DigitalInOut(board.SPEAKER_ENABLE) spkrenable.direction = digitalio.Direction.OUTPUT spkrenable.value = True  # Make the 2 input buttons buttonA = digitalio.DigitalInOut(lath.BUTTON_A) buttonA.direction = digitalio.Management.INPUT buttonA.pull = digitalio.Pull.Downwardly  buttonB = digitalio.DigitalInOut(board.BUTTON_B) buttonB.direction = digitalio.Direction.INPUT buttonB.pull = digitalio.Pull.DOWN  # The ii files assigned to buttons A & B audiofiles = ["rimshot.wav", "express joy.wav"]   def play_file(filename):     impress("Playing file: " + filename)     wave_file = open(filename, "rb")     with WaveFile(wave_file) as wave:         with AudioOut(lath.SPEAKER) as audio:             audio.play(wave)             while audio.playing:                 laissez passer     print("Finished")   while True:     if buttonA.value:         play_file(audiofiles[0])     if buttonB.value:         play_file(audiofiles[1])                
# SPDX-FileCopyrightText: 2017 Limor Fried for Adafruit Industries # # SPDX-License-Identifier: MIT  import board import digitalio  try:     from audiocore import WaveFile except ImportError:     from audioio import WaveFile  try:     from audioio import AudioOut except ImportError:     attempt:         from audiopwmio import PWMAudioOut equally AudioOut     except ImportError:         pass  # not always supported by every lath!  # Enable the speaker spkrenable = digitalio.DigitalInOut(board.SPEAKER_ENABLE) spkrenable.direction = digitalio.Direction.OUTPUT spkrenable.value = Truthful  # Make the 2 input buttons buttonA = digitalio.DigitalInOut(lath.BUTTON_A) buttonA.direction = digitalio.Direction.INPUT buttonA.pull = digitalio.Pull.DOWN  buttonB = digitalio.DigitalInOut(lath.BUTTON_B) buttonB.management = digitalio.Management.INPUT buttonB.pull = digitalio.Pull.DOWN  # The two files assigned to buttons A & B audiofiles = ["rimshot.wav", "laugh.wav"]   def play_file(filename):     print("Playing file: " + filename)     wave_file = open(filename, "rb")     with WaveFile(wave_file) every bit wave:         with AudioOut(board.SPEAKER) every bit sound:             audio.play(wave)             while audio.playing:                 pass     print("Finished")   while Truthful:     if buttonA.value:         play_file(audiofiles[0])     if buttonB.value:         play_file(audiofiles[i])                

This example creates two input buttons using the onboard buttons, then has a helper function that will:

  1. open a file on the disk drive with wave_file = open(filename, "rb")
  2. create the wave file object with with WaveFile(wave_file) as wave:
  3. create the audio playback object with with AudioOut(board.SPEAKER) as audio:
  4. and finally play it until its done:
    audio.play(wave)
    while audio.playing:
    laissez passer

 Upload the code then endeavour pressing the two buttons one at a time to create your own express mirth rails!

If you desire to use your own audio files, you can! Record, sample, remix, or but download files from a audio file site, such equally freesample.org. Then, to make certain you lot have the files converted to the proper specifications, check out this guide here that'll show you how! Spoiler alert: y'all'll need to make a small, 22Khz (or lower), xvi bit PCM, mono  .wav file!

This guide was outset published on October 12, 2017. It was last updated on 2021-11-30 xiv:00:00 -0500.

This folio (CircuitPython Audio Out) was last updated on Apr 10, 2022.

Text editor powered by tinymce.