comparison chordtools/chord2notes.m @ 1:8973548174c1 tip

adding tools to repo
author christopherh
date Mon, 06 May 2013 14:43:47 +0100
parents
children
comparison
equal deleted inserted replaced
0:0a4ad3e72e75 1:8973548174c1
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 % interval2note
19 %
20 % returns: chordnotes (cell array of note strings)
21 % bassnote (string)
22 % success (boolean)
23 % errormessage (string)
24 %
25 %
26 % Author: Christopher Harte, March 2009
27 %
28 % Copyright: Centre for Digital Music, Queen Mary University of London 2005
29 %
30 % This file is part of the C4DM Chord Toolkit V2.0
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, inversion, verbose)
48
49 %set default inversion to 0
50 if nargin < 2
51 inversion = 0;
52 end
53
54 % set verbose default to 0
55 if nargin < 3
56 verbose = 0;
57 end
58
59 errormessage = '';
60 mainlist = '';
61 success = 1;
62 chordnotes = [];
63 bassnote = '';
64
65 % parse the chordsymbol
66 [rootnote,shorthand,interval_list,bass, success, errormessage] = getchordinfo(chordsymbol);
67
68
69
70
71 %if 'no chord' then return N
72 if (success == 1)
73
74
75
76 if rootnote == 'N'
77
78 chordnotes = {};
79 else
80
81
82 % combine shorthand and interval_list and obtain note names for each
83 % interval
84 if success
85
86 [mainlist, success, errormessage] = addshort2list(shorthand, interval_list);
87
88 if success
89
90 % convert list of intervals to list of notes
91 [chordnotes,success,errormessage] = intervals2notes(mainlist,rootnote);
92
93 end
94
95 end
96
97 % Now find the bass note
98
99 if success
100
101 if ~isempty(bass)
102
103 [bassnote,success,errormessage] = interval2note(bass, rootnote);
104
105 if inversion
106 % if the bass note is a member of the chord
107 if length(intersect(bassnote,chordnotes))==1
108 %rotate the chord until the bassnote is the first
109 %element
110 for i=1:length(chordnotes)
111 if strcmp(chordnotes{1},bassnote)
112 break
113 else
114 chordnotes = [chordnotes(2:end); chordnotes(1)];
115 end
116
117 end
118
119 else
120 % insert the bassnote at the start of the chord
121
122 chordnotes = [bassnote; chordnotes];
123
124
125 end
126 end
127 % if success
128 %
129 % ilength = length(chordnotes);
130 % index = 1;
131 %
132 % % check if the bass note is included in the other chord notes
133 % while index<=ilength
134 % if char(bassnote) == char(chordnotes(index))
135 % index = ilength +1;
136 % contains = 1;
137 % else
138 % contains = 0;
139 % index = index +1;
140 % end
141 % end
142 %
143 % if contains == 0
144 %
145 % % chordnotes = [bassnote; chordnotes];
146 % end
147 % end
148 else
149 bassnote = chordnotes(1);
150 end
151
152
153 end
154 end
155 end
156
157 if success == 0
158 errormessage = [errormessage sprintf(['Error in chord2notes: Couldn''t convert chord "' chordsymbol '"\n'])];
159 if verbose ==1
160 fprintf(1,errormessage);
161 end
162
163
164 end
165
166
167