annotate chordtools/addshort2list.m @ 1:8973548174c1 tip

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