Spaces:
Running
on
Zero
Running
on
Zero
File size: 4,765 Bytes
dcca7d2 |
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 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 |
import numpy as np
def note_detection_with_onset_offset_regress(frame_output, onset_output,
onset_shift_output, offset_output, offset_shift_output, velocity_output,
frame_threshold):
"""Process prediction matrices to note events information.
First, detect onsets with onset outputs. Then, detect offsets
with frame and offset outputs.
Args:
frame_output: (frames_num,)
onset_output: (frames_num,)
onset_shift_output: (frames_num,)
offset_output: (frames_num,)
offset_shift_output: (frames_num,)
velocity_output: (frames_num,)
frame_threshold: float
Returns:
output_tuples: list of [bgn, fin, onset_shift, offset_shift, normalized_velocity],
e.g., [
[1821, 1909, 0.47498, 0.3048533, 0.72119445],
[1909, 1947, 0.30730522, -0.45764327, 0.64200014],
...]
"""
output_tuples = []
bgn = None
frame_disappear = None
offset_occur = None
for i in range(onset_output.shape[0]):
if onset_output[i] == 1:
"""Onset detected"""
if bgn:
"""Consecutive onsets. E.g., pedal is not released, but two
consecutive notes being played."""
fin = max(i - 1, 0)
output_tuples.append([bgn, fin, onset_shift_output[bgn],
0, velocity_output[bgn]])
frame_disappear, offset_occur = None, None
bgn = i
if bgn and i > bgn:
"""If onset found, then search offset"""
if frame_output[i] <= frame_threshold and not frame_disappear:
"""Frame disappear detected"""
frame_disappear = i
if offset_output[i] == 1 and not offset_occur:
"""Offset detected"""
offset_occur = i
if frame_disappear:
if offset_occur and offset_occur - bgn > frame_disappear - offset_occur:
"""bgn --------- offset_occur --- frame_disappear"""
fin = offset_occur
else:
"""bgn --- offset_occur --------- frame_disappear"""
fin = frame_disappear
output_tuples.append([bgn, fin, onset_shift_output[bgn],
offset_shift_output[fin], velocity_output[bgn]])
bgn, frame_disappear, offset_occur = None, None, None
if bgn and (i - bgn >= 600 or i == onset_output.shape[0] - 1):
"""Offset not detected"""
fin = i
output_tuples.append([bgn, fin, onset_shift_output[bgn],
offset_shift_output[fin], velocity_output[bgn]])
bgn, frame_disappear, offset_occur = None, None, None
# Sort pairs by onsets
output_tuples.sort(key=lambda pair: pair[0])
return output_tuples
def pedal_detection_with_onset_offset_regress(frame_output, offset_output,
offset_shift_output, frame_threshold):
"""Process prediction array to pedal events information.
Args:
frame_output: (frames_num,)
offset_output: (frames_num,)
offset_shift_output: (frames_num,)
frame_threshold: float
Returns:
output_tuples: list of [bgn, fin, onset_shift, offset_shift],
e.g., [
[1821, 1909, 0.4749851, 0.3048533],
[1909, 1947, 0.30730522, -0.45764327],
...]
"""
output_tuples = []
bgn = None
frame_disappear = None
offset_occur = None
for i in range(1, frame_output.shape[0]):
if frame_output[i] >= frame_threshold and frame_output[i] > frame_output[i - 1]:
"""Pedal onset detected"""
if bgn:
pass
else:
bgn = i
if bgn and i > bgn:
"""If onset found, then search offset"""
if frame_output[i] <= frame_threshold and not frame_disappear:
"""Frame disappear detected"""
frame_disappear = i
if offset_output[i] == 1 and not offset_occur:
"""Offset detected"""
offset_occur = i
if offset_occur:
fin = offset_occur
output_tuples.append([bgn, fin, 0., offset_shift_output[fin]])
bgn, frame_disappear, offset_occur = None, None, None
if frame_disappear and i - frame_disappear >= 10:
"""offset not detected but frame disappear"""
fin = frame_disappear
output_tuples.append([bgn, fin, 0., offset_shift_output[fin]])
bgn, frame_disappear, offset_occur = None, None, None
# Sort pairs by onsets
output_tuples.sort(key=lambda pair: pair[0])
return output_tuples |