To check out this repository please hg clone the following URL, or open the URL using EasyMercurial or your preferred Mercurial client.

Statistics Download as Zip
| Branch: | Revision:

root / _writetools / write_song.m

History | View | Annotate | Download (6.63 KB)

1
function write_song(song, bnet, param, outfilename, filetype, verbose)
2

    
3
prelabels = {'C','Db','D','Eb','E','F','Gb','G','Ab','A','Bb','B','N'};
4
chordindex = bnet.names('chord');
5
metposindex = bnet.names('metpos');
6
if param.dbn.nKey > 0
7
    keyindex = bnet.names('key');
8
end
9

    
10
switch filetype
11
    case 'chordlab'
12
        chordsequence = [song.mpe{chordindex,:}];
13
        oldchord = 0;
14
        oldlabel ='N';
15
        outfile = fopen(outfilename,'w');
16
        if verbose
17
            fprintf(1, '0.0 ');
18
        end
19
        fprintf(outfile, '0.0 ');
20
        for iChord= 1:length(chordsequence)
21
            newchord = song.mpe{chordindex, iChord};
22
            if newchord ~= oldchord
23
                if param.dbn.nKey > 0
24
                    keylabel = param.dbn.keynames{song.mpe{keyindex,iChord}};
25
                else
26
                    keylabel = 'C';
27
                end
28
                if ischar(param.dbn.chordnames{newchord})
29
                    prelabel = ['C',param.dbn.chordnames{newchord}];
30
                    [c.rootnote, c.shorthand, c.degreelist, c.bassdegree, success, error] =...
31
                        getchordinfo(prelabel);
32
                    c.string = addshort2list(c.shorthand, c.degreelist);
33
                    label = [chordroot_spelling(keylabel, param.dbn.chordrootnotes(newchord)-1 ,strsplit(',',c.string)),param.dbn.chordnames{newchord}];
34
                else
35
                    label = 'N';
36
                end
37
                if verbose
38
                    fprintf(1,'%0.3f %s\n%0.3f ', song.beattimes(iChord,1), oldlabel,song.beattimes(iChord,1));
39
                end
40
                fprintf(outfile,'%0.3f %s\n%0.3f ', song.beattimes(iChord,1), oldlabel,song.beattimes(iChord,1));
41
                oldlabel = label;
42
            end
43
            oldchord = newchord;
44
        end
45
        if verbose
46
            fprintf(1,'%0.3f %s', song.beattimes(iChord,2), oldlabel);
47
        end
48
        fprintf(outfile,'%0.3f %s', song.beattimes(iChord,2), oldlabel);
49
        fclose(outfile);
50
    case 'keylab'
51
        keysequence = [song.mpe{keyindex,:}];
52
        oldkey = 0;
53
        oldlabel ='N';
54
        outfile = fopen(outfilename,'w');
55
        if verbose
56
            fprintf(1, '0.0 ');
57
        end
58
        fprintf(outfile, '0.0 ');
59
        for iKey = 1:length(keysequence)
60
            newkey = song.mpe{keyindex, iKey};
61
            if newkey ~= oldkey
62
                keylabel = param.dbn.keynames{song.mpe{keyindex,iKey}};
63
                if verbose
64
                    fprintf(1,'%0.3f %s\n%0.3f ', song.beattimes(iKey,1), oldlabel,song.beattimes(iKey,1));
65
                end
66
                fprintf(outfile,'%0.3f %s\n%0.3f ', song.beattimes(iKey,1), oldlabel,song.beattimes(iKey,1));
67
                oldlabel = keylabel;
68
            end
69
            oldkey = newkey;
70
        end
71
        if verbose
72
            fprintf(1,'%0.3f %s', song.beattimes(iKey,2), oldlabel);
73
        end
74
        fprintf(outfile,'%0.3f %s', song.beattimes(iKey,2), oldlabel);
75
        fclose(outfile);
76
    case 'lilypond'
