-
Notifications
You must be signed in to change notification settings - Fork 1
/
tweak_confirm.m
56 lines (50 loc) · 1.86 KB
/
tweak_confirm.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
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
% confirm new press with previous
raw_presses = old_raw_presses;
CONFIRM = 1;
copy = old_raw_presses;
for c = 1:CONFIRM-1
raw_presses(CONFIRM:num_frames) = and(raw_presses(CONFIRM:num_frames), copy(CONFIRM-c:num_frames-c));
end
%raw_presses(2:num_frames) = and(raw_presses(2:num_frames), raw_presses(1:num_frames-1));
% debounce and release too long presses
notestoplay = zeros(num_frames, map_size(1));
last_pressed = init_count*ones(1, map_size(1)); % for debouncing
last_released = init_count*ones(1, map_size(1)); % for debouncing
DEBOUNCE = 6; %ignore key presses within 6 frames
RELEASE_TIME = 80;
for i = init_count:size(raw_presses, 1)
% copy previous frame key status
notestoplay(i, :) = notestoplay(i - 1, :);
presses = find(raw_presses(i, :)~=0);
for j = 1:numel(presses)
key = presses(j);
currently_pressed = notestoplay(i - 1, key);
if currently_pressed && i - last_pressed(key) > DEBOUNCE
last_released(key) = i;
notestoplay(i, key) = 0;
elseif ~currently_pressed && i - last_released(key) > DEBOUNCE
last_pressed(key) = i;
notestoplay(i, key) = 1;
end
end
% release any keys that have been on for too long
to_release = find(and((notestoplay(i, :) > 0), (i - last_pressed > RELEASE_TIME)) > 0);
last_released(to_release) = i;
notestoplay(i, to_release) = 0;
end
% parse notestoplay for MIDI function
M = [];
startframe = 0;
endframe = 0;
for i = 1:map_size(1)
for j = 2:num_frames
if notestoplay(j, i) == 1 && notestoplay(j-1, i) == 0
startframe = j;
end
if notestoplay(j, i) == 0 && notestoplay(j-1, i) == 1
endframe = j-1;
M = [M; 1, 1, 73-i, 120, startframe/29.97, endframe/29.97];
end
end
end
writemidi(matrix2midi(M), strcat('output', num2str(CONFIRM-1), '.midi'));