matthiasm@8: % matthiasm@8: %DEGREE2NOTE convert a degree to correctly spelled note w.r.t. a root matthiasm@8: % matthiasm@8: % [note,success,errormessage] = degree2note(degree, root {verbose}) matthiasm@8: % matthiasm@8: % Converts a degree to a note (string) with root as reference note matthiasm@8: % for degree interval. matthiasm@8: % matthiasm@8: % Success = 1 if degree is converted correctly, 0 otherwise. matthiasm@8: % matthiasm@8: % If optional argument 'verbose' is 1, function prints any errormessage to matthiasm@8: % the screen. matthiasm@8: % matthiasm@8: % returns: note (string) matthiasm@8: % success (boolean) matthiasm@8: % errormessage (string) matthiasm@8: % matthiasm@8: % See also degrees2notes, parsenote. matthiasm@8: % matthiasm@8: % matthiasm@8: % Author: Christopher Harte, August 2005 matthiasm@8: % matthiasm@8: % Copyright: Centre for Digital Music, Queen Mary University of London 2005 matthiasm@8: % matthiasm@8: % This file is part of the C4DM Chord Toolkit. matthiasm@8: % matthiasm@8: % The C4DM Chord Toolkit is free software; you can redistribute it and/or matthiasm@8: % modify it under the terms of the GNU General Public License as published matthiasm@8: % by the Free Software Foundation; either version 2 of the License, or matthiasm@8: % (at your option) any later version. matthiasm@8: % matthiasm@8: % The C4DM Chord Toolkit is distributed in the hope that it will be useful, matthiasm@8: % but WITHOUT ANY WARRANTY; without even the implied warranty of matthiasm@8: % MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the matthiasm@8: % GNU General Public License for more details. matthiasm@8: % matthiasm@8: % You should have received a copy of the GNU General Public License matthiasm@8: % along with the C4DM Toolkit; if not, write to the Free Software matthiasm@8: % Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA matthiasm@8: matthiasm@8: % matthiasm@8: function [note,success,errormessage] = degree2note(degree, root,verbose) matthiasm@8: matthiasm@8: if nargin < 3 matthiasm@8: verbose = 0; matthiasm@8: end matthiasm@8: errormessage = ''; matthiasm@8: matthiasm@8: error1 = ''; matthiasm@8: error2 = ''; matthiasm@8: matthiasm@8: note = []; matthiasm@8: matthiasm@8: intervaltranslation = [5,0,2,4,-1,1,3,5]; % scale degree translations on line of fifths matthiasm@8: matthiasm@8: fifthpositions = {'F','C','G','D','A','E','B'}; %order of naturals on line of fifths matthiasm@8: matthiasm@8: success = 1; matthiasm@8: matthiasm@8: % parse the root note matthiasm@8: [rootnatural,rootaccs, rsuccess, error1] = parsenote(root); matthiasm@8: matthiasm@8: %parse the degree matthiasm@8: [interval, degreeaccs, present, dsuccess, error2] = parsedegree(degree); matthiasm@8: matthiasm@8: % if parsing symbols was successful matthiasm@8: if(rsuccess && dsuccess); matthiasm@8: matthiasm@8: switch(rootnatural) % find root natural position on line of fifths matthiasm@8: matthiasm@8: case 'F' matthiasm@8: fifthindex = 0; matthiasm@8: case 'C' matthiasm@8: fifthindex = 1; matthiasm@8: case 'G' matthiasm@8: fifthindex = 2; matthiasm@8: case 'D' matthiasm@8: fifthindex = 3; matthiasm@8: case 'A' matthiasm@8: fifthindex = 4; matthiasm@8: case 'E' matthiasm@8: fifthindex = 5; matthiasm@8: case 'B' matthiasm@8: fifthindex = 6; matthiasm@8: matthiasm@8: end matthiasm@8: matthiasm@8: %locate enharmonic root on line of fifths (modulo 6 arithmetic) matthiasm@8: matthiasm@8: fifthoffset = rootaccs*7; matthiasm@8: matthiasm@8: fifthindex = fifthindex + fifthoffset; matthiasm@8: matthiasm@8: matthiasm@8: % calculate interval translation on line of fifths (add 1 to account matthiasm@8: % for matlab referencing of array elements... matthiasm@8: intervaloffset = intervaltranslation(mod(interval,7)+1); matthiasm@8: finalposition = fifthindex + intervaloffset; matthiasm@8: matthiasm@8: matthiasm@8: naturalvalue = mod(finalposition,7); matthiasm@8: matthiasm@8: matthiasm@8: % calculate number of accidentals matthiasm@8: if finalposition <0 matthiasm@8: %if final position is negative then calculate number of flats matthiasm@8: % remembering to include the extra first flat (-1) matthiasm@8: accidentals = fix((finalposition+1)/7) + degreeaccs -1; matthiasm@8: matthiasm@8: else matthiasm@8: % note is a natural or has a number of sharps matthiasm@8: accidentals = fix(finalposition/7) + degreeaccs; matthiasm@8: end matthiasm@8: matthiasm@8: note = fifthpositions(naturalvalue+1); matthiasm@8: matthiasm@8: if accidentals > 0 matthiasm@8: matthiasm@8: for i=1:accidentals matthiasm@8: matthiasm@8: note = strcat(note, '#'); matthiasm@8: matthiasm@8: end matthiasm@8: matthiasm@8: elseif accidentals <=0 matthiasm@8: matthiasm@8: for i=1:abs(accidentals) matthiasm@8: matthiasm@8: note = strcat(note, 'b'); matthiasm@8: matthiasm@8: end matthiasm@8: end matthiasm@8: matthiasm@8: else matthiasm@8: matthiasm@8: success=0; matthiasm@8: matthiasm@8: end matthiasm@8: matthiasm@8: if(success==0) % correct degree therefore return success = 1 matthiasm@8: % if not an integer then the degree string is incorrect matthiasm@8: errormessage = [error1 error2 sprintf(['Error in degree2note: Unrecognised degree "' degree '" or root "' root '"\n'])]; matthiasm@8: matthiasm@8: if verbose == 1 matthiasm@8: fprintf(1,errormessage); matthiasm@8: end matthiasm@8: matthiasm@8: end matthiasm@8: matthiasm@8: matthiasm@8: matthiasm@8: matthiasm@8: matthiasm@8: matthiasm@8: matthiasm@8: matthiasm@8: