function opfiles = rewrite_chordlabs_mirex(listfile,srcprepend,srcext, ...
                                     dstprepend,dstext, ...
                                     ftrprepend,ftrext, ...
                                     skip)
% rewrite_chordlabs_mire(listfile,srcprepend,srcext,dstprepend,dstext,
%                   ftrprepend,ftrext, skip)
%   Convert label files in time-based format and resample them into 
%   beat-synchronous frames.  Each line of listfile specifies a
%   file, the source time-label files are <srcprepend>/<id><srcext>, 
%   the output matlab files are written to <dstprepend>/<id><dstext> 
%   (with the beat times in a vector "bts" and corresponding label 
%   strings in cell array "L").  The beat times are read from
%   matlab file <ftrprepend>/<id><ftrext>, from a variable "bts".
%   Return a cell array of the output files written.
%   * MIREX VERSION * - labels are 0..24
% 2008-08-10 dpwe@ee.columbia.edu  after coversongs/src/calclistftrs

if nargin < 2; srcprepend = ''; end
if nargin < 3; srcext = ''; end
if nargin < 4; dstprepend = ''; end
if nargin < 5; dstext = ''; end
if nargin < 6; dstprepend = ''; end
if nargin < 7; dstext = ''; end
if nargin < 8; skip = 0; end

[files,nfiles] = listfileread(listfile);

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

% preallocate to placate mlint
opfiles{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]);
  ffname = fullfile(ftrprepend,[tline,ftrext]);
  ofname = fullfile(dstprepend,[tline,dstext]);
  
  % info for output file
  vsn = 20080821.1;
  desc = 'MIREX 0..24 labels sampled onto precalculated beat matrix';

  if songn > skip
  
    % load time data (Chris Harte's function)
    %[ts,te,ll] = labread(ifname);
    % Plain text now
    [ts,ll] = textread(ifname);
    % change labels to origin-1
    ll = ll + 1;
    
    % get beats
    F = load(ffname);
    bts = F.bts;

    % figure time-to-beats mapping
    nlabs = length(ts);
    nbts = length(bts);
    % full array of time offsets
    dd = repmat(ts,1,nbts)-repmat(bts,nlabs,1);
    % find nearest times
    [vv,ix] = min(abs(dd'));
    ix = [ix,nbts];
    Cd = zeros(1,nbts);
    for i = 1:nlabs
      Cd(ix(i):ix(i+1)) = i;
    end
    % Actual labels
    L = ll(Cd);
    
    % Make sure the parent directory exists
    ofdir = fileparts(ofname);
    mymkdir(ofdir)

    % Write output file
    save(ofname,'ifname','ffname','bts','L','vsn','desc');
    
    opfiles{songn} = ofname;
  
    disp([datestr(rem(now,1),'HH:MM:SS'), ' song ',num2str(songn),' ', ...
          tline]);

  end
  
end

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
function r = mymkdir(dir)
% r = mymkdir(dir)
%   Ensure that dir exists by creating all its parents as needed.
% 2006-08-06 dpwe@ee.columbia.edu

[x,m,i] = fileattrib(dir);
if x == 0
  [pdir,nn] = fileparts(dir);
  disp(['creating ',dir,' ... ']);
  mymkdir(pdir);
  % trailing slash results in empty nn
  if length(nn) > 0
    mkdir(pdir, nn);
  end
end
