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