adafruit_soundboard

A MicroPython library for the Adafruit Sound Boards in UART mode!

This library has been adapted from the library written by Adafruit for Arduino, available at https://github.com/adafruit/Adafruit_Soundboard_library.

  • Author(s): Mike Mabey
adafruit_soundboard.SB_BAUD

The baud rate for the soundboards. This shouldn’t ever change, since all of the soundboard models use the same value.

See also

Adafruit Audio FX Sound Board Tutorial
Adafruit’s tutorial on the soundboards.
adafruit_soundboard.MIN_VOL
adafruit_soundboard.MAX_VOL

Minimum volume is 0, maximum is 204.

adafruit_soundboard.MAX_FILES

In the Arduino version of this library, it defines the max number of files to be 25.

adafruit_soundboard.DEBUG

A flag for turning on/off debug messages.

class adafruit_soundboard.Soundboard(uart_id, rst_pin=None, vol=None, alt_get_files=False, debug=None, **uart_kwargs)[source]

Control an Adafruit Sound Board via UART.

The Soundboard class handles all communication with the sound board via UART, making it easy to get information about the sound files on the sound board and control playback.

If you need to reset the sound board from your MicroPython code, be sure to provide the rst_pin parameter. The sound board sometimes gets out of UART mode and reverts to the factory default of GPIO trigger mode. When this happens, it will appear as if the sound board has stoped working for no apparent reason. This library is designed to automatically attempt resetting the board if a command fails, since that is a common cause. So, it is a good idea to provide this parameter.

Parameters:
  • uart_id – ID for the UART bus to use. Acceptable values vary by board. Check the documentation for your board for more info.
  • rst_pin – Identifier for the pin (on the MicroPython board) connected to the RST pin of the sound board. Valid identifiers vary by board.
  • vol (int or float) – Initial volume level to set. See vol for more info.
  • alt_get_files (bool) – Uses an alternate method to get the list of track file names. See use_alt_get_files() method for more info.
  • debug (bool) – When not None, will set the debug output flag to the boolean value of this argument using the toggle_debug() method.
  • uart_kwargs (dict) – Additional values passed to the UART.init() method of the UART bus object. Acceptable values here also vary by board. It is not necessary to include the baud rate among these keyword values, because it will be set to SB_BAUD before the UART.init function is called.
files

Return a list of the files on the sound board.

Return type:list
sizes

Return a list of the files’ sizes on the sound board.

Return type:list
lengths

Return a list of the track lengths in seconds.

Note

In my own testing of this method, the board always returns a value of zero seconds for the length for every track, no matter if it’s a WAV or OGG file, short or long track.

Return type:list
file_name(n)[source]

Return the name of track n.

Parameters:n (int) – Index of a file on the sound board or False if the track number doesn’t exist.
Returns:Filename of track n.
Return type:str or bool
track_num(file_name)[source]

Return the track number of the given file name.

Parameters:file_name (str) – File name of the track. Should be one of the values from the files property.
Returns:The track number of the file name or False if not found.
Return type:int or bool
play(track=None)[source]

Play a track on the board.

Parameters:track (int or str) – The index (int) or filename (str) of the track to play.
Returns:If the command was successful.
Return type:bool
play_now(track)[source]

Play a track on the board now, stopping current track if necessary.

Parameters:track (int or str) – The index (int) or filename (str) of the track to play.
Returns:If the command was successful.
Return type:bool
vol

Current volume.

This is implemented as a class property, so you can get and set its value directly. When setting a new volume, you can use an int or a float (assuming your board supports floats). When setting to an int, it should be in the range of 0-204. When set to a float, the value will be interpreted as a percentage of MAX_VOL.

Return type:int
vol_up(vol=None)[source]

Turn volume up by 2 points, return current volume level [0-204].

Parameters:vol (int) – Target volume. When not None, volume will be turned up to be greater than or equal to this value.
Return type:int
vol_down(vol=None)[source]

Turn volume down by 2 points, return current volume level [0-204].

Parameters:vol (int) – Target volume. When not None, volume will be turned down to be less than or equal to this value.
Return type:int
pause()[source]

Pause playback, return if the command was successful.

Return type:bool
unpause()[source]

Continue playback, return if the command was successful.

Return type:bool
stop()[source]

Stop playback, return if the command was successful.

Return type:bool
track_time()[source]

Return the current position of playback and total time of track.

Return type:tuple
track_size()[source]

Return the remaining size and total size.

It seems the remaining track size refers to the number of bytes left for the sound board to process before the playing of the track will be over.

Returns:Remaining track size and total size
Return type:tuple
reset()[source]

Reset the sound board.

Soft reset the board by bringing the RST pin low momentarily (10 ms). This only has effect if the reset pin has been initialized in the constructor.

Doing a soft reset on the board before doing any other actions can help ensure that it has been started in UART control mode, rather than GPIO trigger mode.

See also

Soundboard Pinout
Documentation on the sound boards’ pinouts.
Returns:Whether the reset was successful. If the reset pin was not initialized in the constructor, this will always return False.
Return type:bool
use_alt_get_files(now=False)[source]

Get list of track files using an alternate method.

If the list of files is missing tracks you know are on the sound board, try calling this method. It doesn’t depend on the sound board’s internal command for returning a list of files. Instead, it plays each of the tracks using their track numbers and gets the filename and size from the output of the play command.

Parameters:now (bool) – When set to True, the alternate method of getting the files list will be called immediately. Otherwise, the list of files will be populated the next time the files property is accessed (lazy loading).
Return type:None
static toggle_debug(debug=None)[source]

Turn on/off DEBUG flag.

Parameters:debug – If None, the DEBUG flag will be toggled to have the value opposite of its current value. Otherwise, DEBUG will be set to the boolean value of debug.
Return type:None
adafruit_soundboard.printif(*values, **kwargs)[source]

Print a message if DEBUG is set to True.