function h = soundsc(varargin)
%SOUNDSC Autoscale and play vector as sound.
%   SOUNDSC(Y,...) is the same as SOUND(Y,...) except the data is
%   scaled so that the sound is played as loud as possible without
%   clipping.  
%
%   SOUNDSC(Y,...,SLIM) where SLIM = [SLOW SHIGH] maps the values in Y
%   between SLOW and SHIGH to the full sound range.  The default
%   value is SLIM = [MIN(Y) MAX(Y)].
%
%   See also SOUND.

%   Copyright (c) 1984-98 by The MathWorks, Inc.
%   $Revision: 1.7 $  $Date: 1997/11/21 23:24:07 $

error(nargchk(1,4,nargin));

if isequal(size(varargin{end}),[1 2]),
  slim = varargin{end};
  nin = nargin-1;
else
  nin = nargin;
  mn = mean(mean(varargin{1}));
  mx = max(max(abs(varargin{1}-mn)));
  slim = [-mx mx];
end

% Scale the data
if slim(1)==slim(2)
  varargin{1} = zeros(size(varargin{1}));
else
  varargin{1} = (varargin{1}-mean(slim))/(diff(slim)/2);
end

sound(varargin{1:nin})
