function write_song2(song, bnet, param, outfilename, filetype, verbose)

prelabels = {'C','Db','D','Eb','E','F','Gb','G','Ab','A','Bb','B','N'};
chordindex = bnet.names('chord');
metposindex = bnet.names('metpos');
if param.dbn.nKey > 0
    keyindex = bnet.names('key');
end

switch filetype
    case 'chordlab'
        chordsequence = [song.mpe{chordindex,:}];
        oldchord = 0;
        oldlabel ='N';
        outfile = fopen(outfilename,'w');
        if verbose
            fprintf(1, '0.0 ');
        end
        fprintf(outfile, '0.0 ');
        for iChord= 1:length(chordsequence)
            newchord = song.mpe{chordindex, iChord};
            if newchord ~= oldchord
                if param.dbn.nKey > 0
                    keylabel = param.dbn.keynames{song.mpe{keyindex,iChord}};
                else
                    keylabel = 'C';
                end
                if ischar(param.dbn.chordnames{newchord})
                    prelabel = ['C',param.dbn.chordnames{newchord}];
                    [c.rootnote, c.shorthand, c.degreelist, c.bassdegree, success, error] =...
                        getchordinfo(prelabel);
                    c.string = addshort2list(c.shorthand, c.degreelist);
                    label = [chordroot_spelling(keylabel, param.dbn.chordrootnotes(newchord)-1 ,strsplit(',',c.string)),param.dbn.chordnames{newchord}];
                else
                    label = 'N';
                end
                if verbose
                    fprintf(1,'%0.3f %s\n%0.3f ', song.beattimes(iChord,1), oldlabel,song.beattimes(iChord,1));
                end
                fprintf(outfile,'%0.3f %s\n%0.3f ', song.beattimes(iChord,1), oldlabel,song.beattimes(iChord,1));
                oldlabel = label;
            end
            oldchord = newchord;
        end
        if verbose
            fprintf(1,'%0.3f %s', song.beattimes(iChord,2), oldlabel);
        end
        fprintf(outfile,'%0.3f %s', song.beattimes(iChord,2), oldlabel);
        fclose(outfile);
    case 'keylab'
        keysequence = [song.mpe{keyindex,:}];
        oldkey = 0;
        oldlabel ='N';
        outfile = fopen(outfilename,'w');
        if verbose
            fprintf(1, '0.0 ');
        end
        fprintf(outfile, '0.0 ');
        for iKey = 1:length(keysequence)
            newkey = song.mpe{keyindex, iKey};
            if newkey ~= oldkey
                keylabel = param.dbn.keynames{song.mpe{keyindex,iKey}};
                if verbose
                    fprintf(1,'%0.3f %s\n%0.3f ', song.beattimes(iKey,1), oldlabel,song.beattimes(iKey,1));
                end
                fprintf(outfile,'%0.3f %s\n%0.3f ', song.beattimes(iKey,1), oldlabel,song.beattimes(iKey,1));
                oldlabel = keylabel;
            end
            oldkey = newkey;
        end
        if verbose
            fprintf(1,'%0.3f %s', song.beattimes(iKey,2), oldlabel);
        end
        fprintf(outfile,'%0.3f %s', song.beattimes(iKey,2), oldlabel);
        fclose(outfile);
    case 'lilypond'
        bassindex = bnet.names('bass');
        lilychordnames = repmat(param.dbn.lilytypechordnames,[1,12]);
        lilychordnames{numel(lilychordnames)+1} = '';

        chordsequence = [song.mpe{chordindex,:}];

        nBeat = length(chordsequence);

        keysequence = [song.mpe{keyindex,:}];

