Mozzi  version v1.1.0
sound synthesis library for Arduino
SampleHuffman Class Reference

A sample player for samples encoded with Huffman compression. More...

#include <SampleHuffman.h>

Public Member Functions

 SampleHuffman (uint8_t const *SOUNDDATA, int16_t const *HUFFMAN_DATA, uint32_t const SOUNDDATA_BITS)
 Constructor. More...
int16_t next ()
 Update and return the next audio sample. More...
void setLoopingOn ()
 Turns looping on, with the whole sample length as the loop range.
void setLoopingOff ()
 Turns looping off.
void start ()
 Sets the playhead to the beginning of the sample.

Detailed Description

A sample player for samples encoded with Huffman compression.

This class and the script are adapted from "audioout", an Arduino sketch by Thomas Grill, 2011 http//

Huffman decoding is used on sample differentials, saving 50-70% of space for 8 bit data, depending on the sample rate.

This implementation just plays back one sample each time next() is called, with no speed or other adjustments. It's slow, so it's likely you will only be able to play one sound at a time.

Audio data, Huffman decoder table, sample rate and bit depth are defined in a sounddata.h header file. This file can be generated for a sound file with the accompanying Python script, in Mozzi/extras/python/

Invoke with: python –sndfile=arduinosnd.wav –hdrfile=sounddata.h –bits=8 –name=soundtablename

You can resample and dither your audio file with SOX, e.g. to 8 bits depth @ Mozzi's 16384 Hz sample rate: sox fullglory.wav -b 8 -r 16384 arduinosnd.wav

Alternatively you can export a sound from Audacity, which seems to have less noticeable or no dithering, using Project Rate 16384 Hz and these output options: Other uncompressed files, Header: WAV(Microsoft), Encoding: Unsigned 8 bit PCM

The header file contains two lengthy arrays: One is "SOUNDDATA" which must fit into Flash RAM (available in total: 32k for ATMega328) The other is "HUFFMAN" which must also fit into Flash RAM

Definition at line 49 of file SampleHuffman.h.

Constructor & Destructor Documentation

◆ SampleHuffman()

SampleHuffman::SampleHuffman ( uint8_t const *  SOUNDDATA,
int16_t const *  HUFFMAN_DATA,
uint32_t const  SOUNDDATA_BITS 


SOUNDDATAthe name of the SOUNDDATA table in the huffman sample .h file
HUFFMAN_DATAthe name of the HUFFMAN table in the huffman sample .h file
SOUNDDATA_BITSfrom the huffman sample .h file

Definition at line 59 of file SampleHuffman.h.

Member Function Documentation

◆ next()

int16_t SampleHuffman::next ( )

Update and return the next audio sample.

So far it just plays back one sample at a time without any variable tuning or speed.

the next audio sample
timing: about 5 to 40 us, varies continuously depending on data

Definition at line 70 of file SampleHuffman.h.