% This version does not have rules for how long each drum beat should be (i.e. they can all
% be 32nd notes)
% This version sets a target pattern and gets the drummers to form in such a way to make that pattern,
% using simulated annealing.  Energy metric is a difference signal between target and sum of drummers.
% This does not give any information about drummer interaction in the cirle; rather, it gives information
% about how drummers might synchronize to a group sound.

clear all;
bmax = 16;
quar = bmax/4;
down = 6; %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	

% SET TARGET PATTERN
% random method:
target = floor(rand(1,bmax)*numdrums+1);
for j = 1:2
    for i =1:bmax
        switch target(i)
        case d
            vec = dumwav;
        case b
            vec = bekwav;
        case cl
            vec = conlwav;
        case cm
            vec = conmwav;
        case ch
            vec = conhwav;
        otherwise
            vec = 0;
        end
        snd = vec + click(i)*vec3;
        %wavplay(snd,44100);
        clear snd          
    end
    pause(0.1);
end

% RANDOMLY INITIALIZE DRUMMERS
drum_prim = round(rand(numdrums,bmax));
drum_sum = sum(drum_prim);

alpha = 0.001; %slowest approach
for iter = 1:3000
    diff1 = sum((abs(target-drum_sum)).^2);
    en(iter) = diff1;
    row = ceil(rand*numdrums);
    col = ceil(rand*bmax);
    drum_prim(row,col) = not(drum_prim(row,col));
    drum_sum = sum(drum_prim);
    diff2 = sum((abs(target-drum_sum)).^2);
    delta_diff = diff2-diff1;
    if (delta_diff>0)
        %disp('difference increased');
        prob = exp(-alpha*iter*delta_diff);
        r2 = rand(1);
        % IF r<prob, keep that flip; otherwise:
        if (r2>prob)
            drum_prim(row,col) = not(drum_prim(row,col));
        end
        % ELSE (energy was lowered) keep the choice
    end
    % MAKE THE SOUND____________
    if (iter >= 2990)&(iter<=3000)
        % REPLAY THE TARGET____________
        for i =1:bmax
            switch target(i)
            case d
                vec = dumwav;
            case b
                vec = bekwav;
            case cl
                vec = conlwav;
            case cm
                vec = conmwav;
            case ch
                vec = conhwav;
            otherwise
                vec = 0;
            end
            snd = vec + click(i)*vec3;
            %wavplay(snd,44100);
            clear snd          
        end
        disp('end of target');
        % PLAY THE NEW SOUND______________
        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(['iteration: ',num2str(iter)]);
        
    end   
    % END SOUND___________________
end
figure(1)
plot(en);
title('Energy difference between sum of drummers and target sound');
xlabel('iteration');
ylabel('Energy');
