matthiasm@8: function write_song2(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: matthiasm@8: matthiasm@8: matthiasm@8: chordcount = 0; matthiasm@8: basscount = 0; matthiasm@8: chordduration = 1; matthiasm@8: bassduration = 1; matthiasm@8: matthiasm@8: % time signature stuff matthiasm@8: partial = find(metpossequence == 1,1)-1; matthiasm@8: if partial > 0 matthiasm@8: partial = ['\partial ' lilylength(partial)]; matthiasm@8: else matthiasm@8: partial = ''; matthiasm@8: end matthiasm@8: barlines = find(metpossequence == 1); matthiasm@8: timesig = diff(barlines); % for barlines, not beats matthiasm@8: ind = min(length(timesig),union(find(timesig~=4),find(timesig~=4)+1)); matthiasm@8: non4timesig = barlines(ind); % in beats matthiasm@8: timesig = timesig(ind); matthiasm@8: matthiasm@8: % chord and bass loop matthiasm@8: 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 || chordduration == 4 matthiasm@8: % get the chord name matthiasm@8: chordcount = chordcount + 1; matthiasm@8: if iBeat > 1 matthiasm@8: chordlabelduration(chordcount-1) = chordduration; matthiasm@8: end matthiasm@8: if ischar(param.dbn.chordnames{newchord}) matthiasm@8: preroot = spellnote(newkey, param.dbn.chordrootnotes(newchord),'lily'); matthiasm@8: root{chordcount} = preroot; matthiasm@8: if isempty(strfind(param.dbn.chordnames{newchord},'/')) matthiasm@8: label{chordcount} = lilychordnames{newchord}; matthiasm@8: else matthiasm@8: bass = spellnote(newkey, param.dbn.chordbassnotes(newchord),'lily'); matthiasm@8: label{chordcount} = [strrep(lilychordnames{newchord},' ',''),'/',bass]; matthiasm@8: end matthiasm@8: else matthiasm@8: root{chordcount} = 'r'; matthiasm@8: label{chordcount} = ''; matthiasm@8: end matthiasm@8: chordbeat(chordcount) = 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 || bassduration == 4 matthiasm@8: basscount = basscount + 1; matthiasm@8: if iBeat > 1 matthiasm@8: basslabelduration(basscount-1) = bassduration; matthiasm@8: end matthiasm@8: basslabel{basscount} = spellnote(newkey, param.dbn.chordbassnotes(newbass),'lily'); matthiasm@8: bassbeat(basscount) = iBeat; matthiasm@8: bassduration = 1; matthiasm@8: else matthiasm@8: bassduration = bassduration + 1; matthiasm@8: end matthiasm@8: matthiasm@8: oldbass = newbass; matthiasm@8: oldkey = newkey; matthiasm@8: oldchord = newchord; matthiasm@8: end matthiasm@8: matthiasm@8: matthiasm@8: % finish off the durations etc matthiasm@8: basslabelduration(basscount) = bassduration; matthiasm@8: chordlabelduration(chordcount) = chordduration; matthiasm@8: matthiasm@8: matthiasm@8: chord_string = 'chordsymb = { \chordmode { '; matthiasm@8: bass_string = 'bassnotes = { \override TextScript #''box-padding = #.7 \override TextScript #''font-size = #-3 \override TextScript #''thickness = #.03 \clef bass'; matthiasm@8: for iBeat = 1:nBeat matthiasm@8: chordind = find(chordbeat == iBeat); matthiasm@8: bassind = find(bassbeat == iBeat); matthiasm@8: timesigind = find(non4timesig == iBeat); matthiasm@8: if ~(iBeat > numel(keylabel) || isempty(keylabel{iBeat})) matthiasm@8: chord_string = [chord_string sprintf(' } \\key %s \\chordmode { ', keylabel{iBeat})]; matthiasm@8: bass_string = [bass_string sprintf(' \\key %s', keylabel{iBeat})]; matthiasm@8: end matthiasm@8: if ~isempty(chordind) matthiasm@8: if ~isempty(timesigind) matthiasm@8: chord_string = [chord_string sprintf(' \\time %0.0f/4',timesig(timesigind))]; matthiasm@8: end matthiasm@8: chord_string = [chord_string sprintf(' %s%s%s',root{chordind}, lilylength(chordlabelduration(chordind)), label{chordind})]; matthiasm@8: end matthiasm@8: if ~isempty(bassind) matthiasm@8: bass_string = [bass_string sprintf(' %s%s',basslabel{bassind},lilylength(basslabelduration(bassind)))]; matthiasm@8: matthiasm@8: end matthiasm@8: matthiasm@8: matthiasm@8: end matthiasm@8: fid = fopen(outfilename); matthiasm@8: 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',... matthiasm@8: song.filenametrunk, chord_string, bass_string, partial); matthiasm@8: fclose(fid); matthiasm@8: if verbose matthiasm@8: 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',... matthiasm@8: song.filenametrunk, chord_string, bass_string, partial); matthiasm@8: end matthiasm@8: end matthiasm@8: matthiasm@8: function L = lilylength(beats) matthiasm@8: switch beats matthiasm@8: case 1 matthiasm@8: L = '4'; matthiasm@8: case 2 matthiasm@8: L = '2'; matthiasm@8: case 3 matthiasm@8: L = '2.'; matthiasm@8: case 4 matthiasm@8: L = '1'; matthiasm@8: end