annotate _chordtools/parsechord.m @ 9:4ea6619cb3f5 tip

removed log files
author matthiasm
date Fri, 11 Apr 2014 15:55:11 +0100
parents b5b38998ef3b
children
rev   line source
matthiasm@8 1 %
matthiasm@8 2 %PARSECHORD parse chord symbol to its constituent string elements
matthiasm@8 3 %
matthiasm@8 4 % [rootnote, shorthand, degreelist,
matthiasm@8 5 % bass, success, errormessage] = parsechord(chord, {verbose})
matthiasm@8 6 %
matthiasm@8 7 % Parses the chord symbol string "chord" into its separate string elements.
matthiasm@8 8 %
matthiasm@8 9 % NOTE: This function does not check validity of the chord elements, it
matthiasm@8 10 % merely separates them according to the chord syntax.
matthiasm@8 11 % To obtain chord elements with included validity check use GETCHORDINFO.
matthiasm@8 12 %
matthiasm@8 13 % Success = 1 if symbols parsed correctly, 0 otherwise.
matthiasm@8 14 %
matthiasm@8 15 % If optional argument 'verbose' is 1, function prints any errormessage to
matthiasm@8 16 % the screen.
matthiasm@8 17 %
matthiasm@8 18 % returns: rootnote (note string)
matthiasm@8 19 % shorthand (shorthand string)
matthiasm@8 20 % degreelist (degree-list string)
matthiasm@8 21 % bass (degree string)
matthiasm@8 22 % success (boolean)
matthiasm@8 23 % errormessage (string)
matthiasm@8 24 %
matthiasm@8 25 % See also: getchordinfo
matthiasm@8 26 %
matthiasm@8 27 % Author: Christopher Harte, August 2005
matthiasm@8 28 %
matthiasm@8 29 % Copyright: Centre for Digital Music, Queen Mary University of London 2005
matthiasm@8 30 %
matthiasm@8 31 % This file is part of the C4DM Chord Toolkit.
matthiasm@8 32 %
matthiasm@8 33 % The C4DM Chord Toolkit is free software; you can redistribute it and/or
matthiasm@8 34 % modify it under the terms of the GNU General Public License as published
matthiasm@8 35 % by the Free Software Foundation; either version 2 of the License, or
matthiasm@8 36 % (at your option) any later version.
matthiasm@8 37 %
matthiasm@8 38 % The C4DM Chord Toolkit is distributed in the hope that it will be useful,
matthiasm@8 39 % but WITHOUT ANY WARRANTY; without even the implied warranty of
matthiasm@8 40 % MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
matthiasm@8 41 % GNU General Public License for more details.
matthiasm@8 42 %
matthiasm@8 43 % You should have received a copy of the GNU General Public License
matthiasm@8 44 % along with the C4DM Toolkit; if not, write to the Free Software
matthiasm@8 45 % Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
matthiasm@8 46
matthiasm@8 47 %
matthiasm@8 48 function [rootnote, shorthand, degrees, bass, success, errormessage] = parsechord(chord, verbose)
matthiasm@8 49
matthiasm@8 50 % set verbose default to 0
matthiasm@8 51 if nargin <2
matthiasm@8 52 verbose = 0;
matthiasm@8 53 end
matthiasm@8 54
matthiasm@8 55 ilength = length(chord);
matthiasm@8 56
matthiasm@8 57 % initialise variables
matthiasm@8 58 errormessage = '';
matthiasm@8 59 rootnote = '';
matthiasm@8 60 shorthand = '';
matthiasm@8 61 degrees = '';
matthiasm@8 62 bass = '';
matthiasm@8 63
matthiasm@8 64 success = 1;
matthiasm@8 65 index = 1;
matthiasm@8 66
matthiasm@8 67
matthiasm@8 68 % check for 'no chord' symbol
matthiasm@8 69 if chord(index) == 'N'
matthiasm@8 70 rootnote = chord(index);
matthiasm@8 71 index = index +1;
matthiasm@8 72 % check to see there are no further characters
matthiasm@8 73 if(index<=ilength)
matthiasm@8 74 errormessage = sprintf(['Error in parsechord: \nExtra characters after "no chord" symbol "' chord '"\n']);
matthiasm@8 75 success = 0;
matthiasm@8 76 end
matthiasm@8 77 else
matthiasm@8 78 % parse thechord symbol
matthiasm@8 79
matthiasm@8 80 % the first part of the symbol before a switch character should be the root note
matthiasm@8 81 while ((index <=ilength ))
matthiasm@8 82
matthiasm@8 83 if (chord(index) == ':') || (chord(index) == '/') || ...
matthiasm@8 84 (chord(index) == '(') || (chord(index) == ')')
matthiasm@8 85
matthiasm@8 86 break
matthiasm@8 87 end
matthiasm@8 88
matthiasm@8 89 rootnote(index) = chord(index);
matthiasm@8 90 index = index+1;
matthiasm@8 91
matthiasm@8 92 if(index > ilength) || (chord(index) == '/')
matthiasm@8 93 % if chord is a rootnote on its own or with just a bass note
matthiasm@8 94 % then it is a major chord therefore set shorthand to 'maj'
matthiasm@8 95 shorthand = 'maj';
matthiasm@8 96 end
matthiasm@8 97 end
matthiasm@8 98
matthiasm@8 99 % initialise booleans to record which switch characters we have found
matthiasm@8 100 colon = 0;
matthiasm@8 101 openbracket = 0;
matthiasm@8 102 closebracket = 0;
matthiasm@8 103 slash = 0;
matthiasm@8 104
matthiasm@8 105 % parse the rest of the chord symbol
matthiasm@8 106 while(index <= ilength)
matthiasm@8 107
matthiasm@8 108 % reset temporary index
matthiasm@8 109 tempindex = 1;
matthiasm@8 110
matthiasm@8 111 switch(chord(index))
matthiasm@8 112
matthiasm@8 113 case ':'
matthiasm@8 114
matthiasm@8 115 % if we find a colon after any switch characters have
matthiasm@8 116 % already occured then the symbol is incorrect
matthiasm@8 117 if (colon || openbracket || closebracket || slash)
matthiasm@8 118 errormessage = sprintf(['Error in parsechord: \nIncorrect character sequence in chord "' chord '"\n']);
matthiasm@8 119 success = 0;
matthiasm@8 120 index = ilength+1;
matthiasm@8 121 else
matthiasm@8 122
matthiasm@8 123 % found the first instance of a colon character
matthiasm@8 124 colon = 1;
matthiasm@8 125
matthiasm@8 126 index = index +1;
matthiasm@8 127
matthiasm@8 128 if(index > ilength)
matthiasm@8 129 errormessage = sprintf(['Error in parsechord: \nFound ":" at end of chord string "' chord '"\n']);
matthiasm@8 130 success = 0;
matthiasm@8 131 end
matthiasm@8 132 % colon should be followed by a shorthand string or
matthiasm@8 133 % a degree list contained in brackets
matthiasm@8 134 while (index <= ilength)
matthiasm@8 135
matthiasm@8 136 if (chord(index) == ':') || (chord(index) == '/') || ...
matthiasm@8 137 (chord(index) == '(') || (chord(index) == ')')
matthiasm@8 138
matthiasm@8 139 break
matthiasm@8 140 end
matthiasm@8 141
matthiasm@8 142 % copy character into shorthand
matthiasm@8 143 shorthand(tempindex) = chord(index);
matthiasm@8 144 index = index +1;
matthiasm@8 145 tempindex = tempindex +1;
matthiasm@8 146
matthiasm@8 147 end
matthiasm@8 148
matthiasm@8 149 end
matthiasm@8 150
matthiasm@8 151 case '('
matthiasm@8 152
matthiasm@8 153 % if we have had a colon but no other switch charaters then
matthiasm@8 154 % an open bracket signifies the start of the degree list
matthiasm@8 155 if (colon && ~slash && ~closebracket && ~openbracket)
matthiasm@8 156
matthiasm@8 157 openbracket = 1;
matthiasm@8 158
matthiasm@8 159 index = index +1;
matthiasm@8 160
matthiasm@8 161 while (index <= ilength)
matthiasm@8 162
matthiasm@8 163 if (chord(index) == ':') || (chord(index) == '/') || ...
matthiasm@8 164 (chord(index) == '(') || (chord(index) == ')')
matthiasm@8 165
matthiasm@8 166 break
matthiasm@8 167 end
matthiasm@8 168
matthiasm@8 169 % copy character into degrees
matthiasm@8 170 degrees(tempindex) = chord(index);
matthiasm@8 171 index = index +1;
matthiasm@8 172 tempindex = tempindex +1;
matthiasm@8 173
matthiasm@8 174 end
matthiasm@8 175
matthiasm@8 176 if(index > ilength)
matthiasm@8 177 errormessage = sprintf(['Error in parsechord: \nDegree list brackets not closed in chord "' chord '"\n']);
matthiasm@8 178 success = 0;
matthiasm@8 179 end
matthiasm@8 180
matthiasm@8 181 else
matthiasm@8 182 errormessage = sprintf(['Error in parsechord: \nIncorrect character sequence in chord "' chord '"\n']);
matthiasm@8 183 success = 0;
matthiasm@8 184 index = ilength +1;
matthiasm@8 185 end
matthiasm@8 186
matthiasm@8 187 case ')'
matthiasm@8 188
matthiasm@8 189
matthiasm@8 190 % if we find a closing bracket then we should either be at
matthiasm@8 191 % the end of the symbol or there should be a slash to follow
matthiasm@8 192 if (colon && openbracket && ~slash && ~closebracket)
matthiasm@8 193
matthiasm@8 194 closebracket = 1;
matthiasm@8 195 index = index +1;
matthiasm@8 196
matthiasm@8 197 else
matthiasm@8 198 errormessage = sprintf(['Error in parsechord: \nIncorrect character sequence in chord "' chord '"\n']);
matthiasm@8 199 success = 0;
matthiasm@8 200 index = ilength +1;
matthiasm@8 201 end
matthiasm@8 202
matthiasm@8 203 % check to see that the brackets contained something
matthiasm@8 204 if isempty(degrees)
matthiasm@8 205 errormessage = sprintf(['Error in parsechord: \nBrackets contain no degrees in chord "' chord '"\n']);
matthiasm@8 206 success = 0;
matthiasm@8 207 index = ilength +1;
matthiasm@8 208 end
matthiasm@8 209
matthiasm@8 210
matthiasm@8 211 case '/'
matthiasm@8 212 % forward slash should be followed by a degree string
matthiasm@8 213
matthiasm@8 214 slash = 1;
matthiasm@8 215
matthiasm@8 216 % move on to next character to process the expected bass degree
matthiasm@8 217 index = index +1;
matthiasm@8 218
matthiasm@8 219 % check that we haven't overun the end of the symbol string
matthiasm@8 220 if(index > ilength)
matthiasm@8 221 errormessage = sprintf(['Error in parsechord: \nNo bass degree "/" at end of chord "' chord '"\n']);
matthiasm@8 222 success = 0;
matthiasm@8 223 end
matthiasm@8 224
matthiasm@8 225 % check that if we have had an open bracket that it also
matthiasm@8 226 % had a closed bracket
matthiasm@8 227 if(xor(openbracket,closebracket))
matthiasm@8 228 errormessage = sprintf(['Error in parsechord: \nFound "/" before closing bracket in chord "' chord '"\n']);
matthiasm@8 229 success = 0;
matthiasm@8 230 index = ilength +1;
matthiasm@8 231 end
matthiasm@8 232
matthiasm@8 233 % check that the previous character was not a ':'
matthiasm@8 234 if(chord(index-2) == ':')
matthiasm@8 235 errormessage = sprintf(['Error in parsechord: \nFound "/" directly after ":" in chord "' chord '"\n']);
matthiasm@8 236 success = 0;
matthiasm@8 237 index = ilength +1;
matthiasm@8 238 end
matthiasm@8 239
matthiasm@8 240
matthiasm@8 241 while( index <= ilength )
matthiasm@8 242
matthiasm@8 243 % if we find a switch character after a slash then
matthiasm@8 244 % the symbol is incorrect
matthiasm@8 245 if (chord(index) == ':') || (chord(index) == '/') || ...
matthiasm@8 246 (chord(index) == '(') || (chord(index) == ')')
matthiasm@8 247
matthiasm@8 248 errormessage = sprintf(['Error in parsechord: \nIncorrect character sequence in chord "' chord '"\n']);
matthiasm@8 249 success = 0;
matthiasm@8 250 index = ilength +1;
matthiasm@8 251
matthiasm@8 252 else
matthiasm@8 253
matthiasm@8 254 % copy remaining characters into bass
matthiasm@8 255 bass(tempindex) = chord(index);
matthiasm@8 256 index = index +1;
matthiasm@8 257 tempindex = tempindex +1;
matthiasm@8 258 end
matthiasm@8 259 end
matthiasm@8 260
matthiasm@8 261 otherwise
matthiasm@8 262 errormessage = sprintf(['Error in parsechord: \nUnrecognised chord "' chord '"\n']);
matthiasm@8 263 success = 0;
matthiasm@8 264 index = ilength + 1;
matthiasm@8 265 end
matthiasm@8 266
matthiasm@8 267 end
matthiasm@8 268 end
matthiasm@8 269
matthiasm@8 270 if (verbose == 1) && (success == 0)
matthiasm@8 271 fprintf(1,errormessage);
matthiasm@8 272 end
matthiasm@8 273
matthiasm@8 274