comparison chordtools/parsenote.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 % PARSENOTE
3 %
4 % [natural, accidentals, success, errormessage] = parsenote(note, {verbose})
5 %
6 % Parse a note to a natural character value and a number of accidentals
7 % if accidentals is positive it denotes number of sharps,
8 % if it is negative it denotes number of flats.
9 %
10 % Success = 1 if symbols parsed correctly, 0 otherwise.
11 %
12 % If optional argument 'verbose' is 1, function prints any errormessage to
13 % the screen.
14 %
15 % returns: natural (char)
16 % accidentals (integer)
17 % success (boolean)
18 % errormessage (string)
19 %
20 % Author: Christopher Harte, March 2009
21 %
22 % Copyright: Centre for Digital Music, Queen Mary University of London 2005
23 %
24 % This file is part of the C4DM Chord Toolkit V2.0
25 %
26 % The C4DM Chord Toolkit is free software; you can redistribute it and/or
27 % modify it under the terms of the GNU General Public License as published
28 % by the Free Software Foundation; either version 2 of the License, or
29 % (at your option) any later version.
30 %
31 % The C4DM Chord Toolkit is distributed in the hope that it will be useful,
32 % but WITHOUT ANY WARRANTY; without even the implied warranty of
33 % MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
34 % GNU General Public License for more details.
35 %
36 % You should have received a copy of the GNU General Public License
37 % along with the C4DM Toolkit; if not, write to the Free Software
38 % Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
39
40 %
41 function [natural,accidentals,success, errormessage] = parsenote(note, verbose)
42
43 if nargin < 2
44 verbose = 0;
45 end
46
47 errormessage = '';
48
49 ilength = length(note);
50
51 accidentals = 0;
52 natural = [];
53 success=1;
54
55 index = 1;
56
57 if (isempty(note)) % if we have been passed an empty string
58 success=0;
59
60 else % parse the note string
61
62 switch(note(index))
63
64 case {'A','B','C','D','E','F','G'}
65
66 %first character should be a natural
67 natural = note(index);
68 index= index+1;
69
70 %remaining characters should be sharps or flats
71 while index <= ilength
72
73 switch note(index)
74
75 case 'b' % FLAT
76 accidentals = accidentals - 1; %decrement accidental count
77 index = index + 1;
78
79 case '#' % SHARP
80 accidentals = accidentals + 1; %increment accidental count
81 index = index + 1;
82
83 otherwise
84 % if neither of the above then the note is incorrect
85 success=0;
86 index = ilength+1;
87 end
88 end
89
90 otherwise
91 success=0;
92 end
93 end
94
95 if(success==0) % correct note therefore return success = 1
96 % if not an integer then the note string is incorrect
97 errormessage = sprintf(['Error in parsenote: Unrecognised note "' note '"\n']);
98
99 if verbose ==1
100 fprintf(1,errormessage);
101 end
102 end