% wavdata - fsignal object for functional access to WAV file
%
% wavdata :: string ~'file name',
% -> fsignal(R,C) ~'functional signal at some rate R with C channels'. 
classdef wavdata < fsignal
	properties (GetAccess=public, SetAccess=immutable)
		file
	end
	methods
		function a=wavdata(fn)
			sz = mywavread(fn,'size');
			[y,fs] = mywavread(fn,[1 1]);
			a=a@fsignal(fs, sz(2), floor(sz(1)));
			a.file = fn;
		end

		function X=data(a), X=mywavread(a.file)'; end
		function s=tostring(a), s=sprintf('wavdata(''%s'')', a.file); end

		function y=extract(a,dim,range)
			if dim~=2, error('Can only subrange times, not channels'); end
			y=mywavread(a.file,range)';
		end
	end
end

% mywavread - wrapper around wavread to handle extensionless files
%
% See WAVREAD for usage.
% Note: this only works on systems which have file system links
function varargout=mywavread(file,varargin)
	varargout=cell(nargout);
	if ~isempty(strfind(file,'.')) % has extension
		[varargout{:}]=wavread(file,varargin{:});
	else
		tmp=uniquefile(0,pwd,'','_tmp%s.wav'); % temp file in current directory
		system(['ln -s "' file '" ' tmp]);
		[varargout{:}]=wavread(tmp,varargin{:});
		system(['rm ' tmp]);
	end
end

