Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Step random by Piers Titus #18

Draft
wants to merge 10 commits into
base: master
Choose a base branch
from
104 changes: 53 additions & 51 deletions src/Leds.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,40 +11,40 @@
#define LED_WHITE CRGB(230,255,150)

#define leds(A) physical_leds[led_order[A]]
#define next_step ((current_step+1)%SEQUENCER_NUM_STEPS)
// #define next_step ((current_step+1)%SEQUENCER_NUM_STEPS)

CRGB physical_leds[NUM_LEDS];
#define led_play physical_leds[0]

#define SK6812_BRIGHTNESS 32
#define SK6805_BRIGHTNESS 140

/* The black keys have assigned colors. The white keys are shown in gray */
/* The black keys have most primary colors. The white keys are inbetween */
const CRGB COLORS[] = {
0x444444,
0xFF0001,
0x444444,
0xFFDD00,
0x444444,
0x444444,
0x11FF00,
0x444444,
0x0033DD,
0x444444,
0xFF00FF,
0x444444,
0x444444,
0xFF2209,
0x444444,
0x99FF00,
0x444444,
0x444444,
0x00EE22,
0x444444,
0x0099CC,
0x444444,
0xBB33BB,
0x444444
0xEE0033, // C
0xFF0001, // C#
0xFF4400, // D
0xFFDD00, // D#
0xBBEE00, // E
0x66EE00, // F
0x11FF00, // F#
0x006644, // G
0x0033DD, // G#
0x4411EE, // A
0xFF00FF, // A#
0xFF1166, // B
0xFF1122, // C
0xFF2209, // C#
0xFF8811, // D
0xFFEE22, // D#
0x99DD22, // E
0x44BB22, // F
0x00EE22, // F#
0x00CC44, // G
0x0099CC, // G#
0x4455CC, // A
0xBB33BB, // A#
0xFF6666 // B
};

