%% test note from do to si
% Add clear message
clear all;
%     load sound file 'cat.wav'
    [cat, fs] = audioread('cat.wav');
    % normalise the signal
    cat = cat./ max(max(abs(cat)));
    figure(1);clf;
    plot(cat,'b-');
    % load sound file 'dog.wav'
    [dog, fs] = audioread('dog.wav');
    % normalise the signal
    dog = dog./ max(max(abs(dog)));
    figure(2);clf;
    plot(dog,'r-');
% use pitch-shift function
windowSize=100;
hopSize=2;

% There are 12 notes from C to B, they are:
% out_cat_filtered(1) : do
% out_cat_filtered(2) : do#
% out_cat_filtered(3) : re
% out_cat_filtered(4) : re#
% out_cat_filtered(5) : mi
% out_cat_filtered(6) : fa
% out_cat_filtered(7) : fa#
% out_cat_filtered(8) : so
% out_cat_filtered(9) : so#
% out_cat_filtered(10) : la
% out_cat_filtered(11) : la#
% out_cat_filtered(12) : si

% add if statement to generate all necessary output 

for i = 1:12
    out_cat_filtered{i} = pitchshift(cat(:,1), windowSize, hopSize, i);
end

% %There are 12 notes from C to B, they are:
% out_cat_filtered(1) : do
% out_cat_filtered(2) : do#
% out_cat_filtered(3) : re
% out_cat_filtered(4) : re#
% out_cat_filtered(5) : mi
% out_cat_filtered(6) : fa
% out_cat_filtered(7) : fa#
% out_cat_filtered(8) : so
% out_cat_filtered(9) : so#
% out_cat_filtered(10) : la
% out_cat_filtered(11) : la#
% out_cat_filtered(12) : si

for j = 1:12
    out_dog_filtered{j} = pitchshift(dog(:,1), windowSize, hopSize, j);
end

% %play sound
% for i=1:12
%     sound(out_cat_filtered{1,i},fs);
%     pause
%     sound(out_dog_filtered{1,i},fs);
%     pause
% end

%% recompose melody
%  put dog sound or cat sound into to left and right channels separately.
final_L = [];
final_R = [];
% recompose melody. 
% Do you want to use cat or dog sound?
display = '\n Do you want to use cat or dog sound?\n\n >';
% assign the selected string input to vdariable sound_select
sound_select = input(display,'s');
% add if statement to check if the selected string input = cat or input=dog


 if strcmp('dog',sound_select)
    order = [1, 1, 8, 8, 10, 10, 8,0,6,6,5, 5, 3,3, 1];
    for i = 1:length(order)
        null_signal = zeros(25000,1);
        if order(i) == 0
            final_L = rearrange(null_signal, final_L);
            final_R = rearrange(null_signal, final_R);
        else 
            final_L = rearrange(out_dog_filtered{1,order(i)}, final_L);
            final_R = rearrange(out_dog_filtered{1,order(i)}, final_R);
        end
    end
    final_dog = zeros(2,length(final_L));

    final_dog(1,:) = final_L;
    final_dog(2,:) = final_R;
    %play the filtered signal 
    sound(final_dog,fs)
    %Record the filtered signal 
    audiowrite('dog_final.wav',final_dog',fs);
elseif strcmp('cat',sound_select)
    order = [1, 1, 8, 8, 10, 10, 8,0,6,6,5, 5, 3,3, 1];
    for i = 1:length(order)
        null_signal = zeros(44100,1);
        if order(i) == 0
            final_L = rearrange(null_signal, final_L);
            final_R = rearrange(null_signal, final_R);
        else 
            final_L = rearrange(out_cat_filtered{1,order(i)}, final_L);
            final_R = rearrange(out_cat_filtered{1,order(i)}, final_R);
        end
     end
     
    final_cat = zeros(2,length(final_L));

    final_cat(1,:) = final_L;
    final_cat(2,:) = final_R;
   
    %play the filtered signal 
    sound(final_cat,fs);
   
    audiowrite('cat_final.wav',final_cat',fs);
end

