% jsndfile - audio signal from audio file using JavaSound only
% jsndfile :: path, options(sndstream) -> signal(C,R).
%
% NB: Javazoom MP3 decoder does not get the length of the signal right.
function s=jsndfile(file,varargin)
	string=sprintf('jsndfile(''%s'')',file);
	if strncmp(file,'http://',7)
		mkstream = @(q)urlstream(file,q);
	else
		mkstream = @(q)filestream(file,q);
	end
	s=sndstream(mkstream,'stringfn',@()string,varargin{:});
end

function [str,cleanup]=filestream(file,q)
	jf=jfile(file);
	if ~jf.exists(), error(sprintf('File %s does not exist',file)); end
	if ~q, fprintf('Opening sound file: %s\n',file); end
	str=austream(java.io.FileInputStream(jf));
	cleanup=@nop;
end

function [str,cleanup]=urlstream(url,q)
	jurl=java.net.URL(url);	
	% if ~jf.exists(), error(sprintf('File %s does not exist',file)); end
	if ~q, fprintf('Opening URL: %s\n',url); end
	str=austream(jurl.openStream());
	cleanup=@nop;
end

