annotate chordtools/degree2semitone.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 % DEGREE2SEMITONE
christopherh@1 3 %
christopherh@1 4 % [semitone, success, errormessage] = degree2semitone(degree, accidental, {verbose})
christopherh@1 5 %
christopherh@1 6 % Convert a degree and accidentals pair to a semitone value
christopherh@1 7 %
christopherh@1 8 % Success = 1 if degree converted correctly, 0 otherwise.
christopherh@1 9 %
christopherh@1 10 % If optional argument 'verbose' is 1, function prints any errormessage to
christopherh@1 11 % the screen.
christopherh@1 12 %
christopherh@1 13 % returns: semitone (string)
christopherh@1 14 % success (boolean)
christopherh@1 15 % errormessage (string)
christopherh@1 16 %
christopherh@1 17 % Author: Christopher Harte, March 2009
christopherh@1 18 %
christopherh@1 19 % Copyright: Centre for Digital Music, Queen Mary University of London 2005
christopherh@1 20 %
christopherh@1 21 % This file is part of the C4DM Chord Toolkit V2.0
christopherh@1 22 %
christopherh@1 23 % The C4DM Chord Toolkit is free software; you can redistribute it and/or
christopherh@1 24 % modify it under the terms of the GNU General Public License as published
christopherh@1 25 % by the Free Software Foundation; either version 2 of the License, or
christopherh@1 26 % (at your option) any later version.
christopherh@1 27 %
christopherh@1 28 % The C4DM Chord Toolkit is distributed in the hope that it will be useful,
christopherh@1 29 % but WITHOUT ANY WARRANTY; without even the implied warranty of
christopherh@1 30 % MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
christopherh@1 31 % GNU General Public License for more details.
christopherh@1 32 %
christopherh@1 33 % You should have received a copy of the GNU General Public License
christopherh@1 34 % along with the C4DM Toolkit; if not, write to the Free Software
christopherh@1 35 % Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
christopherh@1 36
christopherh@1 37 %
christopherh@1 38 function [semitone, success, errormessage] = degree2semitone(degree, accidentals, verbose)
christopherh@1 39
christopherh@1 40 if nargin < 3
christopherh@1 41 % verbose defaults 0
christopherh@1 42 verbose = 0;
christopherh@1 43 end
christopherh@1 44
christopherh@1 45 errormessage = '';
christopherh@1 46
christopherh@1 47 % semitone equivalents for degrees
christopherh@1 48 % degree 1,2,3,4,5,6,7
christopherh@1 49 %semitonetranslation = [0,2,4,5,7,9,11];
christopherh@1 50
christopherh@1 51 semitone = 0;
christopherh@1 52
christopherh@1 53 success = 1;
christopherh@1 54
christopherh@1 55 if (degree > 0) && (degree < 50)
christopherh@1 56
christopherh@1 57 % semitone value is the number of semitones equivalent to the degree
christopherh@1 58 % added to the number of accidentals (sharps are positive, flats are
christopherh@1 59 % negative) and the number of octaves above the reference note to
christopherh@1 60 % account for extensions
christopherh@1 61
christopherh@1 62 %semitone = semitonetranslation(mod(degree,8) + fix(degree/8)) + accidentals + 12*fix(degree/8);
christopherh@1 63
christopherh@1 64 semitone = 2*mod(degree-1,7) - floor((mod(degree-1,7)+1)/4) + 12*floor((degree-1)/7)+ accidentals;
christopherh@1 65
christopherh@1 66 else
christopherh@1 67 success = 0;
christopherh@1 68
christopherh@1 69 errormessage = sprintf('Error in degree2semitone: out of range degree "%d"\n', degree);
christopherh@1 70
christopherh@1 71 if verbose == 1
christopherh@1 72 fprintf(1,errormessage);
christopherh@1 73 end
christopherh@1 74 end
christopherh@1 75