annotate _chordtools/chord2midinotes.m @ 9:4ea6619cb3f5 tip

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