% -----------------------------------------------------------------------
%	sw06_playmult_shru
%
%	plays the sound from a SHRU hydrophone.
%	This routine will play multiple, consecutive records.
%
%						Newhall WHOI June 2006
% ----------------------------------------------------------------------
%   Usage:   change user defined vars then
%             sw06_playmult_shru
%
%    subroutines needed for this program
%       sw06_readRec_shru
%
%   Modifactions:    added mp3 conversion (linux specific)
% -----------------------------------------------------------------------


% ----------   user defined variables -----------------------

% There are 7 user defined variables.
 
% filename		the CSD filename
% nrec			What consecutive records to deal with (0:127)
% plotfigures	1 to plot figures ,0 not to
% pauseit		1 to pause between plots, 0 not tofixed_gain = 10^(21/20)
% savesound		1 to save sound in wav file, 0 not to
 
filename = '/home/gringo/arthur/sw06/shru/data/shru3_sw53/D0816/08160157.DAT';  nrec = 115;  %thunder
% filename = '/home/gringo/arthur/sw06/shru/data/shru3_sw53/D0813/08131210.DAT';  nrec = 86;  %ship
% filename = '/home/gringo/arthur/sw06/shru/data/shru3_sw53/D0802/08022334.DAT';  nrec = 9:11;  %mohsen

% filename = '/home/gringo/arthur/sw06/shru/data/shru5_sw49/D0802/08022055.DAT';  nrec = 122; 


% nrec = 80:100;		      % N1:N2 convention for multiple records (1-128)
plotfigures = 1;          % look at signals 
pauseit =  0;             % pause figures (probably not)
savesound = 1;            % save audio to disk
mp3       = 0;            % convert to mp3

norm_fac = 10 ;           % normalization for wav file 1/.1 
norm_fac = 8;             % for adcp explosion  

% -------------- end user defined variables --------------------------	
tic;
  
  sfreq = 2.5e6/256;
  spts = 156250;
  bytes_rec = 1250000;
  bytes_hdr = 1024;           
  total_b_rec = bytes_rec + bytes_hdr;
  nrecords = 128;
   
   
fid=fopen(filename,'rb','ieee-be'); % little endian for shark, big for shru
if(fid == -1)
  error(['Input file ' filename ' not opened... exiting.. ']);
end

fprintf('Working on: file %s\n',filename);

Y = [];
status = fseek(fid,(nrec(1)-1)*(total_b_rec),-1);        % skip to the 1st rec
if(status == -1)
    error('  End of file found when skipping to record #%d\n',nrec(1));
end

for rec=nrec

 
  [data,drh,status]=sw06_readRec_shru(fid); 


   if(status == -1)
      fprintf('  File read error, EOF on rec #%d ...\n',rec);
      break
   else
  
     fprintf('    rec No.%d (index  %d)\n',drh.rec,drh.rec+1);    
     Y = [Y data];
  
     if (plotfigures == 1)
       % plot figure2
       figure(2);clf ; orient portrait
	   %for no filter, phone numbers start at 0, so add 1
       %%%%%%%F = (abs(fft(data(phone+1,:),1024)));
       F = (abs(fft(data,1024)));
       freq = [1:1024]./1024 .* sfreq;

       plot(freq(1:512), 20.* log10(F(1:512)));

       s = sprintf('SHRU Spectrum %s rec %03d-%03d',filename(1:end-4),min(rec),max(rec));
       ylims= get(gca,'ylim');
       %% line([224 224],[ylims(1) ylims(2)],'color','r','linestyle','--');
       %% line([400 400],[ylims(1) ylims(2)],'color','r','linestyle','--');
       title(s); grid on
       drawnow

       % plot figure1
       figure(1);clf ; orient portrait
		  plot(Y*norm_fac)
		  % set(gca,'ylim',[-.1 .1]);
          s = sprintf('SHRU  %s rec %03d-%03d',filename(1:end-4),min(nrec),max(nrec));
		  title(s); grid on
   
       drawnow   
     end
  	  title(s); grid on
      
     if (pauseit == 1)
        disp('  pausing... hit <ret>');
        pause
     end
 
   end   % over if status
end   % over nrecs


if (savesound==1)

    %  Normalize? 

  	% save to wav file
    S = sprintf('shru%c_%s_r%03d.wav',drh(1).shru_num,filename(end-11:end-4),min(nrec));
    S2 = sprintf('shru%c_%s_r%03d_2.wav',drh(1).shru_num,filename(end-11:end-4),min(nrec));
    S3 = sprintf('shru%c_%s_r%03d_3.wav',drh(1).shru_num,filename(end-11:end-4),min(nrec));
    % S = sprintf('shru%1d_%s_r%03d.wav',drh(1).shru_num,filename(1:end-4),min(nrec));  
    wavwrite(Y*norm_fac,drh.rhfs,16,S);
    %%% wavwrite(Y*norm_fac,44100,8,S2);
    %%%% wavwrite(Y*norm_fac,drh.rhfs,8,S3);
    wavwrite(Y(450000:end)*norm_fac,drh.rhfs,16,S3);

   % convert to mp3
   if (mp3)
       [s,w] = system(['lame --silent ', wavname_lg, '.wav   ', wavname_lg, '.mp3 ' ]);
  
   end
   
end

toc;
 
% -------------------------------------------------------------------------

