-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnote2midinum.m
28 lines (26 loc) · 1.04 KB
/
note2midinum.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
function MIDInum = note2midinum(name)
% Convert note name to MIDI number.
%
% Input:
% - name : note name e.g. 'C4' or 'D5#'. You should follow this format.
% Output:
% - MIDInum : MIDI number of the note name
%
% Author: Zhiyao Duan
% Created: 2007
% Last modified: 6/26/2012
SemiNote1 = ('CCDDEFFGGAAB')';
SemiNote2 = (' # # # # # ')';
SemiNote3 = ('CDDEEFGGAABB')';
SemiNote4 = (' b b b b b ')';
if length(name)<3 % e.g. 'C4'
MIDInum = min(strfind(SemiNote1', name(1))); % min is the pitch class
elseif name(3) == '#' % e.g. 'C4#'
MIDInum = max(strfind(SemiNote1', name(1))); % max is the pitch class
elseif name(3) == 'b' % e.g. 'D4b'
MIDInum = min(strfind(SemiNote3', name(1))); % min is the pitch class
else % treat other cases the same as the first case
MIDInum = min(strfind(SemiNote1', name(1)));
end
% 69 = 'A4'
MIDInum = MIDInum-10 + 69 + (str2double(name(2))-4)*12;