annotate chordtools/interval2note.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 %INTERVAL2NOTE convert an interval to correctly spelled note w.r.t. a root
christopherh@1 3 %
christopherh@1 4 % [note,success,errormessage] = interval2note(interval, root {verbose})
christopherh@1 5 %
christopherh@1 6 % Converts an interval to a note (string) with root as reference note
christopherh@1 7 % for interval degree.
christopherh@1 8 %
christopherh@1 9 % Success = 1 if interval is converted correctly, 0 otherwise.
christopherh@1 10 %
christopherh@1 11 % If optional argument 'verbose' is 1, function prints any errormessage to
christopherh@1 12 % the screen.
christopherh@1 13 %
christopherh@1 14 % returns: note (string)
christopherh@1 15 % success (boolean)
christopherh@1 16 % errormessage (string)
christopherh@1 17 %
christopherh@1 18 % See also intervals2notes, parsenote.
christopherh@1 19 %
christopherh@1 20 %
christopherh@1 21 % Author: Christopher Harte, March 2009
christopherh@1 22 %
christopherh@1 23 % Copyright: Centre for Digital Music, Queen Mary University of London 2005
christopherh@1 24 %
christopherh@1 25 % This file is part of the C4DM Chord Toolkit V2.0
christopherh@1 26 %
christopherh@1 27 % The C4DM Chord Toolkit is free software; you can redistribute it and/or
christopherh@1 28 % modify it under the terms of the GNU General Public License as published
christopherh@1 29 % by the Free Software Foundation; either version 2 of the License, or
christopherh@1 30 % (at your option) any later version.
christopherh@1 31 %
christopherh@1 32 % The C4DM Chord Toolkit is distributed in the hope that it will be useful,
christopherh@1 33 % but WITHOUT ANY WARRANTY; without even the implied warranty of
christopherh@1 34 % MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
christopherh@1 35 % GNU General Public License for more details.
christopherh@1 36 %
christopherh@1 37 % You should have received a copy of the GNU General Public License
christopherh@1 38 % along with the C4DM Toolkit; if not, write to the Free Software
christopherh@1 39 % Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
christopherh@1 40
christopherh@1 41 %
christopherh@1 42 function [note,success,errormessage] = interval2note(interval, root,verbose)
christopherh@1 43
christopherh@1 44 if nargin < 3
christopherh@1 45 verbose = 0;
christopherh@1 46 end
christopherh@1 47 errormessage = '';
christopherh@1 48
christopherh@1 49 error1 = '';
christopherh@1 50 error2 = '';
christopherh@1 51
christopherh@1 52 note = [];
christopherh@1 53
christopherh@1 54 degreetranslation = [5,0,2,4,-1,1,3,5]; % scale interval translations on line of fifths
christopherh@1 55
christopherh@1 56 fifthpositions = {'F','C','G','D','A','E','B'}; %order of naturals on line of fifths
christopherh@1 57
christopherh@1 58 success = 1;
christopherh@1 59
christopherh@1 60 % parse the root note
christopherh@1 61 [rootnatural,rootaccs, rsuccess, error1] = parsenote(root);
christopherh@1 62
christopherh@1 63 %parse the interval
christopherh@1 64 [degree, intervalaccs, present, dsuccess, error2] = parseinterval(interval);
christopherh@1 65
christopherh@1 66 % if parsing symbols was successful
christopherh@1 67 if(rsuccess && dsuccess);
christopherh@1 68
christopherh@1 69 switch(rootnatural) % find root natural position on line of fifths
christopherh@1 70
christopherh@1 71 case 'F'
christopherh@1 72 fifthindex = 0;
christopherh@1 73 case 'C'
christopherh@1 74 fifthindex = 1;
christopherh@1 75 case 'G'
christopherh@1 76 fifthindex = 2;
christopherh@1 77 case 'D'
christopherh@1 78 fifthindex = 3;
christopherh@1 79 case 'A'
christopherh@1 80 fifthindex = 4;
christopherh@1 81 case 'E'
christopherh@1 82 fifthindex = 5;
christopherh@1 83 case 'B'
christopherh@1 84 fifthindex = 6;
christopherh@1 85
christopherh@1 86 end
christopherh@1 87
christopherh@1 88 %locate enharmonic root on line of fifths (modulo 6 arithmetic)
christopherh@1 89
christopherh@1 90 fifthoffset = rootaccs*7;
christopherh@1 91
christopherh@1 92 fifthindex = fifthindex + fifthoffset;
christopherh@1 93
christopherh@1 94
christopherh@1 95 % calculate degree translation on line of fifths (add 1 to account
christopherh@1 96 % for matlab referencing of array elements...
christopherh@1 97 degreeoffset = degreetranslation(mod(degree,7)+1);
christopherh@1 98 finalposition = fifthindex + degreeoffset;
christopherh@1 99
christopherh@1 100
christopherh@1 101 naturalvalue = mod(finalposition,7);
christopherh@1 102
christopherh@1 103
christopherh@1 104 % calculate number of accidentals
christopherh@1 105 if finalposition <0
christopherh@1 106 %if final position is negative then calculate number of flats
christopherh@1 107 % remembering to include the extra first flat (-1)
christopherh@1 108 accidentals = fix((finalposition+1)/7) + intervalaccs -1;
christopherh@1 109
christopherh@1 110 else
christopherh@1 111 % note is a natural or has a number of sharps
christopherh@1 112 accidentals = fix(finalposition/7) + intervalaccs;
christopherh@1 113 end
christopherh@1 114
christopherh@1 115 note = fifthpositions(naturalvalue+1);
christopherh@1 116
christopherh@1 117 if accidentals > 0
christopherh@1 118
christopherh@1 119 for i=1:accidentals
christopherh@1 120
christopherh@1 121 note = strcat(note, '#');
christopherh@1 122
christopherh@1 123 end
christopherh@1 124
christopherh@1 125 elseif accidentals <=0
christopherh@1 126
christopherh@1 127 for i=1:abs(accidentals)
christopherh@1 128
christopherh@1 129 note = strcat(note, 'b');
christopherh@1 130
christopherh@1 131 end
christopherh@1 132 end
christopherh@1 133
christopherh@1 134 else
christopherh@1 135
christopherh@1 136 success=0;
christopherh@1 137
christopherh@1 138 end
christopherh@1 139
christopherh@1 140 if(success==0) % correct interval therefore return success = 1
christopherh@1 141 % if not an integer then the interval string is incorrect
christopherh@1 142 errormessage = [error1 error2 sprintf(['Error in interval2note: Unrecognised interval "' interval '" or root "' root '"\n'])];
christopherh@1 143
christopherh@1 144 if verbose == 1
christopherh@1 145 fprintf(1,errormessage);
christopherh@1 146 end
christopherh@1 147
christopherh@1 148 end
christopherh@1 149
christopherh@1 150
christopherh@1 151
christopherh@1 152
christopherh@1 153
christopherh@1 154
christopherh@1 155
christopherh@1 156
christopherh@1 157