view _writetools/write_song.m @ 9:4ea6619cb3f5 tip

removed log files
author matthiasm
date Fri, 11 Apr 2014 15:55:11 +0100
parents b5b38998ef3b
children
line wrap: on
line source
function write_song(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;
        oldlabel ='';
        oldkeylabel = '';
        oldbasslabel = '';

        outfile = fopen(outfilename,'w');
        if verbose
            fprintf(1, '0.0 ');
        end
        fprintf(outfile, '0.0 ');
        chordcount = 0;
        basscount = 0;
        chordduration = 0;
        bassduration = 0;
        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
                % get the chord name
                chordcount = chordcount + 1;
                if ischar(param.dbn.chordnames{newchord})
                    prelabel = ['C',param.dbn.chordnames{newchord}];
                    [c.rootnote, c.shorthand, c.degreelist, c.bassdegree, success, error] =...
                        getchordinfo(prelabel);
                    if isempty(c.bassdegree)
                        preroot = spellnote(newkey, param.dbn.chordrootnotes(newchord));
                        label{iBeat} = [preroot,lilychordnames{newchord}];
                    else
                        preroot = spellnote(newkey, param.dbn.chordrootnotes(newchord));
                        temp = degree2note(c.bassdegree,preroot);
                        bass = ['/',temp{1}];
                        label{iBeat} = [preroot ,lilychordnames{newchord},bass];
                    end
                else
                    label{iBeat} = 'nc';
                end
                label{iBeat} = strrep(label{iBeat},'#','is');
                label{iBeat} = strrep(label{iBeat},'b','es');
                label{iBeat} = lower(label{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
                basscount = basscount + 1;
                if basscount > 1
                    basslabelduration{basscount-1} = bassduration;
                end
                basslabel{basscount} = spellnote(newkey, param.dbn.chordbassnotes(newbass),'lily');
                bassduration = 1;
            else
                bassduration = bassduration + 1;
            end
            oldbass = newbass;
            oldkey = newkey;
            oldchord = newchord;
        end

        % finish off the durations etc
        basslabelduration{basscount} = bassduration;
        
        fclose(outfile);
        for iLabel = 1:numel(basslabel)
            fprintf(1,'%10s %4.0f\n', basslabel{iLabel}, basslabelduration{iLabel})
        end
end