comparison chordtools/intervals2quality.m @ 1:8973548174c1 tip

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