function sound(sig, sf, bits, dev, info);

%  play sound via 16 or 8-bit sound card.
%   this program only runs on Linux or *BSD with sound driver
%   needs command line playing software `vplay' in snd-utils
%
%    sound(sig [, sf] [, bits] [, dev] [, info]);
%         sig : array of sounds (max 2-channels)
%         sf  : sampling freq (Hz) (default : 8000)
%        bits : bits/sample        (default : 16)
%         dev : device number 0:/dev/dsp0, 1:/dev/dsp1, 2:/dev/dsp2
%        info : 0: quiet mode (default)
%               1: show information of D/A conversion
%               2: show information of D/A conversion and command

if(nargin < 1)
  help sound
end
if(nargin < 2)
  sf = 8000;
end
if(nargin < 3)
  bits = 16;
end
if(nargin < 4)
  dev = 0;
end
if(nargin < 5)
  info = 0;
end

if findstr(computer, 'cygwin32')
  cygwin = 1;
else
  cygwin = 0;
end

if findstr(computer, 'pc-msdosmsvc')
  msvc = 1;
else
  msvc = 0;
end

if ~exist('OCTAVE_VERSION')
  t = clock;
  tmpfile = ['/tmp/au', sprintf('%02.0f%02.0f%02.0f', t(4:6)), '.wav'];
else
  tmpfile = [eval('tmpnam();'), '.wav'];
end
if cygwin
  t = clock;
  tmpfile = sprintf('%02.0f%02.0f%02.0f.wav', t(4:6));
end

if find(max(abs(sig)) > 1)
  disp('16-bit short overflow will occur');
end

wavwrite(sig, sf, bits, tmpfile);

if cygwin
  cmdline = sprintf('play %s', tmpfile);
elseif msvc
  cmdline = sprintf('play %s', tmpfile);
else
  if info > 0
    if dev == 0
      cmdline = sprintf('/usr/local/bin/vplay %s', tmpfile);
    else
      cmdline = sprintf('/usr/local/bin/vplay -c%d %s', dev, tmpfile);
    end
  else
    if dev == 0
      cmdline = sprintf('/usr/local/bin/vplay -q %s', tmpfile);
    else
      cmdline = sprintf('/usr/local/bin/vplay -q -c%d %s', dev, tmpfile);
    end
  end
end

if ~exist('OCTAVE_VERSION')
  cmdline = sprintf('! %s', cmdline);
  rmfile = ['!rm ' tmpfile];
  if(info > 1)
    disp(cmdline);
  end
  eval(cmdline);
  eval(rmfile);
else
  rmfile = sprintf('rm %s', tmpfile);
  if(info > 1)
    disp(cmdline);
  end
  if cygwin
    command = sprintf('%s; %s', cmdline, rmfile);
  else
    command = sprintf('%s && %s', cmdline, rmfile);
  end
  system(command);
end

	