THE AUDITORY MODELING TOOLBOX

Applies to version: 1.3.0

View the help

Go to function

AMT_LOAD - Load auxiliary data of a model

Program code:

function varargout=amt_load(model,data,variable)
%AMT_LOAD Load auxiliary data of a model
%   Usage: amt_load(MODEL, DATA);
%
%   AMT_LOAD(model, data) loads the auxiliary data from the file data. The data will loaded 
%   from the directory model located in the local auxdata directory given by
%   amt_auxdatapath. 
%
%   If the file is not in the local auxdata directory, it will be downloaded from
%   the web address given by amt_auxdataurl.
%
%   For sofa files, data must be passed in full, i.e. with the '.sofa'
%   extension.
%
%   The following file types are supported:
%
%   - .wav   output will be as that from audioread
%   - .mat   output as that as from load
%   - .sofa  output as that from SOFAload
%   - others   output is the absolute filename
%
%   AMT_LOAD(model, data, variable) loads just a particular variable
%   from the file. 
%
%   See also: amt_auxdatapath amt_auxdataurl
%
%
%   Url: http://amtoolbox.org/amt-1.3.0/doc/core/amt_load.php

  
%   #Author: Piotr Majdak (2015)
%   #Author: Clara Hollomey (2021)
%   #Author: Clara Hollomey (2023):  SOFA support
%   #Author: Clara Hollomey (2021): SOFA support fix

[~,kv]=amt_configuration;

if strcmpi(data(end-4:end), '.sofa')
    localPath = [amt_basepath, filesep, 'hrtf'];
    localfn=fullfile(localPath,model,data);
else
    localfn=fullfile(localPath,model,data);
end
  % file not found? create directories, and download!
if ~exist(localfn,'file')
    % create dir if not existing
  if ~exist(fullfile(localPath,model),'dir') 
    [success,msg]=mkdir(fullfile(localPath,model));
    if success~=1
      error(msg);
    end
  end
    % download
  amt_disp(['Model: ' model '. Downloading auxiliary data: ' data]);
  [~,~,ext] = fileparts(localfn);
  if strcmpi(ext, '.sofa')
    webfn=[kv.hrtfURL '/' model '/' data];
    webfn(strfind(webfn,'\'))='/';
    webfn=regexprep(webfn,' ','%20');
  else
    webfn = [kv.auxdataURL '/' model '/' data];
    webfn(strfind(webfn,'\'))='/';
    webfn=regexprep(webfn,' ','%20');
  end
  %amt_disp('Searching in current AMT version...')
  if isoctave
    [~, stat]=urlwrite(webfn, localfn);
  else
    try
      outfilename = websave(localfn,webfn);
      stat = 1;
    catch
      stat = 0;
    end
  end

  if ~stat
      %amt_disp('Searching in previous AMT versions...')
    for ii = 2:numel(kv.version)
      %amt_disp(['Trying version ', kv.version{ii}])
      webfn = num2str([kv.downloadURL kv.version{ii} '/auxdata/' model '/' data]);
      if isoctave
        [~,stat]=urlwrite(webfn,localfn);
      else
        try
          outfilename = websave(localfn,webfn);
          stat = 1;
        catch
          stat = 0;
        end
      end  

      if stat
        amt_disp(['Found data in version: ' kv.version{ii}]);
        break;
      end
          
      if ii == numel(kv.version)
        error(['Unable to download file: ' webfn]);
      end
    end
  end

end
  % load the content
[~,~,ext] = fileparts(localfn);
switch lower(ext)
  case '.wav'
    [y,fs]=audioread(localfn);
    varargout{1}=y;
    varargout{2}=fs;
  case '.mat'
    if exist('variable','var')
        varargout{1}=load(localfn,variable);
    else
        varargout{1}=load(localfn);
    end
  case '.sofa'
      %suppress output from SOFAload (TODO; make this dependant on
      %amt_flags)
     [~,output] = evalc('SOFAload(localfn)'); 
     varargout{1} = output;
  otherwise
    varargout{1}=localfn;
end