matthiasm@8: % matthiasm@8: %CHORD2MIDINOTES Generate MIDI note values for a given chord symbol matthiasm@8: % matthiasm@8: % [midinotes, success, errormessage] = chord2midinotes(chordsymbol, {verbose}) matthiasm@8: % matthiasm@8: % Obtains a set of midi note values for notes present in chord "chordsymbol". matthiasm@8: % The voicing is built on the rootnote with all other degrees occuring matthiasm@8: % above in number order unless a bass degree is specified. matthiasm@8: % matthiasm@8: % In the case of the 'no chord' symbol 'N' function returns midinote -1 matthiasm@8: % matthiasm@8: % Success = 1 if chord is converted correctly else 0. matthiasm@8: % matthiasm@8: % If optional argument 'verbose' is 1, function prints any errormessage to matthiasm@8: % the screen. matthiasm@8: % matthiasm@8: % Returns: midinotes (array of integers) matthiasm@8: % success (boolean) matthiasm@8: % errormessage (string) matthiasm@8: % matthiasm@8: % matthiasm@8: % % Author: Christopher Harte, August 2005 matthiasm@8: % matthiasm@8: % Copyright: Centre for Digital Music, Queen Mary University of London 2005 matthiasm@8: % matthiasm@8: % This file is part of the C4DM Chord Toolkit. matthiasm@8: % matthiasm@8: % The C4DM Chord Toolkit is free software; you can redistribute it and/or matthiasm@8: % modify it under the terms of the GNU General Public License as published matthiasm@8: % by the Free Software Foundation; either version 2 of the License, or matthiasm@8: % (at your option) any later version. matthiasm@8: % matthiasm@8: % The C4DM Chord Toolkit is distributed in the hope that it will be useful, matthiasm@8: % but WITHOUT ANY WARRANTY; without even the implied warranty of matthiasm@8: % MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the matthiasm@8: % GNU General Public License for more details. matthiasm@8: % matthiasm@8: % You should have received a copy of the GNU General Public License matthiasm@8: % along with the C4DM Toolkit; if not, write to the Free Software matthiasm@8: % Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA matthiasm@8: matthiasm@8: % matthiasm@8: function [midinotes, success,errormessage] = chord2midinotes(chordsymbol, verbose) matthiasm@8: matthiasm@8: % set verbose default to 0 matthiasm@8: if nargin < 2 matthiasm@8: verbose = 0; matthiasm@8: end matthiasm@8: matthiasm@8: errormessage = ''; matthiasm@8: mainlist = ''; matthiasm@8: success = 1; matthiasm@8: midinotes = 0; matthiasm@8: matthiasm@8: % parse the chordsymbol matthiasm@8: [rootnote,shorthand,degreelist,bass, success, errormessage] = getchordinfo(chordsymbol); matthiasm@8: matthiasm@8: if success matthiasm@8: matthiasm@8: matthiasm@8: %if 'no chord' then return -1 matthiasm@8: if rootnote == 'N' matthiasm@8: matthiasm@8: midinotes = -1; matthiasm@8: else matthiasm@8: matthiasm@8: % get root's pitchclass with respect to middle C and calculate absolute matthiasm@8: % pitch of root in that octave matthiasm@8: if success matthiasm@8: matthiasm@8: [rootpitchclass, success, errormessage] = note2pitchclass(rootnote); matthiasm@8: matthiasm@8: % midinote C4 = 60 so add pitchclass to calculate rootpitch matthiasm@8: rootpitch = 60 + rootpitchclass; matthiasm@8: matthiasm@8: end matthiasm@8: matthiasm@8: matthiasm@8: % combine shorthand and degreelist and obtain semitone values for each matthiasm@8: % degree matthiasm@8: if success matthiasm@8: matthiasm@8: [mainlist, success, errormessage] = addshort2list(shorthand, degreelist); matthiasm@8: matthiasm@8: if success matthiasm@8: matthiasm@8: [semitones,success,errormessage] = degrees2semitones(mainlist); matthiasm@8: matthiasm@8: % add rootpitch to obtain midinotes matthiasm@8: chordpitches = semitones + rootpitch; matthiasm@8: matthiasm@8: end matthiasm@8: matthiasm@8: end matthiasm@8: matthiasm@8: % Now find the bass note matthiasm@8: matthiasm@8: if success matthiasm@8: matthiasm@8: % if we haven't got a bass degree then play the rootnote as the matthiasm@8: % bassnote matthiasm@8: if isempty(bass) matthiasm@8: basspitchclass = rootpitchclass; matthiasm@8: else matthiasm@8: % otherwise convert the bassdegree to a note matthiasm@8: [bassnote,success,errormessage] = degree2note(bass, rootnote); matthiasm@8: matthiasm@8: if success matthiasm@8: matthiasm@8: [basspitchclass, success, errormessage] = note2pitchclass(char(bassnote)); matthiasm@8: matthiasm@8: end matthiasm@8: end matthiasm@8: matthiasm@8: if success matthiasm@8: % calculate bass note so that it is in the octave below the matthiasm@8: % rest of the chord i.e. add to C3 midinote 48 matthiasm@8: basspitch = basspitchclass + 48; matthiasm@8: matthiasm@8: % Put midinote list together matthiasm@8: midinotes = [basspitch, rootpitch]; matthiasm@8: matthiasm@8: for index = 1:length(chordpitches) matthiasm@8: if chordpitches(index) ~= rootpitch matthiasm@8: midinotes = [midinotes, chordpitches(index)]; matthiasm@8: end matthiasm@8: end matthiasm@8: end matthiasm@8: matthiasm@8: end matthiasm@8: end matthiasm@8: matthiasm@8: %alter pitches for chords with high roots matthiasm@8: if rootpitchclass > 6 matthiasm@8: midinotes = midinotes-12; matthiasm@8: end matthiasm@8: matthiasm@8: end matthiasm@8: matthiasm@8: if success == 0 matthiasm@8: errormessage = [errormessage sprintf(['Error in chord2midinotes: Couldn''t convert chord"' chordsymbol '"\n'])]; matthiasm@8: if verbose ==1 matthiasm@8: fprintf(1,errormessage); matthiasm@8: end matthiasm@8: end matthiasm@8: matthiasm@8: matthiasm@8: