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