% makestring.m
% returns: string of length bmax with concatenation of drum patterns, randomly chosen
% inputs:
%   drumpatts: matrix that contains drum pattern primitives (1 primitive per row, padded to bmax with filler)
%   bmax: length of string to be made
%   fill: the number used as filler

function drum = makestring(drumpatts,bmax,fill)
[numrows, bla] = size(drumpatts);
pattlen = 0;
index = 0;
while (index<bmax)
    ran = ceil(rand*numrows); %generate a random number, 1:length of primitives
    temp = drumpatts(ran,:);
    temp(temp==fill) = [];
    pattlen = length(temp); %length of chosen primitive
    if (index+pattlen>bmax) %if you will go past end of the bar
        drum(1,(index+1):(index+length(temp))) = 0; %just fill with silences
        break;
    end
    drum(1,(index+1):(index+length(temp))) = temp; %put primitive into string for drummer 1
    index = index + pattlen; %cumulative index
end
drum(bmax+1:length(drum)) = []; %chop to length bmax