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