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