soundlib – Sound Library for Tones, FX, MP3s

soundlib (including SoundMaker, Tone)

This library provides a high-level interface for playing and mixing sounds of different types on the CodeX. With this, sounds can also be played “in the background” while other code runs.

Example:

Play a tone based on a supported instrument or waveform name

>>> tone = soundlib.get_tone('trumpet')
>>> tone.play()

Instrument

Waveform

flute

sine

recorder

triangle

trumpet

square

violin

sawtooth

noise

random

organ

(none)

synth

see set_synthbuf()

class soundlib.MP3(voice, filename)[source]

A MP3 player with level control

play(loop=False)[source]

Begin generating sound

set_level(value)[source]

Start volume level to the specified relative value (0-100)

stop()[source]

Stop generating sound (be quiet)

class soundlib.Sample(voice, sample)[source]

A sample player with level control

play(loop=False)[source]

Begin (or resume if stop() was called) generating sound

set_level(value)[source]

Start volume level to the specified relative value (0-100)

stop()[source]

Stop generating sound (be quiet)

class soundlib.SoundMaker(max_concurrent_sounds=16, sample_rate=16000)[source]

Provides sounds which can be mixed together concurrently (polyphonic!)

get_mp3(filename, play=True, loop=False)[source]

Get a MP3 object so you can play it

get_sample(sample_buf, play=True, loop=False)[source]

Get a sample object so you can play it. This can be a buffer directly recorded from the CodeX mic, or any 16-bit array you create using array.array (“h”) and fill with samples. Note: sample rate of sample_buf must match self.sample_rate or pitch will be wonky!

get_tone(waveform_name='sine')[source]

Get a Tone object so you can make some noise!

get_wav(filename, play=True, loop=False)[source]

Get a WAV object so you can play it

play(loop=False)[source]

Invoke the play() command on all the active sound players

reset()[source]

Stop and remove all mixer voices.

stop()[source]

Invoke the stop() command on all the active sound players

class soundlib.Tone(voice, sample)[source]

A Tone player with pitch and level control. Tones have variable pitch (frequency), and support several different wave shapes (waveforms).

This is built atop the FuncSample class in the funcgen module. See that module for more details.

Tone uses an audiomixer voice to play a FuncSample sample.

glide(new_pitch, duration)[source]

Glide from current frequency to new pitch, linearly over tm seconds.

Parameters
  • pitch (float) – Target frequency (Hz)

  • tm (float) – Duration of glide (time to reach target pitch)

play()[source]

Start tone. Use envelope to avoid clicking and provide soft note-shaping.

set_envelope(a, d, s, r)[source]

Configure envelope parameters. Initial settings are: (0.003, 0.010, 0.95, 0.003)

Parameters
  • a (float) – Attack (sec) - time from 0 to full amplitude

  • d (float) – Decay (sec) - time from full to Sustain amplitude

  • s (float) – Sustain (fraction) - proportion of full amplitude to hold

  • r (float) – Release (sec) - time from Sustain to 0 amplitude

set_level(value)[source]

Set volume level to the specified relative value (0-100)

set_modulator(mod_name, rate, depth_buf)[source]

Configure a modulator (LFO)

The supplied depth_buf must have nonzero length and will be assumed to represent exactly 1-cycle of the LFO waveform.

Each modulator has its own independent LFO.

Parameters
  • mod_name (str) – Which modulator (“fm”, “am”)

  • rate (float) – LFO frequency (Hz). set to ZERO to disable modulator.

  • depth_buf (array("h")) – Array of 16-bit signed samples

set_pitch(value)[source]

Set pitch to the specified value (in Hertz)

set_synthbuf(array_buf)[source]

For “synth” waveform type, set the source array: 1-cycle of 16-bit samples.

The array can be any length, and will be assumed to contain exactly one cycle. Create with: array_buf = array.array(“h”, [0] * length)

set_wave(wave_name)[source]

Set waveform to specified type (from func_types or aliases)

stop()[source]

Stop tone. Use envelope to avoid clicking and provide soft note-shaping.

voice_play()[source]

Turn on this mixer voice (abruptly, can ‘click’)

voice_stop()[source]

Turn off this mixer voice (abruptly, can ‘click’)

static wave_func(wave_name)[source]

Return the FuncGen code corresponding to wave_name.

class soundlib.WAV(voice, filename)[source]

A WAV player with level control

play(loop=False)[source]

Begin generating sound

set_level(value)[source]

Start volume level to the specified relative value (0-100)

stop()[source]

Stop generating sound (be quiet)

Download Source