PLUS: квантователь ищет объясняющий темп, отчёт демо с F1 после обработки
Browse files- plus/plus_transcribe.py +60 -21
plus/plus_transcribe.py
CHANGED
|
@@ -116,28 +116,64 @@ def stage_cleanup(piece: Piece, options: dict) -> dict:
|
|
| 116 |
def stage_quantize(piece: Piece, options: dict) -> dict:
|
| 117 |
"""Посадка онсетов и длительностей на сетку.
|
| 118 |
|
| 119 |
-
Темп
|
| 120 |
-
|
| 121 |
-
|
| 122 |
-
|
| 123 |
-
|
|
|
|
| 124 |
"""
|
| 125 |
-
bpm = options.get("tempo")
|
| 126 |
-
if bpm is None:
|
| 127 |
-
bpm = piece.bpm or 120.0
|
| 128 |
-
bpm = float(bpm)
|
| 129 |
division = int(options.get("division", 4))
|
| 130 |
strength = float(options.get("strength", 0.85))
|
| 131 |
swing = float(options.get("swing", 0.0))
|
| 132 |
-
|
| 133 |
-
|
| 134 |
-
|
| 135 |
-
|
| 136 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 137 |
beat = 60.0 / bpm
|
| 138 |
step_s = beat / division
|
| 139 |
fitted = 0
|
| 140 |
-
for note in
|
| 141 |
target = round(note.start / step_s) * step_s
|
| 142 |
if swing and int(round(note.start / step_s)) % 2 == 1:
|
| 143 |
target += swing * step_s
|
|
@@ -148,13 +184,16 @@ def stage_quantize(piece: Piece, options: dict) -> dict:
|
|
| 148 |
length = round(note.dur / step_s) * step_s
|
| 149 |
if length > 0:
|
| 150 |
note.dur = max(1e-3, note.dur + (length - note.dur) * strength)
|
| 151 |
-
|
| 152 |
-
|
| 153 |
-
|
| 154 |
-
|
|
|
|
|
|
|
|
|
|
| 155 |
"explained_after": round(after, 3), "notes_moved": fitted,
|
| 156 |
-
"
|
| 157 |
-
"alternatives": ambiguity_of(
|
| 158 |
|
| 159 |
|
| 160 |
def stage_hands(piece: Piece, options: dict) -> dict:
|
|
|
|
| 116 |
def stage_quantize(piece: Piece, options: dict) -> dict:
|
| 117 |
"""Посадка онсетов и длительностей на сетку.
|
| 118 |
|
| 119 |
+
Темп из файла часто врёт: модели пишут заголовок 120, а ноты ставят по
|
| 120 |
+
своей сетке. Поэтому сначала ищем темп, который ОБЪЯСНЯЕТ онсеты (файловый,
|
| 121 |
+
его кратные и скан), и подменяем заголовок, только если найденное объяснение
|
| 122 |
+
заметно лучше. Если даже лучший кандидат объясняет меньше 75 процентов
|
| 123 |
+
онсетов, квантование пропускается: ровнять по чужой сетке хуже, чем не
|
| 124 |
+
ровнять вовсе.
|
| 125 |
"""
|
|
|
|
|
|
|
|
|
|
|
|
|
| 126 |
division = int(options.get("division", 4))
|
| 127 |
strength = float(options.get("strength", 0.85))
|
| 128 |
swing = float(options.get("swing", 0.0))
|
| 129 |
+
forced = options.get("tempo")
|
| 130 |
+
file_bpm = float(piece.bpm or 120.0)
|
| 131 |
+
notes = piece.notes
|
| 132 |
+
if not notes:
|
| 133 |
+
return {"bpm": round(file_bpm, 2), "division": division, "strength": strength,
|
| 134 |
+
"swing": swing, "explained_before": 0.0, "explained_after": 0.0,
|
| 135 |
+
"notes_moved": 0, "corrected_tempo": False, "skipped": True,
|
| 136 |
+
"reason": "нет нот", "alternatives": []}
|
| 137 |
+
|
| 138 |
+
starts = piece.starts()
|
| 139 |
+
sample = starts if len(starts) <= 400 else starts[:: max(1, len(starts) // 400)]
|
| 140 |
+
file_fraction = explained_fraction(starts, file_bpm)
|
| 141 |
+
|
| 142 |
+
def score(candidate):
|
| 143 |
+
return explained_fraction(sample, candidate)
|
| 144 |
+
|
| 145 |
+
# Только простые отношения к темпу файла: плотный скан даёт произвольные
|
| 146 |
+
# темпы вида 108, которые «объясняют» сетку случайно.
|
| 147 |
+
factors = (1.0, 1.5, 2.0 / 3.0, 2.0, 0.5, 3.0, 1.0 / 3.0)
|
| 148 |
+
best_bpm, best_fraction = file_bpm, file_fraction
|
| 149 |
+
if forced is None and options.get("auto_tempo", True):
|
| 150 |
+
for factor in factors:
|
| 151 |
+
candidate = file_bpm * factor
|
| 152 |
+
if not (30 <= candidate <= 260):
|
| 153 |
+
continue
|
| 154 |
+
value = score(candidate)
|
| 155 |
+
if value > best_fraction + 1e-9:
|
| 156 |
+
best_bpm, best_fraction = candidate, value
|
| 157 |
+
if forced is not None:
|
| 158 |
+
best_bpm, best_fraction = float(forced), score(float(forced))
|
| 159 |
+
|
| 160 |
+
threshold = float(options.get("min_explained", 0.75))
|
| 161 |
+
if best_fraction < threshold:
|
| 162 |
+
return {"bpm": round(file_bpm, 2), "division": division, "strength": strength,
|
| 163 |
+
"swing": swing, "explained_before": round(file_fraction, 3),
|
| 164 |
+
"explained_after": round(file_fraction, 3), "notes_moved": 0,
|
| 165 |
+
"corrected_tempo": False, "skipped": True,
|
| 166 |
+
"reason": "сетка объясняет только %.2f онсетов (порог %.2f), тайминг не трогаю"
|
| 167 |
+
% (best_fraction, threshold),
|
| 168 |
+
"best_candidate_bpm": round(best_bpm, 2),
|
| 169 |
+
"alternatives": ambiguity_of(starts, best_bpm)}
|
| 170 |
+
|
| 171 |
+
corrected = abs(np.log2(best_bpm / file_bpm)) > 0.02
|
| 172 |
+
bpm = float(best_bpm)
|
| 173 |
beat = 60.0 / bpm
|
| 174 |
step_s = beat / division
|
| 175 |
fitted = 0
|
| 176 |
+
for note in notes:
|
| 177 |
target = round(note.start / step_s) * step_s
|
| 178 |
if swing and int(round(note.start / step_s)) % 2 == 1:
|
| 179 |
target += swing * step_s
|
|
|
|
| 184 |
length = round(note.dur / step_s) * step_s
|
| 185 |
if length > 0:
|
| 186 |
note.dur = max(1e-3, note.dur + (length - note.dur) * strength)
|
| 187 |
+
notes.sort(key=lambda n: (n.start, n.pitch))
|
| 188 |
+
if corrected:
|
| 189 |
+
# заголовок приводим к найденной сетке, иначе такты в DAW встанут не туда
|
| 190 |
+
piece.tempo_changes = [(0.0, bpm)]
|
| 191 |
+
after = explained_fraction(piece.starts(), bpm)
|
| 192 |
+
return {"bpm": round(bpm, 2), "division": division, "strength": strength,
|
| 193 |
+
"swing": swing, "explained_before": round(file_fraction, 3),
|
| 194 |
"explained_after": round(after, 3), "notes_moved": fitted,
|
| 195 |
+
"corrected_tempo": corrected, "skipped": False,
|
| 196 |
+
"alternatives": ambiguity_of(starts, bpm)}
|
| 197 |
|
| 198 |
|
| 199 |
def stage_hands(piece: Piece, options: dict) -> dict:
|