% Example script of making audio cutting from long recordings.
% Single listening from one recording is needed to get timestamps
%
% Hand clap time instance is updated to Offset-vector
% Author: Anssi Rämö, Nokia Techonologies (2022)
%

clear

OutDir='Ready_DB\';
eval(['!mkdir ' OutDir]);
FadeInOut=1;
FadeLength=4800;
Normalize=1;

% Syncing of recordings made with separate devices. Hand clap can be heard at these
% time instances
%       Eigen  EigenRAW EigenStereo FoA      FoAstereo  LS5      Rode    Objects
Offset=[31.910 31.910   31.910      12.854   12.854     222.696  348.542 348.542];

MasterOffset=Offset(1); % Eigenmike (Time-stamp listening was made from Eigenmike signal)

% Set your cutting times to these vectors
%           1   2     3    4    5    6   7   8   9   10
StartTimes=[33.5 33.0 42.6 51.9 58.4 185 245 305 365 425];
EndTimes  =[61.7 42.5 51.9 61.9 67.7 245 305 365 425 485];

% Put your all input signal here. Same signal may be needed several times,
% if conversion to other formats or some channels are extracted to separate
% ouput files
InName{1}='Eigenmike\eigen_O4_ACN_SN3D_051920_110153.wav'; % Eigen ACN SN3D converted from RAW with Eigenmike VST plugin
InName{2}='Eigenmike\mic_051920_110151.wav';               % Eigen RAW 32chn
InName{3}='Eigenmike\eigen_O4_ACN_SN3D_051920_110153.wav'; % Convert to stereo inside script
InName{4}='SennAFormat\Arboretum_2020May19_rec01.wav';     % Sennheiser Ambeo A-format
InName{5}='SennAFormat\Arboretum_2020May19_rec01.wav';     % Convert to stereo inside script
InName{6}='LS5_XY_Stereo\LS_50178.WAV';                    % Olympus LS-5 XY-stereo
InName{7}='Tascam_Objects_Rode\200519_607_6ch.wav';        % Channels 3 (Left Rode),4 (Right Rode)
InName{8}='Tascam_Objects_Rode\200519_607_6ch.wav';        % Channel 1 (Lasse object), 2 (Anssi object)

% Set output format filenaming here.
OutExt{1}='EigenHoA4';
OutExt{2}='EigenRAW';
OutExt{3}='EigenStereo';
OutExt{4}='FoA';
OutExt{5}='FoAStereo';
OutExt{6}='LS5';
OutExt{7}='RodeStereo';
OutExt{8}='Objects';

% Content Output Name
OutName{1}='Long1';
OutName{2}='Short1';
OutName{3}='Short2';
OutName{4}='Short3';
OutName{5}='Short4';

OutName{6}='BGNoise1';
OutName{7}='BGNoise2';
OutName{8}='BGNoise3';
OutName{9}='BGNoise4';
OutName{10}='BGNoise5';

for k=1:8 % All Output Sample Configurations
    disp(['Loading: ' InName{k} ' Configuration: ' OutExt{k} ]);
    Fullsample=audioread([InName{k}]);
    if (k==3) % Convert HoA to stereo
        Fullsample=[Fullsample(:,1)+Fullsample(:,2) Fullsample(:,1)-Fullsample(:,2)];
    end
    if (k==4) % Convert A-Format to FoA
        Fullsample=ConvAtoBFormat(Fullsample(:,1:4)); % Use your own conversion here
    end
    if (k==5) % Convert A-Format to FoA to stereo
        Fullsample=ConvAtoBFormat(Fullsample(:,1:4));
        Fullsample=[Fullsample(:,1)+Fullsample(:,2) Fullsample(:,1)-Fullsample(:,2)];
    end
    if (k==7) % Select correct channels
        Fullsample=Fullsample(:,3:4);
    end
    if (k==8) % Select correct channels
        Fullsample=Fullsample(:,1:2);
    end
    eval(['!mkdir ' OutDir '\' OutExt{k}]);
    for i=1:length(StartTimes) % All Time Instances
        % Select audio part
        startind=floor(48000*(StartTimes(i)-MasterOffset+Offset(k)));
        endind=ceil(48000*(EndTimes(i)-MasterOffset+Offset(k)));

        disp(['Orig length: ' num2str(length(Fullsample)/48000)]);
        disp(['Start time : ' num2str(startind/48000)]);
        disp(['End time   : ' num2str(endind/48000)]);
        disp(['New length : ' num2str((endind-startind)/48000)]);

        sample=Fullsample(startind:endind,:);
        %normalize
        if (Normalize==1)
            sample=0.9*(sample./max(max(abs(sample))));
        end
        % Fade in and out
        if (FadeInOut==1)
            Channels=min(size(sample));
            FadeIn=repmat((0:1/(FadeLength-1):1)',[1 Channels]).^2;
            sample(1:FadeLength,:)=sample(1:FadeLength,:).*FadeIn;
            FadeOut=repmat((1:-1/(FadeLength-1):0)',[1 Channels]).^2;
            sample(end-FadeLength+1:end,:)=sample(end-FadeLength+1:end,:).*FadeOut;
        end
        disp(['Writing: ' OutDir '\' OutExt{k} '\' OutName{i} '_' OutExt{k} '.wav']);
        audiowrite([OutDir '\' OutExt{k} '\' OutName{i} '_' OutExt{k} '.wav'],sample,48000,'BitsPerSample',16);
    end
end
