annotate chordtools/chord2pitchclasses.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 %CHORD2PITCHCLASSES Convert chord symbol to array of pitch classes
christopherh@1 3 %
christopherh@1 4 % [pitchclasses, bassclass, success, errormessage] = chord2pitchclasses(chordsymbol, {inversion}, {verbose})
christopherh@1 5 %
christopherh@1 6 % Converts a chord symbol to an array of integer pitch class values and a
christopherh@1 7 % bass pitch class value. Pitch class values are referenced w.r.t. C = 0.
christopherh@1 8 %
christopherh@1 9 % In the case of the 'no chord' symbol 'N' function returns an empty array
christopherh@1 10 %
christopherh@1 11 % Success = 1 if notes extracted from chordsymbol correctly, 0 otherwise.
christopherh@1 12 %
christopherh@1 13 % If optional argument 'verbose' is 1, function prints any errormessage to
christopherh@1 14 % the screen.
christopherh@1 15 %
christopherh@1 16 % calls: chord2notes
christopherh@1 17 % notes2pitchclasses
christopherh@1 18 %
christopherh@1 19 % returns: pitchclasses (array of integers)
christopherh@1 20 % bassclass (integer)
christopherh@1 21 % success (boolean)
christopherh@1 22 % errormessage (string)
christopherh@1 23 %
christopherh@1 24 %
christopherh@1 25 % Author: Christopher Harte, March 2009
christopherh@1 26 %
christopherh@1 27 % Copyright: Centre for Digital Music, Queen Mary University of London 2005
christopherh@1 28 %
christopherh@1 29 % This file is part of the C4DM Chord Toolkit V2.0
christopherh@1 30 %
christopherh@1 31 % The C4DM Chord Toolkit is free software; you can redistribute it and/or
christopherh@1 32 % modify it under the terms of the GNU General Public License as published
christopherh@1 33 % by the Free Software Foundation; either version 2 of the License, or
christopherh@1 34 % (at your option) any later version.
christopherh@1 35 %
christopherh@1 36 % The C4DM Chord Toolkit is distributed in the hope that it will be useful,
christopherh@1 37 % but WITHOUT ANY WARRANTY; without even the implied warranty of
christopherh@1 38 % MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
christopherh@1 39 % GNU General Public License for more details.
christopherh@1 40 %
christopherh@1 41 % You should have received a copy of the GNU General Public License
christopherh@1 42 % along with the C4DM Toolkit; if not, write to the Free Software
christopherh@1 43 % Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
christopherh@1 44
christopherh@1 45 %
christopherh@1 46 function [pitchclasses, bassclass, success, errormessage] = chord2pitchclasses(chordsymbol, inversion, verbose)
christopherh@1 47
christopherh@1 48 % set verbose default to 0
christopherh@1 49 if nargin < 3
christopherh@1 50 verbose = 0;
christopherh@1 51 end
christopherh@1 52
christopherh@1 53 if nargin < 2
christopherh@1 54 inversion = 1;
christopherh@1 55 end
christopherh@1 56
christopherh@1 57 errormessage = '';
christopherh@1 58 mainlist = '';
christopherh@1 59 success = 1;
christopherh@1 60 pitchclasses = [];
christopherh@1 61 bassclass = '';
christopherh@1 62 contains = 0;
christopherh@1 63
christopherh@1 64 % get constituent notes
christopherh@1 65 [chordnotes, bassnote, success, errormessage] = chord2notes(chordsymbol);
christopherh@1 66
christopherh@1 67 %convert notes and bass to pitch classes
christopherh@1 68 if success && ~isempty(chordnotes)
christopherh@1 69 % find pitchclasses of notes
christopherh@1 70 [pitchclasses, success, errormessage] = notes2pitchclasses(chordnotes);
christopherh@1 71 end
christopherh@1 72
christopherh@1 73 if success && ~isempty(bassnote)
christopherh@1 74 [bassclass, success, errormessage] = note2pitchclass(char(bassnote));
christopherh@1 75
christopherh@1 76 % check if the bass class is already part of the chord
christopherh@1 77 if success
christopherh@1 78 ilength = length(pitchclasses);
christopherh@1 79
christopherh@1 80 contains = 0;
christopherh@1 81
christopherh@1 82 for index = 1:ilength
christopherh@1 83 if bassclass == pitchclasses(index)
christopherh@1 84 contains = 1;
christopherh@1 85 if inversion
christopherh@1 86 pitchclasses = [pitchclasses(index:end) pitchclasses(1:index-1)];
christopherh@1 87 end
christopherh@1 88 end
christopherh@1 89 end
christopherh@1 90
christopherh@1 91 end
christopherh@1 92
christopherh@1 93 %if the note is not already part of the chord then insert it
christopherh@1 94 if ~contains
christopherh@1 95
christopherh@1 96 % if inversion=1 then insert at start of pitchclasses
christopherh@1 97 if inversion
christopherh@1 98 pitchclasses = [bassclass pitchclasses];
christopherh@1 99 else
christopherh@1 100 relpc = mod(pitchclasses-pitchclasses(1),12);
christopherh@1 101 relbass = mod(bassclass-pitchclasses(1),12);
christopherh@1 102
christopherh@1 103 i=0;
christopherh@1 104 while(i<ilength)
christopherh@1 105 if relbass<relpc(i+1)
christopherh@1 106 break
christopherh@1 107 else
christopherh@1 108 i=i+1;
christopherh@1 109 end
christopherh@1 110 end
christopherh@1 111
christopherh@1 112 if i==ilength
christopherh@1 113 pitchclasses = [pitchclasses bassclass];
christopherh@1 114 else
christopherh@1 115 pitchclasses = [pitchclasses(1:i) bassclass pitchclasses(i+1:end)];
christopherh@1 116 end
christopherh@1 117
christopherh@1 118
christopherh@1 119
christopherh@1 120
christopherh@1 121 end
christopherh@1 122
christopherh@1 123 end
christopherh@1 124
christopherh@1 125 end
christopherh@1 126
christopherh@1 127
christopherh@1 128
christopherh@1 129 if success == 0
christopherh@1 130 errormessage = [errormessage sprintf(['Error in chord2pitchclasses: Couldn''t convert chord "' chordsymbol '"\n'])];
christopherh@1 131 if verbose ==1
christopherh@1 132 fprintf(1,errormessage);
christopherh@1 133 end
christopherh@1 134
christopherh@1 135
christopherh@1 136 end
christopherh@1 137
christopherh@1 138
christopherh@1 139