christopherh@1
|
1 %
|
christopherh@1
|
2 %NOTE2PITCHCLASS convert a note to a pitchclass value w.r.t middle C
|
christopherh@1
|
3 %
|
christopherh@1
|
4 % [pitchclass, success, errormessage] = note2pitchclass(note, {verbose})
|
christopherh@1
|
5 %
|
christopherh@1
|
6 % Converts a note string to a pitch class (integer) with C as reference
|
christopherh@1
|
7 % pitch class 0
|
christopherh@1
|
8 %
|
christopherh@1
|
9 % Success = 1 if symbols parsed correctly, 0 otherwise.
|
christopherh@1
|
10 %
|
christopherh@1
|
11 % If optional argument 'verbose' is 1, function prints any errormessage to
|
christopherh@1
|
12 % the screen.
|
christopherh@1
|
13 %
|
christopherh@1
|
14 % returns: pitchclass (integer)
|
christopherh@1
|
15 % success (boolean)
|
christopherh@1
|
16 % errormessage (string)
|
christopherh@1
|
17 %
|
christopherh@1
|
18 %
|
christopherh@1
|
19 % Author: Christopher Harte, March 2009
|
christopherh@1
|
20 %
|
christopherh@1
|
21 % Copyright: Centre for Digital Music, Queen Mary University of London 2005
|
christopherh@1
|
22 %
|
christopherh@1
|
23 % This file is part of the C4DM Chord Toolkit V2.0
|
christopherh@1
|
24 %
|
christopherh@1
|
25 % The C4DM Chord Toolkit is free software; you can redistribute it and/or
|
christopherh@1
|
26 % modify it under the terms of the GNU General Public License as published
|
christopherh@1
|
27 % by the Free Software Foundation; either version 2 of the License, or
|
christopherh@1
|
28 % (at your option) any later version.
|
christopherh@1
|
29 %
|
christopherh@1
|
30 % The C4DM Chord Toolkit is distributed in the hope that it will be useful,
|
christopherh@1
|
31 % but WITHOUT ANY WARRANTY; without even the implied warranty of
|
christopherh@1
|
32 % MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
christopherh@1
|
33 % GNU General Public License for more details.
|
christopherh@1
|
34 %
|
christopherh@1
|
35 % You should have received a copy of the GNU General Public License
|
christopherh@1
|
36 % along with the C4DM Toolkit; if not, write to the Free Software
|
christopherh@1
|
37 % Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
|
christopherh@1
|
38
|
christopherh@1
|
39 %
|
christopherh@1
|
40 function [pitchclass, success, errormessage] = note2pitchclass(note, verbose)
|
christopherh@1
|
41
|
christopherh@1
|
42 if nargin < 2
|
christopherh@1
|
43 verbose = 0;
|
christopherh@1
|
44 end
|
christopherh@1
|
45
|
christopherh@1
|
46 errormessage = '';
|
christopherh@1
|
47
|
christopherh@1
|
48 ilength = length(note);
|
christopherh@1
|
49
|
christopherh@1
|
50 index = 1;
|
christopherh@1
|
51 success = 1;
|
christopherh@1
|
52
|
christopherh@1
|
53
|
christopherh@1
|
54 % first char should be a natural name A-G
|
christopherh@1
|
55 switch note(index)
|
christopherh@1
|
56
|
christopherh@1
|
57 case 'C' % natural C
|
christopherh@1
|
58 pitchclass = 0;
|
christopherh@1
|
59 index = index + 1;
|
christopherh@1
|
60
|
christopherh@1
|
61 case 'D' % natural D
|
christopherh@1
|
62 pitchclass = 2;
|
christopherh@1
|
63 index = index + 1;
|
christopherh@1
|
64
|
christopherh@1
|
65 case 'E' % natural E
|
christopherh@1
|
66 pitchclass = 4;
|
christopherh@1
|
67 index = index + 1;
|
christopherh@1
|
68
|
christopherh@1
|
69 case 'F' % natural F
|
christopherh@1
|
70 pitchclass = 5;
|
christopherh@1
|
71 index = index + 1;
|
christopherh@1
|
72
|
christopherh@1
|
73 case 'G' % natural G
|
christopherh@1
|
74 pitchclass = 7;
|
christopherh@1
|
75 index = index + 1;
|
christopherh@1
|
76
|
christopherh@1
|
77 case 'A' % natural A
|
christopherh@1
|
78 pitchclass = 9;
|
christopherh@1
|
79 index = index + 1;
|
christopherh@1
|
80
|
christopherh@1
|
81 case 'B' % natural B
|
christopherh@1
|
82 pitchclass = 11;
|
christopherh@1
|
83 index = index + 1;
|
christopherh@1
|
84
|
christopherh@1
|
85 otherwise %Unrecognised character
|
christopherh@1
|
86 errormessage = sprintf(['Error in Note2PitchClass: Unrecognised note "' note '"\n']);
|
christopherh@1
|
87 index = ilength + 1;
|
christopherh@1
|
88 pitchclass=-1;
|
christopherh@1
|
89 success = 0;
|
christopherh@1
|
90
|
christopherh@1
|
91 end
|
christopherh@1
|
92
|
christopherh@1
|
93 % any other characters should be either flats or sharps
|
christopherh@1
|
94 while index <= ilength
|
christopherh@1
|
95
|
christopherh@1
|
96 switch(note(index))
|
christopherh@1
|
97
|
christopherh@1
|
98 case 'b' % FLAT
|
christopherh@1
|
99 pitchclass = pitchclass - 1; %decrement pitchclass value
|
christopherh@1
|
100 index = index + 1;
|
christopherh@1
|
101
|
christopherh@1
|
102 case '#' % SHARP
|
christopherh@1
|
103 pitchclass = pitchclass + 1; %increment pitchclass value
|
christopherh@1
|
104 index = index + 1;
|
christopherh@1
|
105
|
christopherh@1
|
106 otherwise % Unrecognised character
|
christopherh@1
|
107 errormessage = sprintf(['Error in Note2PitchClass: Unrecognised note "' note '"\n']);
|
christopherh@1
|
108 index = ilength + 1;
|
christopherh@1
|
109 pitchclass=-1;
|
christopherh@1
|
110 success = 0;
|
christopherh@1
|
111 end
|
christopherh@1
|
112
|
christopherh@1
|
113 end
|
christopherh@1
|
114
|
christopherh@1
|
115 % Use modulo command to make sure that we are back within range 0-12
|
christopherh@1
|
116 if(success == 1)
|
christopherh@1
|
117
|
christopherh@1
|
118 pitchclass = mod(pitchclass,12);
|
christopherh@1
|
119
|
christopherh@1
|
120 else
|
christopherh@1
|
121 if(verbose == 1)
|
christopherh@1
|
122 fprintf(1,errormessage);
|
christopherh@1
|
123 end
|
christopherh@1
|
124 end
|
christopherh@1
|
125
|
christopherh@1
|
126 |