% Convert SV output format (onset-tab-duration-tab-classID)
% to onset-tab-offset-tab-classID text file

format('long');
fileList = dir('*.txt');

onset_duration = zeros(2,1);
classNames = cell(1);

for j=1:length(fileList)
    
    % Open raw file
    fid = fopen(fileList(j).name,'r+');
    
    % Read 1st line
    tline = fgetl(fid);
    onset_duration(:,1) = sscanf(tline, '%f\t%f\t%*s');
    classNames{1} = char(sscanf(tline, '%*f\t%*f\t%s')');
    
    % Read rest of the lines
    i=1;
    while ischar(tline)
        i = i+1;
        tline = fgetl(fid);
        if (ischar(tline)) 
            onset_duration(:,i) = sscanf(tline, '%f\t%f\t%*s');
            classNames{i} = char(sscanf(tline, '%*f\t%*f\t%s')');
        end;
    end
    linecount = i-1;

    fclose(fid);
    
    
    % Save clean file
    fid = fopen(['Processed/' fileList(j).name],'w+');
    
    for i=1:linecount
        
        fprintf(fid,'%.9f\t%.9f\t',[onset_duration(1,i) onset_duration(1,i)+onset_duration(2,i)]);
        fprintf(fid,'%s\n',[classNames{i}]);    
        
    end;
    
    fclose(fid);
    
end;