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