File size: 10,681 Bytes
a7334d4
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
a1ddd2f
 
 
 
a7334d4
 
 
 
 
 
 
 
a1ddd2f
a7334d4
 
 
 
 
a1ddd2f
a7334d4
 
 
 
 
a1ddd2f
 
8e872fa
a1ddd2f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
bd37c28
a7334d4
 
bc3ffb2
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
a7334d4
 
 
 
a1ddd2f
a7334d4
 
a1ddd2f
a7334d4
 
 
 
a1ddd2f
 
 
a7334d4
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
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344

def regroup_words(
    words: list[dict],               
    max_len: float = 15.0,           
    gap: float = 0.50,               
) -> list[dict]:
    """
    Returns a list of segments with keys:
        'start', 'end', 'text', 'words'
    """

    if not words:
        return []

    segs, seg_words = [], []
    seg_start = words[0]["start"]
    last_end  = seg_start

    for w in words:
        over_max  = (w["end"] - seg_start) > max_len
        long_gap  = (w["start"] - last_end) > gap

        if (seg_words and (over_max or long_gap)):
            segs.append({
                "start": seg_start,
                "end":   last_end,
                "segment":  " ".join(x["word"] for x in seg_words),
            })
            seg_words = []
            seg_start = w["start"]

        seg_words.append(w)
        last_end = w["end"]

    # flush final segment
    segs.append({
        "start": seg_start,
        "end":   last_end,
        "segment":  " ".join(x["word"] for x in seg_words),
    })
    return segs


def text_to_words(text: str) -> list[dict]:
    """
    Convert text format like "word[start:end] word[start:end]..." to word list.
    
    Args:
        text: String in format "It's[4.96:5.52] a[5.52:5.84] long[5.84:6.16]..."
    
    Returns:
        List of word dictionaries with keys: 'word', 'start', 'end'
    """
    import re
    
    if not text.strip():
        return []
    
    # Pattern to match word[start:end] format
    pattern = r'(\S+?)\[([^:]+):([^\]]+)\]'
    matches = re.findall(pattern, text)
    
    words = []
    for word, start_str, end_str in matches:
        try:
            start = float(start_str) if start_str != 'xxx' else 0.0
            end = float(end_str) if end_str != 'xxx' else 0.0
            words.append({
                'word': word,
                'start': start,
                'end': end
            })
        except ValueError:
            # Skip invalid entries
            continue
    
    return words


def words_to_text(words: list[dict]) -> str:
    """
    Convert word list to text format "word[start:end] word[start:end]...".
    
    Args:
        words: List of word dictionaries with keys: 'word', 'start', 'end'
    
    Returns:
        String in format "It's[4.96:5.52] a[5.52:5.84] long[5.84:6.16]..."
    """
    if not words:
        return ""
    
    text_parts = []
    for word in words:
        word_text = word.get('word', '')
        start = word.get('start', 0.0)
        end = word.get('end', 0.0)
        # Format timestamps to max 2 decimal places
        start_str = f"{start:.2f}".rstrip('0').rstrip('.')
        end_str = f"{end:.2f}".rstrip('0').rstrip('.')
        text_parts.append(f"{word_text}[{start_str}:{end_str}]")
    
    return " ".join(text_parts)


def json_to_text(json_data: dict) -> str:
    """
    Convert JSON lyrics data to text format for display.
    Only uses the 'word' layer from the JSON structure.
    Groups words into sentences/lines for better readability.
    
    Args:
        json_data: Dictionary with 'word' key containing list of word objects
    
    Returns:
        String with words grouped into lines: "word[start:end] word[start:end]...\nword[start:end]..."
    """
    if not isinstance(json_data, dict) or 'word' not in json_data:
        return ""
    
    words = json_data['word']
    
    # Group words into segments using the existing regroup_words function
    segments = regroup_words(words, max_len=5, gap=0.50)
    
    # Convert each segment to text format
    segment_lines = []
    for seg in segments:
        # Extract words for this segment based on time range
        seg_words = []
        for word in words:
            if seg['start'] <= word['start'] < seg['end'] or (
                word['start'] <= seg['start'] < word['end']
            ):
                seg_words.append(word)
        
        if seg_words:
            segment_text = words_to_text(seg_words)
            segment_lines.append(segment_text)
    
    return '\n\n'.join(segment_lines)


def round_to_quarter_beats(beat_position: float) -> float:
    """Round beat position to nearest quarter note for sample display."""
    return round(beat_position * 4) / 4


def beats_to_seconds(beat_position: float, bpm: float) -> float:
    """Convert beat position to time in seconds."""
    return (beat_position * 60.0) / bpm


def seconds_to_beats(time_seconds: float, bpm: float) -> float:
    """Convert time in seconds to beat position."""
    return (time_seconds * bpm) / 60.0


