% sinkfun - sink that calls given function with samples.
classdef sinkfun < sinkbase
	properties (GetAccess=private,SetAccess=immutable)
		fun   % [[C,N]]->void
	end
	methods
		function s=sinkfun(fun,channels,rate)
			s.fun=fun;
			s=s@sinkbase(channels,rate);
		end

		function s=tostring(sig)
			s=sprintf('sinkfun(%s)',tostring(sig.fun));
		end

		function s=construct(sig)
			fun=sig.fun;
			t=0;

			s.start   = @nop;
			s.stop    = @nop;
			s.dispose = @nop;
			s.writer  = @writer;

			function w=writer(n)
				w = @next;
				T=(0:n-1)/rate(sig);
				dt=1/rate(sig);
				function rem=next(x), 
					m=size(x,2);
					fun(t+T(1:m),x); rem=0; 
					t=t+n*dt;
				end
			end
		end
	end
end
