matthiasm@8
|
1 %
|
matthiasm@8
|
2 %DEGREE2NOTE convert a degree to correctly spelled note w.r.t. a root
|
matthiasm@8
|
3 %
|
matthiasm@8
|
4 % [note,success,errormessage] = degree2note(degree, root {verbose})
|
matthiasm@8
|
5 %
|
matthiasm@8
|
6 % Converts a degree to a note (string) with root as reference note
|
matthiasm@8
|
7 % for degree interval.
|
matthiasm@8
|
8 %
|
matthiasm@8
|
9 % Success = 1 if degree is converted correctly, 0 otherwise.
|
matthiasm@8
|
10 %
|
matthiasm@8
|
11 % If optional argument 'verbose' is 1, function prints any errormessage to
|
matthiasm@8
|
12 % the screen.
|
matthiasm@8
|
13 %
|
matthiasm@8
|
14 % returns: note (string)
|
matthiasm@8
|
15 % success (boolean)
|
matthiasm@8
|
16 % errormessage (string)
|
matthiasm@8
|
17 %
|
matthiasm@8
|
18 % See also degrees2notes, parsenote.
|
matthiasm@8
|
19 %
|
matthiasm@8
|
20 %
|
matthiasm@8
|
21 % Author: Christopher Harte, August 2005
|
matthiasm@8
|
22 %
|
matthiasm@8
|
23 % Copyright: Centre for Digital Music, Queen Mary University of London 2005
|
matthiasm@8
|
24 %
|
matthiasm@8
|
25 % This file is part of the C4DM Chord Toolkit.
|
matthiasm@8
|
26 %
|
matthiasm@8
|
27 % The C4DM Chord Toolkit is free software; you can redistribute it and/or
|
matthiasm@8
|
28 % modify it under the terms of the GNU General Public License as published
|
matthiasm@8
|
29 % by the Free Software Foundation; either version 2 of the License, or
|
matthiasm@8
|
30 % (at your option) any later version.
|
matthiasm@8
|
31 %
|
matthiasm@8
|
32 % The C4DM Chord Toolkit is distributed in the hope that it will be useful,
|
matthiasm@8
|
33 % but WITHOUT ANY WARRANTY; without even the implied warranty of
|
matthiasm@8
|
34 % MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
matthiasm@8
|
35 % GNU General Public License for more details.
|
matthiasm@8
|
36 %
|
matthiasm@8
|
37 % You should have received a copy of the GNU General Public License
|
matthiasm@8
|
38 % along with the C4DM Toolkit; if not, write to the Free Software
|
matthiasm@8
|
39 % Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
|
matthiasm@8
|
40
|
matthiasm@8
|
41 %
|
matthiasm@8
|
42 function [note,success,errormessage] = degree2note(degree, root,verbose)
|
matthiasm@8
|
43
|
matthiasm@8
|
44 if nargin < 3
|
matthiasm@8
|
45 verbose = 0;
|
matthiasm@8
|
46 end
|
matthiasm@8
|
47 errormessage = '';
|
matthiasm@8
|
48
|
matthiasm@8
|
49 error1 = '';
|
matthiasm@8
|
50 error2 = '';
|
matthiasm@8
|
51
|
matthiasm@8
|
52 note = [];
|
matthiasm@8
|
53
|
matthiasm@8
|
54 intervaltranslation = [5,0,2,4,-1,1,3,5]; % scale degree translations on line of fifths
|
matthiasm@8
|
55
|
matthiasm@8
|
56 fifthpositions = {'F','C','G','D','A','E','B'}; %order of naturals on line of fifths
|
matthiasm@8
|
57
|
matthiasm@8
|
58 success = 1;
|
matthiasm@8
|
59
|
matthiasm@8
|
60 % parse the root note
|
matthiasm@8
|
61 [rootnatural,rootaccs, rsuccess, error1] = parsenote(root);
|
matthiasm@8
|
62
|
matthiasm@8
|
63 %parse the degree
|
matthiasm@8
|
64 [interval, degreeaccs, present, dsuccess, error2] = parsedegree(degree);
|
matthiasm@8
|
65
|
matthiasm@8
|
66 % if parsing symbols was successful
|
matthiasm@8
|
67 if(rsuccess && dsuccess);
|
matthiasm@8
|
68
|
matthiasm@8
|
69 switch(rootnatural) % find root natural position on line of fifths
|
matthiasm@8
|
70
|
matthiasm@8
|
71 case 'F'
|
matthiasm@8
|
72 fifthindex = 0;
|
matthiasm@8
|
73 case 'C'
|
matthiasm@8
|
74 fifthindex = 1;
|
matthiasm@8
|
75 case 'G'
|
matthiasm@8
|
76 fifthindex = 2;
|
matthiasm@8
|
77 case 'D'
|
matthiasm@8
|
78 fifthindex = 3;
|
matthiasm@8
|
79 case 'A'
|
matthiasm@8
|
80 fifthindex = 4;
|
matthiasm@8
|
81 case 'E'
|
matthiasm@8
|
82 fifthindex = 5;
|
matthiasm@8
|
83 case 'B'
|
matthiasm@8
|
84 fifthindex = 6;
|
matthiasm@8
|
85
|
matthiasm@8
|
86 end
|
matthiasm@8
|
87
|
matthiasm@8
|
88 %locate enharmonic root on line of fifths (modulo 6 arithmetic)
|
matthiasm@8
|
89
|
matthiasm@8
|
90 fifthoffset = rootaccs*7;
|
matthiasm@8
|
91
|
matthiasm@8
|
92 fifthindex = fifthindex + fifthoffset;
|
matthiasm@8
|
93
|
matthiasm@8
|
94
|
matthiasm@8
|
95 % calculate interval translation on line of fifths (add 1 to account
|
matthiasm@8
|
96 % for matlab referencing of array elements...
|
matthiasm@8
|
97 intervaloffset = intervaltranslation(mod(interval,7)+1);
|
matthiasm@8
|
98 finalposition = fifthindex + intervaloffset;
|
matthiasm@8
|
99
|
matthiasm@8
|
100
|
matthiasm@8
|
101 naturalvalue = mod(finalposition,7);
|
matthiasm@8
|
102
|
matthiasm@8
|
103
|
matthiasm@8
|
104 % calculate number of accidentals
|
matthiasm@8
|
105 if finalposition <0
|
matthiasm@8
|
106 %if final position is negative then calculate number of flats
|
matthiasm@8
|
107 % remembering to include the extra first flat (-1)
|
matthiasm@8
|
108 accidentals = fix((finalposition+1)/7) + degreeaccs -1;
|
matthiasm@8
|
109
|
matthiasm@8
|
110 else
|
matthiasm@8
|
111 % note is a natural or has a number of sharps
|
matthiasm@8
|
112 accidentals = fix(finalposition/7) + degreeaccs;
|
matthiasm@8
|
113 end
|
matthiasm@8
|
114
|
matthiasm@8
|
115 note = fifthpositions(naturalvalue+1);
|
matthiasm@8
|
116
|
matthiasm@8
|
117 if accidentals > 0
|
matthiasm@8
|
118
|
matthiasm@8
|
119 for i=1:accidentals
|
matthiasm@8
|
120
|
matthiasm@8
|
121 note = strcat(note, '#');
|
matthiasm@8
|
122
|
matthiasm@8
|
123 end
|
matthiasm@8
|
124
|
matthiasm@8
|
125 elseif accidentals <=0
|
matthiasm@8
|
126
|
matthiasm@8
|
127 for i=1:abs(accidentals)
|
matthiasm@8
|
128
|
matthiasm@8
|
129 note = strcat(note, 'b');
|
matthiasm@8
|
130
|
matthiasm@8
|
131 end
|
matthiasm@8
|
132 end
|
matthiasm@8
|
133
|
matthiasm@8
|
134 else
|
matthiasm@8
|
135
|
matthiasm@8
|
136 success=0;
|
matthiasm@8
|
137
|
matthiasm@8
|
138 end
|
matthiasm@8
|
139
|
matthiasm@8
|
140 if(success==0) % correct degree therefore return success = 1
|
matthiasm@8
|
141 % if not an integer then the degree string is incorrect
|
matthiasm@8
|
142 errormessage = [error1 error2 sprintf(['Error in degree2note: Unrecognised degree "' degree '" or root "' root '"\n'])];
|
matthiasm@8
|
143
|
matthiasm@8
|
144 if verbose == 1
|
matthiasm@8
|
145 fprintf(1,errormessage);
|
matthiasm@8
|
146 end
|
matthiasm@8
|
147
|
matthiasm@8
|
148 end
|
matthiasm@8
|
149
|
matthiasm@8
|
150
|
matthiasm@8
|
151
|
matthiasm@8
|
152
|
matthiasm@8
|
153
|
matthiasm@8
|
154
|
matthiasm@8
|
155
|
matthiasm@8
|
156
|
matthiasm@8
|
157
|