comparison Code/Descriptors/yin/private/src/sf_wave.m @ 4:92ca03a8fa99 tip

Update to ICASSP 2013 benchmark
author Dawn Black
date Wed, 13 Feb 2013 11:02:39 +0000
parents
children
comparison
equal deleted inserted replaced
3:e1cfa7765647 4:92ca03a8fa99
1 function y = sf_wave(i, samples, chans)
2 % y=sf_wave(i,samples,chans) - read data from file
3 %
4 % y: data read from file (column vector or matrix)
5 %
6 % i: structure containing information about file
7 % samples: range of samples to read ([start stop]) - default ([]) means entire file
8 % chans: range of channels to read ([lo hi]) - default ([]) means all channels\
9 %
10
11 % Alain de Cheveigné, CNRS/Ircam, 2002.
12 % Copyright (c) 2002 Centre National de la Recherche Scientifique.
13 %
14 % Permission to use, copy, modify, and distribute this software without
15 % fee is hereby granted FOR RESEARCH PURPOSES only, provided that this
16 % copyright notice appears in all copies and in all supporting
17 % documentation, and that the software is not redistributed for any
18 % fee (except for a nominal shipping charge).
19 %
20 % For any other uses of this software, in original or modified form,
21 % including but not limited to consulting, production or distribution
22 % in whole or in part, specific prior permission must be obtained from CNRS.
23 % Algorithms implemented by this software may be claimed by patents owned
24 % by CNRS, France Telecom, Ircam or others.
25 %
26 % The CNRS makes no representations about the suitability of this
27 % software for any purpose. It is provided "as is" without express
28 % or implied warranty. Beware of the bugs.
29
30 if ~nargin | ~isfield(i, 'fname')
31 error('usage: i.fname = "file name", then call y=sf_wave(i)');
32 end
33 if ~isfield(i, 'format')
34 i = sf_info(i);
35 end
36 % defaults
37 if nargin<2 | isempty(samples);
38 if (i.nsamples) samples=[1 i.nsamples]; end;
39 end
40 if nargin<3 | isempty(chans)
41 if (i.nchans) chans=[1 i.nchans]; end;
42 end
43
44 % clip
45 if samples(1) < 1
46 samples(1) = 1;
47 end
48 if samples(2) > i.nsamples
49 samples(2) = i.nsamples;
50 end
51 if samples(2) < samples(1)
52 y=[];return
53 error(['start sample after stop sample: ' num2str(samples(1)) '-' num2str(samples(2))]);
54 end
55 if chans(1) < 1 | chans(2) > i.nchans
56 error(['requested inexistent channels: ' num2str(chans(1)) '-' num2str(chans(2))]);
57 end
58
59
60 % workspace matrix
61 if strcmp(i.format, 'matrix')
62 y = i.fname(samples(1):samples(2), chans(1):chans(2));
63 return
64 end
65
66 % use matlab functions for AU and WAV and MACSND
67 if strcmp(i.format, 'AU')
68 y=auread(i.fname, [samples(1) samples(2)]);
69 y=y(:,chans(1):chans(2));
70 return;
71 end
72 if strcmp(i.format, 'WAV')
73 y=wavread(i.fname, [samples(1) samples(2)]);
74 y=y(:,chans(1):chans(2));
75 return;
76 end
77 if strcmp(i.format, 'MACSND')
78 if 3==exist('readsnd')
79 y = eval('readsnd(i.fname)');
80 else
81 error('cannot read MACSND on this platform');
82 end
83 y = y(samples(1):samples(2),chans(1):chans(2));
84 return;
85 end
86
87 % close if open
88 % if fopen(i.fd)
89 % fclose(i.fd);
90 % end
91
92 if ~isfield(i, 'bytes_to_data')
93 i.bytes_to_data=0;
94 end
95
96 % ascii formats
97 if strcmp(i.format, 'ascii') | strcmp(i.format, 'csv') | strcmp(i.format, 'IWAVE')
98 i.fd = fopen(i.fname, 'rt');
99 fseek(i.fd, i.bytes_to_data, -1);
100
101 switch i.format
102 case 'ascii'
103 nsamples = samples(2) - samples(1) + 1;
104 nchans = chans(2) - chans(1) + 1;
105 y = zeros(nsamples, nchans);
106 % skip to start
107 for j=1:samples(1)-1
108 line = fgetl(i.fd);
109 if isempty(line) | line == -1; error('unexpected eof'); end
110 end
111 k=1;
112 % read
113 for j=samples(1) : samples(2)
114 line = fgetl(i.fd);
115 if isempty(line) | line == -1; error('unexpected eof'); end
116 a = sscanf(line, '%f');
117 y(k,:) = a(chans(1):chans(2))';
118 k = k+1;
119 end
120 case 'cvs'
121 error('not implemented');
122 case 'IWAVE'
123 error('not implemented');
124 end
125 fclose(i.fd);
126 return
127 end
128
129 % binary formats
130 fr = samples(2) - samples(1) + 1;
131 skip_samples = i.nchans * (samples(1) - 1);
132 switch i.format
133 case 'uchar'
134 i.fd = fopen(i.fname, 'r');
135 fseek(i.fd, i.bytes_to_data, -1);
136 fseek(i.fd, skip_samples * 1, 0);
137 y = fread(i.fd, [fr, i.nchans], 'uchar');
138 fclose(i.fd);
139 case 'short'
140 i.fd = fopen(i.fname, 'r');
141 fseek(i.fd, i.bytes_to_data, -1);
142 fseek(i.fd, skip_samples * 2, 0);
143 y = fread(i.fd, [fr, i.nchans], 'short');
144 fclose(i.fd);
145 case 'long'
146 i.fd = fopen(i.fname, 'r');
147 fseek(i.fd, i.bytes_to_data, -1);
148 fseek(i.fd, skip_samples * 4, 0);
149 y = fread(i.fd, [fr, i.nchans], 'long');
150 fclose(i.fd);
151 case 'float'
152 i.fd = fopen(i.fname, 'r');
153 fseek(i.fd, i.bytes_to_data, -1);
154 fseek(i.fd, skip_samples * 4, 0);
155 y = fread(i.fd, [fr, i.nchans], 'float');
156 fclose(i.fd);
157 case 'double'
158 i.fd = fopen(i.fname, 'r');
159 fseek(i.fd, i.bytes_to_data, -1);
160 fseek(i.fd, skip_samples * 8, 0);
161 y = fread(i.fd, [fr, i.nchans], 'double');
162 fclose(i.fd);
163 case 'NIST'
164 i.fd = fopen(i.fname, 'r');
165 fseek(i.fd, i.bytes_to_data, -1);
166 y = zeros(i.nsamples, i.nchans);
167 switch i.sample_coding
168 case 'pcm'
169 fseek(i.fd, skip_samples * 2, 0);
170 y = fread(i.fd, [fr, i.nchans], 'short');
171 otherwise
172 error(['cannot handle NIST sample_coding = ', i.sample_coding]);
173 end
174 fclose(i.fd);
175 case 'ESPS'
176 i.fd = fopen(i.fname, 'r');
177 fseek(i.fd, i.bytes_to_data, -1);
178 fseek(i.fd, skip_samples * 2, 0);
179 y = fread(i.fd, [fr, i.nchans], 'short');
180
181 case 'AIFF'
182 i.fd = fopen(i.fname, 'r', 's');
183 fseek(i.fd, i.bytes_to_data, -1);
184 % should check sample size
185 fseek(i.fd, skip_samples * 2, 0);
186 y = fread(i.fd, [fr, i.nchans], 'short');
187 fclose(i.fd);
188
189 otherwise
190 error(['don''t know how to load format = ', i.format]);
191 end
192
193 y = y(:,chans(1):chans(2));