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