mDrumRack
a basic drum/sample rack
Description
mDrumRack is a convenient wrapper for an array of samples. Load up any number of samples and trigger them to play back. mDrumRack is not technically derived from the mInstrument parent class but implements all of the same functions.
Template Parameters
template <uint64_t mMAX_SAMPLE_LENGTH, uint8_t mNUM_SAMPLES, class T = int8_t>mMAX_SAMPLE_LENGTH: length of the longest sample being included in the rack
mNUM_SAMPLES: how many samples are being added to the rack
T: data type of samples being added to rack
Class Methods
Constructor
mDrumRack(const T **sample list, uint32_t *sample_lengths, uint8_t *base_address = NULL)sample_list array of samples to be loaded into rack
sample_lengths: array of lengths of each sample listed in sample_list
midi_table_name: (OPTIONAL) table of MIDI data you want the instrument to play
void noteOn(uint16_t sample_num, uint16_t vel = 127)Trigger one of the rack’s samples to play and optionally set its volume as a MIDI-style velocity (0-127)
void noteOn(uint16_t sample_num, uint16_t vel, float speed)Trigger one of the rack’s samples to play, set its voolume as a MIDI-style velocity (0-127) and set the sample’s playback speed (1.0 plays sample at normal speed, 2.0 plays at double speed etc)
void noteOff(uint16_t sample_num)Turns off a playing sample
void setSampleVolume(uint16_t sample_num, uint16_t volume)Sets output gain of a sample (0-255 where 255 is unity gain).
int32_t next()Calculates a sample of output. Output will be nominally 16 bits no matter what bit-rate of samples you loaded into the drum rack. MUST BE CALLED IN updateAudio()
Example
// ===== Global variables =====
// assuming we have the following 16-bit samples in the same directory as our sketch
#include "sample0.h"
#include "sample1.h"
#include "sample2.h"
#define NUM_SAMPLES 3
const int16_t *sample_list[NUM_SAMPLES] = {
sample0_DATA,
sample1_DATA,
sample2_DATA
};
uint32_t sample_lengths[NUM_SAMPLES] = {
sample0_NUM_CELLS,
sample1_NUM_CELLS,
sample2_NUM_CELLS
};
#include "midi_sequence.h" // optional midi sequence converted with MIDI converter script. if not using this, leave the last constructor argument below empty
mDrumRack<300000, NUM_SAMPLES, int8_t> my_drums(sample_list, sample_lengths, midi_sequence_data); // first template argument doesn't really matter, it just needs to be at least as large as the longest sample's length
// ===== in updateAudio =====
int64_t out_sample = my_drums.next(); // outputs a nominally 16-bit signal no matter what bitrate of samples were used
// ===== if using a MIDI sequence; call updateMidi() within a 24PPQ process, such as the clockStep function of MEAP_MIDI_TEMPLATE_WITH_CLOCK.h =====
my_drums.updateMidi();