view chordtools/addshort2list.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 source
%
%ADDSHORT2LIST Combine intervals of a shorthand with another interval list
% 
% [fullinterval_list, success, errormessage] = addshort2list(shorthand, interval_list, {verbose})
% 
% Combine shorthand and interval_list to form full interval list for a chord.  
% Takes into account the omit interval character '*' in producing final list.
% 
% Success = 1 if shorthand and interval_list combined correctly, 0 otherwise.
% 
% If optional argument 'verbose' is 1, function prints any errormessage to 
% the screen.
% 
% returns:  fullinterval_list (string)
%           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 [fullinterval_list, success, errormessage] = addshort2list(shorthand, interval_list, verbose)

% set verbose default to 0
if nargin < 3
    verbose = 0;
end

errormessage = '';

% initialise variables
fullinterval_list = '';
shortdegrees = [];
intervaldegrees = [];
addlist = [];
success = 1;

% convert shorthand string to list of intervals
[shortintervals, ok, errors]  = shorthand2intervals(shorthand);

if ok
    % if that works convert the shorthand intervals and interval_list to
    % degrees and accidentals
    
    % add the implied 1 degree to the shorthand list
 %   if isempty(shortintervals)
 %       rootdegree = '1'; % no comma if shorthand is empty
 %   else
 %       rootdegree = '1,'; % comma shows that there are further intervals after this one
 %   end
        
    
    [shortdegrees, ok, errors] = parseintervals(shortintervals); 

    if (ok)

      
      % this part of the algorithm finds all the present degrees and sorts
      % them into ascending numeric order
  
      % First put the shorthand degrees into the full list
      
      [ilength,y] = size(shortdegrees);
   
      for index = 1:ilength
          % convert degree to a semitone value  
          semitone = degree2semitone(shortdegrees(index,1),shortdegrees(index,2));
          
          % Using the semitone value as an index will find redundant
          % intervals and also sort them in number order. Multiplying the degree by its
          % presence value leaves either the degree value or 0. Any element 
          % in the array with degree of 0 will be discarded later
          % We add 1 to the semitone value to handle the '1'  or 'unity'
          % degree that would return a 0 semitone value
          
          % record the degree 
          addlist(semitone+1,1) = shortdegrees(index,1) .* shortdegrees(index,3);
          % record the accidentals as well
          addlist(semitone+1,2) = shortdegrees(index,2);
          
      end
    else
        success = 0;
    end

    [intervaldegrees, ok2, errors] = parseintervals(interval_list);
    ok = ok && ok2;
    
    if ok
      % Now add the intervals from the interval_list taking redundant and 
      % ommitted intervals into account 
      
      [ilength,y] = size(intervaldegrees);
   
      for index = 1:ilength
          % convert degree to a semitone value  
          [semitone, ok, errors] = degree2semitone(intervaldegrees(index,1),intervaldegrees(index,2));
          
          if ok && semitone >= 0
          % record the degree 
          addlist(semitone+1,1) = intervaldegrees(index,1) .* intervaldegrees(index,3);
          % record the accidentals as well
          addlist(semitone+1,2) = intervaldegrees(index,2);
          
          else
             success = 0; 
          end
          
      end
    else
        success=0;
    end
    
    
    % now create fullinterval_list
    if ok

        [ilength,y] = size(addlist);

        for index = 1:ilength

            % if there is a non zero degree in this element, convert 
            % it to an interval symbol and add it to the output list
            if(addlist(index,1) ~=0)

                intervalsymbol = degree2interval(addlist(index,1),addlist(index,2));

                if isempty(fullinterval_list)
                    % if this is the first interval then start list
                    fullinterval_list = intervalsymbol; 
                else
                    % add to list with a separating comma
                    fullinterval_list = [fullinterval_list ',' intervalsymbol];
                end
            end
        end
    else

        success = 0;
        
    end
    
else
    
    success = 0;
    
end


if success == 0
    errormessage = [errors sprintf(['Error in addshort2list: unsuccessful '...
                    'combination  "' shorthand '" and "' interval_list '"\n'])];
    
    if verbose ==1
       fprintf(1,errormessage);
    end
       
       
end