comparison chordtools/note2interval.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 %NOTE2INTERVAL convert a note to an interval w.r.t. a given root
3 %
4 % [interval,success,errormessage] = note2interval(note,root,{octave},{verbose})
5 %
6 % Converts a note string to an interval with respect to a given root note.
7 % Optional value octave gives octave offset to account for extended
8 % degrees.
9 %
10 % Success = 1 if note converted correctly, 0 otherwise.
11 %
12 % Optional argument octave defaults to 0.
13 %
14 % If optional argument 'verbose' is 1, function prints any errormessage to
15 % the screen.
16 %
17 % calls: note2fifthposition
18 %
19 % returns: interval (string)
20 % success (boolean)
21 % errormessage (string)
22 %
23 %
24 % Author: Christopher Harte, March 2009
25 %
26 % Copyright: Centre for Digital Music, Queen Mary University of London 2005
27 %
28 % This file is part of the C4DM Chord Toolkit V2.0
29 %
30 % The C4DM Chord Toolkit is free software; you can redistribute it and/or
31 % modify it under the terms of the GNU General Public License as published
32 % by the Free Software Foundation; either version 2 of the License, or
33 % (at your option) any later version.
34 %
35 % The C4DM Chord Toolkit is distributed in the hope that it will be useful,
36 % but WITHOUT ANY WARRANTY; without even the implied warranty of
37 % MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
38 % GNU General Public License for more details.
39 %
40 % You should have received a copy of the GNU General Public License
41 % along with the C4DM Toolkit; if not, write to the Free Software
42 % Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
43
44 %
45 function [interval,success,errormessage] = note2interval(note, root,octave, verbose)
46
47 % set verbose default to 0
48 if nargin < 3
49 octave = 0;
50 verbose = 0;
51
52 elseif nargin < 4
53 verbose = 0;
54 end
55
56 errormessage = '';
57 success = 1;
58 interval = '';
59
60 % degree translations on the line of fifths
61 fifthtranslations = [1,5,2,6,3,7,4];
62
63
64 % get note and root natural position and accidentals on line of fifths
65 [noteposition, success1,error1] = note2fifthposition(note);
66
67 [rootposition, success2,error2] = note2fifthposition(root);
68
69 if success1 && success2
70
71 % take the difference between the two note positions for relative positions
72 % of notes with respect to one and other
73 fifthsdifference = noteposition - rootposition + 1;
74
75 % natural difference on line of fifths
76 fifthsdegree = mod((fifthsdifference-1),7);
77
78 i=0;
79
80 % find number of accidentals apart on line of fifths
81 if fifthsdifference < 0 % if above 0 then either natural or sharp
82
83 %if final position is negative then calculate number of flats
84 % remembering to include the extra first flat (-1)
85 accidentals = fix((fifthsdifference+1)/7) -1;
86
87 else
88 % note is a natural or has a number of sharps
89 accidentals = fix(fifthsdifference/7);
90
91 end
92
93
94 % put the required number of sharps or flats into the output string
95 if accidentals > 0
96
97 for i=1:accidentals
98
99 interval = ['#' interval];
100
101 end
102
103 elseif accidentals <=0
104
105 for i=1:abs(accidentals)
106
107 interval = ['b' interval];
108
109 end
110 end
111
112
113 % find degree value from translation array
114 degree = fifthtranslations(fifthsdegree+1);
115
116 if octave >= 0
117 degree = degree + 7.*octave;
118 else
119 success = 0;
120 errormessage = 'Error in note2interval: Octave argument is negative';
121 end
122
123
124
125 interval = [interval num2str(degree)];
126
127 else
128 success = 0;
129 errormessage = [error1 error2];
130 end
131
132 if (success == 0) && (verbose == 1)
133 fprintf(1,errormessage);
134 end
135
136