function skew_beatfile(Name,Indir,Outdir,Offset,Slope)
% skew_beatfile(Name,Srcdir,Dstdir,Offset,Slope)
%     Load the isophonics-format beat annotation file from Indir/Name.txt, 
%     (i.e. lines of <start_time_sec> <beat_type>)
%     then apply time mapping T_out = Offset + Slope*T_in; 
%     rewrite new label file to Outdir/Name.txt.
%     Passing Name as a cell array, and Offset and Slope as
%     vectors, writes a whole series of output files.
% 2011-09-19 Colin Raffel <colin@experimentalistsanonymous.com>
%  after skew_labelfile.m, 2010-04-27 Dan Ellis dpwe@ee.columbia.edu

if iscell(Name)
  % Recurse if passed array
  nN = length(Name);
  for i = 1:nN
    skew_labelfile(Name{i}, Indir, Outdir, Offset(i), Slope(i));
  end
  return
end

if length(Name) == 0
  % silently do nothing with an empty name
  return
end


infilename = fullfile(Indir,[Name,'.txt']);

if exist(infilename,'file') == 0
  if exist(Indir)
    error(['Input label directory ',Indir,' does not exist']);
  else
    error(['Input label file ',infilename,' does not exist']);
  end
end

[tstt,beatType] = textread(infilename,'%f %u');

tstto = max(0,Offset + Slope*tstt);

outfilename = fullfile(Outdir,[Name,'.txt']);
%mymkdir(fileparts(outfilename));
fp = fopen(outfilename,'w');
if fp == 0
  error(['Cannot create ', outfilename]);
end

for i = 1:length(tstto)
  fprintf(fp, '%f  %u\n',tstto(i), beatType(i));
end
fclose(fp);
disp(['wrote ',outfilename]);

