Appendix B
%Melody Matrix
m=zeros(36,2);                   
m(1,:)=[-2 1];                   %[note rhythmicvalue]
m(2,:)=[1 1];                    %note expressed in 1/2steps away from A4.
m(3,:)=[5 1];
m(4,:)=[1 1];
m(5,:)=[3 11];
m(6,:)=[-100 1];
m(7,:)=[-2 1];
m(8,:)=[1 1];
m(9,:)=[5 1];
m(10,:)=[1 1];
m(11,:)=[3 11];
m(12,:)=[-100 1];
m(13,:)=[3 1];
m(14,:)=[6 1];
m(15,:)=[10 1];
m(16,:)=[6 1];
m(17,:)=[8 11];
m(18,:)=[-100 1];
m(19,:)=[-2 1];
m(20,:)=[1 1];
m(21,:)=[5 1];
m(22,:)=[1 1];
m(23,:)=[3 11];
m(24,:)=[-100 1];
m(25,:)=[-2 1];
m(26,:)=[3 1];
m(27,:)=[5 1];
m(28,:)=[3 1];
m(29,:)=[1 11];
m(30,:)=[-100 1];
m(31,:)=[-2 1];
m(32,:)=[1 1];
m(33,:)=[5 1];
m(34,:)=[1 1];
m(35,:)=[3 11];
m(36,:)=[-100 1];

%Walking bass Matrix
n(1,:)=[-100 5]; %rest            
n(2,:)=[3 2]; %c-
n(3,:)=[5 2];
n(4,:)=[6 2];
n(5,:)=[8 2];
n(6,:)=[10 2];
n(7,:)=[13 2];
n(8,:)=[15 2];
n(9,:)=[6 2];
n(10,:)=[3 2]; %C-
n(11,:)=[-2 2];
n(12,:)=[1 2];
n(13,:)=[2 2];
n(14,:)=[3 2];
n(15,:)=[5 2];
n(16,:)=[6 2];
n(17,:)=[7 2];
n(18,:)=[8 2];%f-
n(19,:)=[9 2];
n(20,:)=[10 2];
n(21,:)=[11 2];
n(22,:)=[12 2];
n(23,:)=[13 2];
n(24,:)=[14 2];
n(25,:)=[16 2];
n(26,:)=[15 2];%c-
n(27,:)=[3 2];
n(28,:)=[-2 2];
n(29,:)=[1 2];
n(30,:)=[3 2];
n(31,:)=[5 2];
n(32,:)=[6 2];
n(33,:)=[8 2];
n(34,:)=[10 2];%g-
n(35,:)=[5 2];
n(36,:)=[-2 2];
n(37,:)=[-3 2];
n(38,:)=[-4 2];
n(39,:)=[-2 2];
n(40,:)=[-1 2];
n(41,:)=[1 2];
n(42,:)=[3 2];%c-
n(43,:)=[2 2];
n(44,:)=[3 2];
n(45,:)=[4 2];
n(46,:)=[5 2];
n(47,:)=[-1 2];
n(48,:)=[-2 2];
n(49,:)=[1 2];
LC=48*2;
NC=4;
LT=NC*LC+5;

%Reference and Solo orbits
sig=10;                             %Chaotic Lorenz Attractor
r=28;
b=8/3;
F = @(t, y) [sig*(y(2)-y(1)); r*y(1)-y(2)-y(1)*y(3); y(1)*y(2)-b*y(3)];   
%1,2 yo = [2;1;5];                  %Create IC for reference orbit
%1,2 y1 = [2.001;1.001;5];          %previous initial coniditions used
%3yo = [9;-3;0];
%y1 = [9;-2.99999999;5];
%4yo=[-1;2;-3];
%y1=[-1;2;-3.0000001];
%5yo=[4;5;6];
%y1=[3.9999999;5;6];
yo=[-2;2;2];
y1=[-1.9999999;2.0000001;2];
step=.05; %.1 for 1-4
tspan=0:step:step*200;
[ts, ys]=ode45(F, tspan, yo);       %solve for reference orbit
[tt, yy]=ode45(F, tspan, y1);       %solve for variation orbit

