% This version does not have rules for how long each drum beat should be (i.e. they can all
% be 32nd notes)

clear all;
bmax = 32;
quar = 8;
eigh = 4;
sixt = 2;
thir = 1;
down = 8; %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
numdrums = 5;
drum = zeros(numdrums,bmax);
drum_prim = zeros(numdrums,bmax);

%PLAY A BASIC CLICK, QUARTER NOTES
% create 'click' track
click = zeros(1,bmax);
click(1:quar:bmax)=1;
click2 = kron(click,vec3);
% for j = 1:3
%     wavplay(click2,44100);
% end	

% SYNCHRONIZATION OF INITIAL STRINGS
% make 6 or 7 beats (occuring on eighth notes) equal to 1, for each drummer. 
drum_prim(:,(1:eigh:bmax)) = 1;
ind = 1:eigh:bmax;
for i = 1:numdrums
    r = ceil(rand(1,2)*length(ind)); %get 2 random numbers - indices to zero out 2 beats (or 1 if r1 = r2)
    drum_prim(i,(ind(r))) = 0;
end

iter = 1; %initialize
entro_new = 0;
entro_temp = 0;
num28 = 0;
ind = 1;
useind = 1;
while ( (num28 <= 4) ) % 4 drummers have 28 beats filled
    %for j = 1:2 %play pattern twice
        % SOUND THE BEATS:        
        for i =1:bmax
            snd = drum_prim(1,i)*dumwav + drum_prim(2,i)*bekwav + drum_prim(3,i)*conlwav + ...
                  drum_prim(4,i)*conmwav + drum_prim(5,i)*conhwav;     
            snd = snd + click(i)*vec3;
            wavplay(snd,44100);
            clear snd          
        end
        disp('end patter x1');
    %end
    %disp('end patternx2');
    
    %%drum_snd = [d*drum_prim(1,:); b*drum_prim(2,:); cl*drum_prim(3,:); cm*drum_prim(4,:); ch*drum_prim(5,:)];
    % evolution of pattern
    
    entro_new = 0;
    while (entro_new <= entro_temp) %until you have increased the entropy, keep trying
        for i = 1:numdrums
            dist(i) = length(nonzeros(drum_prim(i,:))) / bmax; % n is a vector with the probs (number of beats/32) for each drummer         
        end
        [val,ind] = sort(dist);
        for i = 1:5
            if (val(ind(i))>=28/32)
                disp(['drummer ',num2str(ind(i)),' is full']);
                if (useind<5)
                    useind = useind+1
                end
            else
                useind = ind(1);
            end
        end
        %useind = ind(1);
        entro_temp = -sum(dist.*log2(dist));
        disp(['drum ',num2str(useind),' needs more entropy']);
        
        %Now, increase the Entropy by adding a pattern that has least representation:
        [r,c] = find(drum_prim);
        nonz = sortrows([r c])';
        %CHECK FOR 28 BEATS FILLED in the row chosen for needing more entropy    
        for t = 1:5
            n(t) = sum(ismember(nonz(1,:),t)); %counts how many nonzero elts of each drummer
        end  
        num28 = sum(n==28)

        minnonz = nonz(2,nonz(1,:)==1); %column locations (i.e. beats) of nonzero elements for min. entropy candidate
        rnum = ceil(rand*bmax);
        while ( sum(rnum == minnonz)==1 ) %if the random number is one of the nonzero elts, need to get a new number
            disp('place taken');
            rnum = ceil(rand*bmax);
        end
        newbeat = zeros(numdrums,bmax);
        newbeat(useind,rnum) = 1;
        drum_prim = newbeat + drum_prim;
        for i = 1:numdrums
            dist(i) = length(nonzeros(drum_prim(i,:))) / bmax; % n is a vector with the probs (number of beats/32) for each drummer
        end
        entro_new = -sum(dist.*log2(dist));
    end
    entro(iter) = entro_new;
    iter = iter+1;
    %numbeats = sum(drum_prim,2); %vector:number of beats per drummer
    %num28 = sum(ismember(numbeats,28)); %how many drummers have 28 beats filled
end
    
    