void led_init();
Expand Down Expand Up @@ -99,40 +99,42 @@ void led_init() {
// Updates the LED colour and brightness to match the stored sequence
void led_update() {
for (int l = 0; l < SEQUENCER_NUM_STEPS; l++) {
if (step_enable[l]) {
if (step_enable[l] == 1) {
leds(l) = COLORS[step_note[l]%24];
} else if (step_enable[l] == 2) {
leds(l) = CRGB(57, 64, 37); // skip step
} else {
leds(l) = CRGB::Black;
}

if(note_is_playing) {
leds(((current_step+random_offset)%SEQUENCER_NUM_STEPS)) = LED_WHITE;
} else {
if(!step_enable[((current_step+random_offset)%SEQUENCER_NUM_STEPS)]) {
leds(((current_step+random_offset)%SEQUENCER_NUM_STEPS)) = CRGB::Black;
}
}

if(note_is_playing) {
leds(((current_step+random_offset)%SEQUENCER_NUM_STEPS)) = LED_WHITE;
} else {
if(!step_enable[((current_step+random_offset)%SEQUENCER_NUM_STEPS)]) {
leds(((current_step+random_offset)%SEQUENCER_NUM_STEPS)) = CRGB::Black;
}

if(!sequencer_is_running) {
if(((sequencer_clock % 24) < 12)) {
if(step_enable[next_step]) {
leds(next_step) = COLORS[step_note[next_step]%24];
} else {
leds(next_step) = CRGB::Black;
}
led_play = LED_WHITE;
led_play.fadeLightBy((sequencer_clock % 12)*16);
if(!sequencer_is_running) {
if(((sequencer_clock % 24) < 12)) {
if(step_enable[next_step]) {
leds(next_step) = COLORS[step_note[next_step]%24];
} else {
led_play = CRGB::Black;
if(step_enable[next_step]) {
leds(next_step) = blend(LED_WHITE, COLORS[step_note[next_step]%24], (sequencer_clock % 12)*16);
} else {
leds(next_step) = LED_WHITE;
leds(next_step) = leds(next_step).fadeLightBy((sequencer_clock % 12)*16);
}
leds(next_step) = CRGB::Black;
}
} else {
led_play = LED_WHITE;
led_play.fadeLightBy((sequencer_clock % 12)*16);
} else {
led_play = CRGB::Black;
if(step_enable[next_step]) {
leds(next_step) = blend(LED_WHITE, COLORS[step_note[next_step]%24], (sequencer_clock % 12)*16);
} else {
leds(next_step) = LED_WHITE;
leds(next_step) = leds(next_step).fadeLightBy((sequencer_clock % 12)*16);
}
}
} else {
led_play = LED_WHITE;
}
}
FastLED.show();
Expand Down
56 changes: 44 additions & 12 deletions src/Sequencer.h
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,9 @@ const uint8_t SEQUENCER_NUM_STEPS = 8;
uint8_t step_note[] = { 1,0,6,9,0,4,0,5 };
uint8_t step_enable[] = { 1,0,1,1,1,1,0,1 };
uint8_t step_velocity[] = { 100,100,100,100,100,100,100,100 };
uint8_t step_delay[] = { 0,0,0,0,0,0,0,0 };
uint8_t step_random[] = { 0,0,0,0,0,0,0,0 };
uint8_t num_steps_used = SEQUENCER_NUM_STEPS;

void sequencer_init();
void sequencer_restart();
Expand Down Expand Up @@ -51,12 +54,14 @@ void sequencer_init() {
tempo_handler.setPPQN(PULSES_PER_QUARTER_NOTE);
sequencer_stop();
current_step = SEQUENCER_NUM_STEPS - 1;
next_step = 0;
}

void sequencer_restart() {
MIDI.sendRealTime(midi::Start);
delay(1);
current_step = SEQUENCER_NUM_STEPS - 1;
next_step = 0;
tempo_handler.midi_clock_reset();
sequencer_is_running = true;
sequencer_clock = 0;
Expand Down Expand Up @@ -119,27 +124,51 @@ void sequencer_tick_clock() {
}
}

if(sequencer_is_running && (sequencer_clock % sequencer_divider)==0) {
sequencer_advance();
if(sequencer_is_running) {
if ((sequencer_clock % sequencer_divider) == 0) {
sequencer_advance_without_play();
}
if ((sequencer_clock % sequencer_divider) == step_delay[current_step] * sequencer_divider / 6) {
// deliberately keep the rythm when random is pressed
sequencer_trigger_note();
}
}
sequencer_clock++;
}

void update_next_step() {
next_step = current_step;
for (int i = 0; i < SEQUENCER_NUM_STEPS; i++) {
next_step = (next_step + 1) % SEQUENCER_NUM_STEPS;
if (step_enable[next_step] != 2) break;
}
}

void sequencer_advance_without_play() {
static uint8_t arpeggio_index = 0;

if(!note_is_done_playing) {
sequencer_untrigger_note();
}
current_step++;
current_step%=SEQUENCER_NUM_STEPS;
// update_next_step();
current_step = next_step;
update_next_step();

if (!next_step_is_random && !random_flag) {
random_offset = 0;
} else {
if (next_step_is_random || random_flag || step_random[current_step]) {
random_flag = false;
random_offset = random(1,(SEQUENCER_NUM_STEPS - 2));
//current_step = ((current_step + random(2, SEQUENCER_NUM_STEPS))%SEQUENCER_NUM_STEPS);
random_offset = random(num_steps_used);
// select random step from the used steps
for(int i = 0; i < SEQUENCER_NUM_STEPS; i++) {
if (step_enable[(current_step + i) % SEQUENCER_NUM_STEPS] != 2) {
random_offset--;
if (random_offset < 0) {
random_offset = i;
break;
}
}
}
} else {
random_offset = 0;
}

// Sample keys
Expand All @@ -157,7 +186,8 @@ void sequencer_advance_without_play() {
arpeggio_index++;
}
step_enable[current_step] = 1;
step_velocity[current_step] = INITIAL_VELOCITY;
step_velocity[current_step] = INITIAL_VELOCITY;
step_random[current_step] = 0;
}
}

Expand All @@ -167,7 +197,8 @@ void sequencer_advance() {
}

void sequencer_reset() {
current_step = SEQUENCER_NUM_STEPS;
current_step = SEQUENCER_NUM_STEPS - 1;
next_step = 0;
}

void sequencer_update() {
Expand All @@ -186,7 +217,8 @@ static void sequencer_trigger_note() {

step_velocity[current_step] = INITIAL_VELOCITY;

note_on(step_note[((current_step+random_offset)%SEQUENCER_NUM_STEPS)]+transpose, step_velocity[((current_step+random_offset)%SEQUENCER_NUM_STEPS)], step_enable[((current_step+random_offset)%SEQUENCER_NUM_STEPS)]);
uint8_t step = ((current_step+random_offset)%SEQUENCER_NUM_STEPS);
note_on(step_note[step]+transpose, step_velocity[step], step_enable[step]);
}

static void sequencer_untrigger_note() {
Expand Down
Loading