77
        bassindex = bnet.names('bass');
78
        lilychordnames = repmat(param.dbn.lilytypechordnames,[1,12]);
79
        lilychordnames{numel(lilychordnames)+1} = '';
80

    
81
        chordsequence = [song.mpe{chordindex,:}];
82

    
83
        nBeat = length(chordsequence);
84

    
85
        keysequence = [song.mpe{keyindex,:}];
86

    
87
        label = cell(nBeat,1);
88
        keylabel = cell(nBeat,1);
89

    
90
        metpossequence = [song.mpe{metposindex,:}];
91
        oldchord = 0;
92
        oldkey = 0;
93
        oldbass = 0;
94
        oldlabel ='';
95
        oldkeylabel = '';
96
        oldbasslabel = '';
97

    
98
        outfile = fopen(outfilename,'w');
99
        if verbose
100
            fprintf(1, '0.0 ');
101
        end
102
        fprintf(outfile, '0.0 ');
103
        chordcount = 0;
104
        basscount = 0;
105
        chordduration = 0;
106
        bassduration = 0;
107
        for iBeat= 1:length(chordsequence)
108
            newchord = song.mpe{chordindex, iBeat};
109
            newkey = song.mpe{keyindex, iBeat};
110
            newbass = song.mpe{bassindex, iBeat};
111
            newmetpos = song.mpe{metposindex, iBeat};
112
            if param.dbn.nKey > 0
113
                prekeylabel = param.dbn.keynames{song.mpe{keyindex,iBeat}};
114
            else
115
                prekeylabel = 'C';
116
            end
117
            if newchord ~= oldchord  || newmetpos == 1
118
                % get the chord name
119
                chordcount = chordcount + 1;
120
                if ischar(param.dbn.chordnames{newchord})
121
                    prelabel = ['C',param.dbn.chordnames{newchord}];
122
                    [c.rootnote, c.shorthand, c.degreelist, c.bassdegree, success, error] =...
123
                        getchordinfo(prelabel);
124
                    if isempty(c.bassdegree)
125
                        preroot = spellnote(newkey, param.dbn.chordrootnotes(newchord));
126
                        label{iBeat} = [preroot,lilychordnames{newchord}];
127
                    else
128
                        preroot = spellnote(newkey, param.dbn.chordrootnotes(newchord));
129
                        temp = degree2note(c.bassdegree,preroot);
130
                        bass = ['/',temp{1}];
131
                        label{iBeat} = [preroot ,lilychordnames{newchord},bass];
132
                    end
133
                else
134
                    label{iBeat} = 'nc';
135
                end
136
                label{iBeat} = strrep(label{iBeat},'#','is');
137
                label{iBeat} = strrep(label{iBeat},'b','es');
138
                label{iBeat} = lower(label{iBeat});
139

    
140
%                 oldlabel = label{iBeat};
141
                chordduration = 1;
142
            else
143
                chordduration = chordduration + 1;
144
            end
145
            if oldkey ~= newkey
146
                keylabel{iBeat} = param.dbn.lilykeynames{song.mpe{keyindex,iBeat}};
147
                keylabel{iBeat} = strrep(keylabel{iBeat},'#','is');
148
                keylabel{iBeat} = strrep(keylabel{iBeat},'b','es');
149
                keylabel{iBeat} = lower(keylabel{iBeat});
150
                oldkeylabel = keylabel{iBeat};
151
            end
152
            if oldbass ~= newbass || newmetpos == 1
153
                basscount = basscount + 1;
154
                if basscount > 1
155
                    basslabelduration{basscount-1} = bassduration;
156
                end
157
                basslabel{basscount} = spellnote(newkey, param.dbn.chordbassnotes(newbass),'lily');
158
                bassduration = 1;
159
            else
160
                bassduration = bassduration + 1;
161
            end
162
            oldbass = newbass;
163
            oldkey = newkey;
164
            oldchord = newchord;
165
        end
166

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