Skip to content

Commit

Permalink
sequencer
Browse files Browse the repository at this point in the history
  • Loading branch information
Alexandre Piel committed Jan 15, 2024
1 parent 44febcd commit 4d6bbcb
Show file tree
Hide file tree
Showing 5 changed files with 56 additions and 27 deletions.
23 changes: 20 additions & 3 deletions host/audioPluginHandler.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ class AudioPluginHandler : public AudioPluginHandlerInterface {
// When reached the maximum, there might be a tempo issue
// However, to reach it, it is almost impossible...
uint64_t clockCounter = 0;
bool playing = false;

struct MidiNoteEvent {
uint8_t channel;
Expand Down Expand Up @@ -198,14 +199,30 @@ class AudioPluginHandler : public AudioPluginHandlerInterface {

void clockTick()
{
clockCounter++;
for (Plugin& plugin : plugins) {
plugin.instance->onClockTick(clockCounter);
if (playing) {
clockCounter++;
for (Plugin& plugin : plugins) {
plugin.instance->onClockTick(&clockCounter);
}
}
}

void sendEvent(AudioEventType event)
{
switch (event) {
case AudioEventType::START:
playing = true;
break;

case AudioEventType::STOP:
playing = false;
clockCounter = 0;
break;

case AudioEventType::PAUSE:
playing = false;
break;
}
for (Plugin& plugin : plugins) {
plugin.instance->onEvent(event);
}
Expand Down
23 changes: 10 additions & 13 deletions plugins/audio/Sequencer.h
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ class Sequencer : public Mapping {

uint8_t stepCounter = 0;
uint8_t loopCounter = 0;
uint64_t * clockCounterPtr = NULL;
uint64_t* clockCounterPtr = NULL;

bool active = false;

Expand Down Expand Up @@ -181,12 +181,12 @@ class Sequencer : public Mapping {
setSelectedStep(selectedStep.get());
}

void onClockTick(uint64_t clockCounter)
void onClockTick(uint64_t* clockCounter)
{
clockCounterPtr = &clockCounter;
clockCounterPtr = clockCounter;
// Clock events are sent at a rate of 24 pulses per quarter note
// (24/4 = 6)
if (clockCounter % 6 == 0) {
if (*clockCounter % 6 == 0) {
onStep();
}
}
Expand All @@ -196,6 +196,7 @@ class Sequencer : public Mapping {
switch (event) {
case AudioEventType::STOP: {
active = false;
stepCounter = 0;
for (int i = 0; i < MAX_STEPS; i++) {
if (targetPlugin && steps[i].counter) {
targetPlugin->noteOff(steps[i].note, 0);
Expand All @@ -205,13 +206,12 @@ class Sequencer : public Mapping {
break;
}
case AudioEventType::START:
stepCounter = 0;
loopCounter = 0;
active = true;
break;

case AudioEventType::PAUSE:
active = !active;
active = false;
break;
}
}
Expand Down Expand Up @@ -320,17 +320,14 @@ class Sequencer : public Mapping {
uint8_t* index = (uint8_t*)userdata;
return (void*)stepConditions[*index].name;
}
case 3: // Save pattern
case 3:
return clockCounterPtr;
case 4: // Save pattern
save(folder / *(std::string*)userdata);
return NULL;
case 4: // Rename pattern
case 5: // Rename pattern
rename(folder / *(std::string*)userdata);
return NULL;
// case 5: // Toggle play/pause
// active = !active;
// return NULL;
case 5:
return clockCounterPtr;
}
return NULL;
}
Expand Down
14 changes: 7 additions & 7 deletions plugins/audio/audioPlugin.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,12 @@

class AudioPlugin;

enum AudioEventType {
STOP,
START,
PAUSE,
AUTOSAVE
};
enum AudioEventType {
STOP,
START,
PAUSE,
AUTOSAVE
};

class AudioPluginHandlerInterface {
public:
Expand Down Expand Up @@ -112,7 +112,7 @@ class AudioPlugin {
return NULL;
}

virtual void onClockTick(uint64_t clockCounter)
virtual void onClockTick(uint64_t* clockCounter)
{
}

Expand Down
19 changes: 17 additions & 2 deletions plugins/components/GridSequencerComponent.h
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ class Track {
int16_t trackId = -1;
AudioPlugin* seqPlugin;
std::string name = "Init";
float volume = 1.0f; //rand() % 100 / 100.0f;
float volume = 1.0f; // rand() % 100 / 100.0f;
bool active = false;
Step* steps;
ValueInterface* selectedStep;
Expand Down Expand Up @@ -66,7 +66,8 @@ class GridSequencerComponent : public Component {

Step selectedStepCopy;

uint8_t stepCounter = 0;
uint64_t lastClockCounter = -1;
uint8_t lastStepCounter = -1;

void progressInit()
{
Expand Down Expand Up @@ -384,6 +385,20 @@ class GridSequencerComponent : public Component {
renderNext();
selectedStepCopy = tracks[grid.row].steps[grid.col - 1];
}

if (tracks[0].seqPlugin) {
uint64_t* clockCounterPtr = (uint64_t*)tracks[0].seqPlugin->data(3);
if (clockCounterPtr && lastClockCounter != *clockCounterPtr) {
lastClockCounter = *clockCounterPtr;
uint8_t stepCounter = lastClockCounter / 6 % stepsCount;
if (stepCounter != lastStepCounter) {
lastStepCounter = stepCounter;
// printf("stepCounter: %d (%ld)\n", stepCounter, lastClockCounter);
renderProgress(stepCounter);
draw.renderNext();
}
}
}
};
}

Expand Down
4 changes: 2 additions & 2 deletions plugins/components/SequencerComponent.h
Original file line number Diff line number Diff line change
Expand Up @@ -358,11 +358,11 @@ class SequencerComponent : public Component {
int column = (motion.position.x - position.x) / stepSize.w;
if (fileMode) {
if (row == rowCount && column == columnCount - 2) {
plugin.data(4, (void*)&input.value);
plugin.data(5, (void*)&input.value);
fileMode = false;
renderNext();
} else if (row == rowCount && column == columnCount - 1) {
plugin.data(3, (void*)&input.value);
plugin.data(4, (void*)&input.value);
fileMode = false;
renderNext();
} else {
Expand Down

0 comments on commit 4d6bbcb

Please sign in to comment.