Skip to content

Instantly share code, notes, and snippets.

@zeroeth
Forked from unohee/Bjorklund.cpp
Created January 7, 2016 06:46
Show Gist options
  • Select an option

  • Save zeroeth/19229ff3d42c64eacbfe to your computer and use it in GitHub Desktop.

Select an option

Save zeroeth/19229ff3d42c64eacbfe to your computer and use it in GitHub Desktop.
openframeworks (C++) Version of Bjorklund Algorithm
//
// Bjorklund.cpp
// EuclideanRhythm
//
// Created in 12/18/15.
//
//
#include "Bjorklund.h"
//Edited NoisyLittleBurger's Bjorklund Algorithm in C
//http://www.noisylittlebugger.net/diy/bjorklund/Bjorklund_Working_Final/Bjorklund_algorithm_arduino.txt
//CHANGED :
//1. use dynamic array.
//2. fixed sequence's off-spot problem
//3. added Explanation about Algorithm based on G.Touissant's Paper,
//"The Euclidean Algorithm Generates Traditional Musical Rhythms"
//
Bjorklund::Bjorklund(){
sizeOfSeq = 16;//init
}
void Bjorklund::iter(int step, int pulse){
bool debug = false;
//Bjorklund algorithm------
//now. we create a binary sequence with GCD and so on.
/*
"A common method of representing musical rhythms is as binary sequences, where each bit is considered as one unit of time (for example a 16th note), and a zero bit represents a silence (or an unaccented note) whereas a one bit represents an attack (or onset) of a note" - G. Toussiant.
*/
//do E[k,n]. k is number of one's in sequence, and n is the length of sequence.
//however. note that I create the function with this expression reversly (first is always greater than zero. thus, it's E[n,k] but result of operation is equal.
sizeOfSeq = step;
remainder.resize(step); //set size as same as steps.
count.resize(step); // same.
for(int i = 0; i < step; i++)sequence.push_back(false);
if(pulse > step){pulse = step;}
int divisor = step - pulse; //initial amount of zero's
remainder[0] = pulse; //first elements of remainder is the amount of ones's
index = 0; //we start algorithm from first index.
do{
//do iteration reversly.
//The smaller number is repeatedly subtracted from the greater until the greater is zero or becomes smaller than the smaller
count[index] = divisor / remainder[index];
remainder[index+1] = divisor % remainder[index];
divisor = remainder[index];
index++; //move to next step.
}
while(remainder[index] > 0);
count[index] = divisor;
buildSeq(index); //place one's and zero's
//place sequence in order.
for(int i = sequence.size(); i > 0; i --){
slots.push_back(sequence[i-1]);
}
//then check sequence
if(debug){
for(int i = 0; i < sequence.size(); i ++)cout<<i<<" "<<slots[i]<<
endl;
}
}
void Bjorklund::buildSeq(int index){
//construct a binary sequence of n bits with k one’s, such that the k one’s are distributed as evenly as possible among the zero’s
//check every slots of sequence reversely.
if (index == -1) {
sequence[step] = false; //insert 0 into array
step = step + 1; //move to next
}
else if (index == -2) {
sequence[step] = true; //insert 1 into array
step = step + 1; //move to next
}
else {
for (int i = 0; i < count[index]; i++)
buildSeq(index-1);
if (remainder[index] !=0)
buildSeq(index-2);
}
}
int Bjorklund::getSequence(int index){
//return states of slots.
return slots[index];
}
int Bjorklund::getSize(){
return sizeOfSeq;
}
//
// Bjorklund.h
// EuclideanRhythm
//
// Created by Heewon Oh on 12/18/15.
//
//
#ifndef Bjorklund_h
#define Bjorklund_h
#include "ofMain.h"
class Bjorklund{
public:
Bjorklund();
void iter(int step, int pulse);
void buildSeq(int index);
int getSequence(int index);
int getSize();
private:
vector<bool>sequence; //reversed
vector<bool>slots; //actual binary sequence which will be used.
vector<int>remainder;
vector<int>count;
int sizeOfSeq;
int index;
int step;
};
#endif /* bjorklund_h*/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment