function varargout = dspblkwafi2(action,varargin)
% DSPBLKWAFI2 Wave Audio File Input block helper function.

% Author: D. Orofino
% Copyright 1995-2000 The MathWorks, Inc.
% $Revision: 1.4 $ $Date: 2000/04/03 17:57:57 $

sys = bdroot;
blk = gcb;
if nargin==0, action='dynamic'; end

switch action
case 'init'
   % Setup default values:
   samples=1;nchans=1;fs=1;bits=1;bps=1;
   mode = '';
   err = '';
   
   % Append WAV extension if no extension specified:
   fName = deblank(get_param(blk,'FileName'));
   [p,n,e] = fileparts(fName);
   if isempty(e),
      e = '.wav';
      fName = [fName e];
   end

   % prepend the full path to the file - why?
   % the m-file code (eg, this file) can find the wav file if it
   % is simply on the MATLAB path.  That's great.  However, the
   % S-Function itself wants to open the file for reading, and it
   % does *not* consult the MATLAB path.  If you're not in the
   % directory (and the full path is not specified in the block dialog),
   % then the S-Function will fail to find the file.
   % If a full path was specified in the mask, then we don't want to 
   % use WHICH, because it will nullify the fName when the 
   % specified path is not in the MATLAB path.
   %
   % So, we simply get the full path to the file,
   % ONLY if the name was specified without a path:
   if isempty(p)
       fName = which(fName);
   end
   
   % Name for display:
   % - don't show path
   % - show the file extension no matter what
   dName = [n e];
   
   % Get file parameters:
   try
      [siz,fs,bits,opts] = wavread(fName,'size');
      nchans  = siz(2);
      mode    = ['(' num2str(fs) 'Hz/' num2str(nchans) 'Ch/' num2str(bits) 'b)'];
   catch
      err = lasterr;
   end
   
   varargout = {fName,dName,fs,nchans,mode,err};
   
case 'icon'
   % Icon drawing
   varargout = {};
   
otherwise,
   error('Unknown action specified');
end

% [EOF] dspblkwafi2.m
