christopherh@1: % christopherh@1: %CHORD2NOTES Convert chord symbol to list of constituent notes christopherh@1: % christopherh@1: % [chordnotes, bassnote, success, errormessage] = chord2notes(chordsymbol, {verbose}) christopherh@1: % christopherh@1: % Converts a chord symbol to a cell array of note strings and a bassnote string. christopherh@1: % christopherh@1: % In the case of the 'no chord' symbol 'N' function returns an empty array christopherh@1: % christopherh@1: % Success = 1 if notes extracted from chordsymbol correctly, 0 otherwise. christopherh@1: % christopherh@1: % If optional argument 'verbose' is 1, function prints any errormessage to christopherh@1: % the screen. christopherh@1: % christopherh@1: % calls: getnoteinfo christopherh@1: % parsenote christopherh@1: % addshort2list christopherh@1: % interval2note christopherh@1: % christopherh@1: % returns: chordnotes (cell array of note strings) christopherh@1: % bassnote (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 [chordnotes, bassnote, success, errormessage] = chord2notes(chordsymbol, inversion, verbose) christopherh@1: christopherh@1: %set default inversion to 0 christopherh@1: if nargin < 2 christopherh@1: inversion = 0; christopherh@1: end christopherh@1: christopherh@1: % set verbose default to 0 christopherh@1: if nargin < 3 christopherh@1: verbose = 0; christopherh@1: end christopherh@1: christopherh@1: errormessage = ''; christopherh@1: mainlist = ''; christopherh@1: success = 1; christopherh@1: chordnotes = []; christopherh@1: bassnote = ''; christopherh@1: christopherh@1: % parse the chordsymbol christopherh@1: [rootnote,shorthand,interval_list,bass, success, errormessage] = getchordinfo(chordsymbol); christopherh@1: christopherh@1: christopherh@1: christopherh@1: christopherh@1: %if 'no chord' then return N christopherh@1: if (success == 1) christopherh@1: christopherh@1: christopherh@1: christopherh@1: if rootnote == 'N' christopherh@1: christopherh@1: chordnotes = {}; christopherh@1: else christopherh@1: christopherh@1: christopherh@1: % combine shorthand and interval_list and obtain note names for each christopherh@1: % interval christopherh@1: if success christopherh@1: christopherh@1: [mainlist, success, errormessage] = addshort2list(shorthand, interval_list); christopherh@1: christopherh@1: if success christopherh@1: christopherh@1: % convert list of intervals to list of notes christopherh@1: [chordnotes,success,errormessage] = intervals2notes(mainlist,rootnote); christopherh@1: christopherh@1: end christopherh@1: christopherh@1: end christopherh@1: christopherh@1: % Now find the bass note christopherh@1: christopherh@1: if success christopherh@1: christopherh@1: if ~isempty(bass) christopherh@1: christopherh@1: [bassnote,success,errormessage] = interval2note(bass, rootnote); christopherh@1: christopherh@1: if inversion christopherh@1: % if the bass note is a member of the chord christopherh@1: if length(intersect(bassnote,chordnotes))==1 christopherh@1: %rotate the chord until the bassnote is the first christopherh@1: %element christopherh@1: for i=1:length(chordnotes) christopherh@1: if strcmp(chordnotes{1},bassnote) christopherh@1: break christopherh@1: else christopherh@1: chordnotes = [chordnotes(2:end); chordnotes(1)]; christopherh@1: end christopherh@1: christopherh@1: end christopherh@1: christopherh@1: else christopherh@1: % insert the bassnote at the start of the chord christopherh@1: christopherh@1: chordnotes = [bassnote; chordnotes]; christopherh@1: christopherh@1: christopherh@1: end christopherh@1: end christopherh@1: % if success christopherh@1: % christopherh@1: % ilength = length(chordnotes); christopherh@1: % index = 1; christopherh@1: % christopherh@1: % % check if the bass note is included in the other chord notes christopherh@1: % while index<=ilength christopherh@1: % if char(bassnote) == char(chordnotes(index)) christopherh@1: % index = ilength +1; christopherh@1: % contains = 1; christopherh@1: % else christopherh@1: % contains = 0; christopherh@1: % index = index +1; christopherh@1: % end christopherh@1: % end christopherh@1: % christopherh@1: % if contains == 0 christopherh@1: % christopherh@1: % % chordnotes = [bassnote; chordnotes]; christopherh@1: % end christopherh@1: % end christopherh@1: else christopherh@1: bassnote = chordnotes(1); christopherh@1: end christopherh@1: christopherh@1: christopherh@1: end christopherh@1: end christopherh@1: end christopherh@1: christopherh@1: if success == 0 christopherh@1: errormessage = [errormessage sprintf(['Error in chord2notes: Couldn''t convert chord "' chordsymbol '"\n'])]; christopherh@1: if verbose ==1 christopherh@1: fprintf(1,errormessage); christopherh@1: end christopherh@1: christopherh@1: christopherh@1: end christopherh@1: christopherh@1: christopherh@1: