view chordtools/chord2midinotes.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
%
%CHORD2MIDINOTES Generate MIDI note values for a given chord symbol
% 
% [midinotes, success, errormessage] = chord2midinotes(chordsymbol, {verbose})
% 
% Obtains a set of midi note values for notes present in chord "chordsymbol".
% The voicing is built on the rootnote with all other intervals occuring 
% above in number order unless a bass interval is specified.
%
% In the case of the 'no chord' symbol 'N' function returns midinote -1
%
% Success = 1 if chord is converted correctly else 0.
% 
% If optional argument 'verbose' is 1, function prints any errormessage to 
% the screen.
%
% Returns:  midinotes (array of integers)
%           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 [midinotes, success,errormessage] = chord2midinotes(chordsymbol, verbose)

% set verbose default to 0
if nargin < 2
    verbose = 0;
end

errormessage = '';
mainlist = '';
success = 1;
midinotes = 0;

% parse the chordsymbol
[rootnote,shorthand,interval_list,bass, success, errormessage] = getchordinfo(chordsymbol);

if success


    %if 'no chord' then return -1
    if rootnote == 'N'

        midinotes = -1;
    else

        % get root's pitchclass with respect to middle C and calculate absolute
        % pitch of root in that octave   
        if success

        [rootpitchclass, success, errormessage] = note2pitchclass(rootnote);

        % midinote C4 = 60 so add pitchclass to calculate rootpitch
        rootpitch = 60 + rootpitchclass;

        end


        % combine shorthand and interval_list and obtain semitone values for each
        % interval
        if success

            [mainlist, success, errormessage] = addshort2list(shorthand, interval_list);

            if success

                [semitones,success,errormessage] = intervals2semitones(mainlist);

                % add rootpitch to obtain midinotes
                chordpitches = semitones + rootpitch;

            end

        end

        % Now find the bass note

        if success

            % if we haven't got a bass interval then play the rootnote as the
            % bassnote
            if isempty(bass)
                basspitchclass = rootpitchclass;
            else
                % otherwise convert the bassinterval to a note
                [bassnote,success,errormessage] = interval2note(bass, rootnote);

                if success

                    [basspitchclass, success, errormessage] = note2pitchclass(char(bassnote));

                end
            end

            if success
                % calculate bass note so that it is in the octave below the
                % rest of the chord i.e. add to C3 midinote 48
                basspitch = basspitchclass + 48;

                % Put midinote list together
                midinotes = [basspitch, rootpitch];
                
                for index = 1:length(chordpitches)
                    if chordpitches(index) ~= rootpitch
                       midinotes = [midinotes, chordpitches(index)];
                    end
                end
            end

        end
    end

%alter pitches for chords with high roots
if rootpitchclass > 6
    midinotes = midinotes-12;
end

end
    
if success == 0
    errormessage = [errormessage sprintf(['Error in chord2midinotes: Couldn''t convert chord"' chordsymbol '"\n'])];   
    if verbose ==1
       fprintf(1,errormessage);
    end
end