annotate chordtools/note2interval.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 %NOTE2INTERVAL convert a note to an interval w.r.t. a given root
christopherh@1 3 %
christopherh@1 4 % [interval,success,errormessage] = note2interval(note,root,{octave},{verbose})
christopherh@1 5 %
christopherh@1 6 % Converts a note string to an interval with respect to a given root note.
christopherh@1 7 % Optional value octave gives octave offset to account for extended
christopherh@1 8 % degrees.
christopherh@1 9 %
christopherh@1 10 % Success = 1 if note converted correctly, 0 otherwise.
christopherh@1 11 %
christopherh@1 12 % Optional argument octave defaults to 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 % calls: note2fifthposition
christopherh@1 18 %
christopherh@1 19 % returns: interval (string)
christopherh@1 20 % success (boolean)
christopherh@1 21 % errormessage (string)
christopherh@1 22 %
christopherh@1 23 %
christopherh@1 24 % Author: Christopher Harte, March 2009
christopherh@1 25 %
christopherh@1 26 % Copyright: Centre for Digital Music, Queen Mary University of London 2005
christopherh@1 27 %
christopherh@1 28 % This file is part of the C4DM Chord Toolkit V2.0
christopherh@1 29 %
christopherh@1 30 % The C4DM Chord Toolkit is free software; you can redistribute it and/or
christopherh@1 31 % modify it under the terms of the GNU General Public License as published
christopherh@1 32 % by the Free Software Foundation; either version 2 of the License, or
christopherh@1 33 % (at your option) any later version.
christopherh@1 34 %
christopherh@1 35 % The C4DM Chord Toolkit is distributed in the hope that it will be useful,
christopherh@1 36 % but WITHOUT ANY WARRANTY; without even the implied warranty of
christopherh@1 37 % MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
christopherh@1 38 % GNU General Public License for more details.
christopherh@1 39 %
christopherh@1 40 % You should have received a copy of the GNU General Public License
christopherh@1 41 % along with the C4DM Toolkit; if not, write to the Free Software
christopherh@1 42 % Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
christopherh@1 43
christopherh@1 44 %
christopherh@1 45 function [interval,success,errormessage] = note2interval(note, root,octave, verbose)
christopherh@1 46
christopherh@1 47 % set verbose default to 0
christopherh@1 48 if nargin < 3
christopherh@1 49 octave = 0;
christopherh@1 50 verbose = 0;
christopherh@1 51
christopherh@1 52 elseif nargin < 4
christopherh@1 53 verbose = 0;
christopherh@1 54 end
christopherh@1 55
christopherh@1 56 errormessage = '';
christopherh@1 57 success = 1;
christopherh@1 58 interval = '';
christopherh@1 59
christopherh@1 60 % degree translations on the line of fifths
christopherh@1 61 fifthtranslations = [1,5,2,6,3,7,4];
christopherh@1 62
christopherh@1 63
christopherh@1 64 % get note and root natural position and accidentals on line of fifths
christopherh@1 65 [noteposition, success1,error1] = note2fifthposition(note);
christopherh@1 66
christopherh@1 67 [rootposition, success2,error2] = note2fifthposition(root);
christopherh@1 68
christopherh@1 69 if success1 && success2
christopherh@1 70
christopherh@1 71 % take the difference between the two note positions for relative positions
christopherh@1 72 % of notes with respect to one and other
christopherh@1 73 fifthsdifference = noteposition - rootposition + 1;
christopherh@1 74
christopherh@1 75 % natural difference on line of fifths
christopherh@1 76 fifthsdegree = mod((fifthsdifference-1),7);
christopherh@1 77
christopherh@1 78 i=0;
christopherh@1 79
christopherh@1 80 % find number of accidentals apart on line of fifths
christopherh@1 81 if fifthsdifference < 0 % if above 0 then either natural or sharp
christopherh@1 82
christopherh@1 83 %if final position is negative then calculate number of flats
christopherh@1 84 % remembering to include the extra first flat (-1)
christopherh@1 85 accidentals = fix((fifthsdifference+1)/7) -1;
christopherh@1 86
christopherh@1 87 else
christopherh@1 88 % note is a natural or has a number of sharps
christopherh@1 89 accidentals = fix(fifthsdifference/7);
christopherh@1 90
christopherh@1 91 end
christopherh@1 92
christopherh@1 93
christopherh@1 94 % put the required number of sharps or flats into the output string
christopherh@1 95 if accidentals > 0
christopherh@1 96
christopherh@1 97 for i=1:accidentals
christopherh@1 98
christopherh@1 99 interval = ['#' interval];
christopherh@1 100
christopherh@1 101 end
christopherh@1 102
christopherh@1 103 elseif accidentals <=0
christopherh@1 104
christopherh@1 105 for i=1:abs(accidentals)
christopherh@1 106
christopherh@1 107 interval = ['b' interval];
christopherh@1 108
christopherh@1 109 end
christopherh@1 110 end
christopherh@1 111
christopherh@1 112
christopherh@1 113 % find degree value from translation array
christopherh@1 114 degree = fifthtranslations(fifthsdegree+1);
christopherh@1 115
christopherh@1 116 if octave >= 0
christopherh@1 117 degree = degree + 7.*octave;
christopherh@1 118 else
christopherh@1 119 success = 0;
christopherh@1 120 errormessage = 'Error in note2interval: Octave argument is negative';
christopherh@1 121 end
christopherh@1 122
christopherh@1 123
christopherh@1 124
christopherh@1 125 interval = [interval num2str(degree)];
christopherh@1 126
christopherh@1 127 else
christopherh@1 128 success = 0;
christopherh@1 129 errormessage = [error1 error2];
christopherh@1 130 end
christopherh@1 131
christopherh@1 132 if (success == 0) && (verbose == 1)
christopherh@1 133 fprintf(1,errormessage);
christopherh@1 134 end
christopherh@1 135
christopherh@1 136