% aacfile - audio signal from AAC file using faad
%
% aacfile :: 
%    path ~'path to AAC encoded audio file',
%    options { bits :: natural/16 ~'requested sample resolution' }
% -> signal(C,R).
%   
% Options are also passed to RAWPIPE
function s=aacfile(file,varargin)
	opts=options('bits',16,varargin{:});
	% this is just to get format information - it's no good for getting data 
	s1=sndpipe(sprintf('faad -w %s',bash_arg(file)));

	% can't remember what's wrong with this...
	%	s=sndpipe(sprintf('faad -f 2 -w "%s" | sox -t raw -r %d -b 16 -c %d -e signed - -t au -',file,rate(s1),channels(s1)),'stringfn',@()sprintf('aacfile(''%s'')',file),varargin{:});
	switch opts.bits
		case 16, bits_code=1; 
		case 24, bits_code=2;
		case 32, bits_code=3;
		otherwise, error('Illegal bits-per-sample');
	end

	s=rawpipe( sprintf('faad -f 2 -b %d -w %s',bits_code,bash_arg(file)), ...
				     audio_format(channels(s1),rate(s1),opts.bits), ...
						 'stringfn',@()sprintf('aacfile(''%s'')',file), ...
						 opts);
end

