annotate _writetools/write_song2.m @ 9:4ea6619cb3f5 tip

removed log files
author matthiasm
date Fri, 11 Apr 2014 15:55:11 +0100
parents b5b38998ef3b
children
rev   line source
matthiasm@8 1 function write_song2(song, bnet, param, outfilename, filetype, verbose)
matthiasm@8 2
matthiasm@8 3 prelabels = {'C','Db','D','Eb','E','F','Gb','G','Ab','A','Bb','B','N'};
matthiasm@8 4 chordindex = bnet.names('chord');
matthiasm@8 5 metposindex = bnet.names('metpos');
matthiasm@8 6 if param.dbn.nKey > 0
matthiasm@8 7 keyindex = bnet.names('key');
matthiasm@8 8 end
matthiasm@8 9
matthiasm@8 10 switch filetype
matthiasm@8 11 case 'chordlab'
matthiasm@8 12 chordsequence = [song.mpe{chordindex,:}];
matthiasm@8 13 oldchord = 0;
matthiasm@8 14 oldlabel ='N';
matthiasm@8 15 outfile = fopen(outfilename,'w');
matthiasm@8 16 if verbose
matthiasm@8 17 fprintf(1, '0.0 ');
matthiasm@8 18 end
matthiasm@8 19 fprintf(outfile, '0.0 ');
matthiasm@8 20 for iChord= 1:length(chordsequence)
matthiasm@8 21 newchord = song.mpe{chordindex, iChord};
matthiasm@8 22 if newchord ~= oldchord
matthiasm@8 23 if param.dbn.nKey > 0
matthiasm@8 24 keylabel = param.dbn.keynames{song.mpe{keyindex,iChord}};
matthiasm@8 25 else
matthiasm@8 26 keylabel = 'C';
matthiasm@8 27 end
matthiasm@8 28 if ischar(param.dbn.chordnames{newchord})
matthiasm@8 29 prelabel = ['C',param.dbn.chordnames{newchord}];
matthiasm@8 30 [c.rootnote, c.shorthand, c.degreelist, c.bassdegree, success, error] =...
matthiasm@8 31 getchordinfo(prelabel);
matthiasm@8 32 c.string = addshort2list(c.shorthand, c.degreelist);
matthiasm@8 33 label = [chordroot_spelling(keylabel, param.dbn.chordrootnotes(newchord)-1 ,strsplit(',',c.string)),param.dbn.chordnames{newchord}];
matthiasm@8 34 else
matthiasm@8 35 label = 'N';
matthiasm@8 36 end
matthiasm@8 37 if verbose
matthiasm@8 38 fprintf(1,'%0.3f %s\n%0.3f ', song.beattimes(iChord,1), oldlabel,song.beattimes(iChord,1));
matthiasm@8 39 end
matthiasm@8 40 fprintf(outfile,'%0.3f %s\n%0.3f ', song.beattimes(iChord,1), oldlabel,song.beattimes(iChord,1));
matthiasm@8 41 oldlabel = label;
matthiasm@8 42 end
matthiasm@8 43 oldchord = newchord;
matthiasm@8 44 end
matthiasm@8 45 if verbose
matthiasm@8 46 fprintf(1,'%0.3f %s', song.beattimes(iChord,2), oldlabel);
matthiasm@8 47 end
matthiasm@8 48 fprintf(outfile,'%0.3f %s', song.beattimes(iChord,2), oldlabel);
matthiasm@8 49 fclose(outfile);
matthiasm@8 50 case 'keylab'
matthiasm@8 51 keysequence = [song.mpe{keyindex,:}];
matthiasm@8 52 oldkey = 0;
matthiasm@8 53 oldlabel ='N';
matthiasm@8 54 outfile = fopen(outfilename,'w');
matthiasm@8 55 if verbose
matthiasm@8 56 fprintf(1, '0.0 ');
matthiasm@8 57 end
matthiasm@8 58 fprintf(outfile, '0.0 ');
matthiasm@8 59 for iKey = 1:length(keysequence)
matthiasm@8 60 newkey = song.mpe{keyindex, iKey};
matthiasm@8 61 if newkey ~= oldkey
matthiasm@8 62 keylabel = param.dbn.keynames{song.mpe{keyindex,iKey}};
matthiasm@8 63 if verbose
matthiasm@8 64 fprintf(1,'%0.3f %s\n%0.3f ', song.beattimes(iKey,1), oldlabel,song.beattimes(iKey,1));
matthiasm@8 65 end
matthiasm@8 66 fprintf(outfile,'%0.3f %s\n%0.3f ', song.beattimes(iKey,1), oldlabel,song.beattimes(iKey,1));
matthiasm@8 67 oldlabel = keylabel;
matthiasm@8 68 end
matthiasm@8 69 oldkey = newkey;
matthiasm@8 70 end
matthiasm@8 71 if verbose
matthiasm@8 72 fprintf(1,'%0.3f %s', song.beattimes(iKey,2), oldlabel);
matthiasm@8 73 end
matthiasm@8 74 fprintf(outfile,'%0.3f %s', song.beattimes(iKey,2), oldlabel);
matthiasm@8 75 fclose(outfile);
matthiasm@8 76 case 'lilypond'
matthiasm@8 77 bassindex = bnet.names('bass');
matthiasm@8 78 lilychordnames = repmat(param.dbn.lilytypechordnames,[1,12]);
matthiasm@8 79 lilychordnames{numel(lilychordnames)+1} = '';
matthiasm@8 80
matthiasm@8 81 chordsequence = [song.mpe{chordindex,:}];
matthiasm@8 82
matthiasm@8 83 nBeat = length(chordsequence);
matthiasm@8 84
matthiasm@8 85 keysequence = [song.mpe{keyindex,:}];
matthiasm@8 86
matthiasm@8 87 % label = cell(nBeat,1);
matthiasm@8 88 % keylabel = cell(nBeat,1);
matthiasm@8 89
matthiasm@8 90 metpossequence = [song.mpe{metposindex,:}];
matthiasm@8 91 oldchord = 0;
matthiasm@8 92 oldkey = 0;
matthiasm@8 93 oldbass = 0;
matthiasm@8 94
matthiasm@8 95
matthiasm@8 96
matthiasm@8 97 chordcount = 0;
matthiasm@8 98 basscount = 0;
matthiasm@8 99 chordduration = 1;
matthiasm@8 100 bassduration = 1;
matthiasm@8 101
matthiasm@8 102 % time signature stuff
matthiasm@8 103 partial = find(metpossequence == 1,1)-1;
matthiasm@8 104 if partial > 0
matthiasm@8 105 partial = ['\partial ' lilylength(partial)];
matthiasm@8 106 else
matthiasm@8 107 partial = '';
matthiasm@8 108 end
matthiasm@8 109 barlines = find(metpossequence == 1);
matthiasm@8 110 timesig = diff(barlines); % for barlines, not beats
matthiasm@8 111 ind = min(length(timesig),union(find(timesig~=4),find(timesig~=4)+1));
matthiasm@8 112 non4timesig = barlines(ind); % in beats
matthiasm@8 113 timesig = timesig(ind);
matthiasm@8 114
matthiasm@8 115 % chord and bass loop
matthiasm@8 116
matthiasm@8 117 for iBeat= 1:length(chordsequence)
matthiasm@8 118 newchord = song.mpe{chordindex, iBeat};
matthiasm@8 119 newkey = song.mpe{keyindex, iBeat};
matthiasm@8 120 newbass = song.mpe{bassindex, iBeat};
matthiasm@8 121 newmetpos = song.mpe{metposindex, iBeat};
matthiasm@8 122 if param.dbn.nKey > 0
matthiasm@8 123 prekeylabel = param.dbn.keynames{song.mpe{keyindex,iBeat}};
matthiasm@8 124 else
matthiasm@8 125 prekeylabel = 'C';
matthiasm@8 126 end
matthiasm@8 127 if newchord ~= oldchord || newmetpos == 1 || chordduration == 4
matthiasm@8 128 % get the chord name
matthiasm@8 129 chordcount = chordcount + 1;
matthiasm@8 130 if iBeat > 1
matthiasm@8 131 chordlabelduration(chordcount-1) = chordduration;
matthiasm@8 132 end
matthiasm@8 133 if ischar(param.dbn.chordnames{newchord})
matthiasm@8 134 preroot = spellnote(newkey, param.dbn.chordrootnotes(newchord),'lily');
matthiasm@8 135 root{chordcount} = preroot;
matthiasm@8 136 if isempty(strfind(param.dbn.chordnames{newchord},'/'))
matthiasm@8 137 label{chordcount} = lilychordnames{newchord};
matthiasm@8 138 else
matthiasm@8 139 bass = spellnote(newkey, param.dbn.chordbassnotes(newchord),'lily');
matthiasm@8 140 label{chordcount} = [strrep(lilychordnames{newchord},' ',''),'/',bass];
matthiasm@8 141 end
matthiasm@8 142 else
matthiasm@8 143 root{chordcount} = 'r';
matthiasm@8 144 label{chordcount} = '';
matthiasm@8 145 end
matthiasm@8 146 chordbeat(chordcount) = iBeat;
matthiasm@8 147
matthiasm@8 148 % oldlabel = label{iBeat};
matthiasm@8 149 chordduration = 1;
matthiasm@8 150 else
matthiasm@8 151 chordduration = chordduration + 1;
matthiasm@8 152 end
matthiasm@8 153 if oldkey ~= newkey
matthiasm@8 154 keylabel{iBeat} = param.dbn.lilykeynames{song.mpe{keyindex,iBeat}};
matthiasm@8 155 % keylabel{iBeat} = strrep(keylabel{iBeat},'#','is');
matthiasm@8 156 % keylabel{iBeat} = strrep(keylabel{iBeat},'b','es');
matthiasm@8 157 % keylabel{iBeat} = lower(keylabel{iBeat});
matthiasm@8 158 % oldkeylabel = keylabel{iBeat};
matthiasm@8 159 end
matthiasm@8 160 if oldbass ~= newbass || newmetpos == 1 || bassduration == 4
matthiasm@8 161 basscount = basscount + 1;
matthiasm@8 162 if iBeat > 1
matthiasm@8 163 basslabelduration(basscount-1) = bassduration;
matthiasm@8 164 end
matthiasm@8 165 basslabel{basscount} = spellnote(newkey, param.dbn.chordbassnotes(newbass),'lily');
matthiasm@8 166 bassbeat(basscount) = iBeat;
matthiasm@8 167 bassduration = 1;
matthiasm@8 168 else
matthiasm@8 169 bassduration = bassduration + 1;
matthiasm@8 170 end
matthiasm@8 171
matthiasm@8 172 oldbass = newbass;
matthiasm@8 173 oldkey = newkey;
matthiasm@8 174 oldchord = newchord;
matthiasm@8 175 end
matthiasm@8 176
matthiasm@8 177
matthiasm@8 178 % finish off the durations etc
matthiasm@8 179 basslabelduration(basscount) = bassduration;
matthiasm@8 180 chordlabelduration(chordcount) = chordduration;
matthiasm@8 181
matthiasm@8 182
matthiasm@8 183 chord_string = 'chordsymb = { \chordmode { ';
matthiasm@8 184 bass_string = 'bassnotes = { \override TextScript #''box-padding = #.7 \override TextScript #''font-size = #-3 \override TextScript #''thickness = #.03 \clef bass';
matthiasm@8 185 for iBeat = 1:nBeat
matthiasm@8 186 chordind = find(chordbeat == iBeat);
matthiasm@8 187 bassind = find(bassbeat == iBeat);
matthiasm@8 188 timesigind = find(non4timesig == iBeat);
matthiasm@8 189 if ~(iBeat > numel(keylabel) || isempty(keylabel{iBeat}))
matthiasm@8 190 chord_string = [chord_string sprintf(' } \\key %s \\chordmode { ', keylabel{iBeat})];
matthiasm@8 191 bass_string = [bass_string sprintf(' \\key %s', keylabel{iBeat})];
matthiasm@8 192 end
matthiasm@8 193 if ~isempty(chordind)
matthiasm@8 194 if ~isempty(timesigind)
matthiasm@8 195 chord_string = [chord_string sprintf(' \\time %0.0f/4',timesig(timesigind))];
matthiasm@8 196 end
matthiasm@8 197 chord_string = [chord_string sprintf(' %s%s%s',root{chordind}, lilylength(chordlabelduration(chordind)), label{chordind})];
matthiasm@8 198 end
matthiasm@8 199 if ~isempty(bassind)
matthiasm@8 200 bass_string = [bass_string sprintf(' %s%s',basslabel{bassind},lilylength(basslabelduration(bassind)))];
matthiasm@8 201
matthiasm@8 202 end
matthiasm@8 203
matthiasm@8 204
matthiasm@8 205 end
matthiasm@8 206 fid = fopen(outfilename);
matthiasm@8 207 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 208 song.filenametrunk, chord_string, bass_string, partial);
matthiasm@8 209 fclose(fid);
matthiasm@8 210 if verbose
matthiasm@8 211 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 212 song.filenametrunk, chord_string, bass_string, partial);
matthiasm@8 213 end
matthiasm@8 214 end
matthiasm@8 215
matthiasm@8 216 function L = lilylength(beats)
matthiasm@8 217 switch beats
matthiasm@8 218 case 1
matthiasm@8 219 L = '4';
matthiasm@8 220 case 2
matthiasm@8 221 L = '2';
matthiasm@8 222 case 3
matthiasm@8 223 L = '2.';
matthiasm@8 224 case 4
matthiasm@8 225 L = '1';
matthiasm@8 226 end