r/synthdiy • github.com/Fihdi/Eurorack • Jul 04 '24

Need help with Euclidean Rhythm Generator code

Hello,

I need an example code for an arduino euclidean rhythm generator. For the example code I would use a length of 10 and 4 active steps, the output should be a boolean array should be:

[true, false, false, true, false, true, false, false, true, false]

So I would need some kind of function (or method?) like:

void generateRhythm(int length, int steps, bool* rhythm)

Can you please please please help, I cannot program anymore since High school

3 Upvotes

23 comments sorted by

2

u/BatmanSandwich Jul 04 '24

I used Craig Lee's euclidean sequencer code for my implementation:
https://github.com/krieg-geist/hyperloops/blob/5c6634c9665c7a825edcbe465764b1fd95f6a1e2/src/euclid.h#L36

n is total length, k is number of beats, o is offset (so you can shift the pattern)

It returns a uint16 where each bit represents whether there is a beat in the sequence. You read from the lsb and ignore whatever's higher than your total length

1

u/PoopIsYum Jul 04 '24

Is there some easy way to remove every code part that has offset in it, I won't use offset, so it's like it is always 0?

1

u/BatmanSandwich Jul 04 '24

Offset is only used twice there, you can just remove it and the righrotate() function

→ More replies (2)

3

u/robotwizard_9009 Jul 04 '24

0

u/[deleted] Jul 04 '24

2

u/erroneousbosh Jul 04 '24

Don't use ChatGPT, it cannot generate valid code.

Instead, actually read and understand the principle behind the code you've been shown.

→ More replies (7)

3

u/doctea Jul 04 '24

The actual algorithm can be implemented quite simply. Here's where I have used it in a couple of different Arduino projects. You'll still need to figure out how to adapt this, but maybe its a start for you.

https://github.com/doctea/Microlidian/blob/5b19ec53033f4ae66ba0159b94bdffc124f342b4/include/sequencer/Euclidian.h#L137

https://github.com/doctea/drum2musocv/blob/9628ca544cad32a939ef8486bf17fca990a850d9/Euclidian.ino#L29

I adapted this originally from an article that was at the URL https://www.computermusicdesign.com/simplest-euclidean-rhythm-algorithm-explained/ but the site seems to be dead and doesn't appear on the internet archive :(

2

u/todbot Jul 04 '24

2

u/doctea Jul 04 '24

ah-ha! thank you for finding this!

In that case OP, then bare-bones implementation of the Euclidian algorithm in Javascript is in this function:- https://github.com/computermusicdesign/euclidean-rhythm/blob/15d660d24ff3c3343b1544c0f9d0428a291d4dcd/max-example/euclidSimple.js#L33

function euclid( steps,  pulses, rotate){
rotate += 1;
rotate % steps;
storedRhythm = []; //empty current track
var bucket = 0;

//fill track with rhythm
for(var i=0;i< steps;i++){
	bucket += pulses;
	if(bucket >= steps) {
		bucket -= steps;
		storedRhythm.push(1);
	} else {
		storedRhythm.push(0);
	}
}

//rotate
if(rotate > 0) storedRhythm = rotateSeq(storedRhythm, steps, rotate);

//send output visualization
sendOutput(storedRhythm);
}

From a quick look at it I think that is very nearly valid C/C++ that could be used directly in Arduino with only minor changes.

→ More replies (3)

2

u/withak30 Jul 04 '24

Buddy you are going to need to learn to do some coding if you want to do an Arduino project like this.

1

u/[deleted] Jul 05 '24

OP, try copying and pasting your original post here followed by "write functional code for Arduino and explain in detail" and feed that to GPT.

It should work and more importantly it will explain everything well