matthiasm@8: % matthiasm@8: % PARSEDEGREE matthiasm@8: % matthiasm@8: % [interval, accidentals, success, errormessage] = parsedegree(degree, {verbose}) matthiasm@8: % matthiasm@8: % Parse a degree to an interval value and a number of accidentals matthiasm@8: % if accidentals is positive it denotes number of sharps, matthiasm@8: % if it is negative it denotes number of flats. matthiasm@8: % If the omit degree character '*' is found then present is returned as 0 matthiasm@8: % otherwise it is 1. matthiasm@8: % matthiasm@8: % Success = 1 if symbols parsed correctly, 0 otherwise. matthiasm@8: % matthiasm@8: % If optional argument 'verbose' is 1, function prints any errormessage to matthiasm@8: % the screen. matthiasm@8: % matthiasm@8: % returns: interval (integer) matthiasm@8: % accidentals (integer) matthiasm@8: % present (boolean) matthiasm@8: % success (boolean) matthiasm@8: % errormessage (string) matthiasm@8: % matthiasm@8: % Author: Christopher Harte, August 2005 matthiasm@8: % matthiasm@8: % Copyright: Centre for Digital Music, Queen Mary University of London 2005 matthiasm@8: % matthiasm@8: % This file is part of the C4DM Chord Toolkit. matthiasm@8: % matthiasm@8: % The C4DM Chord Toolkit is free software; you can redistribute it and/or matthiasm@8: % modify it under the terms of the GNU General Public License as published matthiasm@8: % by the Free Software Foundation; either version 2 of the License, or matthiasm@8: % (at your option) any later version. matthiasm@8: % matthiasm@8: % The C4DM Chord Toolkit is distributed in the hope that it will be useful, matthiasm@8: % but WITHOUT ANY WARRANTY; without even the implied warranty of matthiasm@8: % MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the matthiasm@8: % GNU General Public License for more details. matthiasm@8: % matthiasm@8: % You should have received a copy of the GNU General Public License matthiasm@8: % along with the C4DM Toolkit; if not, write to the Free Software matthiasm@8: % Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA matthiasm@8: matthiasm@8: % matthiasm@8: function [interval,accidentals,present,success, errormessage] = parsedegree(degree, verbose) matthiasm@8: matthiasm@8: if nargin < 2 matthiasm@8: verbose = 0; matthiasm@8: end matthiasm@8: matthiasm@8: ilength = length(degree); matthiasm@8: matthiasm@8: errormessage = ''; matthiasm@8: accidentals = 0; matthiasm@8: interval = 0; matthiasm@8: success=1; matthiasm@8: present = 1; matthiasm@8: matthiasm@8: index = 1; matthiasm@8: matthiasm@8: % if the input string is not empty matthiasm@8: if (isempty(degree) == 0) matthiasm@8: matthiasm@8: matthiasm@8: % check for omit degree '*' matthiasm@8: if degree(index) == '*' matthiasm@8: present = 0; matthiasm@8: index = index +1; matthiasm@8: end matthiasm@8: matthiasm@8: matthiasm@8: tempstring = ''; matthiasm@8: tempindex = 1; matthiasm@8: % parse the degree string matthiasm@8: while index <= ilength matthiasm@8: matthiasm@8: switch degree(index) matthiasm@8: matthiasm@8: case 'b' % FLAT matthiasm@8: accidentals = accidentals - 1; %decrement accidental count matthiasm@8: index = index + 1; matthiasm@8: matthiasm@8: case '#' % SHARP matthiasm@8: accidentals = accidentals + 1; %increment accidental count matthiasm@8: index = index + 1; matthiasm@8: matthiasm@8: case {'1','2','3','4','5','6','7','8','9'} matthiasm@8: % if neither of the above then remaining string should be matthiasm@8: % an integer interval value matthiasm@8: tempstring(tempindex) = degree(index); matthiasm@8: matthiasm@8: tempindex = tempindex+1; matthiasm@8: index = index+1; matthiasm@8: matthiasm@8: otherwise matthiasm@8: % unrecognised symbol matthiasm@8: success=0; matthiasm@8: index = ilength+1; matthiasm@8: end matthiasm@8: end matthiasm@8: else matthiasm@8: success=0; matthiasm@8: end matthiasm@8: matthiasm@8: if success matthiasm@8: matthiasm@8: % convert the interval string to an integer matthiasm@8: [interval, success] = str2num(tempstring); matthiasm@8: matthiasm@8: % check it worked and that the interval is valid matthiasm@8: if isempty(interval) || (interval <= 0) matthiasm@8: success = 0; matthiasm@8: end matthiasm@8: end matthiasm@8: matthiasm@8: if(success==0) % correct degree therefore return success = 1 matthiasm@8: % if not an integer then the degree string is incorrect matthiasm@8: errormessage = sprintf(['Error in parsedegree: Unrecognised degree "' degree '"\n']); matthiasm@8: interval = 0; matthiasm@8: if verbose == 1 matthiasm@8: fprintf(1, errormessage); matthiasm@8: end matthiasm@8: matthiasm@8: end