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

adding tools to repo
author christopherh
date Mon, 06 May 2013 14:43:47 +0100
parents
children
line wrap: on
line source
%
%INTERVALS2QUALITY Return the quality of a given interval_list
% 
% [quality,success, errormessage] = intervals2quality(interval_list, {verbose})
% 
% Converts a list of interval strings to an integer value denoting the
% chord's quality. Quality values are from the enumeration:
%
% 0   Major
% 1   Minor
% 2   Diminished
% 3   Augmented
% 4   Suspended
%
% Success = 1 if symbols parsed correctly, 0 otherwise. 
%
% If optional argument 'verbose' is 1, function prints any errormessage to 
% the screen.
% 
% calls:    intervals2semitones
% 
% returns:  quality (integer)
%           success  (boolean)  
%           errormessage (string)
%
%
% Author: Christopher Harte,  March 2009
% 
% Copyright: Centre for Digital Music, Queen Mary University of London 2005 
%
% This file is part of the C4DM Chord Toolkit V2.0  
%
% The C4DM Chord Toolkit is free software; you can redistribute it and/or 
% modify it under the terms of the GNU General Public License as published 
% by the Free Software Foundation; either version 2 of the License, or
% (at your option) any later version.
%
% The C4DM Chord Toolkit is distributed in the hope that it will be useful,
% but WITHOUT ANY WARRANTY; without even the implied warranty of
% MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
% GNU General Public License for more details.
%
% You should have received a copy of the GNU General Public License
% along with the C4DM Toolkit; if not, write to the Free Software
% Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA

%
function [quality,success,errormessage] = intervals2quality(interval_list,verbose)




quality = '';
errormessage = '';

if nargin < 2
    verbose = 0;
end

% define templates for the 5 different triad chords (four quality families
% plus suspension) - weights mean maj thirds will have more effect than min
% 3rds which in turn have more effect than 5ths and then 2nds and 4ths

templates = [[1,0,0,0,6,0,0,4,0,0,0,0]; ... % maj
             [1,0,0,5,0,0,0,4,0,0,0,0]; ... % min
             [1,0,0,5,0,0,4,0,0,0,0,0]; ... % dim
             [1,0,0,0,6,0,0,0,4,0,0,0]; ... % aug
             [1,0,2,0,0,2,0,4,0,0,0,0]];    % sus
             
         
% get the semitone equivalents of the intervals in the interval list             
[semitones,success,error] = intervals2semitones(interval_list);

indexa = 1;

% initialise a binary vector showing which semitones are present 
present = zeros(1,12);

while indexa <= 3 && indexa <= length(semitones) 
    
    % for each of the first three semitones in the list make its position a
    % one in the vector 'present' 
    
    if semitones(indexa) < 12
    
        present(semitones(indexa)+1) = 1;
    
    end
    indexa = indexa +1;
    
end

% multiply present by the templates matrix to give a vector of scores for
% the possible qualities
qvector = templates * present';


% find maximum value from the qualities vector
% this function benfits from the max function's picking of the first
% maximum value if there are several equal ones so is predisposed toward 
% major if the quality is not obvious from the input. (e.g. C:(1) returns major)  
[value,index] = max(qvector);

% take 1 from index to give correct enumeration
quality = index-1;


if(success==0) 

    errormessage = sprintf([error 'Error in intervals2quality: incorrect interval in list "' interval_list '"\n']);
    
    if verbose == 1
        fprintf(1, errormessage);
    end
end