christopherh@1: % christopherh@1: %NOTE2INTERVAL convert a note to an interval w.r.t. a given root christopherh@1: % christopherh@1: % [interval,success,errormessage] = note2interval(note,root,{octave},{verbose}) christopherh@1: % christopherh@1: % Converts a note string to an interval with respect to a given root note. christopherh@1: % Optional value octave gives octave offset to account for extended christopherh@1: % degrees. christopherh@1: % christopherh@1: % Success = 1 if note converted correctly, 0 otherwise. christopherh@1: % christopherh@1: % Optional argument octave defaults to 0. christopherh@1: % christopherh@1: % If optional argument 'verbose' is 1, function prints any errormessage to christopherh@1: % the screen. christopherh@1: % christopherh@1: % calls: note2fifthposition christopherh@1: % christopherh@1: % returns: interval (string) christopherh@1: % success (boolean) christopherh@1: % errormessage (string) christopherh@1: % christopherh@1: % christopherh@1: % Author: Christopher Harte, March 2009 christopherh@1: % christopherh@1: % Copyright: Centre for Digital Music, Queen Mary University of London 2005 christopherh@1: % christopherh@1: % This file is part of the C4DM Chord Toolkit V2.0 christopherh@1: % christopherh@1: % The C4DM Chord Toolkit is free software; you can redistribute it and/or christopherh@1: % modify it under the terms of the GNU General Public License as published christopherh@1: % by the Free Software Foundation; either version 2 of the License, or christopherh@1: % (at your option) any later version. christopherh@1: % christopherh@1: % The C4DM Chord Toolkit is distributed in the hope that it will be useful, christopherh@1: % but WITHOUT ANY WARRANTY; without even the implied warranty of christopherh@1: % MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the christopherh@1: % GNU General Public License for more details. christopherh@1: % christopherh@1: % You should have received a copy of the GNU General Public License christopherh@1: % along with the C4DM Toolkit; if not, write to the Free Software christopherh@1: % Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA christopherh@1: christopherh@1: % christopherh@1: function [interval,success,errormessage] = note2interval(note, root,octave, verbose) christopherh@1: christopherh@1: % set verbose default to 0 christopherh@1: if nargin < 3 christopherh@1: octave = 0; christopherh@1: verbose = 0; christopherh@1: christopherh@1: elseif nargin < 4 christopherh@1: verbose = 0; christopherh@1: end christopherh@1: christopherh@1: errormessage = ''; christopherh@1: success = 1; christopherh@1: interval = ''; christopherh@1: christopherh@1: % degree translations on the line of fifths christopherh@1: fifthtranslations = [1,5,2,6,3,7,4]; christopherh@1: christopherh@1: christopherh@1: % get note and root natural position and accidentals on line of fifths christopherh@1: [noteposition, success1,error1] = note2fifthposition(note); christopherh@1: christopherh@1: [rootposition, success2,error2] = note2fifthposition(root); christopherh@1: christopherh@1: if success1 && success2 christopherh@1: christopherh@1: % take the difference between the two note positions for relative positions christopherh@1: % of notes with respect to one and other christopherh@1: fifthsdifference = noteposition - rootposition + 1; christopherh@1: christopherh@1: % natural difference on line of fifths christopherh@1: fifthsdegree = mod((fifthsdifference-1),7); christopherh@1: christopherh@1: i=0; christopherh@1: christopherh@1: % find number of accidentals apart on line of fifths christopherh@1: if fifthsdifference < 0 % if above 0 then either natural or sharp christopherh@1: christopherh@1: %if final position is negative then calculate number of flats christopherh@1: % remembering to include the extra first flat (-1) christopherh@1: accidentals = fix((fifthsdifference+1)/7) -1; christopherh@1: christopherh@1: else christopherh@1: % note is a natural or has a number of sharps christopherh@1: accidentals = fix(fifthsdifference/7); christopherh@1: christopherh@1: end christopherh@1: christopherh@1: christopherh@1: % put the required number of sharps or flats into the output string christopherh@1: if accidentals > 0 christopherh@1: christopherh@1: for i=1:accidentals christopherh@1: christopherh@1: interval = ['#' interval]; christopherh@1: christopherh@1: end christopherh@1: christopherh@1: elseif accidentals <=0 christopherh@1: christopherh@1: for i=1:abs(accidentals) christopherh@1: christopherh@1: interval = ['b' interval]; christopherh@1: christopherh@1: end christopherh@1: end christopherh@1: christopherh@1: christopherh@1: % find degree value from translation array christopherh@1: degree = fifthtranslations(fifthsdegree+1); christopherh@1: christopherh@1: if octave >= 0 christopherh@1: degree = degree + 7.*octave; christopherh@1: else christopherh@1: success = 0; christopherh@1: errormessage = 'Error in note2interval: Octave argument is negative'; christopherh@1: end christopherh@1: christopherh@1: christopherh@1: christopherh@1: interval = [interval num2str(degree)]; christopherh@1: christopherh@1: else christopherh@1: success = 0; christopherh@1: errormessage = [error1 error2]; christopherh@1: end christopherh@1: christopherh@1: if (success == 0) && (verbose == 1) christopherh@1: fprintf(1,errormessage); christopherh@1: end christopherh@1: christopherh@1: