To check out this repository please hg clone the following URL, or open the URL using EasyMercurial or your preferred Mercurial client.

Statistics Download as Zip
| Branch: | Revision:

root / _chordtools / chord2midinotes.m @ 8:b5b38998ef3b

History | View | Annotate | Download (4.18 KB)

1
%
2
%CHORD2MIDINOTES Generate MIDI note values for a given chord symbol
3
% 
4
% [midinotes, success, errormessage] = chord2midinotes(chordsymbol, {verbose})
5
% 
6
% Obtains a set of midi note values for notes present in chord "chordsymbol".
7
% The voicing is built on the rootnote with all other degrees occuring 
8
% above in number order unless a bass degree is specified.
9
%
10
% In the case of the 'no chord' symbol 'N' function returns midinote -1
11
%
12
% Success = 1 if chord is converted correctly else 0.
13
% 
14
% If optional argument 'verbose' is 1, function prints any errormessage to 
15
% the screen.
16
%
17
% Returns:  midinotes (array of integers)
18
%           success (boolean)
19
%           errormessage (string)
20
%
21
%
22
% % Author: Christopher Harte,  August 2005
23
% 
24
% Copyright: Centre for Digital Music, Queen Mary University of London 2005 
25
%
26
% This file is part of the C4DM Chord Toolkit.  
27
%
28
% The C4DM Chord Toolkit is free software; you can redistribute it and/or 
29
% modify it under the terms of the GNU General Public License as published 
30
% by the Free Software Foundation; either version 2 of the License, or
31
% (at your option) any later version.
32
%
33
% The C4DM Chord Toolkit is distributed in the hope that it will be useful,
34
% but WITHOUT ANY WARRANTY; without even the implied warranty of
35
% MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
36
% GNU General Public License for more details.
37
%
38
% You should have received a copy of the GNU General Public License
39
% along with the C4DM Toolkit; if not, write to the Free Software
40
% Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
41

    
42
%
43
function [midinotes, success,errormessage] = chord2midinotes(chordsymbol, verbose)
44

    
45
% set verbose default to 0
46
if nargin < 2
47
    verbose = 0;
48
end
49

    
50
errormessage = '';
51
mainlist = '';
52
success = 1;
53
midinotes = 0;
54

    
55
% parse the chordsymbol
56
[rootnote,shorthand,degreelist,bass, success, errormessage] = getchordinfo(chordsymbol);
57

    
58
if success
59

    
60

    
61
    %if 'no chord' then return -1
62
    if rootnote == 'N'
63

    
64
        midinotes = -1;
65
    else
66

    
67
        % get root's pitchclass with respect to middle C and calculate absolute
68
        % pitch of root in that octave   
69
        if success
70

    
71
        [rootpitchclass, success, errormessage] = note2pitchclass(rootnote);
72

    
73
        % midinote C4 = 60 so add pitchclass to calculate rootpitch
74
        rootpitch = 60 + rootpitchclass;
75

    
76
        end
77

    
78

    
79
        % combine shorthand and degreelist and obtain semitone values for each
80
        % degree
81
        if success
82

    
83
            [mainlist, success, errormessage] = addshort2list(shorthand, degreelist);
84

    
85
            if success
86

    
87
                [semitones,success,errormessage] = degrees2semitones(mainlist);
88

    
89
                % add rootpitch to obtain midinotes
90
                chordpitches = semitones + rootpitch;
91

    
92
            end
93

    
94
        end
95

    
96
        % Now find the bass note
97

    
98
        if success
99

    
100
            % if we haven't got a bass degree then play the rootnote as the
101
            % bassnote
102
            if isempty(bass)
103
                basspitchclass = rootpitchclass;
104
            else
105
                % otherwise convert the bassdegree to a note
106
                [bassnote,success,errormessage] = degree2note(bass, rootnote);
107

    
108
                if success
109

    
110
                    [basspitchclass, success, errormessage] = note2pitchclass(char(bassnote));
111

    
112
                end
113
            end
114

    
115
            if success
116
                % calculate bass note so that it is in the octave below the
117
                % rest of the chord i.e. add to C3 midinote 48
118
                basspitch = basspitchclass + 48;
119

    
120
                % Put midinote list together
121
                midinotes = [basspitch, rootpitch];
122
                
123
                for index = 1:length(chordpitches)
124
                    if chordpitches(index) ~= rootpitch
125
                       midinotes = [midinotes, chordpitches(index)];
126
                    end
127
                end
128
            end
129

    
130
        end
131
    end
132

    
133
%alter pitches for chords with high roots
134
if rootpitchclass > 6
135
    midinotes = midinotes-12;
136
end
137

    
138
end
139
    
140
if success == 0
141
    errormessage = [errormessage sprintf(['Error in chord2midinotes: Couldn''t convert chord"' chordsymbol '"\n'])];   
142
    if verbose ==1
143
       fprintf(1,errormessage);
144
    end
145
end  
146
    
147
    
148