function parts2csv(song, filename, varargin)
if nargin > 2 && strcmp(varargin{1}, 'integrated')
    parts = song.integratedparts;
else 
    parts = song.parts;
end

beattimes = song.beattimes;
% beattimes = [0; beattimes];
indices = [];
levels = [];
letters = {};

for iPart = 1:length(parts)
    for iInstance = 1:length(parts(iPart).indices)
       indices = [indices parts(iPart).indices(iInstance)];
       levels = [levels parts(iPart).level];
       letters = [letters parts(iPart).letter];
    end
end

[sorted, sortind] = sort(indices);

f = fopen(filename,'w');

for iSeg = 1:length(indices)
    fprintf(f,'%1.2f,%1.3f,"%s"\n',beattimes(sorted(iSeg)),levels(sortind(iSeg)),letters{sortind(iSeg)});
end

fclose(f);