def convert_text_time_to_beats(text: str, bpm: float, round_to_quarters: bool = False) -> str:
    """
    Convert time-based text format to beats-based format.
    
    Args:
        text: String in format "word[start_sec:end_sec] ..."
        bpm: Beats per minute for conversion
        round_to_quarters: If True, round beats to quarter notes (for sample display)
        
    Returns:
        String in format "word[start_beat:end_beat] ..."
    """
    if not text.strip():
        return ""
    
    words = text_to_words(text)
    beat_words = []
    
    for word in words:
        start_beat = seconds_to_beats(word['start'], bpm)
        end_beat = seconds_to_beats(word['end'], bpm)
        
        # Round to quarter notes for sample display
        if round_to_quarters:
            start_beat = round_to_quarter_beats(start_beat)
            end_beat = round_to_quarter_beats(end_beat)
        
        # Format to reasonable precision
        start_str = f"{start_beat:.2f}".rstrip('0').rstrip('.')
        end_str = f"{end_beat:.2f}".rstrip('0').rstrip('.')
        
        beat_words.append(f"{word['word']}[{start_str}:{end_str}]")
    
    return " ".join(beat_words)


def beats_to_text_with_regrouping(text: str, bpm: float, round_to_quarters: bool = False) -> str:
    """
    Convert time-based text to beats format with regrouping (like time mode).
    
    Args:
        text: String in format "word[start_sec:end_sec] ..."
        bpm: Beats per minute for conversion
        round_to_quarters: If True, round beats to quarter notes (for sample display)
        
    Returns:
        String with beats format grouped into lines
    """
    if not text.strip():
        return ""
    
    # First convert to beats format
    words = text_to_words(text)
    beat_words = []
    
    for word in words:
        start_beat = seconds_to_beats(word['start'], bpm)
        end_beat = seconds_to_beats(word['end'], bpm)
        
        # Round to quarter notes for sample display
        if round_to_quarters:
            start_beat = round_to_quarter_beats(start_beat)
            end_beat = round_to_quarter_beats(end_beat)
        
        beat_words.append({
            'word': word['word'],
            'start': start_beat,
            'end': end_beat
        })
    
    # Group beats into segments (using beat positions instead of seconds)
    segments = regroup_words(beat_words, max_len=20, gap=2.0)  # 20 beats max, 2 beat gap
    
    # Convert each segment to text format
    segment_lines = []
    for seg in segments:
        # Extract words for this segment based on beat range
        seg_words = []
        for word in beat_words:
            if seg['start'] <= word['start'] < seg['end'] or (
                word['start'] <= seg['start'] < word['end']
            ):
                seg_words.append(word)
        
        if seg_words:
            segment_text = words_to_text(seg_words)  # This will format as word[beat:beat]
            segment_lines.append(segment_text)
    
    return '\n\n'.join(segment_lines)


def convert_text_beats_to_time(text: str, bpm: float) -> str:
    """
    Convert beats-based text format to time-based format.
    
    Args:
        text: String in format "word[start_beat:end_beat] ..."
        bpm: Beats per minute for conversion
        
    Returns:
        String in format "word[start_sec:end_sec] ..."
    """
    if not text.strip():
        return ""
    
    # Parse beats format (same pattern as time format)
    words = text_to_words(text)
    time_words = []
    
    for word in words:
        # Convert beat positions to time
        start_time = beats_to_seconds(word['start'], bpm)
        end_time = beats_to_seconds(word['end'], bpm)
        
        # Format to reasonable precision
        start_str = f"{start_time:.2f}".rstrip('0').rstrip('.')
        end_str = f"{end_time:.2f}".rstrip('0').rstrip('.')
        
        time_words.append(f"{word['word']}[{start_str}:{end_str}]")
    
    return " ".join(time_words)


def convert_text_beats_to_time_with_regrouping(text: str, bpm: float) -> str:
    """
    Convert beats-based text format to time-based format while preserving line structure.
    
    Args:
        text: String in format "word[start_beat:end_beat] ..." (can be multi-line)
        bpm: Beats per minute for conversion
        
    Returns:
        String in format "word[start_sec:end_sec] ..." with preserved line breaks
    """
    if not text.strip():
        return ""
    
    # Process each line separately to preserve segmentation
    lines = text.split('\n')
    converted_lines = []
    
    for line in lines:
        line = line.strip()
        if not line:
            # Preserve empty lines
            converted_lines.append("")
            continue
            
        # Convert this line from beats to time
        words = text_to_words(line)
        time_words = []
        
        for word in words:
            # Convert beat positions to time
            start_time = beats_to_seconds(word['start'], bpm)
            end_time = beats_to_seconds(word['end'], bpm)
            
            # Format to reasonable precision
            start_str = f"{start_time:.2f}".rstrip('0').rstrip('.')
            end_str = f"{end_time:.2f}".rstrip('0').rstrip('.')
            
            time_words.append(f"{word['word']}[{start_str}:{end_str}]")
        
        if time_words:
            converted_lines.append(" ".join(time_words))
    
    return "\n".join(converted_lines)


def text_to_json(text: str) -> dict:
    """
    Convert text format to JSON structure expected by the model.
    Creates the 'word' layer that the model needs.
    Handles multi-line input by joining lines.
    
    Args:
        text: String in format "word[start:end] word[start:end]..." (can be multi-line)
    
    Returns:
        Dictionary with 'word' key containing list of word objects
    """
    # Join multiple lines into single line for parsing
    single_line_text = ' '.join(line.strip() for line in text.split('\n') if line.strip())
    words = text_to_words(single_line_text)
    return {"word": words}