function [out,f] = roommix( in, p, m)
% Return a real room recording simulation
%
%  Usage:
%    [out,f] = roommix( in, p, m);
%
%  Inputs:
%    in: NxM matrix, containing one source in every row
%    p: N vector, nth element containing the position of the nth source
%    m: N vector, nth element containing the position of the nth microphone
%
%  Outputs:
%    out: If there is only one output it returns the mixing
%         filters in an NxN FIR matrix structure,
%         otherwise an NxM matrix containing the mixtures
%    f: Contains the mixing filters in an NxN FIR matrix structure
%
%  Notes:
%    There are 8 different positions for both sources and mikes.  The
%    elements of p and m should be an integer from 1 to 8

% Get size of problem
[r,c] = size( in);

% Default positions and mikes
if nargin == 1
  m = [1 7];
  p = [5 3];
end

% Load room filters
for i = 1:r
	for j = 1:r
		f{i,j} = roomi( p(j), m(i));
	end
end

% Conpensate for bad low freq response and decimate to 22050
% Original filters are at 44100.
for i = 1:r*r
	f{i} = filter( [.5 -.45], 1, f{i});
	f{i} = decimate( f{i}, 2);
end

% Convolve and mix if needed
if nargout == 2
	out = zeros( size( in));
	for i = 1:r
		for j = 1:r
			out(i,:) = out(i,:) + fftfilt( f{i,j}, in(j,:));
		end
	end
end

% Just return filter
if nargout == 1
  out = f;
end
