annotate toolboxes/MIRtoolbox1.3.2/MIRToolbox/mirread.m @ 0:cc4b1211e677 tip

initial commit to HG from Changeset: 646 (e263d8a21543) added further path and more save "camirversion.m"
author Daniel Wolff
date Fri, 19 Aug 2016 13:07:06 +0200
parents
children
rev   line source
Daniel@0 1 function [d,tp,fp,f,b,n,ch] = mirread(extract,orig,load,folder,verbose)
Daniel@0 2 % Read the audio file ORIG, at temporal position indicated by EXTRACT. If
Daniel@0 3 % EXTRACT is empty, all the audio file is loaded.
Daniel@0 4 % If LOAD is set to 0, just the meta-data is collected, and the actual
Daniel@0 5 % audio data is not taken into consideration. If it is set to 1, the
Daniel@0 6 % data are loaded from the current directory. If LOAD is a string, it
Daniel@0 7 % is considered as the path of the directory.
Daniel@0 8 % If FOLDER is set to 1, no error is returned if an audio file cannot be
Daniel@0 9 % loaded.
Daniel@0 10 % Output:
Daniel@0 11 % D is the audio signal,
Daniel@0 12 % TP are the temporal positions,
Daniel@0 13 % FP are the two extreme temporal positions (used for frame positions),
Daniel@0 14 % F is the sampling rate,
Daniel@0 15 % B is the resolution in number of bits,
Daniel@0 16 % N is the file name.
Daniel@0 17 % CH are the channel index.
Daniel@0 18
Daniel@0 19 if nargin < 5
Daniel@0 20 verbose = 0;
Daniel@0 21 end
Daniel@0 22 d = {};
Daniel@0 23 f = {};
Daniel@0 24 b = {};
Daniel@0 25 tp = {};
Daniel@0 26 fp = {};
Daniel@0 27 n = {};
Daniel@0 28 ch = {};
Daniel@0 29 try
Daniel@0 30 [d,f,b,tp,fp,n,ch] = audioread(extract,@wavread,orig,load,verbose,folder);
Daniel@0 31 catch
Daniel@0 32 err.wav = lasterr;
Daniel@0 33 try
Daniel@0 34 [d,f,b,tp,fp,n,ch] = audioread(extract,@auread,orig,load,verbose,folder);
Daniel@0 35 catch
Daniel@0 36 err.au = lasterr;
Daniel@0 37 try
Daniel@0 38 [d,f,b,tp,fp,n,ch] = audioread(extract,@mp3read,orig,load,verbose,folder);
Daniel@0 39 catch
Daniel@0 40 err.mp3 = lasterr;
Daniel@0 41 try
Daniel@0 42 [d,f,b,tp,fp,n,ch] = audioread(extract,@aiffread,orig,load,verbose,folder);
Daniel@0 43 catch
Daniel@0 44 err.aiff = lasterr;
Daniel@0 45 if length(orig)>4 && strcmpi(orig(end-3:end),'.bdf')
Daniel@0 46 try
Daniel@0 47 [d,f,b,tp,fp,n,ch] = audioread(extract,@bdfread,orig,load,verbose,folder);
Daniel@0 48 catch
Daniel@0 49 if not(strcmp(err.wav(1:16),'Error using ==> ') && folder)
Daniel@0 50 misread(orig, err);
Daniel@0 51 end
Daniel@0 52 end
Daniel@0 53 else
Daniel@0 54 if not(strcmp(err.wav(1:16),'Error using ==> ') && folder)
Daniel@0 55 misread(orig, err);
Daniel@0 56 end
Daniel@0 57 end
Daniel@0 58 end
Daniel@0 59 end
Daniel@0 60 end
Daniel@0 61 end
Daniel@0 62
Daniel@0 63
Daniel@0 64 function [d,f,b,tp,fp,n,ch] = audioread(extract,reader,file,load,verbose,folder)
Daniel@0 65 n = file;
Daniel@0 66 if folder
Daniel@0 67 file = ['./',file];
Daniel@0 68 end
Daniel@0 69 if load
Daniel@0 70 if isempty(extract)
Daniel@0 71 [s,f,b] = reader(file);
Daniel@0 72 else
Daniel@0 73 [unused,f,b] = reader(file,1);
Daniel@0 74 s = reader(file,extract(1:2));
Daniel@0 75 if length(extract) > 3
Daniel@0 76 s = s(:,extract(4));
Daniel@0 77 end
Daniel@0 78 end
Daniel@0 79 if verbose
Daniel@0 80 disp([file,' loaded.']);
Daniel@0 81 end
Daniel@0 82 d{1} = reshape(s,size(s,1),1,size(s,2)); %channels along dim 3
Daniel@0 83 ch = 1:size(s,2);
Daniel@0 84 if isempty(extract) || extract(3)
Daniel@0 85 tp{1} = (0:size(s,1)-1)'/f;
Daniel@0 86 else
Daniel@0 87 tp{1} = (extract(1)-1+(0:size(s,1)-1))'/f;
Daniel@0 88 end
Daniel@0 89 if isempty(s)
Daniel@0 90 fp{1} = 0;
Daniel@0 91 else
Daniel@0 92 fp{1} = tp{1}([1 end]);
Daniel@0 93 end
Daniel@0 94 else
Daniel@0 95 [unused,f,b] = reader(file,1);
Daniel@0 96 dsize = reader(file,'size');
Daniel@0 97 d = dsize(1);
Daniel@0 98 tp = {};
Daniel@0 99 fp = {};
Daniel@0 100 ch = dsize(2);
Daniel@0 101 end
Daniel@0 102
Daniel@0 103
Daniel@0 104 function [y,fs,nbits] = bdfread(file,check)
Daniel@0 105 DAT = openbdf(file);
Daniel@0 106 NRec = DAT.Head.NRec;
Daniel@0 107 if not(length(check)==2)
Daniel@0 108 b = readbdf(DAT,1);
Daniel@0 109 y = length(b.Record(43,:)) * NRec;
Daniel@0 110 else
Daniel@0 111 y = [];
Daniel@0 112 if mirwaitbar
Daniel@0 113 handle = waitbar(0,'Loading BDF channel...');
Daniel@0 114 else
Daniel@0 115 handle = 0;
Daniel@0 116 end
Daniel@0 117 for i = 1:NRec
Daniel@0 118 b = readbdf(DAT,i);
Daniel@0 119 y = [y;b.Record(43,:)'];
Daniel@0 120 if handle
Daniel@0 121 waitbar(i/NRec,handle);
Daniel@0 122 end
Daniel@0 123 end
Daniel@0 124 if handle
Daniel@0 125 delete(handle)
Daniel@0 126 end
Daniel@0 127 end
Daniel@0 128 fs = DAT.Head.SampleRate(43);
Daniel@0 129 nbits = NaN;
Daniel@0 130
Daniel@0 131
Daniel@0 132 function misread(file,err)
Daniel@0 133 display('Here are the error message returned by each reader:');
Daniel@0 134 display(err.wav);
Daniel@0 135 display(err.au);
Daniel@0 136 display(err.mp3);
Daniel@0 137 display(err.aiff);
Daniel@0 138 mirerror('MIRREAD',['Cannot open file ',file]);