b@2: function stripzeros(foldername) b@2: % STRIPZEROS strips off an array of samples that equal zero at the b@2: % beginning and end of an audio file. b@2: % b@2: % by Brecht De Man at Centre for Digital Music on 17 November 2014 b@2: b@2: tic; % start measuring time b@2: list = dir([foldername '/*.wav']); % get names of files b@2: b@2: % remove hidden files from list b@2: % see http://www.mathworks.co.uk/matlabcentral/newsreader/view_thread/258220 b@2: for k = length(list):-1:1 b@2: fname = list(k).name; b@2: if fname(1) == '.' b@2: list(k) = [ ]; b@2: end b@2: end b@2: b@2: for i = 1:length(list) % go over each file in the folder b@2: [audio, fs] = audioread([foldername '/' list(i).name]); b@2: b@2: numSamples = size(audio,1); b@2: numChannels = size(audio,2); b@2: bitDepth = 24; % not automated here b@2: startIndex = zeros(numChannels, 1); b@2: stopIndex = zeros(numChannels, 1); b@2: b@2: % first and last non-zero elements (for all channels) b@2: for channel = 1:numChannels b@2: % ? more efficient way to find first and last non-zero sample? b@2: indexOfNonZeroSamples = find(audio(:,channel)); % returns indices non-zero elements b@2: startIndex(channel) = min(indexOfNonZeroSamples); b@2: stopIndex(channel) = max(indexOfNonZeroSamples); b@2: end b@2: b@2: start = max(startIndex); b@2: stop = min(stopIndex); b@2: b@2: % if startIndex and stopIndex are both equal to first and last sample: b@2: % do nothing. b@2: % else: b@2: if start>1 || stop