% waveplay.m  reads file  *.wav  can play the file at three speeds, writes the .dat
function waveplay
  clear % use clear and pack on command line
  format compact
  filename=input('enter full file name of a wave audio file ?', 's')
  sprintf('playing file %s \n',filename)
   % [y,Fs,bits] = wavread(filename); replaced by audioread, no bits
  [y,Fs] = audioread(filename);
  % wavplay(y,Fs); replaced by audioplayer
  audioplayer(y,Fs);
  'Fs sample frequency in Hz '
  Fs
  % 'number of bits per sample'
  % bits
  'number of samples '
  n=size(y)
  'maximum value in y '
  max(y)
  'minimum value in y '
  min(y)
  'mean value of y '
  mean(y)
  
  fid=fopen(strcat(filename,'.dat'), 'w');
  for i=1:n % delete leading zeros
    if y(i)~=0 && y(i+1)~=0
      i1=i;
      break
    end
  end
  for i=n:-1:1 % delete trailing zeros
    if y(i)~=0 && y(i-1)~=0
      i2=i;
      break
    end
  end
  avgy=mean(y(i1:i2));
  yy=y(i1:i2)-avgy;
  maxyy=max(abs(yy));
  yy=yy./maxyy;
  fprintf(fid,'%f\n',yy);
  fclose(fid);
  sound(yy,Fs)
  % sound(yy,2*Fs) % double speed
  % sound(yy,2*Fs/3) % half speed
  sprintf('wrote file %s \n', strcat(filename,'.dat'))
end
