view sinks/@sinktake/sinktake.m @ 2:7357e1dc2ad6

Simplified scheduler library with new schedule representation.
author samer
date Sat, 22 Dec 2012 16:17:51 +0000
parents 672052bd81f8
children
line wrap: on
line source
% sinktake - limit capacity of sink
%
% sinktake :: N:natural, sink(C,R) -> sink(C,R).
classdef sinktake < sink
	properties (GetAccess=private,SetAccess=immutable)
		length
		dest
	end
	methods
		function s=sinktake(n,dest)
			s.length=n;
			s.dest=dest;
		end

		function s=tostring(sig)
			s=sprintf('take(%d,%s)',sig.length,tostring(sig.dest));
			end
		end

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

		function s=construct(sink)
			sc=construct(sink.dest);
			len=uint32(sink.length);

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

			function r=writer(n)
				rc=sc.writer(n);
				r = @write;
				n=uint32(n);
				function rem=write(x)
					n=size(x,2); % how much to write this time
					if len>n % not planning to stop this time
						rem=rc(x); 
						len=len-(n-rem); % reduce n by amount written
					elseif len>0 % only write upto len samples
						rem=rc(x(:,1:len))+(n-len);
					else
						rem=n;
					end
				end
			end
		end
	end
end