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