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