matthiasm@8
|
1 function write_song(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 oldlabel ='';
|
matthiasm@8
|
95 oldkeylabel = '';
|
matthiasm@8
|
96 oldbasslabel = '';
|
matthiasm@8
|
97
|
matthiasm@8
|
98 outfile = fopen(outfilename,'w');
|
matthiasm@8
|
99 if verbose
|
matthiasm@8
|
100 fprintf(1, '0.0 ');
|
matthiasm@8
|
101 end
|
matthiasm@8
|
102 fprintf(outfile, '0.0 ');
|
matthiasm@8
|
103 chordcount = 0;
|
matthiasm@8
|
104 basscount = 0;
|
matthiasm@8
|
105 chordduration = 0;
|
matthiasm@8
|
106 bassduration = 0;
|
matthiasm@8
|
107 for iBeat= 1:length(chordsequence)
|
matthiasm@8
|
108 newchord = song.mpe{chordindex, iBeat};
|
matthiasm@8
|
109 newkey = song.mpe{keyindex, iBeat};
|
matthiasm@8
|
110 newbass = song.mpe{bassindex, iBeat};
|
matthiasm@8
|
111 newmetpos = song.mpe{metposindex, iBeat};
|
matthiasm@8
|
112 if param.dbn.nKey > 0
|
matthiasm@8
|
113 prekeylabel = param.dbn.keynames{song.mpe{keyindex,iBeat}};
|
matthiasm@8
|
114 else
|
matthiasm@8
|
115 prekeylabel = 'C';
|
matthiasm@8
|
116 end
|
matthiasm@8
|
117 if newchord ~= oldchord || newmetpos == 1
|
matthiasm@8
|
118 % get the chord name
|
matthiasm@8
|
119 chordcount = chordcount + 1;
|
matthiasm@8
|
120 if ischar(param.dbn.chordnames{newchord})
|
matthiasm@8
|
121 prelabel = ['C',param.dbn.chordnames{newchord}];
|
matthiasm@8
|
122 [c.rootnote, c.shorthand, c.degreelist, c.bassdegree, success, error] =...
|
matthiasm@8
|
123 getchordinfo(prelabel);
|
matthiasm@8
|
124 if isempty(c.bassdegree)
|
matthiasm@8
|
125 preroot = spellnote(newkey, param.dbn.chordrootnotes(newchord));
|
matthiasm@8
|
126 label{iBeat} = [preroot,lilychordnames{newchord}];
|
matthiasm@8
|
127 else
|
matthiasm@8
|
128 preroot = spellnote(newkey, param.dbn.chordrootnotes(newchord));
|
matthiasm@8
|
129 temp = degree2note(c.bassdegree,preroot);
|
matthiasm@8
|
130 bass = ['/',temp{1}];
|
matthiasm@8
|
131 label{iBeat} = [preroot ,lilychordnames{newchord},bass];
|
matthiasm@8
|
132 end
|
matthiasm@8
|
133 else
|
matthiasm@8
|
134 label{iBeat} = 'nc';
|
matthiasm@8
|
135 end
|
matthiasm@8
|
136 label{iBeat} = strrep(label{iBeat},'#','is');
|
matthiasm@8
|
137 label{iBeat} = strrep(label{iBeat},'b','es');
|
matthiasm@8
|
138 label{iBeat} = lower(label{iBeat});
|
matthiasm@8
|
139
|
matthiasm@8
|
140 % oldlabel = label{iBeat};
|
matthiasm@8
|
141 chordduration = 1;
|
matthiasm@8
|
142 else
|
matthiasm@8
|
143 chordduration = chordduration + 1;
|
matthiasm@8
|
144 end
|
matthiasm@8
|
145 if oldkey ~= newkey
|
matthiasm@8
|
146 keylabel{iBeat} = param.dbn.lilykeynames{song.mpe{keyindex,iBeat}};
|
matthiasm@8
|
147 keylabel{iBeat} = strrep(keylabel{iBeat},'#','is');
|
matthiasm@8
|
148 keylabel{iBeat} = strrep(keylabel{iBeat},'b','es');
|
matthiasm@8
|
149 keylabel{iBeat} = lower(keylabel{iBeat});
|
matthiasm@8
|
150 oldkeylabel = keylabel{iBeat};
|
matthiasm@8
|
151 end
|
matthiasm@8
|
152 if oldbass ~= newbass || newmetpos == 1
|
matthiasm@8
|
153 basscount = basscount + 1;
|
matthiasm@8
|
154 if basscount > 1
|
matthiasm@8
|
155 basslabelduration{basscount-1} = bassduration;
|
matthiasm@8
|
156 end
|
matthiasm@8
|
157 basslabel{basscount} = spellnote(newkey, param.dbn.chordbassnotes(newbass),'lily');
|
matthiasm@8
|
158 bassduration = 1;
|
matthiasm@8
|
159 else
|
matthiasm@8
|
160 bassduration = bassduration + 1;
|
matthiasm@8
|
161 end
|
matthiasm@8
|
162 oldbass = newbass;
|
matthiasm@8
|
163 oldkey = newkey;
|
matthiasm@8
|
164 oldchord = newchord;
|
matthiasm@8
|
165 end
|
matthiasm@8
|
166
|
matthiasm@8
|
167 % finish off the durations etc
|
matthiasm@8
|
168 basslabelduration{basscount} = bassduration;
|
matthiasm@8
|
169
|
matthiasm@8
|
170 fclose(outfile);
|
matthiasm@8
|
171 for iLabel = 1:numel(basslabel)
|
matthiasm@8
|
172 fprintf(1,'%10s %4.0f\n', basslabel{iLabel}, basslabelduration{iLabel})
|
matthiasm@8
|
173 end
|
matthiasm@8
|
174 end |