% feature extraction
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% batch file for feature extraction
% features are computed for each testfile and stored for later usage
%
%
%
%
%
%
%
%
%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

clear

dirFeatures = 'features/';

fileList = {
    %     'TestData/','Beethoven_5_bernstein_0_22.wav';
    %     'TestData/','OGL25009_22050_mono.wav';
    %     'TestData/','OGL25009_22050_mono.wav';
    'chordTestfiles/','Beatles_AllMyLoving_Beatles_1963-WithTheBeatles-03_0-25.wav';
    'chordTestfiles/','Beatles_AllYouNeedIsLove_Beatles_1967-MagicalMysteryTour-11_163-193.wav';
    'chordTestfiles/','Beatles_AndILoveHer_Beatles_1964-AHardDaysNight-05_0-30.wav';
    'chordTestfiles/','Beatles_BabysInBlack_Beatles_1964-BeatlesForSale-03_0-37.wav';
    'chordTestfiles/','Beatles_BackInTheUSSR_Beatles_1968-TheBeatlesTheWhiteAlbumDisc1-01_10-40.wav';
    'chordTestfiles/','Beatles_CryBabyCry_Beatles_1968-TheBeatlesTheWhiteAlbumDisc2-11_30-60.wav';
    'chordTestfiles/','Beatles_HelterSkelter_Beatles_1968-TheBeatlesTheWhiteAlbumDisc2-06_0-30.wav';
    'chordTestfiles/','Beatles_IAmTheWalrus_Beatles_1967-MagicalMysteryTour-06_0-30.wav';
    'chordTestfiles/','Beatles_ImOnlySleeping_Beatles_1966-Revolver-03_0-35.wav';
    'chordTestfiles/','Beatles_LetItBe_Beatles_1970-LetItBe-06_120-146.wav';
    'chordTestfiles/','Beatles_SavoyTruffle_Beatles_1968-TheBeatlesTheWhiteAlbumDisc2-10_0-30.wav';
    'chordTestfiles/','Beatles_TellMeWhy_Beatles_1964-AHardDaysNight-06_0-30.wav';
    'chordTestfiles/','Beatles_YouCantDoThat_Beatles_1964-AHardDaysNight-12_0-30.wav';
    };

parameter.win_len = 4096;
parameter.hop_size = 2048;


for n = 1:size(fileList,1)

    %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
    % load wav file
    %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%


    wavfile = fileList{n,2};
    [f_audio,sideinfo_audio] = wav_to_audio('', fileList{n,1}, wavfile );
    parameter.fs = sideinfo_audio.wav.fs;


    %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
    % lin F axis spectrogram
    %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%


    parameter.F = 0;

    [TFRData, featureRate, F, T] = audio_to_spectrogram(f_audio,parameter);

    TFRData = abs(TFRData).^2;
    maxF = ceil(4186./ (parameter.fs ./parameter.win_len));
    TFRData = TFRData(1:maxF,:);
    F = F(1:maxF);

    save([dirFeatures wavfile(1:end-4) '__spectrogram' ], 'TFRData' , 'F', 'T', 'featureRate' );


    %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
    % log F axis
    %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

    parameter.F = 440 * 2.^((( 21:1/4:108) - 69) / 12);

    [TFRData, featureRate, F, T] = audio_to_spectrogram(f_audio,parameter);
    TFRData = abs(TFRData).^2;

    save([dirFeatures wavfile(1:end-4) '__logSpectrogram' ], 'TFRData' , 'F', 'T', 'featureRate' );


    %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
    %% FB pitch
    %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

    shiftFB = [0, 1, 3, 5];
    F = 440 * 2.^((( 0.25:1/4:120) - 69) / 12);
    f_idx = (F>=440 * 2.^((( 21) - 69) / 12) & F<=440 * 2.^((( 108) - 69) / 12));

    pitch_c = cell(length(shiftFB),1);
    for idx =1:length(shiftFB)
        parameter.shiftFB = shiftFB(idx);
        [pitch_c{idx},sideinfo] = audio_to_pitchSTMSP_via_FB(f_audio,parameter);
    end

    TFRData = zeros(size(pitch_c{1},1),length(shiftFB).*size(pitch_c{1},2));
    for idx = 1:length(shiftFB)
        TFRData(:,idx:length(shiftFB):end) = pitch_c{length(shiftFB)-idx+1};
    end
    TFRData = TFRData(:,f_idx)';
    F = F(f_idx);
    parameter.featureRate = sideinfo.pitchSTMSP.featureRate;


    save([dirFeatures wavfile(1:end-4) '__pitchRepresentation' ], 'TFRData' , 'F', 'T', 'featureRate' );



end