annotate chordtools/chord2midinotes.m @ 1:8973548174c1 tip

adding tools to repo
author christopherh
date Mon, 06 May 2013 14:43:47 +0100
parents
children
rev   line source
christopherh@1 1 %
christopherh@1 2 %CHORD2MIDINOTES Generate MIDI note values for a given chord symbol
christopherh@1 3 %
christopherh@1 4 % [midinotes, success, errormessage] = chord2midinotes(chordsymbol, {verbose})
christopherh@1 5 %
christopherh@1 6 % Obtains a set of midi note values for notes present in chord "chordsymbol".
christopherh@1 7 % The voicing is built on the rootnote with all other intervals occuring
christopherh@1 8 % above in number order unless a bass interval is specified.
christopherh@1 9 %
christopherh@1 10 % In the case of the 'no chord' symbol 'N' function returns midinote -1
christopherh@1 11 %
christopherh@1 12 % Success = 1 if chord is converted correctly else 0.
christopherh@1 13 %
christopherh@1 14 % If optional argument 'verbose' is 1, function prints any errormessage to
christopherh@1 15 % the screen.
christopherh@1 16 %
christopherh@1 17 % Returns: midinotes (array of integers)
christopherh@1 18 % success (boolean)
christopherh@1 19 % errormessage (string)
christopherh@1 20 %
christopherh@1 21 %
christopherh@1 22 % % Author: Christopher Harte, March 2009
christopherh@1 23 %
christopherh@1 24 % Copyright: Centre for Digital Music, Queen Mary University of London 2005
christopherh@1 25 %
christopherh@1 26 % This file is part of the C4DM Chord Toolkit V2.0
christopherh@1 27 %
christopherh@1 28 % The C4DM Chord Toolkit is free software; you can redistribute it and/or
christopherh@1 29 % modify it under the terms of the GNU General Public License as published
christopherh@1 30 % by the Free Software Foundation; either version 2 of the License, or
christopherh@1 31 % (at your option) any later version.
christopherh@1 32 %
christopherh@1 33 % The C4DM Chord Toolkit is distributed in the hope that it will be useful,
christopherh@1 34 % but WITHOUT ANY WARRANTY; without even the implied warranty of
christopherh@1 35 % MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
christopherh@1 36 % GNU General Public License for more details.
christopherh@1 37 %
christopherh@1 38 % You should have received a copy of the GNU General Public License
christopherh@1 39 % along with the C4DM Toolkit; if not, write to the Free Software
christopherh@1 40 % Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
christopherh@1 41
christopherh@1 42 %
christopherh@1 43 function [midinotes, success,errormessage] = chord2midinotes(chordsymbol, verbose)
christopherh@1 44
christopherh@1 45 % set verbose default to 0
christopherh@1 46 if nargin < 2
christopherh@1 47 verbose = 0;
christopherh@1 48 end
christopherh@1 49
christopherh@1 50 errormessage = '';
christopherh@1 51 mainlist = '';
christopherh@1 52 success = 1;
christopherh@1 53 midinotes = 0;
christopherh@1 54
christopherh@1 55 % parse the chordsymbol
christopherh@1 56 [rootnote,shorthand,interval_list,bass, success, errormessage] = getchordinfo(chordsymbol);
christopherh@1 57
christopherh@1 58 if success
christopherh@1 59
christopherh@1 60
christopherh@1 61 %if 'no chord' then return -1
christopherh@1 62 if rootnote == 'N'
christopherh@1 63
christopherh@1 64 midinotes = -1;
christopherh@1 65 else
christopherh@1 66
christopherh@1 67 % get root's pitchclass with respect to middle C and calculate absolute
christopherh@1 68 % pitch of root in that octave
christopherh@1 69 if success
christopherh@1 70
christopherh@1 71 [rootpitchclass, success, errormessage] = note2pitchclass(rootnote);
christopherh@1 72
christopherh@1 73 % midinote C4 = 60 so add pitchclass to calculate rootpitch
christopherh@1 74 rootpitch = 60 + rootpitchclass;
christopherh@1 75
christopherh@1 76 end
christopherh@1 77
christopherh@1 78
christopherh@1 79 % combine shorthand and interval_list and obtain semitone values for each
christopherh@1 80 % interval
christopherh@1 81 if success
christopherh@1 82
christopherh@1 83 [mainlist, success, errormessage] = addshort2list(shorthand, interval_list);
christopherh@1 84
christopherh@1 85 if success
christopherh@1 86
christopherh@1 87 [semitones,success,errormessage] = intervals2semitones(mainlist);
christopherh@1 88
christopherh@1 89 % add rootpitch to obtain midinotes
christopherh@1 90 chordpitches = semitones + rootpitch;
christopherh@1 91
christopherh@1 92 end
christopherh@1 93
christopherh@1 94 end
christopherh@1 95
christopherh@1 96 % Now find the bass note
christopherh@1 97
christopherh@1 98 if success
christopherh@1 99
christopherh@1 100 % if we haven't got a bass interval then play the rootnote as the
christopherh@1 101 % bassnote
christopherh@1 102 if isempty(bass)
christopherh@1 103 basspitchclass = rootpitchclass;
christopherh@1 104 else
christopherh@1 105 % otherwise convert the bassinterval to a note
christopherh@1 106 [bassnote,success,errormessage] = interval2note(bass, rootnote);
christopherh@1 107
christopherh@1 108 if success
christopherh@1 109
christopherh@1 110 [basspitchclass, success, errormessage] = note2pitchclass(char(bassnote));
christopherh@1 111
christopherh@1 112 end
christopherh@1 113 end
christopherh@1 114
christopherh@1 115 if success
christopherh@1 116 % calculate bass note so that it is in the octave below the
christopherh@1 117 % rest of the chord i.e. add to C3 midinote 48
christopherh@1 118 basspitch = basspitchclass + 48;
christopherh@1 119
christopherh@1 120 % Put midinote list together
christopherh@1 121 midinotes = [basspitch, rootpitch];
christopherh@1 122
christopherh@1 123 for index = 1:length(chordpitches)
christopherh@1 124 if chordpitches(index) ~= rootpitch
christopherh@1 125 midinotes = [midinotes, chordpitches(index)];
christopherh@1 126 end
christopherh@1 127 end
christopherh@1 128 end
christopherh@1 129
christopherh@1 130 end
christopherh@1 131 end
christopherh@1 132
christopherh@1 133 %alter pitches for chords with high roots
christopherh@1 134 if rootpitchclass > 6
christopherh@1 135 midinotes = midinotes-12;
christopherh@1 136 end
christopherh@1 137
christopherh@1 138 end
christopherh@1 139
christopherh@1 140 if success == 0
christopherh@1 141 errormessage = [errormessage sprintf(['Error in chord2midinotes: Couldn''t convert chord"' chordsymbol '"\n'])];
christopherh@1 142 if verbose ==1
christopherh@1 143 fprintf(1,errormessage);
christopherh@1 144 end
christopherh@1 145 end
christopherh@1 146
christopherh@1 147
christopherh@1 148