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