function [x, asf, abits] = rec(sec, sf, ch, bits, dev, mode)

% record from your soundcard (only Linux/*BSD)
% you need `vrec' program from snd-util by Hannu Savolainen 1993
%          and vplay.akira.patch by Akira Nishimura 1998
%
%    [x, asf, abits] = rec(sec [, sf] [, ch] [, bits] [, dev] [, mode]);
%        x : recorded signal vector
%      asf : actural sampling freq (Hz)
%    abits : actural sampling resolution (bits)
%      sec : recording time (sec)
%       sf : sampling frequency  (Hz)   (default: 8000Hz)
%       ch : number of channels         (default: 1)
%     bits : sampling resolution (bits) (default: 16bits)
%     dev  : audio device number (default:0) /dev/dsp?
%     mode : 'i' interactive mode, 'a' auto start mode

if nargin < 1
  help rec;
  return;
end
if nargin < 2
  sf = 8000;
end
if nargin < 3
  ch = 1;
end
if nargin < 4
  bits = 16;
end
if nargin < 5
  dev = 0;
end
if nargin < 6
  mode = 'i';
end

if(bits ~= 16 & bits ~= 8)
  fprintf(2, 'Caution!! sampling resolution is not WAV standard\n');
end

if(is_matlab)
  t = clock;
  fname = ['/tmp/mat-', sprintf('%02.0f%02.0f%02.0f', t(4:6))];
else
  fname = tmpnam;
end
if(ch == 1)
  cmd = sprintf('vrec -c %d -Ew -s%d -t%f -b%d %s.wav', dev, sf, sec, bits, fname);
else
  if(ch == 2)
    cmd = sprintf('vrec -c %d -Ew -S -s%d -t%f -b%d %s.wav', dev, sf, sec, bits, fname);
  else
    error('more than 2-channels is not supported\n');
  end
end

if mode == 'i'
  input ('Please hit ENTER and speak afterwards!\n', 1);
end

if(is_matlab)
  eval(strcat('!', cmd));
else
  system(cmd);
end

[x, asf, abits] = wavread(fname);

if(is_matlab)
  eval(sprintf('!rm %s.wav', fname));
else
  system(sprintf('rm %s.wav', fname));
end