%         label = cell(nBeat,1);
%         keylabel = cell(nBeat,1);

        metpossequence = [song.mpe{metposindex,:}];
        oldchord = 0;
        oldkey = 0;
        oldbass = 0;



        chordcount = 0;
        basscount = 0;
        chordduration = 1;
        bassduration = 1;
        
        % time signature stuff
        partial = find(metpossequence == 1,1)-1;
        if partial > 0
            partial = ['\partial ' lilylength(partial)];
        else 
            partial = '';
        end
        barlines = find(metpossequence == 1);
        timesig = diff(barlines); % for barlines, not beats
        ind = min(length(timesig),union(find(timesig~=4),find(timesig~=4)+1));
        non4timesig = barlines(ind);  % in beats
        timesig = timesig(ind);
        
        % chord and bass loop

        for iBeat= 1:length(chordsequence)
            newchord = song.mpe{chordindex, iBeat};
            newkey = song.mpe{keyindex, iBeat};
            newbass = song.mpe{bassindex, iBeat};
            newmetpos = song.mpe{metposindex, iBeat};
            if param.dbn.nKey > 0
                prekeylabel = param.dbn.keynames{song.mpe{keyindex,iBeat}};
            else
                prekeylabel = 'C';
            end
            if newchord ~= oldchord  || newmetpos == 1 || chordduration == 4
                % get the chord name
                chordcount = chordcount + 1;
                if iBeat > 1
                    chordlabelduration(chordcount-1) = chordduration;
                end
                if ischar(param.dbn.chordnames{newchord})
                    preroot = spellnote(newkey, param.dbn.chordrootnotes(newchord),'lily');
                    root{chordcount} = preroot;
                    if isempty(strfind(param.dbn.chordnames{newchord},'/'))
                        label{chordcount} = lilychordnames{newchord};
                    else
                        bass = spellnote(newkey, param.dbn.chordbassnotes(newchord),'lily');
                        label{chordcount} = [strrep(lilychordnames{newchord},' ',''),'/',bass];
                    end
                else
                    root{chordcount} = 'r';
                    label{chordcount} = '';
                end
                chordbeat(chordcount) = iBeat;

%                 oldlabel = label{iBeat};
                chordduration = 1;
            else
                chordduration = chordduration + 1;
            end
            if oldkey ~= newkey
                keylabel{iBeat} = param.dbn.lilykeynames{song.mpe{keyindex,iBeat}};
%                 keylabel{iBeat} = strrep(keylabel{iBeat},'#','is');
%                 keylabel{iBeat} = strrep(keylabel{iBeat},'b','es');
%                 keylabel{iBeat} = lower(keylabel{iBeat});
%                 oldkeylabel = keylabel{iBeat};
            end
            if oldbass ~= newbass || newmetpos == 1 || bassduration == 4
                basscount = basscount + 1;
                if iBeat > 1
                    basslabelduration(basscount-1) = bassduration;
                end
                basslabel{basscount} = spellnote(newkey, param.dbn.chordbassnotes(newbass),'lily');
                bassbeat(basscount) = iBeat;
                bassduration = 1;
            else                
                bassduration = bassduration + 1;
            end

            oldbass = newbass;
            oldkey = newkey;
            oldchord = newchord;
        end
        

        % finish off the durations etc
        basslabelduration(basscount) = bassduration;
        chordlabelduration(chordcount) = chordduration;

        
        chord_string = 'chordsymb = { \chordmode { ';
        bass_string = 'bassnotes = { \override TextScript #''box-padding = #.7 \override TextScript #''font-size = #-3 \override TextScript #''thickness = #.03 \clef bass';
        for iBeat = 1:nBeat
            chordind = find(chordbeat == iBeat);
            bassind = find(bassbeat == iBeat);
            timesigind = find(non4timesig == iBeat);
            if ~(iBeat > numel(keylabel) || isempty(keylabel{iBeat}))
                chord_string = [chord_string sprintf('  } \\key %s \\chordmode { ', keylabel{iBeat})];
                bass_string = [bass_string sprintf(' \\key %s', keylabel{iBeat})];
            end
            if ~isempty(chordind)
                if ~isempty(timesigind)
                    chord_string = [chord_string sprintf(' \\time %0.0f/4',timesig(timesigind))];
                end
                chord_string = [chord_string sprintf(' %s%s%s',root{chordind}, lilylength(chordlabelduration(chordind)), label{chordind})];
            end
            if ~isempty(bassind)
                bass_string = [bass_string sprintf(' %s%s',basslabel{bassind},lilylength(basslabelduration(bassind)))];

            end
                

        end
        fid = fopen(outfilename);
        fprintf(fid,'\\header{title = "%s"}\n%s}}\n%s\n}\n<<\n\\new ChordNames {\n\\set chordChanges = ##t\n%s\n\\chordsymb\n\n}\n\\new Voice {\n\\chordsymb\n}\n\\new Voice {\n\\bassnotes\n}\n>>\n\\version "2.10.33"\n',...
            song.filenametrunk, chord_string, bass_string, partial);
        fclose(fid);
        if verbose
            fprintf(1,'\\header{title = "%s"}\n%s}}\n%s\n}\n<<\n\\new ChordNames {\n\\set chordChanges = ##t\n%s\n\\chordsymb\n\n}\n\\new Voice {\n\\chordsymb\n}\n\\new Voice {\n\\bassnotes\n}\n>>\n\\version "2.10.33"\n',...
                song.filenametrunk, chord_string, bass_string, partial);
        end
end

function L = lilylength(beats)
switch beats
    case 1
        L = '4';
    case 2
        L = '2';
    case 3
        L = '2.';
    case 4
        L = '1';
end