function auwrite(y,filename)
%AUWRITE Write mu-law encoded audio file.
%	AUWRITE(Y,'filename') converts Y to mu-law encoded bytes
%	and writes it to the specified audio file.
%	/dev/audio is used if the file name is not given.

%	Copyright (c) 1984-93 by The MathWorks, Inc.

localsound = 0;
if nargin < 2
   filename = '/dev/audio';
   machinename = getenv('HOST');
   dspname = getenv('DISPLAY');
   ii = find(dspname == ':');
   dspname  = dspname(1:ii-1);
% if host isn't same as display, not local, unless the display is :0.0
   if strcmp(dspname,machinename) | isempty(dspname)
      localsound  = 1;
   end
end
mu = lin2mu(y);
if ~localsound % make temporary filename and place in current directory.
   t = clock;
   hm = pwd;
   filename = [hm, '/', sprintf('tp%02.0f%02.0f%02.0f', t(4), t(5), t(6))];
end
fp = fopen(filename,'wb');
if fp == -1
   disp('Audio capabilities are not available on this machine or you')
   disp('do not have write privileges for this file.')
else
   fwrite(fp,mu,'uchar');
   fclose(fp);
end
if ~localsound
   cp = computer;
   commandsh = 'rsh';
   if cp(1) == 'H'
      commandsh = 'remsh';
   end
   eval(['!', commandsh, ' ', dspname, ' "cat ', filename,' > /dev/audio"']);
   delete(filename);
end
