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