function opfiles = calclistftrs(listfile,srcprepend,srcext, ...
                                dstprepend,dstext,skip,fctr,fsd, ...
                                tmean, tsd)
% calclistftrs(listfile,srcprepend,srcext,dstprepend,dstext,fctr,fsd,tmean,tsd)
%   Take listile, a list of input MP3 or WAV files and calculate 
%   beat-synchronous chroma features for each one. 
%   input file names each have srcprepend prepended and srcext appended;
%   features are written to .mat files with the same root name but
%   with dstprepend prepended and dstext appended.  First <skip>
%   items are skipped (for resumption of interrupted runs).
%   fctr and fsd specify a spectral window used to extract chroma 
%   elements with center at fctr Hz, and gaussian log-F half-width
%   of fsd octaves.
%   Return a cell array of the output files written.
% 2006-07-14 dpwe@ee.columbia.edu

%   Copyright (c) 2006 Columbia University.
% 
%   This file is part of LabROSA-coversongID
% 
%   LabROSA-coversongID is free software; you can redistribute it and/or modify
%   it under the terms of the GNU General Public License version 2 as
%   published by the Free Software Foundation.
% 
%   LabROSA-coversongID is distributed in the hope that it will be useful, but
%   WITHOUT ANY WARRANTY; without even the implied warranty of
%   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
%   General Public License for more details.
% 
%   You should have received a copy of the GNU General Public License
%   along with LabROSA-coversongID; if not, write to the Free Software
%   Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
%   02110-1301 USA
% 
%   See the file "COPYING" for the text of the license.

if nargin < 2; srcprepend = ''; end
if nargin < 3; srcext = ''; end
if nargin < 4; dstprepend = ''; end
if nargin < 5; dstext = ''; end
if nargin < 6; skip = 0; end
% downweight fundamentals below here
if nargin < 7; fctr = 400; end
if nargin < 8; fsd = 1.0; end
if nargin < 9; ctype = 1; end
if nargin < 10; tmean = 240.0; end
if nargin < 11; tsd = 1.0; end

ntempos = length(tmean);
if(length(tsd)==1)
    tsd = repmat(tsd,1,ntempos);
end

[files,nfiles] = listfileread(listfile);

if nfiles < 1
  error(['No sound file names read from list file "',listfile,'"']);
end

% preallocate to placate mlint
opfiles{ntempos,nfiles} = '';

% Process every input file
for songn = 1:nfiles
  tline = files{songn};

  % figure out input file names
  if length(srcext) > 0
    if strcmp(tline(end-length(srcext)+1:end), srcext)
      % chop off srcext already there
      tline = tline(1:end-length(srcext));
    end
  else
    % no srcext specified - must be part of input file name
    % separate name and extension for input file
    [srcpath, srcname, srcext] = fileparts(tline);
    tline = fullfile(srcpath,srcname);
  end
    
  % So file names are:
  ifname = fullfile(srcprepend,[tline,srcext]);
  for tt = 1:ntempos
    ofname{tt} = fullfile(dstprepend,['tmean',num2str(tmean(tt))], ...
                          [tline,dstext]);
  end
  
  if songn > skip
  
    % wav files or mp3 files
    if strcmp(srcext,'.mp3')
      [d,sr] = mp3read(ifname,'size');
      if sr >= 32000
        ds = 2;
      else
        ds = 1;
      end
      [d,sr] = mp3read(ifname,0,1,ds); 
    else
      [d,sr]=wavread(ifname);
    end

    %      [F,bts] = chrombeatftrs(d,sr,fctr,fsd,ctype,tmean(tt),tsd(tt));
    % in-line now
    fftlen = 2 ^ (round(log(sr*(2048/22050))/log(2)));
    ffthop = fftlen/4;
    ifsr = sr/ffthop;
    nbin = 12;
    f_ctr = 400;
    f_sd = 1.0;
    
    Y = chromagram_IF(d,sr,fftlen,nbin,f_ctr,f_sd);

    bpms = [];
    
    for tt = 1:ntempos
    
      ofdir = fileparts(ofname{tt});
      % Make sure the parent directory exists
      mymkdir(ofdir)

      % Get the beats for this tempo
      [bts,oe,oesr] = beat2(d,sr,[tmean(tt) tsd(tt)],400,0);
      % avoid recalculation of onset envelope 2nd time
      d = oe;
      sr = oesr;
      
      % condense the chroma per beat
      F = beatavg(Y,bts*ifsr);

      save(ofname{tt},'ifname','F','bts');

      bpms = [bpms, 60/median(diff(bts))];
      
      opfiles{tt,songn} = ofname{tt};
  
    end
    
    disp([datestr(rem(now,1),'HH:MM:SS'), ' song ',num2str(songn),' ', ...
          tline,' bpms=', sprintf('%.0f ',bpms)]);

  end
  
end

% Rearrange opfiles into single list
opfiles = opfiles(:)';