audiomixer – Support for audio mixing
Available on these boards
- class audiomixer.Mixer(voice_count: int = 2, buffer_size: int = 1024, channel_count: int = 2, bits_per_sample: int = 16, samples_signed: bool = True, sample_rate: int = 8000)
Mixes one or more audio samples together into one sample.
Create a Mixer object that can mix multiple channels with the same sample rate. Samples are accessed and controlled with the mixer’s
audiomixer.MixerVoiceobjects.- Parameters:
voice_count (int) – The maximum number of voices to mix
buffer_size (int) – The total size in bytes of the buffers to mix into
channel_count (int) – The number of channels the source samples contain. 1 = mono; 2 = stereo.
bits_per_sample (int) – The bits per sample of the samples being played
samples_signed (bool) – Samples are signed (True) or unsigned (False)
sample_rate (int) – The sample rate to be used for all samples
Playing a wave file from flash:
import board import audioio import audiocore import audiomixer import digitalio a = audioio.AudioOut(board.A0) music = audiocore.WaveFile(open("cplay-5.1-16bit-16khz.wav", "rb")) drum = audiocore.WaveFile(open("drum.wav", "rb")) mixer = audiomixer.Mixer(voice_count=2, sample_rate=16000, channel_count=1, bits_per_sample=16, samples_signed=True) print("playing") # Have AudioOut play our Mixer source a.play(mixer) # Play the first sample voice mixer.voice[0].play(music) while mixer.playing: # Play the second sample voice mixer.voice[1].play(drum) time.sleep(1) print("stopped")
- __exit__() None
Automatically deinitializes the hardware when exiting a context. See Lifetime and ContextManagers for more info.
- sample_rate: int
32 bit value that dictates how quickly samples are played in Hertz (cycles per second).
- voice: Tuple[MixerVoice, Ellipsis]
A tuple of the mixer’s
audiomixer.MixerVoiceobject(s).>>> mixer.voice (<MixerVoice>,)
- play(sample: circuitpython_typing.AudioSample, *, voice: int = 0, loop: bool = False) Mixer
Plays the sample once when loop=False and continuously when loop=True. Does not block. Use
playingto block.Sample must be an
audiocore.WaveFile,audiocore.RawSample,audiomixer.Mixeroraudiomp3.MP3Decoder.The sample must match the Mixer’s encoding settings given in the constructor.
- Returns:
The mixer object itself. Can be used for chaining, ie:
audio.play(mixer.play(sample)).- Return type:
Chorus
- class audiomixer.MixerVoice
Voice objects used with Mixer
Used to access and control samples with
audiomixer.Mixer.MixerVoice instance object(s) created by
audiomixer.Mixer.- play(sample: circuitpython_typing.AudioSample, *, loop: bool = False) None
Plays the sample once when
loop=False, and continuously whenloop=True. Does not block. Useplayingto block.Sample must be an
audiocore.WaveFile,audiocore.RawSample,audiomixer.Mixeroraudiomp3.MP3Decoder.The sample must match the
audiomixer.Mixer’s encoding settings given in the constructor.
- end() None
Sets looping to False if sample is playing. This allows the looped sample to complete its current playback and end further looping
- level: synthio.BlockInput
The volume level of a voice, as a floating point number between 0 and 1. If your board does not support synthio, this property will only accept a float value.
- panning: synthio.BlockInput
Defines the channel(s) in which the voice appears, as a floating point number between -1 and 1. If your board does not support synthio, this property will only accept a float value. This property is ignored if
audiomixer.Mixer.channel_count=1.-1 is left channel only, 0 is both channels, and 1 is right channel. For fractional values, the note plays at full amplitude in one channel and partial amplitude in the other channel. For instance -.5 plays at full amplitude in the left channel and 1/2 amplitude in the right channel.