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