comparison aux/batchResample.m @ 2:5e72201496c8

Bug fixes, added stripzeros function, added new loudness function, moved general documentation to top level, MATLAB_R2014b compatibility
author Brecht De Man <b.deman@qmul.ac.uk>
date Mon, 17 Nov 2014 19:43:43 +0000
parents 4fd284285159
children 2afd6ff39f08
comparison
equal deleted inserted replaced
1:b64c9fb34bd0 2:5e72201496c8
1 function [] = batchResample(foldername, fsnew, bitDepth) 1 function [] = batchresample(foldername, fsnew, bitdepthnew)
2 % BATCH RESAMPLE converts sample rate of all files in folder. 2 % BATCHRESAMPLE converts sample rate of all files in folder.
3 % 3 %
4 % by Brecht De Man at Centre for Digital Music on 13 April 2014 4 % by Brecht De Man at Centre for Digital Music on 13 April 2014
5 5
6 % TODO read sampling rate without reading whole file 6 if nargin < 2
7 fsnew = 96000;
8 end
7 9
8 if nargin <3 10 if nargin < 3
9 bitDepth = 24; 11 bitdepthnew = 24;
10 end 12 end
11 13
12 currentfolder = pwd; 14 currentfolder = pwd;
13 cd(foldername); % go to specified folder 15 cd(foldername); % go to specified folder
14 % go over all wav files in this folder 16 % go over all wav files in this folder
17 % remove hidden files from list 19 % remove hidden files from list
18 % see http://www.mathworks.co.uk/matlabcentral/newsreader/view_thread/258220 20 % see http://www.mathworks.co.uk/matlabcentral/newsreader/view_thread/258220
19 for k = length(files):-1:1 21 for k = length(files):-1:1
20 fname = files(k).name; 22 fname = files(k).name;
21 if fname(1) == '.' 23 if fname(1) == '.'
22 files(k) = [ ]; 24 files(k) = [];
23 end 25 end
24 end 26 end
25 27
26 for k=1:length(files) 28 for k=1:length(files)
27 disp(['Reading ' files(k).name '...']); 29 info = audioinfo(files(k).name);
28 30 bitdepth = info.BitsPerSample;
29 [audio,fs] = audioread(files(k).name); % read audio 31 fs = info.SampleRate;
30 32
31 if fs==fsnew 33 if fs==fsnew && bitdepth == bitdepthnew
32 warning('Sampling rate of original audio file is equal to current sampling rate'); 34 disp([files(k).name ' already at ' num2str(fs) ' Hz, ' num2str(bitdepth) ' bit.']);
33 else 35 else
34 resampledAudio = resample(audio, fsnew, fs); 36 [audio,fs] = audioread(files(k).name); % read audio
35 audiowrite([files(k).name],resampledAudio, fsnew, 'BitsPerSample', bitDepth); 37 disp([files(k).name ' was ' num2str(fs) ' Hz, ' num2str(bitdepth) ' bit.']);
38 if fs ~= fsnew
39 audio = resample(audio, fsnew, fs);
40 end
41 audiowrite([files(k).name], audio, fsnew, ...
42 'BitsPerSample', bitdepthnew);
36 end 43 end
37 end 44 end
45
38 cd(currentfolder); % go back to original folder 46 cd(currentfolder); % go back to original folder
39 47
40 end 48 end