RawSample – A raw audio sample buffer

An in-memory sound sample

class audioio.RawSample(buffer, *, channel_count=1, sample_rate=8000)

Create a RawSample based on the given buffer of signed values. If channel_count is more than 1 then each channel’s samples should alternate. In other words, for a two channel buffer, the first sample will be for channel 1, the second sample will be for channel two, the third for channel 1 and so on.

  • buffer (array) – An array.array with samples
  • channel_count (int) – The number of channels in the buffer
  • sample_rate (int) – The desired playback sample rate

Simple 8ksps 440 Hz sin wave:

import audioio
import board
import array
import time
import math

# Generate one period of sine wav.
length = 8000 // 440
sine_wave = array.array("h", [0] * length)
for i in range(length):
    sine_wave[i] = int(math.sin(math.pi * 2 * i / 18) * (2 ** 15))

dac = audioio.AudioOut(board.SPEAKER)
sine_wave = audioio.RawSample(sine_wave)
dac.play(sine_wave, loop=True)

Deinitialises the AudioOut and releases any hardware resources for reuse.


No-op used by Context Managers.


Automatically deinitializes the hardware when exiting a context. See Lifetime and ContextManagers for more info.


32 bit value that dictates how quickly samples are played in Hertz (cycles per second). When the sample is looped, this can change the pitch output without changing the underlying sample. This will not change the sample rate of any active playback. Call play again to change it.