clear all;
bmax = 32;
quar = 8; % quarter note length 
eigh = quar/2;
sixt = eigh/2;
thir = sixt/2;
down = 4; %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);

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 db1 db2 db3 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

while(1)
	%generate strings for 2 drummers
	drum1 = makestring(db,bmax,fill);
	drum2 = makestring(con,bmax,fill);
%while(1)	
	%compare drummers
	% first just get beat info; make all drum sounds into 1's
	drum1_prim = zeros(1,bmax);
	drum2_prim = zeros(1,bmax);
	drum1_prim(drum1~=0) = 1; %make nonzero elements of drum pattern into 1's
	drum2_prim(drum2~=0) = 1;
	%%%num2str(drum2_prim)
	
	numtwos = sum(ismember(drum1_prim + drum2_prim,2));
	while (numtwos>3)
        disp('too similar');
        drum2_prim = [drum2_prim(2:bmax) drum2_prim(1)];
        %%%num2str(drum2_prim)
        numtwos = sum(ismember(drum1_prim + drum2_prim,2)); %counts number of twos (i.e. where drummers play at same time)  
	end
	clear drum1_prim drum2_prim numtwos
    
% 	numz = 30;
% 	dbvecpad = zeros(1,numz*bmax);
% 	dbvecpad(1:numz:end) = drum1; %place drum1 value at every 100th slot, zeros in between
% 	dddd=rand(1,30);
% 	dbvecpad = kron(dbvecpad,dddd);
    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
           maxlen = max([length(vec1) length(vec2)]);
           
           snd = zpad(vec1,maxlen) + zpad(vec2,maxlen); %zero pad to length of longest drum sample
           wavplay(snd,44100);
           %clear vec1 vec2 maxlen snd
           
        end
        %drum1 = evolvestring(drum1);
        %drum2 = evolvestring(drum2);
    end
    disp('end patternx2');
end
%figure(1);stem(dbvecpad,'.');
%figure(2);stem(dbvecpad2,'.');
