Speaker

The StudioBot is equiped with a basic speaker. This is suitable for playing simple sounds and complex sounds that come from an MP3 source.

Basic Sounds

The most basic sounds can be a simple tone.

import board
import simpleio

simpleio.tone(board.DAC, 440, duration=1.0)

We can make tones from the speaker

from audiocore import RawSample
from audioio import AudioOut
import board
import time
import audiobusio
import array
import math

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))
    
audio = AudioOut(board.DAC)
sine_wave_sample = RawSample(sine_wave)
audio.play(sine_wave_sample, loop=True)
time.sleep(1) #how long we want to play the sound for
audio.stop()

MP3 Files

Our Robot can actually play MP3 files

Further examples and information can be found in this Adafruit article.

Last updated