diff chordtools/interval2note.m @ 1:8973548174c1 tip

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