annotate _chordtools/degrees2quality.m @ 9:4ea6619cb3f5 tip

removed log files
author matthiasm
date Fri, 11 Apr 2014 15:55:11 +0100
parents b5b38998ef3b
children
rev   line source
matthiasm@8 1 %
matthiasm@8 2 %DEGREES2QUALITY convert a degreelist to chord quality enumeration
matthiasm@8 3 %
matthiasm@8 4 % [quality,success, errormessage] = degrees2quality(degreelist, {verbose})
matthiasm@8 5 %
matthiasm@8 6 % Converts a list of degree strings to an integer value denoting the
matthiasm@8 7 % chord's quality. Quality values are from the enumeration:
matthiasm@8 8 %
matthiasm@8 9 % 0 Major
matthiasm@8 10 % 1 Minor
matthiasm@8 11 % 2 Diminished
matthiasm@8 12 % 3 Augmented
matthiasm@8 13 % 4 Suspended
matthiasm@8 14 %
matthiasm@8 15 % Success = 1 if symbols parsed correctly, 0 otherwise.
matthiasm@8 16 %
matthiasm@8 17 % If optional argument 'verbose' is 1, function prints any errormessage to
matthiasm@8 18 % the screen.
matthiasm@8 19 %
matthiasm@8 20 % calls: degrees2semitones
matthiasm@8 21 %
matthiasm@8 22 % returns: quality (integer)
matthiasm@8 23 % success (boolean)
matthiasm@8 24 % errormessage (string)
matthiasm@8 25 %
matthiasm@8 26 %
matthiasm@8 27 % Author: Christopher Harte, August 2005
matthiasm@8 28 %
matthiasm@8 29 % Copyright: Centre for Digital Music, Queen Mary University of London 2005
matthiasm@8 30 %
matthiasm@8 31 % This file is part of the C4DM Chord Toolkit.
matthiasm@8 32 %
matthiasm@8 33 % The C4DM Chord Toolkit is free software; you can redistribute it and/or
matthiasm@8 34 % modify it under the terms of the GNU General Public License as published
matthiasm@8 35 % by the Free Software Foundation; either version 2 of the License, or
matthiasm@8 36 % (at your option) any later version.
matthiasm@8 37 %
matthiasm@8 38 % The C4DM Chord Toolkit is distributed in the hope that it will be useful,
matthiasm@8 39 % but WITHOUT ANY WARRANTY; without even the implied warranty of
matthiasm@8 40 % MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
matthiasm@8 41 % GNU General Public License for more details.
matthiasm@8 42 %
matthiasm@8 43 % You should have received a copy of the GNU General Public License
matthiasm@8 44 % along with the C4DM Toolkit; if not, write to the Free Software
matthiasm@8 45 % Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
matthiasm@8 46
matthiasm@8 47 %
matthiasm@8 48 function [quality,success,errormessage] = degrees2quality(degreelist,verbose)
matthiasm@8 49
matthiasm@8 50
matthiasm@8 51
matthiasm@8 52
matthiasm@8 53 quality = '';
matthiasm@8 54 errormessage = '';
matthiasm@8 55
matthiasm@8 56 if nargin < 2
matthiasm@8 57 verbose = 0;
matthiasm@8 58 end
matthiasm@8 59
matthiasm@8 60 % define templates for the 5 different triad chords (four quality families
matthiasm@8 61 % plus suspension) - weights mean maj thirds will have more effect than min
matthiasm@8 62 % 3rds which in turn have more effect than 5ths and then 2nds and 4ths
matthiasm@8 63
matthiasm@8 64 templates = [[1,0,0,0,6,0,0,4,0,0,0,0]; ... % maj
matthiasm@8 65 [1,0,0,5,0,0,0,4,0,0,0,0]; ... % min
matthiasm@8 66 [1,0,0,5,0,0,4,0,0,0,0,0]; ... % dim
matthiasm@8 67 [1,0,0,0,6,0,0,0,4,0,0,0]; ... % aug
matthiasm@8 68 [1,0,2,0,0,2,0,4,0,0,0,0]]; % sus
matthiasm@8 69
matthiasm@8 70
matthiasm@8 71 % get the semitone equivalents of the degrees in the degree list
matthiasm@8 72 [semitones,success,error] = degrees2semitones(degreelist);
matthiasm@8 73
matthiasm@8 74 indexa = 1;
matthiasm@8 75
matthiasm@8 76 % initialise a binary vector showing which semitones are present
matthiasm@8 77 present = zeros(1,12);
matthiasm@8 78
matthiasm@8 79 while indexa <= 3 && indexa <= length(semitones)
matthiasm@8 80
matthiasm@8 81 % for each of the first three semitones in the list make its position a
matthiasm@8 82 % one in the vector 'present'
matthiasm@8 83
matthiasm@8 84 if semitones(indexa) < 12
matthiasm@8 85
matthiasm@8 86 present(semitones(indexa)+1) = 1;
matthiasm@8 87
matthiasm@8 88 end
matthiasm@8 89 indexa = indexa +1;
matthiasm@8 90
matthiasm@8 91 end
matthiasm@8 92
matthiasm@8 93 % multiply present by the templates matrix to give a vector of scores for
matthiasm@8 94 % the possible qualities
matthiasm@8 95 qvector = templates * present';
matthiasm@8 96
matthiasm@8 97
matthiasm@8 98 % find maximum value from the qualities vector
matthiasm@8 99 % this function benfits from the max function's picking of the first
matthiasm@8 100 % maximum value if there are several equal ones so is predisposed toward
matthiasm@8 101 % major if the quality is not obvious from the input. (e.g. C:(1) returns major)
matthiasm@8 102 [value,index] = max(qvector);
matthiasm@8 103
matthiasm@8 104 % take 1 from index to give correct enumeration
matthiasm@8 105 quality = index-1;
matthiasm@8 106
matthiasm@8 107
matthiasm@8 108 if(success==0)
matthiasm@8 109
matthiasm@8 110 errormessage = sprintf([error 'Error in degrees2quality: incorrect degree in list "' degreelist '"\n']);
matthiasm@8 111
matthiasm@8 112 if verbose == 1
matthiasm@8 113 fprintf(1, errormessage);
matthiasm@8 114 end
matthiasm@8 115 end