christopherh@1: % christopherh@1: % PARSENOTE christopherh@1: % christopherh@1: % [natural, accidentals, success, errormessage] = parsenote(note, {verbose}) christopherh@1: % christopherh@1: % Parse a note to a natural character value and a number of accidentals christopherh@1: % if accidentals is positive it denotes number of sharps, christopherh@1: % if it is negative it denotes number of flats. christopherh@1: % christopherh@1: % Success = 1 if symbols parsed correctly, 0 otherwise. christopherh@1: % christopherh@1: % If optional argument 'verbose' is 1, function prints any errormessage to christopherh@1: % the screen. christopherh@1: % christopherh@1: % returns: natural (char) christopherh@1: % accidentals (integer) christopherh@1: % success (boolean) christopherh@1: % errormessage (string) christopherh@1: % christopherh@1: % Author: Christopher Harte, March 2009 christopherh@1: % christopherh@1: % Copyright: Centre for Digital Music, Queen Mary University of London 2005 christopherh@1: % christopherh@1: % This file is part of the C4DM Chord Toolkit V2.0 christopherh@1: % christopherh@1: % The C4DM Chord Toolkit is free software; you can redistribute it and/or christopherh@1: % modify it under the terms of the GNU General Public License as published christopherh@1: % by the Free Software Foundation; either version 2 of the License, or christopherh@1: % (at your option) any later version. christopherh@1: % christopherh@1: % The C4DM Chord Toolkit is distributed in the hope that it will be useful, christopherh@1: % but WITHOUT ANY WARRANTY; without even the implied warranty of christopherh@1: % MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the christopherh@1: % GNU General Public License for more details. christopherh@1: % christopherh@1: % You should have received a copy of the GNU General Public License christopherh@1: % along with the C4DM Toolkit; if not, write to the Free Software christopherh@1: % Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA christopherh@1: christopherh@1: % christopherh@1: function [natural,accidentals,success, errormessage] = parsenote(note, verbose) christopherh@1: christopherh@1: if nargin < 2 christopherh@1: verbose = 0; christopherh@1: end christopherh@1: christopherh@1: errormessage = ''; christopherh@1: christopherh@1: ilength = length(note); christopherh@1: christopherh@1: accidentals = 0; christopherh@1: natural = []; christopherh@1: success=1; christopherh@1: christopherh@1: index = 1; christopherh@1: christopherh@1: if (isempty(note)) % if we have been passed an empty string christopherh@1: success=0; christopherh@1: christopherh@1: else % parse the note string christopherh@1: christopherh@1: switch(note(index)) christopherh@1: christopherh@1: case {'A','B','C','D','E','F','G'} christopherh@1: christopherh@1: %first character should be a natural christopherh@1: natural = note(index); christopherh@1: index= index+1; christopherh@1: christopherh@1: %remaining characters should be sharps or flats christopherh@1: while index <= ilength christopherh@1: christopherh@1: switch note(index) christopherh@1: christopherh@1: case 'b' % FLAT christopherh@1: accidentals = accidentals - 1; %decrement accidental count christopherh@1: index = index + 1; christopherh@1: christopherh@1: case '#' % SHARP christopherh@1: accidentals = accidentals + 1; %increment accidental count christopherh@1: index = index + 1; christopherh@1: christopherh@1: otherwise christopherh@1: % if neither of the above then the note is incorrect christopherh@1: success=0; christopherh@1: index = ilength+1; christopherh@1: end christopherh@1: end christopherh@1: christopherh@1: otherwise christopherh@1: success=0; christopherh@1: end christopherh@1: end christopherh@1: christopherh@1: if(success==0) % correct note therefore return success = 1 christopherh@1: % if not an integer then the note string is incorrect christopherh@1: errormessage = sprintf(['Error in parsenote: Unrecognised note "' note '"\n']); christopherh@1: christopherh@1: if verbose ==1 christopherh@1: fprintf(1,errormessage); christopherh@1: end christopherh@1: end