clear all;
bmax = 32;
quar = 8; % quarter note length 
eigh = quar/2;
sixt = eigh/2;
thir = sixt/2;
down = 5; %how much to downsample
dumwav = wavread('drumsamp/dum.wav');
dumwav = decimate(dumwav,down);
%wavplay(dumwav,44100);
bekwav = wavread('drumsamp/bek.wav');
bekwav = decimate(bekwav,down);
conlwav = wavread('drumsamp/lo_conga.wav');
conlwav = decimate(conlwav,down);
conmwav = wavread('drumsamp/mid_conga.wav');
conmwav = decimate(conmwav,down);%this track is stereo
conhwav = wavread('drumsamp/hi_conga.wav');
conhwav = decimate(conhwav,down);

% create 'click' track
vec3 = (rand(1,500) + sin(1:500));
% make all sounds same length
maxlen = max([length(dumwav) length(bekwav) length(conlwav) length(conmwav) length(conhwav)]);
dumwav = zpad(dumwav,maxlen);
bekwav = zpad(bekwav,maxlen);
conlwav = zpad(conlwav,maxlen);
conmwav = zpad(conmwav,maxlen);
conhwav = zpad(conhwav,maxlen);
dumwav = zpad(dumwav,maxlen);
vec3 = zpad(vec3,maxlen);


d=1; %dum
b=2; %bec
cl=3;%low conga
cm=4; %mid conga
ch=5; %high conga

fill = 55;
%set up primitives

%dumbek
db1 = [d zeros(1,quar-1)];
db2 = [b zeros(1,sixt-1)];
db3 = [b zeros(1,thir-1)];
lens = [length(db1) length(db2) length(db3)];
fillamt = max(lens);
db1 = [db1 fill*ones(1,fillamt-lens(1))];
db2 = [db2 fill*ones(1,fillamt-lens(2))];
db3 = [db3 fill*ones(1,fillamt-lens(3))];
db = [db1;db2;db3];
%[rdb,cdb]=size(db);
clear fillamt lens

%conga
c1 = [cl zeros(1,quar-1)]; %low conga, once per quarter
c2 = [cm zeros(1,thir-1)]; %mid conga, once per thirty-second note
c3 = [ch zeros(1,eigh-1)];%high conga, once per eighth xxxhalf note 2*quar-1
lens = [length(c1) length(c2) length(c3)];
fillmax = max(lens);
%make all patterns same length
c1 = [c1 fill*ones(1,fillmax-lens(1))];
c2 = [c2 fill*ones(1,fillmax-lens(2))];
c3 = [c3 fill*ones(1,fillmax-lens(3))];
con = [c1;c2;c3];
%[rcon,ccon] = size(con);
clear c1 c2 c3 fillmax lens


%generate strings for 2 drummers
drum1 = makestring(db,bmax,fill);
drum2 = [makestring(con,bmax,fill);makestring(con,bmax,fill);makestring(con,bmax,fill);makestring(con,bmax,fill)
    makestring(con,bmax,fill);makestring(con,bmax,fill)]; %make a few possibilities
[rdrum2,cdrum2] = size(drum2);	

%compare drummers - synchronization
% first just get beat info; make all drum sounds into 1's
drum1_prim = zeros(rdrum2,bmax);
drum2_prim = zeros(rdrum2,bmax);
drum1_prim(:,drum1~=0) = 1; %make nonzero elements of drum pattern into 1's
drum2_prim(drum2~=0) = 1;	
numtwos = sum(ismember(drum1_prim + drum2_prim,2),2); %counts number of two's (ie simul. drum), sum along each row
prob = cumsum(numtwos/sum(numtwos)); % make CDF of probabilities
ind = 1+sum(rand(1)>prob); %gives a random index, favouring higher synchronization
drum2 = drum2(ind,:); %pick that sequence
   
encnt = 1; %initialize
while ( (length(nonzeros(drum1)) <= bmax-4) & (length(nonzeros(drum2)) <= bmax-4) ) %don't fill up too much
    for j = 1:2 %play pattern twice
        for i =1:bmax
            switch drum1(i)
            case d
                vec1 = dumwav;
            case b
                vec1 = bekwav;
            otherwise
                vec1 = 0;
            end
            switch drum2(i)
            case cl
                vec2 = conlwav;
            case cm
                vec2 = conmwav;%left channel of stereo
            case ch
                vec2 = conhwav;
            otherwise
                vec2 = 0;
            end
            
            if ((i == 1) | (i==8) | (i==16) | (i==24) )
                temp = vec3;
            else
                temp = 0;
            end      
            
            snd = vec1 + vec2 + temp;
            wavplay(snd,44100);
            clear vec1 vec2 snd
            
        end
    end
    disp('end patternx2');
    
    % evolution of pattern
    % start by finding entropy
    n1 = hist(drum1,[d b]);
    n2 = hist(drum2,[cl cm ch]);
    dist = 1/bmax * [n1 n2]; %normalize, concatenate distributions
    [val,ind] = sort(dist);
    entro(encnt) = -sum(dist.*log2(dist));
      
    %Now, increase the Entropy by adding a pattern that has least representation:
    if (ind(1)<=2) %if minimum index is 1 or 2 -> drummer 1
        disp('drum1 needs more entropy');
        temp = db(ind(1),:);
        temp(db(ind(1),:)==fill) = [];
        randind = ceil(bmax*rand);
        while(randind>bmax-length(temp))
            randind = randind-1;
        end
        if (drum1(randind:randind+length(temp)-1)==0) %make sure added pattern can fit
            drum1(randind:randind+length(temp)-1) = drum1(randind:randind+length(temp)-1) + temp;
        end
    else %add entropy to drummer 2
        disp('drum2 needs more entropy');
        temp = con(ind(1)-2,:);
        temp(con(ind(1)-2,:)==fill) = [];
        randind = ceil(bmax*rand);
        while(randind>bmax-length(temp))
            randind = randind-1;
        end
        if (drum2(randind:randind+length(temp)-1)==0)
            drum2(randind:randind+length(temp)-1) = drum2(randind:randind+length(temp)-1) + temp;
        end
    end
    encnt = encnt +1;
end

