matthiasm@8: function write_song(song, bnet, param, outfilename, filetype, verbose) matthiasm@8: matthiasm@8: prelabels = {'C','Db','D','Eb','E','F','Gb','G','Ab','A','Bb','B','N'}; matthiasm@8: chordindex = bnet.names('chord'); matthiasm@8: metposindex = bnet.names('metpos'); matthiasm@8: if param.dbn.nKey > 0 matthiasm@8: keyindex = bnet.names('key'); matthiasm@8: end matthiasm@8: matthiasm@8: switch filetype matthiasm@8: case 'chordlab' matthiasm@8: chordsequence = [song.mpe{chordindex,:}]; matthiasm@8: oldchord = 0; matthiasm@8: oldlabel ='N'; matthiasm@8: outfile = fopen(outfilename,'w'); matthiasm@8: if verbose matthiasm@8: fprintf(1, '0.0 '); matthiasm@8: end matthiasm@8: fprintf(outfile, '0.0 '); matthiasm@8: for iChord= 1:length(chordsequence) matthiasm@8: newchord = song.mpe{chordindex, iChord}; matthiasm@8: if newchord ~= oldchord matthiasm@8: if param.dbn.nKey > 0 matthiasm@8: keylabel = param.dbn.keynames{song.mpe{keyindex,iChord}}; matthiasm@8: else matthiasm@8: keylabel = 'C'; matthiasm@8: end matthiasm@8: if ischar(param.dbn.chordnames{newchord}) matthiasm@8: prelabel = ['C',param.dbn.chordnames{newchord}]; matthiasm@8: [c.rootnote, c.shorthand, c.degreelist, c.bassdegree, success, error] =... matthiasm@8: getchordinfo(prelabel); matthiasm@8: c.string = addshort2list(c.shorthand, c.degreelist); matthiasm@8: label = [chordroot_spelling(keylabel, param.dbn.chordrootnotes(newchord)-1 ,strsplit(',',c.string)),param.dbn.chordnames{newchord}]; matthiasm@8: else matthiasm@8: label = 'N'; matthiasm@8: end matthiasm@8: if verbose matthiasm@8: fprintf(1,'%0.3f %s\n%0.3f ', song.beattimes(iChord,1), oldlabel,song.beattimes(iChord,1)); matthiasm@8: end matthiasm@8: fprintf(outfile,'%0.3f %s\n%0.3f ', song.beattimes(iChord,1), oldlabel,song.beattimes(iChord,1)); matthiasm@8: oldlabel = label; matthiasm@8: end matthiasm@8: oldchord = newchord; matthiasm@8: end matthiasm@8: if verbose matthiasm@8: fprintf(1,'%0.3f %s', song.beattimes(iChord,2), oldlabel); matthiasm@8: end matthiasm@8: fprintf(outfile,'%0.3f %s', song.beattimes(iChord,2), oldlabel); matthiasm@8: fclose(outfile); matthiasm@8: case 'keylab' matthiasm@8: keysequence = [song.mpe{keyindex,:}]; matthiasm@8: oldkey = 0; matthiasm@8: oldlabel ='N'; matthiasm@8: outfile = fopen(outfilename,'w'); matthiasm@8: if verbose matthiasm@8: fprintf(1, '0.0 '); matthiasm@8: end matthiasm@8: fprintf(outfile, '0.0 '); matthiasm@8: for iKey = 1:length(keysequence) matthiasm@8: newkey = song.mpe{keyindex, iKey}; matthiasm@8: if newkey ~= oldkey matthiasm@8: keylabel = param.dbn.keynames{song.mpe{keyindex,iKey}}; matthiasm@8: if verbose matthiasm@8: fprintf(1,'%0.3f %s\n%0.3f ', song.beattimes(iKey,1), oldlabel,song.beattimes(iKey,1)); matthiasm@8: end matthiasm@8: fprintf(outfile,'%0.3f %s\n%0.3f ', song.beattimes(iKey,1), oldlabel,song.beattimes(iKey,1)); matthiasm@8: oldlabel = keylabel; matthiasm@8: end matthiasm@8: oldkey = newkey; matthiasm@8: end matthiasm@8: if verbose matthiasm@8: fprintf(1,'%0.3f %s', song.beattimes(iKey,2), oldlabel); matthiasm@8: end matthiasm@8: fprintf(outfile,'%0.3f %s', song.beattimes(iKey,2), oldlabel); matthiasm@8: fclose(outfile); matthiasm@8: case 'lilypond' matthiasm@8: bassindex = bnet.names('bass'); matthiasm@8: lilychordnames = repmat(param.dbn.lilytypechordnames,[1,12]); matthiasm@8: lilychordnames{numel(lilychordnames)+1} = ''; matthiasm@8: matthiasm@8: chordsequence = [song.mpe{chordindex,:}]; matthiasm@8: matthiasm@8: nBeat = length(chordsequence); matthiasm@8: matthiasm@8: keysequence = [song.mpe{keyindex,:}]; matthiasm@8: matthiasm@8: label = cell(nBeat,1); matthiasm@8: keylabel = cell(nBeat,1); matthiasm@8: matthiasm@8: metpossequence = [song.mpe{metposindex,:}]; matthiasm@8: oldchord = 0; matthiasm@8: oldkey = 0; matthiasm@8: oldbass = 0; matthiasm@8: oldlabel =''; matthiasm@8: oldkeylabel = ''; matthiasm@8: oldbasslabel = ''; matthiasm@8: matthiasm@8: outfile = fopen(outfilename,'w'); matthiasm@8: if verbose matthiasm@8: fprintf(1, '0.0 '); matthiasm@8: end matthiasm@8: fprintf(outfile, '0.0 '); matthiasm@8: chordcount = 0; matthiasm@8: basscount = 0; matthiasm@8: chordduration = 0; matthiasm@8: bassduration = 0; matthiasm@8: for iBeat= 1:length(chordsequence) matthiasm@8: newchord = song.mpe{chordindex, iBeat}; matthiasm@8: newkey = song.mpe{keyindex, iBeat}; matthiasm@8: newbass = song.mpe{bassindex, iBeat}; matthiasm@8: newmetpos = song.mpe{metposindex, iBeat}; matthiasm@8: if param.dbn.nKey > 0 matthiasm@8: prekeylabel = param.dbn.keynames{song.mpe{keyindex,iBeat}}; matthiasm@8: else matthiasm@8: prekeylabel = 'C'; matthiasm@8: end matthiasm@8: if newchord ~= oldchord || newmetpos == 1 matthiasm@8: % get the chord name matthiasm@8: chordcount = chordcount + 1; matthiasm@8: if ischar(param.dbn.chordnames{newchord}) matthiasm@8: prelabel = ['C',param.dbn.chordnames{newchord}]; matthiasm@8: [c.rootnote, c.shorthand, c.degreelist, c.bassdegree, success, error] =... matthiasm@8: getchordinfo(prelabel); matthiasm@8: if isempty(c.bassdegree) matthiasm@8: preroot = spellnote(newkey, param.dbn.chordrootnotes(newchord)); matthiasm@8: label{iBeat} = [preroot,lilychordnames{newchord}]; matthiasm@8: else matthiasm@8: preroot = spellnote(newkey, param.dbn.chordrootnotes(newchord)); matthiasm@8: temp = degree2note(c.bassdegree,preroot); matthiasm@8: bass = ['/',temp{1}]; matthiasm@8: label{iBeat} = [preroot ,lilychordnames{newchord},bass]; matthiasm@8: end matthiasm@8: else matthiasm@8: label{iBeat} = 'nc'; matthiasm@8: end matthiasm@8: label{iBeat} = strrep(label{iBeat},'#','is'); matthiasm@8: label{iBeat} = strrep(label{iBeat},'b','es'); matthiasm@8: label{iBeat} = lower(label{iBeat}); matthiasm@8: matthiasm@8: % oldlabel = label{iBeat}; matthiasm@8: chordduration = 1; matthiasm@8: else matthiasm@8: chordduration = chordduration + 1; matthiasm@8: end matthiasm@8: if oldkey ~= newkey matthiasm@8: keylabel{iBeat} = param.dbn.lilykeynames{song.mpe{keyindex,iBeat}}; matthiasm@8: keylabel{iBeat} = strrep(keylabel{iBeat},'#','is'); matthiasm@8: keylabel{iBeat} = strrep(keylabel{iBeat},'b','es'); matthiasm@8: keylabel{iBeat} = lower(keylabel{iBeat}); matthiasm@8: oldkeylabel = keylabel{iBeat}; matthiasm@8: end matthiasm@8: if oldbass ~= newbass || newmetpos == 1 matthiasm@8: basscount = basscount + 1; matthiasm@8: if basscount > 1 matthiasm@8: basslabelduration{basscount-1} = bassduration; matthiasm@8: end matthiasm@8: basslabel{basscount} = spellnote(newkey, param.dbn.chordbassnotes(newbass),'lily'); matthiasm@8: bassduration = 1; matthiasm@8: else matthiasm@8: bassduration = bassduration + 1; matthiasm@8: end matthiasm@8: oldbass = newbass; matthiasm@8: oldkey = newkey; matthiasm@8: oldchord = newchord; matthiasm@8: end matthiasm@8: matthiasm@8: % finish off the durations etc matthiasm@8: basslabelduration{basscount} = bassduration; matthiasm@8: matthiasm@8: fclose(outfile); matthiasm@8: for iLabel = 1:numel(basslabel) matthiasm@8: fprintf(1,'%10s %4.0f\n', basslabel{iLabel}, basslabelduration{iLabel}) matthiasm@8: end matthiasm@8: end