% sinkmap - sink that applies arbitrart function before sending to subsink.
%
% sinkmap ::
%    ([[C1,N]] -> [[C2,N]]) ~'function to tranform samples',
%    (C2:natural -> C1:natural) ~'function to compute number of channels',
%    sink(C2,R)
% -> sink(C1,R).
classdef sinkmap < sink
	properties (GetAccess=private,SetAccess=immutable)
		fun
		dest
		chans
	end
	methods
		function s=sinkmap(f,chf,sig)
			s.fun=f;
			s.sig=sig;
			s.chans=chf(channels(dest));
		end

		function c=channels(s), c=s.chans; end
		function c=rate(s), c=rate(s.dest); end
		function s=construct(sig)

			f=sig.fun;
			s1=construct(sig.dest);
			s.start   = s1.start;
			s.stop    = s1.stop;
			s.dispose = s1.dispose;
			s.writer  = @writer;

			function r=writer(n)
				r1=s1.writer(n);
				r =@(x)r1(f(x));
			end
		end
		function s=tostring(sig)
			s=sprintf('map(%s,%s)',tostring(sig.fun),tostring(sig.dest));
			end
		end
	end
end
