comparison chordtools/interval2note.m @ 1:8973548174c1 tip

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