diff chordtools/chord2pitchclasses.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/chord2pitchclasses.m	Mon May 06 14:43:47 2013 +0100
@@ -0,0 +1,139 @@
+%
+%CHORD2PITCHCLASSES Convert chord symbol to array of pitch classes
+% 
+% [pitchclasses, bassclass, success, errormessage] = chord2pitchclasses(chordsymbol, {inversion}, {verbose})
+% 
+% Converts a chord symbol to an array of integer pitch class values and a
+% bass pitch class value. Pitch class values are referenced w.r.t. C = 0.
+%
+% In the case of the 'no chord' symbol 'N' function returns an empty array
+%
+% Success = 1 if notes extracted from chordsymbol correctly, 0 otherwise.
+% 
+% If optional argument 'verbose' is 1, function prints any errormessage to 
+% the screen.
+% 
+% calls:    chord2notes
+%           notes2pitchclasses
+% 
+% returns:  pitchclasses (array of integers)
+%           bassclass (integer)           
+%           success (boolean)
+%           errormessage (string)
+%
+%
+% 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 [pitchclasses, bassclass, success, errormessage] = chord2pitchclasses(chordsymbol, inversion, verbose)
+
+% set verbose default to 0
+if nargin < 3
+    verbose = 0;
+end
+
+if nargin < 2
+    inversion = 1;
+end
+
+errormessage = '';
+mainlist = '';
+success = 1;
+pitchclasses = [];
+bassclass = '';
+contains = 0;
+ 
+% get constituent notes
+[chordnotes, bassnote, success, errormessage] = chord2notes(chordsymbol);
+
+%convert notes and bass to pitch classes
+if success && ~isempty(chordnotes)
+% find pitchclasses of notes
+[pitchclasses, success, errormessage] = notes2pitchclasses(chordnotes);
+end
+
+if success && ~isempty(bassnote)
+[bassclass, success, errormessage] = note2pitchclass(char(bassnote));
+
+    % check if the bass class is already part of the chord
+    if success
+        ilength = length(pitchclasses);
+
+        contains = 0;
+
+        for index = 1:ilength
+            if bassclass == pitchclasses(index)
+                contains = 1;
+                if inversion
+                    pitchclasses = [pitchclasses(index:end) pitchclasses(1:index-1)];
+                end
+            end
+        end
+
+    end
+
+    %if the note is not already part of the chord then insert it
+    if ~contains
+
+        % if inversion=1 then insert at start of pitchclasses
+        if inversion
+            pitchclasses = [bassclass pitchclasses];
+        else
+            relpc = mod(pitchclasses-pitchclasses(1),12);
+            relbass = mod(bassclass-pitchclasses(1),12);
+            
+            i=0;
+            while(i<ilength)
+                if relbass<relpc(i+1)
+                    break
+                else
+                    i=i+1;
+                end
+            end
+
+            if i==ilength
+                pitchclasses = [pitchclasses bassclass];
+            else
+                pitchclasses = [pitchclasses(1:i) bassclass pitchclasses(i+1:end)];
+            end
+            
+            
+            
+            
+        end
+        
+    end
+
+end    
+    
+
+
+if success == 0
+    errormessage = [errormessage sprintf(['Error in chord2pitchclasses: Couldn''t convert chord "' chordsymbol '"\n'])];   
+    if verbose ==1
+       fprintf(1,errormessage);
+    end
+       
+       
+end  
+    
+    
+    
\ No newline at end of file