function sw06_makewav_shru(filename, recnum, norm);
% -----------------------------------------------------------
%  sw06_makewav_shru
%   
%   Function that reads a SHRU data file and saves the audio as
%    a 16 bit .wav file.   See also  sw06_playmult_shru.m
% 
%   function sw06_makewav_shru(filename, recnum, norm_fac);
%
%    filename must be an argument or will prompt
%    default recnum  = 1      ( 1 to 128)
%    default norm_fac = 1     ( 10 is normal)
%
%    subroutines needed for this program
%       sw06_readRec_shru
% --------------------------------------------------------------

switch nargin
  case 0,
     filename = input('Enter Filename: ','s');
     recnum = 1;
     norm_fac = 1;
  case 1,
     recnum = 1;
     norm_fac = 1;
  case 2,
     norm_fac = 1;
  case 3,
     ;
  otherwise
     error('check argument list...\n');
end
  

if (recnum < 1 || recnum > 128)
    error('recnum must be between 1 and 128\n');
end

if (norm_fac > 4 || norm_fac < 0)
    error('nrom_fac must be between 0 and 4\n');
end
    
  
% -------------------------------------------------------------------- 
 bytes_hdr = 1024;
 bytes_dat = 1250000;
 bytes_rec = bytes_hdr + bytes_dat;
  
% --------------------------------------------------------------------
% read record to save

fid = fopen(filename,'rb','ieee-be');       % big endian for shru

status = fseek(fid,bytes_rec*(recnum-1),'bof');
if (status < 0)
      drh=[];data=[];
      fprintf('Error: End of file found during record #%d seek...\n',recnum);
      return;
end

[data,drh,status] = sw06_readRec_shru(fid);    % read all records for jjth file, SHRU is a structure
if (status < 0)
      drh=[];data=[];
      fprintf('Error: End of file found during record %d read...\n',recnum);
      return;
end

fclose(fid);
       
% ------------------------------------------------------------------------ 
% save as 16 bit .wav file

wavname = sprintf('shru%s_%s_%s_r%3.3d.wav', drh.shru_num, filename(1:end-4), ...
                              drh.atime([1 2 4 5]), recnum);

% normalize and write
wavwrite(data*norm_fac,drh.rhfs,16,wavname); 
   
   % convert to mp3 ?   linux specific
   %% [s,w] = system(['lame --silent ', wavname_lg, '.wav   ', wavname_lg, '.mp3 ' ]);
   %%%% [s,w] = system(['rm ',wavname_lg, '.wav   ']);

% -----------------------------------------------------------------------------------------     
 
