Dawn@4: function y = sf_wave(i, samples, chans) Dawn@4: % y=sf_wave(i,samples,chans) - read data from file Dawn@4: % Dawn@4: % y: data read from file (column vector or matrix) Dawn@4: % Dawn@4: % i: structure containing information about file Dawn@4: % samples: range of samples to read ([start stop]) - default ([]) means entire file Dawn@4: % chans: range of channels to read ([lo hi]) - default ([]) means all channels\ Dawn@4: % Dawn@4: Dawn@4: % Alain de Cheveigné, CNRS/Ircam, 2002. Dawn@4: % Copyright (c) 2002 Centre National de la Recherche Scientifique. Dawn@4: % Dawn@4: % Permission to use, copy, modify, and distribute this software without Dawn@4: % fee is hereby granted FOR RESEARCH PURPOSES only, provided that this Dawn@4: % copyright notice appears in all copies and in all supporting Dawn@4: % documentation, and that the software is not redistributed for any Dawn@4: % fee (except for a nominal shipping charge). Dawn@4: % Dawn@4: % For any other uses of this software, in original or modified form, Dawn@4: % including but not limited to consulting, production or distribution Dawn@4: % in whole or in part, specific prior permission must be obtained from CNRS. Dawn@4: % Algorithms implemented by this software may be claimed by patents owned Dawn@4: % by CNRS, France Telecom, Ircam or others. Dawn@4: % Dawn@4: % The CNRS makes no representations about the suitability of this Dawn@4: % software for any purpose. It is provided "as is" without express Dawn@4: % or implied warranty. Beware of the bugs. Dawn@4: Dawn@4: if ~nargin | ~isfield(i, 'fname') Dawn@4: error('usage: i.fname = "file name", then call y=sf_wave(i)'); Dawn@4: end Dawn@4: if ~isfield(i, 'format') Dawn@4: i = sf_info(i); Dawn@4: end Dawn@4: % defaults Dawn@4: if nargin<2 | isempty(samples); Dawn@4: if (i.nsamples) samples=[1 i.nsamples]; end; Dawn@4: end Dawn@4: if nargin<3 | isempty(chans) Dawn@4: if (i.nchans) chans=[1 i.nchans]; end; Dawn@4: end Dawn@4: Dawn@4: % clip Dawn@4: if samples(1) < 1 Dawn@4: samples(1) = 1; Dawn@4: end Dawn@4: if samples(2) > i.nsamples Dawn@4: samples(2) = i.nsamples; Dawn@4: end Dawn@4: if samples(2) < samples(1) Dawn@4: y=[];return Dawn@4: error(['start sample after stop sample: ' num2str(samples(1)) '-' num2str(samples(2))]); Dawn@4: end Dawn@4: if chans(1) < 1 | chans(2) > i.nchans Dawn@4: error(['requested inexistent channels: ' num2str(chans(1)) '-' num2str(chans(2))]); Dawn@4: end Dawn@4: Dawn@4: Dawn@4: % workspace matrix Dawn@4: if strcmp(i.format, 'matrix') Dawn@4: y = i.fname(samples(1):samples(2), chans(1):chans(2)); Dawn@4: return Dawn@4: end Dawn@4: Dawn@4: % use matlab functions for AU and WAV and MACSND Dawn@4: if strcmp(i.format, 'AU') Dawn@4: y=auread(i.fname, [samples(1) samples(2)]); Dawn@4: y=y(:,chans(1):chans(2)); Dawn@4: return; Dawn@4: end Dawn@4: if strcmp(i.format, 'WAV') Dawn@4: y=wavread(i.fname, [samples(1) samples(2)]); Dawn@4: y=y(:,chans(1):chans(2)); Dawn@4: return; Dawn@4: end Dawn@4: if strcmp(i.format, 'MACSND') Dawn@4: if 3==exist('readsnd') Dawn@4: y = eval('readsnd(i.fname)'); Dawn@4: else Dawn@4: error('cannot read MACSND on this platform'); Dawn@4: end Dawn@4: y = y(samples(1):samples(2),chans(1):chans(2)); Dawn@4: return; Dawn@4: end Dawn@4: Dawn@4: % close if open Dawn@4: % if fopen(i.fd) Dawn@4: % fclose(i.fd); Dawn@4: % end Dawn@4: Dawn@4: if ~isfield(i, 'bytes_to_data') Dawn@4: i.bytes_to_data=0; Dawn@4: end Dawn@4: Dawn@4: % ascii formats Dawn@4: if strcmp(i.format, 'ascii') | strcmp(i.format, 'csv') | strcmp(i.format, 'IWAVE') Dawn@4: i.fd = fopen(i.fname, 'rt'); Dawn@4: fseek(i.fd, i.bytes_to_data, -1); Dawn@4: Dawn@4: switch i.format Dawn@4: case 'ascii' Dawn@4: nsamples = samples(2) - samples(1) + 1; Dawn@4: nchans = chans(2) - chans(1) + 1; Dawn@4: y = zeros(nsamples, nchans); Dawn@4: % skip to start Dawn@4: for j=1:samples(1)-1 Dawn@4: line = fgetl(i.fd); Dawn@4: if isempty(line) | line == -1; error('unexpected eof'); end Dawn@4: end Dawn@4: k=1; Dawn@4: % read Dawn@4: for j=samples(1) : samples(2) Dawn@4: line = fgetl(i.fd); Dawn@4: if isempty(line) | line == -1; error('unexpected eof'); end Dawn@4: a = sscanf(line, '%f'); Dawn@4: y(k,:) = a(chans(1):chans(2))'; Dawn@4: k = k+1; Dawn@4: end Dawn@4: case 'cvs' Dawn@4: error('not implemented'); Dawn@4: case 'IWAVE' Dawn@4: error('not implemented'); Dawn@4: end Dawn@4: fclose(i.fd); Dawn@4: return Dawn@4: end Dawn@4: Dawn@4: % binary formats Dawn@4: fr = samples(2) - samples(1) + 1; Dawn@4: skip_samples = i.nchans * (samples(1) - 1); Dawn@4: switch i.format Dawn@4: case 'uchar' Dawn@4: i.fd = fopen(i.fname, 'r'); Dawn@4: fseek(i.fd, i.bytes_to_data, -1); Dawn@4: fseek(i.fd, skip_samples * 1, 0); Dawn@4: y = fread(i.fd, [fr, i.nchans], 'uchar'); Dawn@4: fclose(i.fd); Dawn@4: case 'short' Dawn@4: i.fd = fopen(i.fname, 'r'); Dawn@4: fseek(i.fd, i.bytes_to_data, -1); Dawn@4: fseek(i.fd, skip_samples * 2, 0); Dawn@4: y = fread(i.fd, [fr, i.nchans], 'short'); Dawn@4: fclose(i.fd); Dawn@4: case 'long' Dawn@4: i.fd = fopen(i.fname, 'r'); Dawn@4: fseek(i.fd, i.bytes_to_data, -1); Dawn@4: fseek(i.fd, skip_samples * 4, 0); Dawn@4: y = fread(i.fd, [fr, i.nchans], 'long'); Dawn@4: fclose(i.fd); Dawn@4: case 'float' Dawn@4: i.fd = fopen(i.fname, 'r'); Dawn@4: fseek(i.fd, i.bytes_to_data, -1); Dawn@4: fseek(i.fd, skip_samples * 4, 0); Dawn@4: y = fread(i.fd, [fr, i.nchans], 'float'); Dawn@4: fclose(i.fd); Dawn@4: case 'double' Dawn@4: i.fd = fopen(i.fname, 'r'); Dawn@4: fseek(i.fd, i.bytes_to_data, -1); Dawn@4: fseek(i.fd, skip_samples * 8, 0); Dawn@4: y = fread(i.fd, [fr, i.nchans], 'double'); Dawn@4: fclose(i.fd); Dawn@4: case 'NIST' Dawn@4: i.fd = fopen(i.fname, 'r'); Dawn@4: fseek(i.fd, i.bytes_to_data, -1); Dawn@4: y = zeros(i.nsamples, i.nchans); Dawn@4: switch i.sample_coding Dawn@4: case 'pcm' Dawn@4: fseek(i.fd, skip_samples * 2, 0); Dawn@4: y = fread(i.fd, [fr, i.nchans], 'short'); Dawn@4: otherwise Dawn@4: error(['cannot handle NIST sample_coding = ', i.sample_coding]); Dawn@4: end Dawn@4: fclose(i.fd); Dawn@4: case 'ESPS' Dawn@4: i.fd = fopen(i.fname, 'r'); Dawn@4: fseek(i.fd, i.bytes_to_data, -1); Dawn@4: fseek(i.fd, skip_samples * 2, 0); Dawn@4: y = fread(i.fd, [fr, i.nchans], 'short'); Dawn@4: Dawn@4: case 'AIFF' Dawn@4: i.fd = fopen(i.fname, 'r', 's'); Dawn@4: fseek(i.fd, i.bytes_to_data, -1); Dawn@4: % should check sample size Dawn@4: fseek(i.fd, skip_samples * 2, 0); Dawn@4: y = fread(i.fd, [fr, i.nchans], 'short'); Dawn@4: fclose(i.fd); Dawn@4: Dawn@4: otherwise Dawn@4: error(['don''t know how to load format = ', i.format]); Dawn@4: end Dawn@4: Dawn@4: y = y(:,chans(1):chans(2));