annotate chordtools/parseinterval.m @ 1:8973548174c1 tip

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