matthiasm@8: % matthiasm@8: %DEGREES2QUALITY convert a degreelist to chord quality enumeration matthiasm@8: % matthiasm@8: % [quality,success, errormessage] = degrees2quality(degreelist, {verbose}) matthiasm@8: % matthiasm@8: % Converts a list of degree strings to an integer value denoting the matthiasm@8: % chord's quality. Quality values are from the enumeration: matthiasm@8: % matthiasm@8: % 0 Major matthiasm@8: % 1 Minor matthiasm@8: % 2 Diminished matthiasm@8: % 3 Augmented matthiasm@8: % 4 Suspended 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: % calls: degrees2semitones matthiasm@8: % matthiasm@8: % returns: quality (integer) matthiasm@8: % success (boolean) matthiasm@8: % errormessage (string) matthiasm@8: % 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 [quality,success,errormessage] = degrees2quality(degreelist,verbose) matthiasm@8: matthiasm@8: matthiasm@8: matthiasm@8: matthiasm@8: quality = ''; matthiasm@8: errormessage = ''; matthiasm@8: matthiasm@8: if nargin < 2 matthiasm@8: verbose = 0; matthiasm@8: end matthiasm@8: matthiasm@8: % define templates for the 5 different triad chords (four quality families matthiasm@8: % plus suspension) - weights mean maj thirds will have more effect than min matthiasm@8: % 3rds which in turn have more effect than 5ths and then 2nds and 4ths matthiasm@8: matthiasm@8: templates = [[1,0,0,0,6,0,0,4,0,0,0,0]; ... % maj matthiasm@8: [1,0,0,5,0,0,0,4,0,0,0,0]; ... % min matthiasm@8: [1,0,0,5,0,0,4,0,0,0,0,0]; ... % dim matthiasm@8: [1,0,0,0,6,0,0,0,4,0,0,0]; ... % aug matthiasm@8: [1,0,2,0,0,2,0,4,0,0,0,0]]; % sus matthiasm@8: matthiasm@8: matthiasm@8: % get the semitone equivalents of the degrees in the degree list matthiasm@8: [semitones,success,error] = degrees2semitones(degreelist); matthiasm@8: matthiasm@8: indexa = 1; matthiasm@8: matthiasm@8: % initialise a binary vector showing which semitones are present matthiasm@8: present = zeros(1,12); matthiasm@8: matthiasm@8: while indexa <= 3 && indexa <= length(semitones) matthiasm@8: matthiasm@8: % for each of the first three semitones in the list make its position a matthiasm@8: % one in the vector 'present' matthiasm@8: matthiasm@8: if semitones(indexa) < 12 matthiasm@8: matthiasm@8: present(semitones(indexa)+1) = 1; matthiasm@8: matthiasm@8: end matthiasm@8: indexa = indexa +1; matthiasm@8: matthiasm@8: end matthiasm@8: matthiasm@8: % multiply present by the templates matrix to give a vector of scores for matthiasm@8: % the possible qualities matthiasm@8: qvector = templates * present'; matthiasm@8: matthiasm@8: matthiasm@8: % find maximum value from the qualities vector matthiasm@8: % this function benfits from the max function's picking of the first matthiasm@8: % maximum value if there are several equal ones so is predisposed toward matthiasm@8: % major if the quality is not obvious from the input. (e.g. C:(1) returns major) matthiasm@8: [value,index] = max(qvector); matthiasm@8: matthiasm@8: % take 1 from index to give correct enumeration matthiasm@8: quality = index-1; matthiasm@8: matthiasm@8: matthiasm@8: if(success==0) matthiasm@8: matthiasm@8: errormessage = sprintf([error 'Error in degrees2quality: incorrect degree in list "' degreelist '"\n']); matthiasm@8: matthiasm@8: if verbose == 1 matthiasm@8: fprintf(1, errormessage); matthiasm@8: end matthiasm@8: end