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