From 514284a22e9bec1b302f79a657f2a376f30a3cee Mon Sep 17 00:00:00 2001
From: Sting <loic.allegre@ensiie.fr>
Date: Tue, 25 Jul 2023 00:11:13 +0200
Subject: [PATCH] Fix overly long syls at end of lines

---
 autosyl/segment.py | 15 ++++++++++++++-
 1 file changed, 14 insertions(+), 1 deletion(-)

diff --git a/autosyl/segment.py b/autosyl/segment.py
index adacae0..6a54e36 100644
--- a/autosyl/segment.py
+++ b/autosyl/segment.py
@@ -56,13 +56,26 @@ def segment(songfile, reference_syls=None, syls_per_line=10, last_syl_dur=500, v
     onsets = (onsets + delay)/100
     #print(onsets)
 
+    sig = madmom.audio.signal.Signal(songfile, num_channels=1)
+    spec = madmom.audio.spectrogram.Spectrogram(sig)
+    filt_spec = madmom.audio.spectrogram.FilteredSpectrogram(spec, filterbank=madmom.audio.filters.LogFilterbank, num_bands=24)
+    log_spec = madmom.audio.spectrogram.LogarithmicSpectrogram(filt_spec, add=1)
+
+    magnitude = np.max(log_spec[:,:100], axis=1)
+
+    magnitude_threshold = 0.75
+    magnitude_start = 2
+
     if reference_syls:
         syls = []
         onset_index = 0
         for line in reference_syls:
             #print(onset_index, " : ", line)
             l = [[onsets[onset_index + i], line[i][1]] for i in range(len(line)-1)]
-            l.append([word_align[onset_index + (len(line) - 2)][1]/100, ''])
+            next_drop = words_onsets[onset_index + len(line) - 2] + magnitude_start
+            while magnitude[next_drop] > magnitude_threshold:
+                next_drop += 1
+            l.append([min(word_align[onset_index + (len(line) - 2)][1]/100, next_drop/100), ''])
             syls.append(l)
             onset_index += (len(line) - 1)
     else:
-- 
GitLab