%Solo Matrix
s=zeros(200,2);                     %create a solo matrix
for cc=1:200                        %compare var to ref orbit
    compm=400;                      %assign s(i,x) to smallest m(j,x)>s(j,x 
    compr=400;                      %assign s(i,y) to smallest m(j,y)>s(j,y 
    for dd=1:36
        if ys(dd,1)-yy(cc,1)>0 && ys(dd,1)-yy(cc,1)<compm
            s(cc,1)=m(dd,1);
            compm=ys(dd,1)-yy(cc,1);
        else
        end
        if ys(dd,2)-yy(cc,2)>0 && ys(dd,2)-yy(cc,2)<compr
           s(cc,2)=m(dd,2);
          compr=ys(dd,2)-yy(cc,2);
        else
        end
    end
end

%Sound Generator
hz=16000;                           %Define sample rate of wav
L=2500;                             %define length of 1/8th note 
p=zeros(1,L*(LT-LC+6));             
l=zeros(1,L*LT);
r=0;
for a=1:200                         %create array for melody/solo of wav
    if a<37                         %information
       for q=1:m(a,2)*L
        p(r+q)=1/5*(.99*sin(q/hz*2*pi*2^(m(a,1)/12)*440)+.6*sin(2*q/hz*2*pi*2^(m(a,1)/12)*440)+.6*sin(3*q/hz*2*pi*2^(m(a,1)/12)*440)+.15*sin(4*q/hz*2*pi*2^(m(a,1)/12)*440)+.15*sin(5*q/hz*2*pi*2^(m(a,1)/12)*440));
       end
    r=r+m(a,2)*L;
    elseif r+1<=(LT-LC-5)*L
       for q=1:s(a-36,2)*L
        %p(r+q)=.99*sin(q/hz*2*pi*2^(s(a-36,1)/12)*440);
           p(r+q)=1/5*(.99*sin(q/hz*2*pi*2^(s(a-36,1)/12)*440)+.6*sin(2*q/hz*2*pi*2^(s(a-36,1)/12)*440)+.6*sin(3*q/hz*2*pi*2^(s(a-36,1)/12)*440)+.15*sin(4*q/hz*2*pi*2^(s(a-36,1)/12)*440)+.15*sin(5*q/hz*2*pi*2^(s(a-36,1)/12)*440));
       end
    r=r+s(a-36,2)*L;
    else
    end
end
ending=zeros(1,5*L);                %add melody and ending to the end p
p=[p(1:L*(LT-LC-5)) p(1:(LC)*L) ending];

r=0;
for aa=1:193                        %create array for bassline information
    if aa<50                        %define bassline for multiple choruses
    elseif aa<98
        n(aa,:)=n(aa-48,:);
    elseif aa<146
        n(aa,:)=n(aa-96,:);
    else
        n(aa,:)=n(aa-144,:);
    end
    
    for q=1:n(aa,2)*L
        %l(r+q)=.99*sin(q/hz*2*pi*2^(n(aa,1)/12)*110);
        l(r+q)=1/5*(.99*sin(q/hz*2*pi*2^(n(aa,1)/12)*55)+.56*sin(2*q/hz*2*pi*2^(n(aa,1)/12)*55)+.27*sin(3*q/hz*2*pi*2^(n(aa,1)/12)*55)+.15*sin(4*q/hz*2*pi*2^(n(aa,1)/12)*55)+.1*sin(5*q/hz*2*pi*2^(n(aa,1)/12)*55));
    end
    r=r+n(aa,2)*L;
end

z=(p+l)/2;                          %combine bass and melody/solo
wavwrite(z,hz,'blue trane6');       %write the array to wav