3 new melody strategies
This commit is contained in:
@@ -0,0 +1,102 @@
|
||||
#ifndef ISORHYTHM_STRATEGY_H
|
||||
#define ISORHYTHM_STRATEGY_H
|
||||
|
||||
#include "MelodyStrategy.h"
|
||||
#include <Arduino.h>
|
||||
|
||||
class IsorhythmStrategy : public MelodyStrategy {
|
||||
public:
|
||||
void generate(Step (*sequence)[NUM_STEPS], int track, int numSteps, int* scaleNotes, int numScaleNotes, int seed, int intensity) override {
|
||||
randomSeed(seed);
|
||||
if (numScaleNotes == 0) return;
|
||||
|
||||
// 1. Create the Color (pitch pattern)
|
||||
// Intensity affects color length. Higher intensity = longer, more complex color.
|
||||
int colorLength = 2 + random(intensity + 2); // 2 to intensity+3 notes
|
||||
if (colorLength > numScaleNotes) colorLength = numScaleNotes;
|
||||
if (colorLength > 8) colorLength = 8; // Keep it reasonable
|
||||
|
||||
int color[8];
|
||||
// Pick unique notes from the scale for the color
|
||||
int scaleIndices[12];
|
||||
for(int i=0; i<numScaleNotes; i++) scaleIndices[i] = i;
|
||||
for(int i=0; i<numScaleNotes; i++) { // shuffle
|
||||
int r = random(numScaleNotes);
|
||||
int temp = scaleIndices[i];
|
||||
scaleIndices[i] = scaleIndices[r];
|
||||
scaleIndices[r] = temp;
|
||||
}
|
||||
for(int i=0; i<colorLength; i++) {
|
||||
color[i] = scaleIndices[i];
|
||||
}
|
||||
|
||||
// 2. Create the Talea (rhythmic pattern)
|
||||
// Intensity affects talea length and density.
|
||||
int taleaLength = 4 + random(numSteps / 2); // 4 to 12 steps for a 16-step sequence
|
||||
if (taleaLength > numSteps) taleaLength = numSteps;
|
||||
|
||||
bool talea[NUM_STEPS];
|
||||
int pulses = 2 + random(taleaLength - 2); // At least 2 pulses
|
||||
|
||||
// Euclidean distribution for a nice rhythm
|
||||
int bucket = 0;
|
||||
for(int i=0; i<taleaLength; i++) {
|
||||
bucket += pulses;
|
||||
if (bucket >= taleaLength) {
|
||||
bucket -= taleaLength;
|
||||
talea[i] = true;
|
||||
} else {
|
||||
talea[i] = false;
|
||||
}
|
||||
}
|
||||
|
||||
// 3. Apply Color and Talea to the sequence
|
||||
int colorIdx = 0;
|
||||
for (int i = 0; i < numSteps; i++) {
|
||||
int taleaIdx = i % taleaLength;
|
||||
if (talea[taleaIdx]) {
|
||||
int octave = 3 + random(3);
|
||||
int noteScaleIndex = color[colorIdx % colorLength];
|
||||
sequence[track][i].note = 12 * octave + scaleNotes[noteScaleIndex];
|
||||
sequence[track][i].accent = (i % 4 == 0); // Accent on downbeats
|
||||
sequence[track][i].tie = false;
|
||||
colorIdx++;
|
||||
} else {
|
||||
sequence[track][i].note = -1;
|
||||
sequence[track][i].accent = false;
|
||||
sequence[track][i].tie = false;
|
||||
}
|
||||
}
|
||||
randomSeed(micros());
|
||||
}
|
||||
|
||||
void generateScale(int* scaleNotes, int& numScaleNotes) override {
|
||||
numScaleNotes = random(5, 8);
|
||||
for (int i = 0; i < 12; i++) scaleNotes[i] = i;
|
||||
for (int i = 0; i < 12; i++) {
|
||||
int j = random(12);
|
||||
int temp = scaleNotes[i];
|
||||
scaleNotes[i] = scaleNotes[j];
|
||||
scaleNotes[j] = temp;
|
||||
}
|
||||
sortArray(scaleNotes, numScaleNotes);
|
||||
}
|
||||
|
||||
void mutate(Step (*sequence)[NUM_STEPS], int track, int numSteps, int* scaleNotes, int numScaleNotes, int intensity) override {
|
||||
// Mutation: rotate the talea (rhythmic displacement)
|
||||
int rotation = random(1, numSteps);
|
||||
Step temp[NUM_STEPS];
|
||||
for(int i=0; i<numSteps; i++) {
|
||||
temp[i] = sequence[track][(i + rotation) % numSteps];
|
||||
}
|
||||
for(int i=0; i<numSteps; i++) {
|
||||
sequence[track][i] = temp[i];
|
||||
}
|
||||
}
|
||||
|
||||
const char* getName() override {
|
||||
return "Isorhythm";
|
||||
}
|
||||
};
|
||||
|
||||
#endif
|
||||
Reference in New Issue
Block a user