fix&feat: 12_SUBTAB.md chỉnh sửa audioclip ở subtab

This commit is contained in:
2026-07-19 08:22:13 +07:00
parent a6ec14b38b
commit d2e0010d88
5 changed files with 3550 additions and 761 deletions
+44
View File
@@ -0,0 +1,44 @@
import os
import sys
sys.path.insert(0, os.path.join(os.path.dirname(__file__), ".."))
import numpy as np
import pytest
from app.core.sub_tab_dsp import SubTabDSPEngine
def test_speed_ratio():
sr = 44100
y = np.sin(2 * np.pi * 440 * np.linspace(0, 1, sr, endpoint=False)).astype(np.float32)
# Pitch-preserving speed change
y_stretched = SubTabDSPEngine.change_speed(y, sr, 2.0, preserve_pitch=True)
assert abs(len(y_stretched) - sr // 2) < 2000 # Librosa might have frame alignment differences
# Simple resampling speed change
y_resampled = SubTabDSPEngine.change_speed(y, sr, 2.0, preserve_pitch=False)
assert len(y_resampled) == sr // 2
def test_normalize():
y = np.array([0.1, -0.5, 0.2, 0.4], dtype=np.float32)
y_norm = SubTabDSPEngine.normalize(y, target_db=0.0)
assert np.max(np.abs(y_norm)) == 1.0
def test_merge_back_to_parent():
sr = 1000
parent = np.ones(5000, dtype=np.float32)
edited = np.zeros(2000, dtype=np.float32)
# Merge at t=1.0s (index 1000), original duration 1.5s (1500 samples)
res = SubTabDSPEngine.merge_back_to_parent(
parent_track_audio=parent,
sr=sr,
edited_sub_audio=edited,
start_seconds=1.0,
original_duration_seconds=1.5
)
# Expected length: 5000 - 1500 + 2000 = 5500
assert len(res) == 5500
# Before 1.0s (1000 samples) should be mostly parent values (1.0)
assert np.allclose(res[:900], 1.0)
# Inside the edited range should be zero (except crossfades)
assert np.allclose(res[1100:2900], 0.0)