Daniel@0: function [d,tp,fp,f,b,n,ch] = mirread(extract,orig,load,folder,verbose) Daniel@0: % Read the audio file ORIG, at temporal position indicated by EXTRACT. If Daniel@0: % EXTRACT is empty, all the audio file is loaded. Daniel@0: % If LOAD is set to 0, just the meta-data is collected, and the actual Daniel@0: % audio data is not taken into consideration. If it is set to 1, the Daniel@0: % data are loaded from the current directory. If LOAD is a string, it Daniel@0: % is considered as the path of the directory. Daniel@0: % If FOLDER is set to 1, no error is returned if an audio file cannot be Daniel@0: % loaded. Daniel@0: % Output: Daniel@0: % D is the audio signal, Daniel@0: % TP are the temporal positions, Daniel@0: % FP are the two extreme temporal positions (used for frame positions), Daniel@0: % F is the sampling rate, Daniel@0: % B is the resolution in number of bits, Daniel@0: % N is the file name. Daniel@0: % CH are the channel index. Daniel@0: Daniel@0: if nargin < 5 Daniel@0: verbose = 0; Daniel@0: end Daniel@0: d = {}; Daniel@0: f = {}; Daniel@0: b = {}; Daniel@0: tp = {}; Daniel@0: fp = {}; Daniel@0: n = {}; Daniel@0: ch = {}; Daniel@0: try Daniel@0: [d,f,b,tp,fp,n,ch] = audioread(extract,@wavread,orig,load,verbose,folder); Daniel@0: catch Daniel@0: err.wav = lasterr; Daniel@0: try Daniel@0: [d,f,b,tp,fp,n,ch] = audioread(extract,@auread,orig,load,verbose,folder); Daniel@0: catch Daniel@0: err.au = lasterr; Daniel@0: try Daniel@0: [d,f,b,tp,fp,n,ch] = audioread(extract,@mp3read,orig,load,verbose,folder); Daniel@0: catch Daniel@0: err.mp3 = lasterr; Daniel@0: try Daniel@0: [d,f,b,tp,fp,n,ch] = audioread(extract,@aiffread,orig,load,verbose,folder); Daniel@0: catch Daniel@0: err.aiff = lasterr; Daniel@0: if length(orig)>4 && strcmpi(orig(end-3:end),'.bdf') Daniel@0: try Daniel@0: [d,f,b,tp,fp,n,ch] = audioread(extract,@bdfread,orig,load,verbose,folder); Daniel@0: catch Daniel@0: if not(strcmp(err.wav(1:16),'Error using ==> ') && folder) Daniel@0: misread(orig, err); Daniel@0: end Daniel@0: end Daniel@0: else Daniel@0: if not(strcmp(err.wav(1:16),'Error using ==> ') && folder) Daniel@0: misread(orig, err); Daniel@0: end Daniel@0: end Daniel@0: end Daniel@0: end Daniel@0: end Daniel@0: end Daniel@0: Daniel@0: Daniel@0: function [d,f,b,tp,fp,n,ch] = audioread(extract,reader,file,load,verbose,folder) Daniel@0: n = file; Daniel@0: if folder Daniel@0: file = ['./',file]; Daniel@0: end Daniel@0: if load Daniel@0: if isempty(extract) Daniel@0: [s,f,b] = reader(file); Daniel@0: else Daniel@0: [unused,f,b] = reader(file,1); Daniel@0: s = reader(file,extract(1:2)); Daniel@0: if length(extract) > 3 Daniel@0: s = s(:,extract(4)); Daniel@0: end Daniel@0: end Daniel@0: if verbose Daniel@0: disp([file,' loaded.']); Daniel@0: end Daniel@0: d{1} = reshape(s,size(s,1),1,size(s,2)); %channels along dim 3 Daniel@0: ch = 1:size(s,2); Daniel@0: if isempty(extract) || extract(3) Daniel@0: tp{1} = (0:size(s,1)-1)'/f; Daniel@0: else Daniel@0: tp{1} = (extract(1)-1+(0:size(s,1)-1))'/f; Daniel@0: end Daniel@0: if isempty(s) Daniel@0: fp{1} = 0; Daniel@0: else Daniel@0: fp{1} = tp{1}([1 end]); Daniel@0: end Daniel@0: else Daniel@0: [unused,f,b] = reader(file,1); Daniel@0: dsize = reader(file,'size'); Daniel@0: d = dsize(1); Daniel@0: tp = {}; Daniel@0: fp = {}; Daniel@0: ch = dsize(2); Daniel@0: end Daniel@0: Daniel@0: Daniel@0: function [y,fs,nbits] = bdfread(file,check) Daniel@0: DAT = openbdf(file); Daniel@0: NRec = DAT.Head.NRec; Daniel@0: if not(length(check)==2) Daniel@0: b = readbdf(DAT,1); Daniel@0: y = length(b.Record(43,:)) * NRec; Daniel@0: else Daniel@0: y = []; Daniel@0: if mirwaitbar Daniel@0: handle = waitbar(0,'Loading BDF channel...'); Daniel@0: else Daniel@0: handle = 0; Daniel@0: end Daniel@0: for i = 1:NRec Daniel@0: b = readbdf(DAT,i); Daniel@0: y = [y;b.Record(43,:)']; Daniel@0: if handle Daniel@0: waitbar(i/NRec,handle); Daniel@0: end Daniel@0: end Daniel@0: if handle Daniel@0: delete(handle) Daniel@0: end Daniel@0: end Daniel@0: fs = DAT.Head.SampleRate(43); Daniel@0: nbits = NaN; Daniel@0: Daniel@0: Daniel@0: function misread(file,err) Daniel@0: display('Here are the error message returned by each reader:'); Daniel@0: display(err.wav); Daniel@0: display(err.au); Daniel@0: display(err.mp3); Daniel@0: display(err.aiff); Daniel@0: mirerror('MIRREAD',['Cannot open file ',file]);