FabGL2 2.0.0
Graphics, audio, GUI and retro-computing library for the ESP32 (ESP-IDF)
Loading...
Searching...
No Matches
DrumSample Class Reference

The sound of a drum instrument: 16 bit linear or 8 bit mu-law samples. More...

#include <drummachine.h>

Public Member Functions

 DrumSample ()
 Creates an empty sample.
 DrumSample (int16_t const *pcmData, int length, int sampleRate)
 Creates a sample that refers to constant 16 bit linear data (not copied, not freed).
 DrumSample (uint8_t const *muLawData, int length, int sampleRate)
 Creates a sample that refers to constant mu-law data (not copied, not freed).
void clear ()
 Releases the samples (the sample becomes empty).
void const * data () const
 Gets the samples (int16_t or mu-law bytes, see format()).
int durationMS () const
 Gets the duration in milliseconds.
bool empty () const
 Determines whether the sample is empty.
DrumSampleFormat format () const
 Gets the sample format.
int length () const
 Gets the number of samples.
DrumSampleError loadWAV (char const *filename, int sampleRate, int maxLength=22050 *4, DrumSampleFormat format=DrumSampleFormat::PCM16)
 Loads a WAV file.
bool owned () const
 Determines whether the samples are owned (loaded from a file).
int sampleRate () const
 Gets the sample rate in Hertz.

Static Public Member Functions

static int16_t decode (uint8_t value)
 Decodes a mu-law value.
static uint8_t encode (int value)
 Encodes a 16 bit linear value to mu-law.
static void initDecodeTable ()
 Builds the decoding table (called by DrumMachine).

Detailed Description

The sound of a drum instrument: 16 bit linear or 8 bit mu-law samples.

Samples are stored as 16 bit linear values (DrumSampleFormat::PCM16) or as 8 bit G.711 mu-law values (DrumSampleFormat::MuLaw, the companding used by the classic sampling drum machines of the eighties): half the memory, but the quantization noise (about 37 dB below the signal) follows the waveform and is audible on the tails of the tones (bass drum, toms) when the sound generator is oversampled (SoundGenerator::setOversampling()).

A sample either refers to constant data (for example a factory sound in flash) or owns a buffer loaded from a WAV file with loadWAV(). Loaded samples go to PSRAM when available.

Example:

DrumSample kick(kickData, sizeof(kickData) / 2, 22050);   // constant data, int16_t kickData[]
DrumSample user;
if (user.loadWAV("/sdcard/drums/kick.wav", 22050) == DrumSampleError::None)
  drumMachine.setSample(0, &user);

Definition at line 91 of file drummachine.h.


The documentation for this class was generated from the following files: