%define chords
dim = 15;
%one=[1;8;3;10;-4;5;12;-2];
onesop=[1;3;5;8;10;12];
onealt=[-2;1;3;5;8];
oneten=onesop;
onebas=[-4;-2;1;3;5;8];
%two=[2;9;4;11;-3;6;-1];
twosop=[2;4;6;9;11];
twoalt=[-1;2;4;6];
twoten=twosop;
twobas=[-3;-1;2;4;6];
%four=[4;11;-3;6;-1;1;8];
foursop=[1;4;6;8;11];
fouralt=[-1;1;4;6;8];
fourten=foursop;
fourbas=[-3;-1;1;4;6;8];
%five=[5;12;-2;7;0;2;9];
fivesop=[2;5;7;9;12];
fivealt=[-2;0;2;5;7;9;12];
fiveten=fivesop;
fivebas=[-2;0;2;5;7];
%six=[6;-1;1;8;3;10;-4];
sixsop=[1;3;6;8;10];
sixalt=[-1;1;3;6;8];
sixten=sixsop;
sixbas=[-4;-1;1;3;6;8];
%seven=[7;0;2;9;4;11;-3];
sevensop=[2;4;7;9;11];
sevenalt=[0;2;4;7];
seventen=sevensop;
sevenbas=[-3;0;2;4;7];

quality=[1;2;4;5;6;7]; 

chord = zeros(5,dim); %1st row sop, then alt, ten, bas, and then quality
chord(5,1) = 1; %first chord must be tonic chord
chord(4,1) = 1; %bass on one, root position tonic
for i = 1:3
    chord(i,1) = randsample(onesop,1); %define first chord as tonic
end

for a = 2:dim-6
    k = rand();
    n = rand();
    %leading tone must resolve
    if chord(1,a-1) == 7
        chord (1, a) = 8;
    else
        %determine soprano movement via random walk
        if k <= .33
            chord(1,a) = chord(1,a-1);
        elseif (.33 < k) && (k <=.66)
            if chord (1,a-1) == 1 %soprano range cannot go below 1
                chord (1,a) = chord (1,a-1);
            else
                chord(1,a) = chord(1,a-1) - 1;
            end
        elseif (.66 < k) && (k <=1)
            if chord (1,a-1) == 12 %soprano range cannot exceed 12
                chord (1,a) = chord (1,a-1);
            else
                chord (1,a) = chord(1,a-1) + 1;
            end
        end
    end
    %determine chord quality from soprano note
    if chord(1,a) == 1
        if n <=0.33 
            chord(5,a) = 1;
        elseif (.33 < n) && (n <=.66)
            chord(5,a) = 6;
        elseif (.66 < n) && (n<=1)
            chord(5,a) = 4;
        end
    elseif chord(1,a) == 2
        if n <=0.33 
            chord(5,a) = 2;
        elseif (.33 < n) && (n <=.66)
            chord(5,a) = 5;
        elseif (.66 < n) && (n<=1)
            chord(5,a) = 7;
        end
    elseif chord(1,a) == 3
        if n <=0.5 
            chord(5,a) = 1;
        elseif (.5 < n) && (n<=1)
            chord(5,a) = 6;
        end
    elseif chord(1,a) == 4
        if n <=0.33 
            chord(5,a) = 2;
        elseif (.33 < n) && (n <=.66)
            chord(5,a) = 4;
        elseif (.66 < n) && (n<=1)
            chord(5,a) = 7;
        end
    elseif chord(1,a) == 5
        if n <=0.5
            chord(5,a) = 1;
        elseif (.5 < n) && (n<=1)
            chord(5,a) = 5;
        end
    elseif chord(1,a) == 6
        if n <=0.33 
            chord(5,a) = 2;
        elseif (.33 < n) && (n <=.66)
            chord(5,a) = 4;
        elseif (.66 < n) && (n<=1)
            chord(5,a) = 6;
        end
    elseif chord(1,a) == 7
        if n <=0.5
            chord(5,a) = 5;
        elseif (.5 < n) && (n<=1)
            chord(5,a) = 7;
        end
    elseif chord(1,a) == 8
        if n <=0.33 
            chord(5,a) = 1;
        elseif (.33 < n) && (n <=.66)
            chord(5,a) = 6;
        elseif (.66 < n) && (n<=1)
            chord(5,a) = 4;
        end
    elseif chord(1,a) == 9
        if n <=0.33 
            chord(5,a) = 2;
        elseif (.33 < n) && (n <=.66)
            chord(5,a) = 5;
        elseif (.66 < n) && (n<=1)
            chord(5,a) = 7;
        end
    elseif chord(1,a) == 10
        if n <=0.5 
            chord(5,a) = 1;
        elseif (.5 < n) && (n<=1)
            chord(5,a) = 6;
        end
    elseif chord(1,a) == 11
        if n <=0.33 
            chord(5,a) = 4;
        elseif (.33 < n) && (n <=.66)
            chord(5,a) = 2;
        elseif (.66 < n) && (n<=1)
            chord(5,a) = 7;
        end
    elseif chord(1,a) == 12
        if n <=0.5 
            chord(5,a) = 1;
        elseif (.5 < n) && (n<=1)
            chord(5,a) = 5;
        end
    end
    if chord(5,a) == 1
        chord(2,a) = randsample(onealt,1);
        chord(3,a) = randsample(oneten,1);
        chord(4,a) = randsample(onebas,1);
    elseif chord(5,a) == 2
        chord(2,a) = randsample(twoalt,1);
        chord(3,a) = randsample(twoten,1);
        chord(4,a) = randsample(twobas,1);
    elseif chord(5,a) == 4
        chord(2,a) = randsample(fouralt,1);
        chord(3,a) = randsample(fourten,1);
        chord(4,a) = randsample(fourbas,1);
    elseif chord(5,a) == 5
        chord(2,a) = randsample(fivealt,1);
        chord(3,a) = randsample(fiveten,1);
        chord(4,a) = randsample(fivebas,1);
    elseif chord(5,a) == 6
        chord(2,a) = randsample(sixalt,1);
        chord(3,a) = randsample(sixten,1);
        chord(4,a) = randsample(sixbas,1);
    elseif chord(5,a) == 7
        chord(2,a) = randsample(sevenalt,1);
        chord(3,a) = randsample(seventen,1);
        chord(4,a) = randsample(sevenbas,1);
    end
    chord
end
d = rand(); %will use to determine if final predominant is four or two
if chord(1,dim-6) == 1;
    if d <= 0.5
        chord(5,dim-5) = 2;
        chord(1,dim-5) = 2;
        chord(2,dim-5) = randsample(twoalt,1);
        chord(3,dim-5) = randsample(twoten,1);
        chord(4,dim-5) = randsample(twobas,1);
        chord(1,dim-3) = 2;
        chord(1,dim-1) = 1;
    else
        chord(5,dim-5) = 4;
        chord(1,dim-5) = 1;
        chord(2,dim-5) = randsample(fouralt,1);
        chord(3,dim-5) = randsample(fourten,1);
        chord(4,dim-5) = randsample(fourbas,1);
        chord(1,dim-3) = 2;
        chord(1,dim-1) = 1;
    end
elseif chord(1,dim-6) == 2;
    if d <= 0.5
        chord(5,dim-5) = 2;
        chord(1,dim-5) = 2;
        chord(2,dim-5) = randsample(twoalt,1);
        chord(3,dim-5) = randsample(twoten,1);
        chord(4,dim-5) = randsample(twobas,1);
        chord(1,dim-3) = 2;
        chord(1,dim-1) = 1;
    else
        chord(5,dim-5) = 4;
        chord(1,dim-5) = 1;
        chord(2,dim-5) = randsample(fouralt,1);
        chord(3,dim-5) = randsample(fourten,1);
        chord(4,dim-5) = randsample(fourbas,1);
        chord(1,dim-3) = 2;
        chord(1,dim-1) = 1;
    end
elseif chord(1,dim-6) == 3;
    if d <= 0.5
        chord(5,dim-5) = 2;
        chord(1,dim-5) = 2;
        chord(2,dim-5) = randsample(twoalt,1);
        chord(3,dim-5) = randsample(twoten,1);
        chord(4,dim-5) = randsample(twobas,1);
        chord(1,dim-3) = 2;
        chord(1,dim-1) = 1;
    else
        chord(5,dim-5) = 4;
        chord(1,dim-5) = 4;
        chord(2,dim-5) = randsample(fouralt,1);
        chord(3,dim-5) = randsample(fourten,1);
        chord(4,dim-5) = randsample(fourbas,1);
        chord(1,dim-3) = 2;
        chord(1,dim-1) = 3;
    end
elseif chord(1,dim-6) == 4;
    if d <= 0.5
        chord(5,dim-5) = 2;
        chord(1,dim-5) = 4;
        chord(2,dim-5) = randsample(twoalt,1);
        chord(3,dim-5) = randsample(twoten,1);
        chord(4,dim-5) = randsample(twobas,1);
        chord(1,dim-3) = 2;
        chord(1,dim-1) = 3;
    else
        chord(5,dim-5) = 4;
        chord(1,dim-5) = 4;
        chord(2,dim-5) = randsample(fouralt,1);
        chord(3,dim-5) = randsample(fourten,1);
        chord(4,dim-5) = randsample(fourbas,1);
        chord(1,dim-3) = 2;
        chord(1,dim-1) = 3;
    end
elseif chord(1,dim-6) == 5;
    if d <= 0.5
        chord(5,dim-5) = 2;
        chord(1,dim-5) = 4;
        chord(2,dim-5) = randsample(twoalt,1);
        chord(3,dim-5) = randsample(twoten,1);
        chord(4,dim-5) = randsample(twobas,1);
        chord(1,dim-3) = 2;
        chord(1,dim-1) = 3;
    else
        chord(5,dim-5) = 4;
        chord(1,dim-5) = 4;
        chord(2,dim-5) = randsample(fouralt,1);
        chord(3,dim-5) = randsample(fourten,1);
        chord(4,dim-5) = randsample(fourbas,1);
        chord(1,dim-3) = 2;
        chord(1,dim-1) = 3;
    end
elseif chord(1,dim-6) == 6;
    if d <= 0.5
        chord(5,dim-5) = 2;
        chord(1,dim-5) = 6;
        chord(2,dim-5) = randsample(twoalt,1);
        chord(3,dim-5) = randsample(twoten,1);
        chord(4,dim-5) = randsample(twobas,1);
        chord(1,dim-3) = 7;
        chord(1,dim-1) = 8;
    else
        chord(5,dim-5) = 4;
        chord(1,dim-5) = 6;
        chord(2,dim-5) = randsample(fouralt,1);
        chord(3,dim-5) = randsample(fourten,1);
        chord(4,dim-5) = randsample(fourbas,1);
        chord(1,dim-3) = 7;
        chord(1,dim-1) = 8;
    end
elseif chord(1,dim-6) == 7;
    if d <= 0.5
        chord(5,dim-5) = 2;
        chord(1,dim-5) = 6;
        chord(2,dim-5) = randsample(twoalt,1);
        chord(3,dim-5) = randsample(twoten,1);
        chord(4,dim-5) = randsample(twobas,1);
        chord(1,dim-3) = 7;
        chord(1,dim-1) = 8;
    else
        chord(5,dim-5) = 6;
        chord(1,dim-5) = 8;
        chord(2,dim-5) = randsample(fouralt,1);
        chord(3,dim-5) = randsample(fourten,1);
        chord(4,dim-5) = randsample(fourbas,1);
        chord(1,dim-3) = 7;
        chord(1,dim-1) = 8;
    end
elseif chord(1,dim-6) == 8;
    if d <= 0.5
        chord(5,dim-5) = 2;
        chord(1,dim-5) = 9;
        chord(2,dim-5) = randsample(twoalt,1);
        chord(3,dim-5) = randsample(twoten,1);
        chord(4,dim-5) = randsample(twobas,1);
        chord(1,dim-3) = 9;
        chord(1,dim-1) = 8;
    else
        chord(5,dim-5) = 4;
        chord(1,dim-5) = 8;
        chord(2,dim-5) = randsample(fouralt,1);
        chord(3,dim-5) = randsample(fourten,1);
        chord(4,dim-5) = randsample(fourbas,1);
        chord(1,dim-3) = 7;
        chord(1,dim-1) = 8;
    end
elseif chord(1,dim-6) == 9;
    if d <= 0.5
        chord(5,dim-5) = 2;
        chord(1,dim-5) = 9;
        chord(2,dim-5) = randsample(twoalt,1);
        chord(3,dim-5) = randsample(twoten,1);
        chord(4,dim-5) = randsample(twobas,1);
        chord(1,dim-3) = 9;
        chord(1,dim-1) = 8;
    else
        chord(5,dim-5) = 4;
        chord(1,dim-5) = 8;
        chord(2,dim-5) = randsample(fouralt,1);
        chord(3,dim-5) = randsample(fourten,1);
        chord(4,dim-5) = randsample(fourbas,1);
        chord(1,dim-3) = 7;
        chord(1,dim-1) = 8;
    end
elseif chord(1,dim-6) == 10;
    if d <= 0.5
        chord(5,dim-5) = 2;
        chord(1,dim-5) = 9;
        chord(2,dim-5) = randsample(twoalt,1);
        chord(3,dim-5) = randsample(twoten,1);
        chord(4,dim-5) = randsample(twobas,1);
        chord(1,dim-3) = 9;
        chord(1,dim-1) = 8;
    else
        chord(5,dim-5) = 4;
        chord(1,dim-5) = 11;
        chord(2,dim-5) = randsample(fouralt,1);
        chord(3,dim-5) = randsample(fourten,1);
        chord(4,dim-5) = randsample(fourbas,1);
        chord(1,dim-3) = 9;
        chord(1,dim-1) = 10;
    end
elseif chord(1,dim-6) == 11;
    if d <= 0.5
        chord(5,dim-5) = 2;
        chord(1,dim-5) = 11;
        chord(2,dim-5) = randsample(twoalt,1);
        chord(3,dim-5) = randsample(twoten,1);
        chord(4,dim-5) = randsample(twobas,1);
        chord(1,dim-3) = 9;
        chord(1,dim-1) = 10;
    else
        chord(5,dim-5) = 4;
        chord(1,dim-5) = 11;
        chord(2,dim-5) = randsample(fouralt,1);
        chord(3,dim-5) = randsample(fourten,1);
        chord(4,dim-5) = randsample(fourbas,1);
        chord(1,dim-3) = 9;
        chord(1,dim-1) = 10;
    end
elseif chord(1,dim-6) == 12;
    if d <= 0.5
        chord(5,dim-5) = 2;
        chord(1,dim-5) = 11;
        chord(2,dim-5) = randsample(twoalt,1);
        chord(3,dim-5) = randsample(twoten,1);
        chord(4,dim-5) = randsample(twobas,1);
        chord(1,dim-3) = 9;
        chord(1,dim-1) = 10;
    else
        chord(5,dim-5) = 4;
        chord(1,dim-5) = 11;
        chord(2,dim-5) = randsample(fouralt,1);
        chord(3,dim-5) = randsample(fourten,1);
        chord(4,dim-5) = randsample(fourbas,1);
        chord(1,dim-3) = 9;
        chord(1,dim-1) = 10;
    end
end 
chord(5,dim-3) = 5;
chord(2,dim-3) = randsample(fivealt,1);
chord(3,dim-3) = randsample(fiveten,1);
chord(4,dim-3) = 5; %root position
chord(5,dim-1) = 1;
chord(2,dim-1) = randsample(onealt,1);
chord(3,dim-1) = randsample(oneten,1);
chord(4,dim-1) = 1; %root position
chord(:,dim-4) = chord(:,dim-5);
chord(:,dim-2) = chord(:,dim-3);
chord(:,dim) = chord(:,dim-1);
chord

x = (0:1:16);
aa = x;
aa(:) = 10;
ab = x;
ab(:) = 8;
ac = x;
ac(:) = 6;
ad = x;
ad(:) = 4;
ae = x;
ae(:) = 2;
af = x;
af(:) = -2;
ag = x;
ag(:) = -4;
ah = x;
ah(:) = -6;
ai = x;
ai(:) = -8;
aj = x;
aj(:) = -10;

ys = chord(1,:)-1;
ya = chord(2,:)-1;
yt = chord(3,:)-8;
yb = chord(4,:)-8;
xx = (1:dim);

set(0,'DefaultAxesColorOrder',[0.4,0.4,0.4])
plot(x,aa,x,ab,x,ac,x,ad,x,ae,x,af,x,ag,x,ah,x,ai,x,aj,xx,ys,'o',xx,ya,'o',xx,yt,'o',xx,yb,'o','MarkerSize',20)
    