
%COMPARECHORDS
%
% [match,success,errormessage] = comparechords(chord1, chord2, comparison,
% cardinality, inversion,verbose)
%
%
% COMPARISON OPTIONS:
%
% CHORDS
% s = string comparison
% ov = ordered pnset comparison
% op = ordered pcset comparison
% uv = ordered pnset comparison
% up = ordered pcset comparison
% 
% mx08 = compare using MIREX08 chord mapping
% mx09 = compare using MIREX09 chord mapping
%
% CHORDTYPES (root-blind)
% q = quality
% oi = ordered relative interval comparison
% or = ordered relative pitchclass set comparison
% ui = unordered relative interval comparison
% ur = unordered relative pitchclass set comparison


function [match,success,errormessage] = comparechords2(chord1, chord2, comparison, cardinality, inversion,verbose)

errormessage = '';
success = 1;
match = 0;

% remember these just in case there is an error to report
c1 = chord1;
c2 = chord2;

% initialise things if not specified...
if  nargin<3
    comparison = 's';
end

if  nargin<4
    cardinality = 6; % maximum cardinality in beatles collection
end

if  nargin<5
    inversion = 1;  % assume people want to keep inversion information
end

if  nargin<6
    verbose = 0;  
end



% if inversion is 0, strip bass intervals off the chords if they have them
if inversion==0
   chord1 = stripinversion(chord1); 
   chord2 = stripinversion(chord2);   
end
   
mx=0;
% strip roots from chords and replace with C if this is a chordtype comparison
switch lower(comparison)

    case  {'mx08','mx09'}
        mx=1;
        
    case {'q','oi','or','ui','ur'}
        chord1 = striproot(chord1,1);
        chord2 = striproot(chord2,1);
        
    case {'s','ov','op','uv','up'}
        % do nothing in this case...
        
    otherwise
        % not a recognised option so exit with errormessage
        errormessage = 'Error in comparechords: unrecognised comparison type';
        success = 0;
        
end


if (success)
    if mx
        
        % use a mirex 08/09 comparison
        
        year = str2num(comparison(3:4));
        
        if isempty(year)
            success = 0;
        else

           [val1,success] = mxmap(chord1, year);
           if success
                [val2,success] = mxmap(chord2, year);
            
                if success
                   if abs(val1-val2)==0
                       match = 1;              
                   end
                end
            end
        end
        
    else
        
       
        if length(comparison)==2
            % if the comparison is 2 chars then it is a set-matching option
            switch comparison
                case {'oi','ov','uv','ui'}
                    [set1x,bn,success,errormessage] = chord2notes(chord1,1);
                    if success
                        [set2x,bn,success,errormessage] = chord2notes(chord2,1);
                        % cludge to get the sets the right way round, fix
                        % later...
                        if success
                            set1 = set1x';
                            set2 = set2x';
                        end
                    end
                case {'ur','or','op','up'}
                    [set1, bc1, success,errormessage] = chord2pitchclasses(chord1);
                    if success
                        [set2, bc2,success,errormessage] = chord2pitchclasses(chord2);
                    end
                otherwise
                    %by this stage there shouldn't be an otherwise...
                    success = 0;
            end


            if success
                switch comparison
                    case {'ui','ur','uv','up'}
                      % do an unordered match 
                        [match,success,errormessage] = compareunorderedsets(set1,set2,cardinality);    

                    case {'ov','op','oi','or'}
                      % do an ordered match
                        [match,success,errormessage] = compareorderedsets(set1,set2,cardinality);    

                    otherwise
                        %by this stage there shouldn't be an otherwise...
                        success = 0;
                end
            end

        else
            % otherwise must be a string match
            [match,success,errormessage] = compareorderedsets(chord1,chord2);    
        end
    end
end


if success==0
    errormessage = [errormessage sprintf(['Error in comparechords: Couldn''t compare chords "' c1 '" and "' c2 '"\n'])];
    if verbose ==1
       fprintf(1,errormessage);
    end
end
