To check out this repository please hg clone the following URL, or open the URL using EasyMercurial or your preferred Mercurial client.
root / _chordtools / chord2notes.m @ 8:b5b38998ef3b
History | View | Annotate | Download (3.65 KB)
| 1 |
% |
|---|---|
| 2 |
%CHORD2NOTES Convert chord symbol to list of constituent notes |
| 3 |
% |
| 4 |
% [chordnotes, bassnote, success, errormessage] = chord2notes(chordsymbol, {verbose})
|
| 5 |
% |
| 6 |
% Converts a chord symbol to a cell array of note strings and a bassnote string. |
| 7 |
% |
| 8 |
% In the case of the 'no chord' symbol 'N' function returns an empty array |
| 9 |
% |
| 10 |
% Success = 1 if notes extracted from chordsymbol correctly, 0 otherwise. |
| 11 |
% |
| 12 |
% If optional argument 'verbose' is 1, function prints any errormessage to |
| 13 |
% the screen. |
| 14 |
% |
| 15 |
% calls: getnoteinfo |
| 16 |
% parsenote |
| 17 |
% addshort2list |
| 18 |
% degree2note |
| 19 |
% |
| 20 |
% returns: chordnotes (cell array of note strings) |
| 21 |
% bassnote (string) |
| 22 |
% success (boolean) |
| 23 |
% errormessage (string) |
| 24 |
% |
| 25 |
% |
| 26 |
% Author: Christopher Harte, August 2005 |
| 27 |
% |
| 28 |
% Copyright: Centre for Digital Music, Queen Mary University of London 2005 |
| 29 |
% |
| 30 |
% This file is part of the C4DM Chord Toolkit. |
| 31 |
% |
| 32 |
% The C4DM Chord Toolkit is free software; you can redistribute it and/or |
| 33 |
% modify it under the terms of the GNU General Public License as published |
| 34 |
% by the Free Software Foundation; either version 2 of the License, or |
| 35 |
% (at your option) any later version. |
| 36 |
% |
| 37 |
% The C4DM Chord Toolkit is distributed in the hope that it will be useful, |
| 38 |
% but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 39 |
% MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 40 |
% GNU General Public License for more details. |
| 41 |
% |
| 42 |
% You should have received a copy of the GNU General Public License |
| 43 |
% along with the C4DM Toolkit; if not, write to the Free Software |
| 44 |
% Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA |
| 45 |
|
| 46 |
% |
| 47 |
function [chordnotes, bassnote, success, errormessage] = chord2notes(chordsymbol, verbose) |
| 48 |
|
| 49 |
% set verbose default to 0 |
| 50 |
if nargin < 2 |
| 51 |
verbose = 0; |
| 52 |
end |
| 53 |
|
| 54 |
errormessage = ''; |
| 55 |
mainlist = ''; |
| 56 |
success = 1; |
| 57 |
chordnotes = []; |
| 58 |
bassnote = ''; |
| 59 |
|
| 60 |
% parse the chordsymbol |
| 61 |
[rootnote,shorthand,degreelist,bass, success, errormessage] = getchordinfo(chordsymbol); |
| 62 |
|
| 63 |
|
| 64 |
|
| 65 |
|
| 66 |
%if 'no chord' then return N |
| 67 |
if (success == 1) |
| 68 |
|
| 69 |
|
| 70 |
|
| 71 |
if rootnote == 'N' |
| 72 |
|
| 73 |
chordnotes = []; |
| 74 |
else |
| 75 |
|
| 76 |
|
| 77 |
% combine shorthand and degreelist and obtain note names for each |
| 78 |
% degree |
| 79 |
if success |
| 80 |
|
| 81 |
[mainlist, success, errormessage] = addshort2list(shorthand, degreelist); |
| 82 |
|
| 83 |
if success |
| 84 |
|
| 85 |
% convert list of degrees to list of notes |
| 86 |
[chordnotes,success,errormessage] = degrees2notes(mainlist,rootnote); |
| 87 |
|
| 88 |
end |
| 89 |
|
| 90 |
end |
| 91 |
|
| 92 |
% Now find the bass note |
| 93 |
|
| 94 |
if success |
| 95 |
|
| 96 |
if ~isempty(bass) |
| 97 |
|
| 98 |
[bassnote,success,errormessage] = degree2note(bass, rootnote); |
| 99 |
|
| 100 |
if success |
| 101 |
|
| 102 |
ilength = length(chordnotes); |
| 103 |
index = 1; |
| 104 |
while index<=ilength |
| 105 |
if char(bassnote) == char(chordnotes(index)) |
| 106 |
index = ilength +1; |
| 107 |
contains = 1; |
| 108 |
else |
| 109 |
contains = 0; |
| 110 |
index = index +1; |
| 111 |
end |
| 112 |
end |
| 113 |
|
| 114 |
if contains == 0 |
| 115 |
|
| 116 |
chordnotes = [bassnote; chordnotes]; |
| 117 |
end |
| 118 |
end |
| 119 |
else |
| 120 |
bassnote = rootnote; |
| 121 |
end |
| 122 |
|
| 123 |
|
| 124 |
end |
| 125 |
end |
| 126 |
end |
| 127 |
|
| 128 |
if success == 0 |
| 129 |
errormessage = [errormessage sprintf(['Error in chord2notes: Couldn''t convert chord"' chordsymbol '"\n'])]; |
| 130 |
if verbose ==1 |
| 131 |
fprintf(1,errormessage); |
| 132 |
end |
| 133 |
|
| 134 |
|
| 135 |
end |
| 136 |
|
| 137 |
|
| 138 |
|