141 void const *
data()
const {
return m_data; }
153 bool empty()
const {
return m_length == 0; }
156 bool owned()
const {
return m_owned; }
159 int durationMS()
const {
return m_sampleRate ? (int) ((int64_t) m_length * 1000 / m_sampleRate) : 0; }
168 static uint8_t
encode(
int value);
177 static int16_t
decode(uint8_t value) {
return s_decodeTable[value]; }
183 static int16_t s_decodeTable[256];
226 DrumPattern() {
clear(); }
242 int get(
int step,
int instrument)
const {
return (
steps[step] >> (instrument * 2)) & 3; }
251 void set(
int step,
int instrument,
int velocity);
269 void setRow(
int instrument,
char const * text);
275 void setName(
char const * value);
376 void setLevel(
int instrument,
int value);
379 int level(
int instrument) {
return m_instruments[instrument].level; }
387 void setTune(
int instrument,
int cents);
390 int tune(
int instrument) {
return m_instruments[instrument].tune; }
398 void setDecay(
int instrument,
int value);
401 int decay(
int instrument) {
return m_instruments[instrument].decay; }
409 void setMute(
int instrument,
bool value) { m_instruments[instrument].mute = value; }
412 bool mute(
int instrument) {
return m_instruments[instrument].mute; }
422 void setChokeGroup(
int instrument,
int group) { m_instruments[instrument].chokeGroup = group; }
425 int chokeGroup(
int instrument) {
return m_instruments[instrument].chokeGroup; }
433 void trigger(
int instrument,
int velocity = 2);
442 uint8_t
hitCount(
int instrument) {
return m_hitCount[instrument]; }
542 int songPosition() {
return m_playing && m_songMode ? m_songPos : -1; }
545 int step() {
return m_playing ? m_step : -1; }
574 int recordHit(
int instrument,
int velocity = 2);
581 void setErase(uint32_t mask) { m_eraseMask = mask; }
606 static constexpr int ENV_ONE = 1 << 16;
607 static constexpr int ENV_FLOOR = 64;
608 static constexpr int GATE_BLOCKS = 24;
612 volatile uint32_t pitchInc;
613 volatile int32_t decayCoef;
614 volatile int16_t level;
618 volatile uint8_t chokeGroup;
619 volatile uint8_t generation;
640 class Channel :
public WaveformGenerator {
642 Channel(DrumMachine * dm) : m_dm(dm), m_pos(
BLOCK_SIZE) { }
646 m_dm->renderBlock(m_block);
649 return m_block[m_pos++];
653 int x = (getSample16() + 128) >> 8;
654 return x > 127 ? 127 : (x < -127 ? -127 : x);
656 void setFrequency(
int value) { }
664 static constexpr int HIT_QUEUE_SIZE = 32;
670 void renderBlock(int16_t * dest);
671 void sequencerAdvance();
673 void startVoice(
int instrument,
int velocity);
675 void switchPattern(DrumPattern *
pattern);
678 SoundGenerator * m_soundGenerator;
685 int32_t m_velocityGain[4];
687 Hit m_hits[HIT_QUEUE_SIZE];
688 volatile int m_hitsWrite;
689 volatile int m_hitsRead;
691 DrumPattern *
volatile m_pattern;
692 DrumPattern *
volatile m_nextPattern;
694 volatile int m_songLength;
695 volatile bool m_songLoop;
696 volatile bool m_songMode;
697 volatile int m_songPos;
698 volatile bool m_playing;
699 volatile bool m_startRequest;
701 volatile int32_t m_counter;
702 volatile int32_t m_stepLength[2];
703 volatile bool m_timingChanged;
704 volatile int m_loops;
705 volatile uint32_t m_eraseMask;
708 volatile bool m_metronome;
709 int m_metronomeLevel;
716 volatile uint32_t m_renderCount;
717 int32_t m_outputLevel;
DrumSampleFormat format() const
Gets the sample format.
DrumSample()
Creates an empty sample.
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.
void clear()
Releases the samples (the sample becomes empty).
int sampleRate() const
Gets the sample rate in Hertz.
void setTempo(int bpm)
Sets the tempo of the playing pattern.
int chokeGroup(int instrument)
Gets the choke group of an instrument.
void setDecay(int instrument, int value)
Sets the decay of an instrument.
void setTune(int instrument, int cents)
Sets the tuning of an instrument.
int decay(int instrument)
Gets the decay of an instrument (0..100).
uint8_t hitCount(int instrument)
Counts the hits of an instrument (live and sequencer), to show the activity.
void setVelocityGains(int soft, int normal, int accent)
Sets the gain of the velocities.
bool sounding(int instrument)
Determines whether an instrument is sounding.
int sampleRate()
Gets the sample rate of the sound generator.
void setSwing(int percent)
Sets the swing of the playing pattern.
void setSong(DrumPattern *const *patterns, int count, bool loop=true)
Sets the song: a list of patterns played in sequence by playSong().
WaveformGenerator & channel()
Gets the sound generator channel that plays the drum machine.
void setSample(int instrument, DrumSample const *sample)
Sets the sound of an instrument.
static constexpr int VOICES
Number of voices (sounds playing at the same time).
static constexpr int MAX_TEMPO
Maximum tempo (BPM).
bool playingSong()
Determines whether the sequencer is playing the song.
void setPattern(DrumPattern *pattern)
Sets the pattern to play (now).
static constexpr int INSTRUMENTS
Number of instruments.
int loops()
Counts the completed repetitions of the playing pattern.
void playSong(int position=0)
Starts playing the song.
bool mute(int instrument)
Determines whether an instrument is muted.
int step()
Gets the step that is playing (-1 = stopped).
static int stepDurationMS(int tempo)
Calculates the duration of a step.
int tune(int instrument)
Gets the tuning of an instrument in cents.
void play()
Starts playing the pattern from the first step.
bool metronome()
Determines whether the metronome is enabled.
void setNoiseGate(int level)
Sets the noise gate of the voices.
bool playing()
Determines whether the sequencer is playing.
void stop()
Stops the sequencer (the sounds play until they end).
int recordHit(int instrument, int velocity=2)
Records a hit in the playing pattern, quantized to the nearest step, and plays it.
DrumPattern * pattern()
Gets the playing pattern.
int level(int instrument)
Gets the level of an instrument (0..100).
void setErase(uint32_t mask)
Erases the hits of instruments while the playhead passes them.
int outputLevel()
Gets the output level (peak, with a short decay), to show a meter.
DrumPattern * queuedPattern()
Gets the queued pattern, nullptr if none.
void trigger(int instrument, int velocity=2)
Plays an instrument now.
static constexpr int MIN_TEMPO
Minimum tempo (BPM).
int songPosition()
Gets the song entry that is playing (-1 = not playing the song).
void setMute(int instrument, bool value)
Mutes an instrument: the sequencer doesn't play it (trigger() still does).
void setChokeGroup(int instrument, int group)
Sets the choke group of an instrument.
void setMetronome(bool value, int volume=60)
Enables the metronome: a click on every quarter note, accented on the first beat.
static constexpr int BLOCK_SIZE
Samples rendered at once: the timing resolution of the sequencer.
void queuePattern(DrumPattern *pattern)
Sets the pattern to play at the end of the current one (immediately when stopped).
static constexpr int MAX_SONG_STEPS
Maximum number of entries of a song.
DrumSample const * sample(int instrument)
Gets the sound of an instrument.
void begin(SoundGenerator *soundGenerator)
Attaches the drum machine to the sound generator and starts the sound generator.
void setLevel(int instrument, int value)
Sets the level of an instrument.
bool owned() const
Determines whether the samples are owned (loaded from a file).
DrumSampleFormat format() const
Gets the sample format.
int durationMS() const
Gets the duration in milliseconds.
DrumSample()
Creates an empty sample.
static void initDecodeTable()
Builds the decoding table (called by DrumMachine).
static uint8_t encode(int value)
Encodes a 16 bit linear value to mu-law.
bool empty() const
Determines whether the sample is empty.
static int16_t decode(uint8_t value)
Decodes a mu-law value.
void const * data() const
Gets the samples (int16_t or mu-law bytes, see format()).
int length() const
Gets the number of samples.
int sampleRate() const
Gets the sample rate in Hertz.
The sound of a drum instrument: 16 bit linear or 8 bit mu-law samples.
SoundGenerator handles audio output.
This file contains the FabGL configuration settings, such as queue sizes, task stack sizes and priori...
DrumSampleFormat
Sample format of a DrumSample.
DrumSampleError
Result of DrumSample::loadWAV().
This file contains all the classes of the FabGL sound system.
void clearSteps()
Removes all hits of all instruments.
int get(int step, int instrument) const
Gets the velocity of an instrument at a step.
void setRow(int instrument, char const *text)
Sets the hits of an instrument from a text, one character per step.
void setName(char const *value)
Sets the name (truncated to NAME_LENGTH characters).
static constexpr int INSTRUMENTS
Number of instruments.
bool empty() const
Determines whether the pattern has no hits.
static constexpr int NAME_LENGTH
Maximum name length (terminator not included).
void clearInstrument(int instrument)
Removes all hits of an instrument.
void set(int step, int instrument, int velocity)
Sets the velocity of an instrument at a step.
void clear()
Removes all hits and sets the defaults: 16 steps, 120 BPM, no swing, no name.
static constexpr int MAX_STEPS
Maximum number of steps.
uint32_t steps[MAX_STEPS]
A drum pattern: up to 32 steps (1/16 notes) for 16 instruments.