function [] = stereo2mono(foldername)
% STEREO2MONO Turns all stereo WAV files in a specified folder into two
% mono WAV files, named origfilename_L.wav and origfilename_R.wav
%
% written by Brecht De Man at C4DM,QMUL on 26 April 2013

currentfolder = pwd;
cd(foldername); % go to specified folder
% go over all wav files in this folder
files = dir('*.wav');

% remove hidden files
% see http://www.mathworks.co.uk/matlabcentral/newsreader/view_thread/258220
for k = length(files):-1:1
    fname = files(k).name;
    if fname(1) == '.'
        files(k) = [ ];
    end
end

for k=1:length(files)
        disp(['Reading' files(k).name '...']);
        
        % TODO: check stereo without reading file
        [audio,fs] = audioread(files(k).name); % read audio
        % check if stereo; if so, get channels and save as separate wavfile
        if size(audio,2)==2
            audiowrite([files(k).name(1:end-4) '_L.wav'],audio(:,1),fs, 'BitsPerSample', 24);
            audiowrite([files(k).name(1:end-4) '_R.wav'],audio(:,2),fs, 'BitsPerSample', 24);
        end
end
cd(currentfolder); % go back to original folder

end