To check out this repository please hg clone the following URL, or open the URL using EasyMercurial or your preferred Mercurial client.
root / _chordtools / addshort2list.m
History | View | Annotate | Download (5.34 KB)
| 1 |
% |
|---|---|
| 2 |
%ADDSHORT2LIST Combine degrees of a shorthand with another degree list |
| 3 |
% |
| 4 |
% [fulldegreelist, success, errormessage] = addshort2list(shorthand, degreelist, {verbose})
|
| 5 |
% |
| 6 |
% Combine shorthand and degreelist to form full degree list for a chord. |
| 7 |
% Takes into account the omit degree character '*' in producing final list. |
| 8 |
% |
| 9 |
% Success = 1 if shorthand and degreelist combined correctly, 0 otherwise. |
| 10 |
% |
| 11 |
% If optional argument 'verbose' is 1, function prints any errormessage to |
| 12 |
% the screen. |
| 13 |
% |
| 14 |
% returns: fulldegreelist (string) |
| 15 |
% success (boolean) |
| 16 |
% errormessage (string) |
| 17 |
% |
| 18 |
% Author: Christopher Harte, August 2005 |
| 19 |
% |
| 20 |
% Copyright: Centre for Digital Music, Queen Mary University of London 2005 |
| 21 |
% |
| 22 |
% This file is part of the C4DM Chord Toolkit. |
| 23 |
% |
| 24 |
% The C4DM Chord Toolkit is free software; you can redistribute it and/or |
| 25 |
% modify it under the terms of the GNU General Public License as published |
| 26 |
% by the Free Software Foundation; either version 2 of the License, or |
| 27 |
% (at your option) any later version. |
| 28 |
% |
| 29 |
% The C4DM Chord Toolkit is distributed in the hope that it will be useful, |
| 30 |
% but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 31 |
% MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 32 |
% GNU General Public License for more details. |
| 33 |
% |
| 34 |
% You should have received a copy of the GNU General Public License |
| 35 |
% along with the C4DM Toolkit; if not, write to the Free Software |
| 36 |
% Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA |
| 37 |
|
| 38 |
% |
| 39 |
function [fulldegreelist, success, errormessage] = addshort2list(shorthand, degreelist, verbose) |
| 40 |
|
| 41 |
% set verbose default to 0 |
| 42 |
if nargin < 3 |
| 43 |
verbose = 0; |
| 44 |
end |
| 45 |
|
| 46 |
errormessage = ''; |
| 47 |
|
| 48 |
% initialise variables |
| 49 |
fulldegreelist = ''; |
| 50 |
shortintervals = []; |
| 51 |
degreeintervals = []; |
| 52 |
addlist = []; |
| 53 |
success = 1; |
| 54 |
|
| 55 |
% convert shorthand string to list of degrees |
| 56 |
[shortdegrees, ok, errors] = shorthand2degrees(shorthand); |
| 57 |
|
| 58 |
if ok |
| 59 |
% if that works convert the shorthand degrees and degreelist to |
| 60 |
% intervals and accidentals |
| 61 |
|
| 62 |
% add the implied 1 interval to the shorthand list |
| 63 |
if isempty(shortdegrees) |
| 64 |
rootinterval = '1'; % no comma if shorthand is empty |
| 65 |
else |
| 66 |
rootinterval = '1,'; % comma shows that there are further degrees after this one |
| 67 |
end |
| 68 |
|
| 69 |
|
| 70 |
[shortintervals, ok, errors] = parsedegreelist([rootinterval shortdegrees]); |
| 71 |
|
| 72 |
if (ok) |
| 73 |
|
| 74 |
|
| 75 |
% this part of the algorithm finds all the present intervals and sorts |
| 76 |
% them into ascending numeric order |
| 77 |
|
| 78 |
% First put the shorthand intervals into the full list |
| 79 |
|
| 80 |
[ilength,y] = size(shortintervals); |
| 81 |
|
| 82 |
for index = 1:ilength |
| 83 |
% convert interval to a semitone value |
| 84 |
semitone = interval2semitone(shortintervals(index,1),shortintervals(index,2)); |
| 85 |
|
| 86 |
% Using the semitone value as an index will find redundant |
| 87 |
% degrees and also sort them in number order. Multiplying the interval by its |
| 88 |
% presence value leaves either the interval value or 0. Any element |
| 89 |
% in the array with interval of 0 will be discarded later |
| 90 |
% We add 1 to the semitone value to handle the '1' or 'unity' |
| 91 |
% interval that would return a 0 semitone value |
| 92 |
|
| 93 |
% record the interval |
| 94 |
addlist(semitone+1,1) = shortintervals(index,1) .* shortintervals(index,3); |
| 95 |
% record the accidentals as well |
| 96 |
addlist(semitone+1,2) = shortintervals(index,2); |
| 97 |
|
| 98 |
end |
| 99 |
else |
| 100 |
success = 0; |
| 101 |
end |
| 102 |
|
| 103 |
[degreeintervals, ok2, errors] = parsedegreelist(degreelist); |
| 104 |
ok = ok && ok2; |
| 105 |
|
| 106 |
if ok |
| 107 |
% Now add the degrees from the degreelist taking redundant and |
| 108 |
% ommitted degrees into account |
| 109 |
|
| 110 |
[ilength,y] = size(degreeintervals); |
| 111 |
|
| 112 |
for index = 1:ilength |
| 113 |
% convert interval to a semitone value |
| 114 |
[semitone, ok, errors] = interval2semitone(degreeintervals(index,1),degreeintervals(index,2)); |
| 115 |
|
| 116 |
if ok && semitone >= 0 |
| 117 |
% record the interval |
| 118 |
addlist(semitone+1,1) = degreeintervals(index,1) .* degreeintervals(index,3); |
| 119 |
% record the accidentals as well |
| 120 |
addlist(semitone+1,2) = degreeintervals(index,2); |
| 121 |
|
| 122 |
else |
| 123 |
success = 0; |
| 124 |
end |
| 125 |
|
| 126 |
end |
| 127 |
else |
| 128 |
success=0; |
| 129 |
end |
| 130 |
|
| 131 |
|
| 132 |
% now create fulldegreelist |
| 133 |
if ok |
| 134 |
|
| 135 |
[ilength,y] = size(addlist); |
| 136 |
|
| 137 |
for index = 1:ilength |
| 138 |
|
| 139 |
% if there is a non zero interval in this element, convert |
| 140 |
% it to a degree symbol and add it to the output list |
| 141 |
if(addlist(index,1) ~=0) |
| 142 |
|
| 143 |
degreesymbol = interval2degree(addlist(index,1),addlist(index,2)); |
| 144 |
|
| 145 |
if isempty(fulldegreelist) |
| 146 |
% if this is the first degree then start list |
| 147 |
fulldegreelist = degreesymbol; |
| 148 |
else |
| 149 |
% add to list with a separating comma |
| 150 |
fulldegreelist = [fulldegreelist ',' degreesymbol]; |
| 151 |
end |
| 152 |
end |
| 153 |
end |
| 154 |
else |
| 155 |
|
| 156 |
success = 0; |
| 157 |
|
| 158 |
end |
| 159 |
|
| 160 |
else |
| 161 |
|
| 162 |
success = 0; |
| 163 |
|
| 164 |
end |
| 165 |
|
| 166 |
|
| 167 |
if success == 0 |
| 168 |
errormessage = [errors sprintf(['Error in addshort2list: unsuccessful '... |
| 169 |
'combination "' shorthand '" and "' degreelist '"\n'])]; |
| 170 |
|
| 171 |
if verbose ==1 |
| 172 |
fprintf(1,errormessage); |
| 173 |
end |
| 174 |
|
| 175 |
|
| 176 |
end |
| 177 |
|
| 178 |
|