generated from dellevin/template
22
This commit is contained in:
@@ -146,6 +146,13 @@
|
||||
}
|
||||
.slice-btn:hover { background: #f0f0f0; color: #333; border-color: #ccc; }
|
||||
.slice-btn.play-btn:hover { color: #0078d4; border-color: #0078d4; }
|
||||
.param-save-btn {
|
||||
margin-left: auto; padding: 3px 10px; border: 1px solid #e0e0e0;
|
||||
border-radius: 5px; background: #fff; color: #666; font-size: 11px;
|
||||
font-weight: 600; cursor: pointer; transition: all 0.15s;
|
||||
display: inline-flex; align-items: center; gap: 4px;
|
||||
}
|
||||
.param-save-btn:hover { background: #f0f0f0; color: #333; border-color: #ccc; }
|
||||
|
||||
/* 进度条 */
|
||||
.progress-bar {
|
||||
@@ -181,6 +188,27 @@
|
||||
|
||||
.status-text { font-size: 12px; color: #999; margin: 8px 0; }
|
||||
|
||||
/* 片段选择 */
|
||||
.seg-row {
|
||||
display: flex; align-items: center; gap: 8px; padding: 6px 8px;
|
||||
border-bottom: 1px solid #f0f0f0; cursor: pointer; border-radius: 4px;
|
||||
transition: background 0.15s;
|
||||
}
|
||||
.seg-row:hover { background: #f5f5f5; }
|
||||
.seg-row.selected { background: #e8f0fe; }
|
||||
.seg-row.dimmed { opacity: 0.4; }
|
||||
.seg-check {
|
||||
width: 16px; height: 16px; accent-color: #0078d4; cursor: pointer;
|
||||
flex-shrink: 0;
|
||||
}
|
||||
.seg-ops { display: flex; gap: 4px; margin-bottom: 8px; }
|
||||
.seg-op-btn {
|
||||
padding: 3px 10px; border: 1px solid #e0e0e0; border-radius: 4px;
|
||||
background: #fff; color: #666; font-size: 11px; cursor: pointer;
|
||||
transition: all 0.15s;
|
||||
}
|
||||
.seg-op-btn:hover { background: #f0f0f0; border-color: #ccc; }
|
||||
|
||||
/* Toast */
|
||||
.toast-container { position: fixed; top: 20px; right: 20px; z-index: 200; display: flex; flex-direction: column; gap: 8px; }
|
||||
.toast {
|
||||
@@ -193,6 +221,90 @@
|
||||
.toast.info { background: #e3f2fd; color: #1565c0; border: 1px solid #bbdefb; }
|
||||
@keyframes slideIn { from { transform: translateX(100%); opacity: 0; } to { transform: translateX(0); opacity: 1; } }
|
||||
|
||||
/* 模式切换 */
|
||||
.mode-toggle {
|
||||
display: flex; gap: 0; margin-bottom: 12px;
|
||||
border: 1px solid #e0e0e0; border-radius: 6px; overflow: hidden;
|
||||
}
|
||||
.mode-btn {
|
||||
flex: 1; padding: 7px 0; text-align: center;
|
||||
font-size: 12px; font-weight: 600; cursor: pointer;
|
||||
background: #fff; color: #999; border: none;
|
||||
transition: all 0.15s;
|
||||
}
|
||||
.mode-btn.active { background: #0078d4; color: #fff; }
|
||||
.mode-btn:first-child { border-right: 1px solid #e0e0e0; }
|
||||
.mode-btn.active:first-child { border-right-color: #0078d4; }
|
||||
|
||||
/* 切割点列表 */
|
||||
.cut-list { margin-top: 8px; }
|
||||
.cut-row {
|
||||
display: flex; align-items: center; gap: 6px;
|
||||
padding: 5px 0; border-bottom: 1px solid #f0f0f0;
|
||||
}
|
||||
.cut-row:last-child { border-bottom: none; }
|
||||
.cut-idx { width: 24px; font-size: 12px; font-weight: 700; color: #999; text-align: center; }
|
||||
.cut-input {
|
||||
flex: 1; padding: 4px 8px; border: 1px solid #e0e0e0; border-radius: 4px;
|
||||
font-size: 12px; text-align: center; outline: none;
|
||||
transition: border-color 0.15s;
|
||||
}
|
||||
.cut-input:focus { border-color: #0078d4; }
|
||||
.cut-del {
|
||||
width: 24px; height: 24px; border: none; background: none;
|
||||
color: #ccc; cursor: pointer; font-size: 12px; border-radius: 4px;
|
||||
display: flex; align-items: center; justify-content: center;
|
||||
transition: all 0.15s;
|
||||
}
|
||||
.cut-del:hover { background: #fce4ec; color: #c62828; }
|
||||
.cut-add {
|
||||
display: flex; align-items: center; justify-content: center; gap: 4px;
|
||||
width: 100%; padding: 6px; border: 1px dashed #e0e0e0; border-radius: 6px;
|
||||
background: none; color: #999; font-size: 12px; cursor: pointer;
|
||||
margin-top: 6px; transition: all 0.15s;
|
||||
}
|
||||
.cut-add:hover { border-color: #0078d4; color: #0078d4; }
|
||||
.cut-hint { font-size: 11px; color: #aaa; margin-top: 8px; line-height: 1.5; }
|
||||
|
||||
/* 时间轴 (HTML div 实现) */
|
||||
.timeline-wrap { position: relative; margin-bottom: 28px; margin-top: 22px; padding-bottom: 22px; }
|
||||
#timelineBar {
|
||||
position: relative; height: 100px;
|
||||
border-radius: 8px; border: 1px solid #e0e0e0;
|
||||
cursor: crosshair; user-select: none; overflow: visible;
|
||||
background: #f0f4f8;
|
||||
}
|
||||
#timelineBar.readonly { cursor: default; }
|
||||
#timelineBar.readonly .cut-marker { cursor: default; }
|
||||
#waveformCanvas {
|
||||
position: absolute; top: 0; left: 0; width: 100%; height: 100%;
|
||||
border-radius: 8px; pointer-events: none;
|
||||
}
|
||||
.cut-marker {
|
||||
position: absolute; top: 0; width: 24px; height: 100%;
|
||||
transform: translateX(-50%); z-index: 2; cursor: grab;
|
||||
}
|
||||
.cut-marker:active { cursor: grabbing; }
|
||||
.cut-marker-line {
|
||||
position: absolute; left: 50%; top: 0; bottom: 0;
|
||||
width: 2px; background: #e53935; transform: translateX(-50%);
|
||||
}
|
||||
.cut-marker-tri {
|
||||
position: absolute; left: 50%; top: -1px;
|
||||
transform: translateX(-50%);
|
||||
width: 0; height: 0;
|
||||
border-left: 6px solid transparent;
|
||||
border-right: 6px solid transparent;
|
||||
border-top: 8px solid #e53935;
|
||||
pointer-events: none;
|
||||
}
|
||||
.cut-marker-label {
|
||||
position: absolute; left: 50%; bottom: -20px;
|
||||
transform: translateX(-50%);
|
||||
font-size: 10px; color: #e53935; font-weight: 600;
|
||||
white-space: nowrap; pointer-events: none;
|
||||
}
|
||||
|
||||
@media (max-width: 768px) {
|
||||
body { flex-direction: column; height: auto; overflow: auto; }
|
||||
.left-panel { width: 100% !important; height: auto; border-right: none; border-bottom: 1px solid #e8e8e8; overflow-y: visible; }
|
||||
@@ -239,11 +351,17 @@
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<!-- 参数 -->
|
||||
<div class="section-card">
|
||||
<!-- 模式切换 -->
|
||||
<div class="mode-toggle">
|
||||
<div class="mode-btn active" id="modeAuto" onclick="setCutMode('auto')">自动切割</div>
|
||||
<div class="mode-btn" id="modeManual" onclick="setCutMode('manual')">手动切割</div>
|
||||
</div>
|
||||
|
||||
<!-- 自动模式:参数 -->
|
||||
<div class="section-card" id="autoParamsCard">
|
||||
<div class="section-title">
|
||||
<i class="fas fa-wrench"></i> 分割参数
|
||||
<button class="slice-btn" style="margin-left:auto;" onclick="saveConfig()" title="保存参数"><i class="fas fa-check"></i></button>
|
||||
<button class="param-save-btn" onclick="saveConfig()"><i class="fas fa-check"></i> 保存</button>
|
||||
</div>
|
||||
<div class="param-row">
|
||||
<span class="param-label">静音阈值</span>
|
||||
@@ -277,6 +395,16 @@
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<!-- 手动模式:切割点 -->
|
||||
<div class="section-card" id="manualCutCard" style="display:none;">
|
||||
<div class="section-title">
|
||||
<i class="fas fa-scissors"></i> 切割点
|
||||
</div>
|
||||
<div class="cut-list" id="cutList"></div>
|
||||
<button class="cut-add" onclick="addCutPointRow()"><i class="fas fa-plus"></i> 添加切割点</button>
|
||||
<div class="cut-hint">点击时间轴添加标记,拖动标记调整位置,点击已有标记删除</div>
|
||||
</div>
|
||||
|
||||
<!-- 操作按钮 -->
|
||||
<div class="btn-row">
|
||||
<button class="action-btn analyze" id="btnAnalyze" onclick="doAnalyze()" disabled>
|
||||
@@ -285,6 +413,9 @@
|
||||
<button class="action-btn slice" id="btnSlice" onclick="doSlice()" disabled>
|
||||
<i class="fas fa-scissors"></i> 开始切割
|
||||
</button>
|
||||
<button class="action-btn slice" id="btnManualSlice" onclick="doManualSlice()" style="display:none;" disabled>
|
||||
<i class="fas fa-scissors"></i> 开始切割
|
||||
</button>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
@@ -298,7 +429,15 @@
|
||||
<i class="fas fa-scissors"></i>
|
||||
<p>上传音频文件后开始分析</p>
|
||||
</div>
|
||||
<div id="resultArea" style="display:none;">
|
||||
<div id="timelineArea" style="display:none;">
|
||||
<div class="timeline-wrap" id="timelineBar"></div>
|
||||
<div id="redoWrap" style="display:none;text-align:center;margin-top:8px;">
|
||||
<button class="action-btn slice" style="width:auto;padding:8px 20px;font-size:13px;" onclick="redoManualSlice()">
|
||||
<i class="fas fa-redo"></i> 切割点已更改,点击重新切割
|
||||
</button>
|
||||
</div>
|
||||
</div>
|
||||
<div id="resultArea" style="display:none;margin-top: 20px">
|
||||
<div class="top-actions" id="topActions" style="display:none;">
|
||||
<button class="top-btn primary" onclick="downloadAll()">
|
||||
<i class="fas fa-folder-plus"></i> 打包下载全部
|
||||
@@ -317,6 +456,16 @@
|
||||
let taskId = null;
|
||||
let pollTimer = null;
|
||||
|
||||
// 手动切割状态
|
||||
let cutMode = 'auto';
|
||||
let cutPoints = [];
|
||||
let audioDuration = 0;
|
||||
let cutPointsChanged = false;
|
||||
let selectedSegments = []; // 选中的片段索引
|
||||
let manualSegSelected = []; // 手动模式:左侧面板勾选的片段
|
||||
let manualSegChanged = false; // 用户是否手动调整过勾选
|
||||
let manualSegInit = false; // 新建预览时重置勾选
|
||||
|
||||
function showMessage(text, type, duration) {
|
||||
type = type || 'info'; duration = duration || 3000;
|
||||
var t = document.createElement('div');
|
||||
@@ -404,11 +553,23 @@
|
||||
document.getElementById('infoDur').textContent = formatDur(info.duration);
|
||||
document.getElementById('infoName').textContent = data.filename;
|
||||
document.getElementById('btnAnalyze').disabled = false;
|
||||
// 存储时长用于手动模式
|
||||
audioDuration = info.duration;
|
||||
cutPoints = [];
|
||||
cutPointsChanged = false;
|
||||
waveformPeaks = null;
|
||||
// 重置右侧面板
|
||||
document.getElementById('emptyState').style.display = 'flex';
|
||||
document.getElementById('resultArea').style.display = 'none';
|
||||
document.getElementById('timelineArea').style.display = 'none';
|
||||
document.getElementById('redoWrap').style.display = 'none';
|
||||
document.getElementById('sliceList').innerHTML = '';
|
||||
document.getElementById('topActions').style.display = 'none';
|
||||
// 两种模式都显示时间轴(自动模式只读,手动模式可交互)
|
||||
showTimeline(cutMode === 'auto');
|
||||
updateManualBtn();
|
||||
// 异步加载波形数据
|
||||
loadWaveform();
|
||||
} catch (e) {
|
||||
showMessage('上传失败: ' + e.message, 'error');
|
||||
document.getElementById('uploadLabel').textContent = '点击或拖拽音频文件到此处';
|
||||
@@ -440,6 +601,8 @@
|
||||
document.getElementById('statusText').textContent = '分析完成: 检测到 ' + data.count + ' 个片段';
|
||||
renderPreview(data.preview, data.sample_rate);
|
||||
document.getElementById('btnSlice').disabled = false;
|
||||
// 自动模式也显示时间轴(只读)
|
||||
showTimeline(true);
|
||||
} catch (e) {
|
||||
showMessage('分析失败: ' + e.message, 'error');
|
||||
} finally {
|
||||
@@ -449,18 +612,95 @@
|
||||
}
|
||||
|
||||
function renderPreview(preview, sr) {
|
||||
var html = '<table class="preview-table"><thead><tr>'
|
||||
+ '<th>#</th><th>时长</th><th>采样数</th></tr></thead><tbody>';
|
||||
// 默认全部选中
|
||||
selectedSegments = [];
|
||||
for (var i = 0; i < preview.length; i++) selectedSegments.push(i);
|
||||
|
||||
// 计算每段起止秒数(用于切割前预览播放)
|
||||
window._previewRanges = [];
|
||||
var cumSamples = 0;
|
||||
for (var i = 0; i < preview.length; i++) {
|
||||
var startSec = cumSamples / sr;
|
||||
cumSamples += preview[i].samples;
|
||||
var endSec = cumSamples / sr;
|
||||
window._previewRanges.push([startSec, endSec]);
|
||||
}
|
||||
|
||||
var html = '<div style="margin-bottom:10px;">'
|
||||
+ '<span style="font-size:13px;font-weight:600;color:#333;"><i class="fas fa-list-check" style="color:#0078d4;margin-right:4px;"></i> 片段选择</span>'
|
||||
+ '<span style="font-size:11px;color:#999;margin-left:8px;">点击取消勾选不需要的片段</span>'
|
||||
+ '</div>'
|
||||
+ '<div class="seg-ops">'
|
||||
+ '<button class="seg-op-btn" onclick="selectAllSegments()">全选</button>'
|
||||
+ '<button class="seg-op-btn" onclick="deselectAllSegments()">全不选</button>'
|
||||
+ '<span id="segCount" style="font-size:11px;color:#999;margin-left:4px;">已选 ' + preview.length + '/' + preview.length + '</span>'
|
||||
+ '</div>';
|
||||
for (var i = 0; i < preview.length; i++) {
|
||||
var p = preview[i];
|
||||
html += '<tr><td>' + (i + 1) + '</td><td>' + formatDur(p.duration) + '</td><td>' + p.samples.toLocaleString() + '</td></tr>';
|
||||
html += '<div class="seg-row selected" id="segRow' + i + '" onclick="toggleSegment(' + i + ')">'
|
||||
+ '<input type="checkbox" class="seg-check" id="segCb' + i + '" checked tabindex="-1">'
|
||||
+ '<span style="width:24px;font-size:13px;font-weight:700;color:#999;text-align:center;">' + (i + 1) + '</span>'
|
||||
+ '<span style="flex:1;font-size:13px;color:#333;">' + formatDur(p.duration) + '</span>'
|
||||
+ '<span style="font-size:11px;color:#999;">' + p.samples.toLocaleString() + ' samples</span>'
|
||||
+ '<button class="slice-btn play-btn" onclick="event.stopPropagation();playPreviewSeg(' + i + ')" title="试听"><i class="fas fa-play"></i></button>'
|
||||
+ '</div>';
|
||||
}
|
||||
html += '</tbody></table>';
|
||||
document.getElementById('sliceList').innerHTML = html;
|
||||
}
|
||||
|
||||
function playPreviewSeg(index) {
|
||||
if (!taskId || !window._previewRanges || !window._previewRanges[index]) return;
|
||||
if (currentAudio) { currentAudio.pause(); currentAudio = null; }
|
||||
var r = window._previewRanges[index];
|
||||
var audio = new Audio('/audio-slicer/preview-range/' + taskId + '?start=' + r[0] + '&end=' + r[1]);
|
||||
audio.play();
|
||||
currentAudio = audio;
|
||||
}
|
||||
|
||||
function toggleSegment(idx) {
|
||||
var pos = selectedSegments.indexOf(idx);
|
||||
if (pos >= 0) {
|
||||
selectedSegments.splice(pos, 1);
|
||||
} else {
|
||||
selectedSegments.push(idx);
|
||||
selectedSegments.sort(function(a, b) { return a - b; });
|
||||
}
|
||||
// Sync checkbox visual state
|
||||
var cb = document.getElementById('segCb' + idx);
|
||||
if (cb) cb.checked = (pos < 0);
|
||||
updateSegUI();
|
||||
}
|
||||
|
||||
function selectAllSegments() {
|
||||
var rows = document.querySelectorAll('.seg-row');
|
||||
selectedSegments = [];
|
||||
for (var i = 0; i < rows.length; i++) selectedSegments.push(i);
|
||||
updateSegUI();
|
||||
}
|
||||
|
||||
function deselectAllSegments() {
|
||||
selectedSegments = [];
|
||||
updateSegUI();
|
||||
}
|
||||
|
||||
function updateSegUI() {
|
||||
var rows = document.querySelectorAll('.seg-row');
|
||||
for (var i = 0; i < rows.length; i++) {
|
||||
var sel = selectedSegments.indexOf(i) >= 0;
|
||||
rows[i].className = 'seg-row' + (sel ? ' selected' : ' dimmed');
|
||||
var cb = document.getElementById('segCb' + i);
|
||||
if (cb) cb.checked = sel;
|
||||
}
|
||||
var total = rows.length;
|
||||
var el = document.getElementById('segCount');
|
||||
if (el) el.textContent = '已选 ' + selectedSegments.length + '/' + total;
|
||||
var btnSlice = document.getElementById('btnSlice');
|
||||
if (btnSlice) btnSlice.disabled = selectedSegments.length === 0;
|
||||
}
|
||||
|
||||
async function doSlice() {
|
||||
if (!taskId) return;
|
||||
if (selectedSegments.length === 0) { showMessage('请至少选择一个片段', 'error'); return; }
|
||||
var btn = document.getElementById('btnSlice');
|
||||
btn.disabled = true;
|
||||
btn.innerHTML = '<i class="fas fa-refresh"></i> 切割中...';
|
||||
@@ -474,7 +714,7 @@
|
||||
try {
|
||||
var res = await fetch('/audio-slicer/slice', {
|
||||
method: 'POST', headers: {'Content-Type': 'application/json'},
|
||||
body: JSON.stringify({ task_id: taskId })
|
||||
body: JSON.stringify({ task_id: taskId, selected_indices: selectedSegments })
|
||||
});
|
||||
var data = await res.json();
|
||||
if (!data.success) {
|
||||
@@ -511,6 +751,14 @@
|
||||
document.getElementById('btnSlice').disabled = false;
|
||||
document.getElementById('btnSlice').innerHTML = '<i class="fas fa-scissors"></i> 开始切割';
|
||||
document.getElementById('btnAnalyze').disabled = false;
|
||||
document.getElementById('btnManualSlice').disabled = false;
|
||||
document.getElementById('btnManualSlice').innerHTML = '<i class="fas fa-scissors"></i> 重新切割';
|
||||
// 手动模式下保持时间轴可见,支持拖动调整后重新切割
|
||||
if (cutMode === 'manual') {
|
||||
document.getElementById('timelineArea').style.display = 'block';
|
||||
cutPointsChanged = false;
|
||||
document.getElementById('redoWrap').style.display = 'none';
|
||||
}
|
||||
} else if (data.status === 'error') {
|
||||
clearInterval(pollTimer);
|
||||
pollTimer = null;
|
||||
@@ -519,6 +767,8 @@
|
||||
document.getElementById('btnSlice').disabled = false;
|
||||
document.getElementById('btnSlice').innerHTML = '<i class="fas fa-scissors"></i> 开始切割';
|
||||
document.getElementById('btnAnalyze').disabled = false;
|
||||
document.getElementById('btnManualSlice').disabled = false;
|
||||
document.getElementById('btnManualSlice').innerHTML = '<i class="fas fa-scissors"></i> 重新切割';
|
||||
}
|
||||
} catch (e) {}
|
||||
}, 500);
|
||||
@@ -561,6 +811,469 @@
|
||||
a.download = '';
|
||||
a.click();
|
||||
}
|
||||
|
||||
// ── 模式切换 ──────────────────────────────────────────────────────
|
||||
function setCutMode(mode) {
|
||||
cutMode = mode;
|
||||
document.getElementById('modeAuto').classList.toggle('active', mode === 'auto');
|
||||
document.getElementById('modeManual').classList.toggle('active', mode === 'manual');
|
||||
document.getElementById('autoParamsCard').style.display = mode === 'auto' ? 'block' : 'none';
|
||||
document.getElementById('manualCutCard').style.display = mode === 'manual' ? 'block' : 'none';
|
||||
document.getElementById('btnAnalyze').style.display = mode === 'auto' ? '' : 'none';
|
||||
document.getElementById('btnSlice').style.display = mode === 'auto' ? '' : 'none';
|
||||
document.getElementById('btnManualSlice').style.display = mode === 'manual' ? '' : 'none';
|
||||
|
||||
// 切换时清空右侧面板
|
||||
document.getElementById('resultArea').style.display = 'none';
|
||||
document.getElementById('redoWrap').style.display = 'none';
|
||||
document.getElementById('sliceList').innerHTML = '';
|
||||
document.getElementById('topActions').style.display = 'none';
|
||||
document.getElementById('progressWrap').style.display = 'none';
|
||||
cutPointsChanged = false;
|
||||
|
||||
// 两种模式都显示时间轴(自动模式只读,手动模式可交互)
|
||||
if (audioDuration > 0) {
|
||||
showTimeline(mode === 'auto');
|
||||
}
|
||||
}
|
||||
|
||||
function showTimeline(readOnly) {
|
||||
document.getElementById('emptyState').style.display = 'none';
|
||||
document.getElementById('timelineArea').style.display = 'block';
|
||||
renderTimeline(readOnly);
|
||||
}
|
||||
|
||||
function updateManualBtn() {
|
||||
var btn = document.getElementById('btnManualSlice');
|
||||
if (btn) btn.disabled = !(taskId && cutPoints.length > 0 && manualSegSelected.length > 0);
|
||||
}
|
||||
|
||||
// ── 切割点管理 ────────────────────────────────────────────────────
|
||||
function addCutPoint(sec) {
|
||||
sec = Math.round(sec * 100) / 100;
|
||||
if (sec <= 0 || sec >= audioDuration) return;
|
||||
for (var i = 0; i < cutPoints.length; i++) {
|
||||
if (Math.abs(cutPoints[i] - sec) < 0.05) return;
|
||||
}
|
||||
cutPoints.push(sec);
|
||||
cutPoints.sort(function(a, b) { return a - b; });
|
||||
manualSegInit = true;
|
||||
renderCutList();
|
||||
renderTimeline();
|
||||
updateManualBtn();
|
||||
markCutPointsChanged();
|
||||
}
|
||||
|
||||
function removeCutPoint(idx) {
|
||||
cutPoints.splice(idx, 1);
|
||||
manualSegInit = true;
|
||||
renderCutList();
|
||||
renderTimeline();
|
||||
updateManualBtn();
|
||||
markCutPointsChanged();
|
||||
}
|
||||
|
||||
function addCutPointRow() {
|
||||
if (audioDuration <= 0) { showMessage('请先上传音频', 'error'); return; }
|
||||
// 默认添加到中间位置
|
||||
var sec = audioDuration / 2;
|
||||
if (cutPoints.length > 0) {
|
||||
// 找最大的间隔中点
|
||||
var maxGap = 0, maxIdx = 0;
|
||||
var pts = [0].concat(cutPoints).concat([audioDuration]);
|
||||
for (var i = 1; i < pts.length; i++) {
|
||||
var gap = pts[i] - pts[i - 1];
|
||||
if (gap > maxGap) { maxGap = gap; maxIdx = i; }
|
||||
}
|
||||
sec = (pts[maxIdx - 1] + pts[maxIdx]) / 2;
|
||||
}
|
||||
addCutPoint(sec);
|
||||
}
|
||||
|
||||
function renderCutList() {
|
||||
var list = document.getElementById('cutList');
|
||||
if (!list) return;
|
||||
if (cutPoints.length === 0) {
|
||||
list.innerHTML = '<div style="text-align:center;color:#ccc;font-size:12px;padding:8px;">暂无切割点</div>';
|
||||
manualSegSelected = [];
|
||||
return;
|
||||
}
|
||||
|
||||
// 新预览时重置勾选
|
||||
if (manualSegInit) {
|
||||
manualSegInit = false;
|
||||
manualSegSelected = [];
|
||||
manualSegChanged = false;
|
||||
}
|
||||
|
||||
var html = '';
|
||||
for (var i = 0; i < cutPoints.length; i++) {
|
||||
html += '<div class="cut-row">'
|
||||
+ '<span class="cut-idx"><i class="fas fa-scissors" style="font-size:10px;color:#e53935;"></i></span>'
|
||||
+ '<input class="cut-input" value="' + formatTime(cutPoints[i]) + '" '
|
||||
+ 'onchange="updateCutPoint(' + i + ', this.value)" '
|
||||
+ 'onkeydown="if(event.key===\'Enter\')this.blur()">'
|
||||
+ '<button class="cut-del" onclick="removeCutPoint(' + i + ')" title="删除"><i class="fas fa-times"></i></button>'
|
||||
+ '</div>';
|
||||
}
|
||||
|
||||
var boundaries = [0].concat(cutPoints).concat([audioDuration]);
|
||||
// 确保勾选数组长度匹配
|
||||
if (manualSegSelected.length !== boundaries.length - 1) {
|
||||
manualSegSelected = [];
|
||||
for (var j = 0; j < boundaries.length - 1; j++) manualSegSelected.push(j);
|
||||
}
|
||||
|
||||
html += '<div style="border-top:1px solid #e0e0e0;margin-top:8px;padding-top:8px;">'
|
||||
+ '<div style="font-size:11px;font-weight:600;color:#999;margin-bottom:6px;">'
|
||||
+ '<i class="fas fa-list-check" style="color:#0078d4;margin-right:3px;"></i>预览片段 '
|
||||
+ '<span style="font-weight:400;color:#aaa;">点击取消勾选不需要的片段</span>'
|
||||
+ '</div>'
|
||||
+ '<div class="seg-ops">'
|
||||
+ '<button class="seg-op-btn" onclick="selectManualSegments()">全选</button>'
|
||||
+ '<span id="manualSegCount" style="font-size:11px;color:#999;margin-left:4px;"></span>'
|
||||
+ '</div>';
|
||||
|
||||
for (var i = 0; i < boundaries.length - 1; i++) {
|
||||
var s = boundaries[i], e = boundaries[i + 1];
|
||||
var dur = e - s;
|
||||
var sel = manualSegSelected.indexOf(i) >= 0;
|
||||
html += '<div class="seg-row' + (sel ? ' selected' : ' dimmed') + '" onclick="toggleManualSeg(' + i + ')">'
|
||||
+ '<input type="checkbox" class="seg-check" id="mSegCb' + i + '"' + (sel ? ' checked' : '') + ' tabindex="-1">'
|
||||
+ '<span style="width:24px;font-size:13px;font-weight:700;color:#999;text-align:center;">' + (i + 1) + '</span>'
|
||||
+ '<span style="flex:1;font-size:12px;color:#555;">' + formatTime(s) + ' → ' + formatTime(e) + '</span>'
|
||||
+ '<span style="font-size:11px;color:#999;width:50px;text-align:right;">' + formatDur(dur) + '</span>'
|
||||
+ '<button class="cut-del" style="color:#0078d4;" onclick="event.stopPropagation();playPreview(' + s + ',' + e + ')" title="试听"><i class="fas fa-play"></i></button>'
|
||||
+ '</div>';
|
||||
}
|
||||
html += '</div>';
|
||||
list.innerHTML = html;
|
||||
updateManualSegCount();
|
||||
}
|
||||
|
||||
function updateCutPoint(idx, val) {
|
||||
var sec = parseTime(val);
|
||||
if (isNaN(sec) || sec <= 0 || sec >= audioDuration) {
|
||||
showMessage('无效的时间', 'error');
|
||||
renderCutList();
|
||||
return;
|
||||
}
|
||||
cutPoints[idx] = Math.round(sec * 100) / 100;
|
||||
cutPoints.sort(function(a, b) { return a - b; });
|
||||
renderCutList();
|
||||
renderTimeline();
|
||||
markCutPointsChanged();
|
||||
}
|
||||
|
||||
var previewAudio = null;
|
||||
function playPreview(start, end) {
|
||||
if (!taskId) return;
|
||||
if (previewAudio) { previewAudio.pause(); previewAudio = null; }
|
||||
previewAudio = new Audio('/audio-slicer/preview-range/' + taskId + '?start=' + start + '&end=' + end);
|
||||
previewAudio.play();
|
||||
}
|
||||
|
||||
function toggleManualSeg(idx) {
|
||||
var pos = manualSegSelected.indexOf(idx);
|
||||
if (pos >= 0) {
|
||||
manualSegSelected.splice(pos, 1);
|
||||
} else {
|
||||
manualSegSelected.push(idx);
|
||||
manualSegSelected.sort(function(a, b) { return a - b; });
|
||||
}
|
||||
manualSegChanged = true;
|
||||
var cb = document.getElementById('mSegCb' + idx);
|
||||
if (cb) cb.checked = (pos < 0);
|
||||
var row = cb ? cb.closest('.seg-row') : null;
|
||||
if (row) row.className = 'seg-row' + (pos < 0 ? ' selected' : ' dimmed');
|
||||
updateManualSegCount();
|
||||
updateManualBtn();
|
||||
}
|
||||
|
||||
function selectManualSegments() {
|
||||
manualSegSelected = [];
|
||||
for (var i = 0; i < cutPoints.length + 1; i++) manualSegSelected.push(i);
|
||||
manualSegChanged = true;
|
||||
renderCutList();
|
||||
}
|
||||
|
||||
|
||||
|
||||
function updateManualSegCount() {
|
||||
var total = cutPoints.length + 1;
|
||||
var el = document.getElementById('manualSegCount');
|
||||
if (el) el.textContent = '已选 ' + manualSegSelected.length + '/' + total;
|
||||
}
|
||||
|
||||
function formatTime(sec) {
|
||||
var m = Math.floor(sec / 60);
|
||||
var s = (sec % 60).toFixed(2);
|
||||
if (s < 10) s = '0' + s;
|
||||
return m + ':' + s;
|
||||
}
|
||||
|
||||
function parseTime(str) {
|
||||
str = str.trim();
|
||||
if (str.indexOf(':') !== -1) {
|
||||
var parts = str.split(':');
|
||||
return parseFloat(parts[0]) * 60 + parseFloat(parts[1]);
|
||||
}
|
||||
return parseFloat(str);
|
||||
}
|
||||
|
||||
// ── 时间轴 (HTML div 实现,参考 audio-cutter 项目) ────────────────
|
||||
var waveformPeaks = null;
|
||||
|
||||
function renderTimeline(readOnly) {
|
||||
var bar = document.getElementById('timelineBar');
|
||||
if (!bar || audioDuration <= 0) return;
|
||||
|
||||
bar.className = readOnly ? 'readonly' : '';
|
||||
|
||||
var html = '<canvas id="waveformCanvas"></canvas>';
|
||||
// 时间刻度(在波形条上方)
|
||||
var step = Math.max(1, Math.ceil(audioDuration / 10));
|
||||
for (var t = 0; t <= audioDuration; t += step) {
|
||||
var pct = (t / audioDuration * 100).toFixed(2);
|
||||
html += '<div style="position:absolute;left:' + pct + '%;top:0;bottom:0;border-left:1px solid rgba(0,0,0,0.08);z-index:0;"></div>'
|
||||
+ '<div style="position:absolute;left:' + pct + '%;top:-18px;transform:translateX(-50%);font-size:10px;color:#999;pointer-events:none;">' + formatTime(t) + '</div>';
|
||||
}
|
||||
// 切割标记 div(倒三角在上,时间在下)
|
||||
for (var i = 0; i < cutPoints.length; i++) {
|
||||
var pct = (cutPoints[i] / audioDuration * 100).toFixed(2);
|
||||
html += '<div class="cut-marker" data-idx="' + i + '" style="left:' + pct + '%;">'
|
||||
+ '<div class="cut-marker-tri"></div>'
|
||||
+ '<div class="cut-marker-line"></div>'
|
||||
+ '<div class="cut-marker-label">' + formatTime(cutPoints[i]) + '</div></div>';
|
||||
}
|
||||
bar.innerHTML = html;
|
||||
|
||||
// 绘制波形
|
||||
if (waveformPeaks) drawWaveform();
|
||||
|
||||
if (readOnly) return; // 只读模式不绑定交互事件
|
||||
|
||||
// 时间轴空白区域点击 → 添加切割点
|
||||
if (!bar._bound) {
|
||||
bar._bound = true;
|
||||
bar.addEventListener('click', function(e) {
|
||||
if (bar._suppressClick) { bar._suppressClick = false; return; }
|
||||
if (bar.classList.contains('readonly')) return;
|
||||
if (e.target.closest('.cut-marker')) return;
|
||||
var rect = bar.getBoundingClientRect();
|
||||
var sec = (e.clientX - rect.left) / rect.width * audioDuration;
|
||||
if (sec > 0.5 && sec < audioDuration - 0.5) addCutPoint(sec);
|
||||
});
|
||||
}
|
||||
|
||||
// 为每个标记绑定拖动
|
||||
var markers = bar.querySelectorAll('.cut-marker');
|
||||
for (var i = 0; i < markers.length; i++) {
|
||||
bindMarkerDrag(markers[i], bar);
|
||||
}
|
||||
}
|
||||
|
||||
function drawWaveform() {
|
||||
var canvas = document.getElementById('waveformCanvas');
|
||||
if (!canvas || !waveformPeaks || waveformPeaks.length === 0) return;
|
||||
var bar = document.getElementById('timelineBar');
|
||||
canvas.width = bar.clientWidth;
|
||||
canvas.height = bar.clientHeight;
|
||||
var ctx = canvas.getContext('2d');
|
||||
var W = canvas.width, H = canvas.height;
|
||||
var peaks = waveformPeaks;
|
||||
var barW = W / peaks.length;
|
||||
var midY = H / 2;
|
||||
|
||||
// 背景渐变
|
||||
var grad = ctx.createLinearGradient(0, 0, 0, H);
|
||||
grad.addColorStop(0, '#b3d4fc');
|
||||
grad.addColorStop(0.5, '#0078d4');
|
||||
grad.addColorStop(1, '#b3d4fc');
|
||||
ctx.fillStyle = grad;
|
||||
|
||||
for (var i = 0; i < peaks.length; i++) {
|
||||
var h = peaks[i] * midY * 0.9;
|
||||
var x = i * barW;
|
||||
ctx.fillRect(x, midY - h, Math.max(barW - 0.5, 1), h * 2);
|
||||
}
|
||||
}
|
||||
|
||||
async function loadWaveform() {
|
||||
if (!taskId) return;
|
||||
try {
|
||||
var res = await fetch('/audio-slicer/waveform-peaks/' + taskId + '?samples=500');
|
||||
var data = await res.json();
|
||||
if (data.peaks) {
|
||||
var max = 0;
|
||||
for (var i = 0; i < data.peaks.length; i++) {
|
||||
if (data.peaks[i] > max) max = data.peaks[i];
|
||||
}
|
||||
if (max > 0) {
|
||||
for (var i = 0; i < data.peaks.length; i++) {
|
||||
data.peaks[i] = data.peaks[i] / max;
|
||||
}
|
||||
}
|
||||
waveformPeaks = data.peaks;
|
||||
drawWaveform();
|
||||
}
|
||||
} catch (e) {}
|
||||
}
|
||||
|
||||
function bindMarkerDrag(el, bar) {
|
||||
el.addEventListener('pointerdown', function(e) {
|
||||
if (e.button === 2) return;
|
||||
e.preventDefault();
|
||||
e.stopPropagation();
|
||||
el.style.touchAction = 'none';
|
||||
var startX = e.clientX;
|
||||
var hasMoved = false;
|
||||
var idx = parseInt(el.dataset.idx);
|
||||
|
||||
function onMove(e2) {
|
||||
e2.preventDefault();
|
||||
var dx = e2.clientX - startX;
|
||||
if (!hasMoved && Math.abs(dx) >= 5) {
|
||||
hasMoved = true;
|
||||
el.style.cursor = 'grabbing';
|
||||
el.style.zIndex = '10';
|
||||
}
|
||||
if (hasMoved) {
|
||||
var rect = bar.getBoundingClientRect();
|
||||
var sec = (e2.clientX - rect.left) / rect.width * audioDuration;
|
||||
sec = Math.round(sec * 100) / 100;
|
||||
sec = Math.max(0.1, Math.min(audioDuration - 0.1, sec));
|
||||
cutPoints[idx] = sec;
|
||||
el.style.left = (sec / audioDuration * 100).toFixed(2) + '%';
|
||||
el.querySelector('.cut-marker-label').textContent = formatTime(sec);
|
||||
startX = e2.clientX;
|
||||
}
|
||||
}
|
||||
|
||||
function onUp() {
|
||||
document.removeEventListener('pointermove', onMove);
|
||||
document.removeEventListener('pointerup', onUp);
|
||||
el.style.touchAction = '';
|
||||
el.style.cursor = '';
|
||||
el.style.zIndex = '';
|
||||
if (hasMoved) {
|
||||
cutPoints.sort(function(a, b) { return a - b; });
|
||||
renderCutList();
|
||||
renderTimeline();
|
||||
markCutPointsChanged();
|
||||
bar._suppressClick = true;
|
||||
} else {
|
||||
removeCutPoint(idx);
|
||||
}
|
||||
}
|
||||
|
||||
document.addEventListener('pointermove', onMove);
|
||||
document.addEventListener('pointerup', onUp);
|
||||
});
|
||||
}
|
||||
|
||||
// ── 手动切割 ──────────────────────────────────────────────────────
|
||||
async function doManualSlice() {
|
||||
if (!taskId || cutPoints.length === 0) return;
|
||||
if (manualSegSelected.length === 0) { showMessage('请至少选择一个片段', 'error'); return; }
|
||||
var btn = document.getElementById('btnManualSlice');
|
||||
btn.disabled = true;
|
||||
btn.innerHTML = '<i class="fas fa-refresh"></i> 处理中...';
|
||||
|
||||
// 先提交切割点获取片段
|
||||
try {
|
||||
var res = await fetch('/audio-slicer/manual-slice', {
|
||||
method: 'POST', headers: {'Content-Type': 'application/json'},
|
||||
body: JSON.stringify({ task_id: taskId, cut_points: cutPoints })
|
||||
});
|
||||
var data = await res.json();
|
||||
if (!data.success) {
|
||||
showMessage(data.error, 'error');
|
||||
btn.disabled = false;
|
||||
btn.innerHTML = '<i class="fas fa-scissors"></i> 开始切割';
|
||||
return;
|
||||
}
|
||||
document.getElementById('emptyState').style.display = 'none';
|
||||
document.getElementById('resultArea').style.display = 'block';
|
||||
document.getElementById('statusText').textContent = '已标记 ' + data.count + ' 段,开始切割...';
|
||||
renderPreview(data.preview, data.sample_rate);
|
||||
} catch (e) {
|
||||
showMessage('请求失败: ' + e.message, 'error');
|
||||
btn.disabled = false;
|
||||
btn.innerHTML = '<i class="fas fa-scissors"></i> 开始切割';
|
||||
return;
|
||||
}
|
||||
|
||||
// 切割选中的片段
|
||||
document.getElementById('progressWrap').style.display = 'block';
|
||||
document.getElementById('progressFill').style.width = '0%';
|
||||
document.getElementById('topActions').style.display = 'none';
|
||||
document.getElementById('sliceList').innerHTML = '';
|
||||
|
||||
try {
|
||||
var res = await fetch('/audio-slicer/slice', {
|
||||
method: 'POST', headers: {'Content-Type': 'application/json'},
|
||||
body: JSON.stringify({ task_id: taskId, selected_indices: manualSegSelected })
|
||||
});
|
||||
var data = await res.json();
|
||||
if (!data.success) {
|
||||
showMessage(data.error, 'error');
|
||||
btn.disabled = false;
|
||||
btn.innerHTML = '<i class="fas fa-scissors"></i> 开始切割';
|
||||
return;
|
||||
}
|
||||
cutPointsChanged = false;
|
||||
document.getElementById('redoWrap').style.display = 'none';
|
||||
startPoll();
|
||||
} catch (e) {
|
||||
showMessage('请求失败: ' + e.message, 'error');
|
||||
btn.disabled = false;
|
||||
btn.innerHTML = '<i class="fas fa-scissors"></i> 开始切割';
|
||||
}
|
||||
}
|
||||
|
||||
function markCutPointsChanged() {
|
||||
if (!cutPointsChanged && document.getElementById('resultArea').style.display !== 'none') {
|
||||
cutPointsChanged = true;
|
||||
document.getElementById('redoWrap').style.display = 'block';
|
||||
}
|
||||
}
|
||||
|
||||
async function redoManualSlice() {
|
||||
if (!taskId || cutPoints.length === 0) return;
|
||||
if (manualSegSelected.length === 0) { showMessage('请至少选择一个片段', 'error'); return; }
|
||||
document.getElementById('redoWrap').style.display = 'none';
|
||||
cutPointsChanged = false;
|
||||
// 跳过重新预览,直接用当前勾选切割
|
||||
var btn = document.getElementById('btnManualSlice');
|
||||
btn.disabled = true;
|
||||
btn.innerHTML = '<i class="fas fa-refresh"></i> 切割中...';
|
||||
document.getElementById('progressWrap').style.display = 'block';
|
||||
document.getElementById('progressFill').style.width = '0%';
|
||||
document.getElementById('statusText').textContent = '正在重新切割...';
|
||||
document.getElementById('topActions').style.display = 'none';
|
||||
document.getElementById('sliceList').innerHTML = '';
|
||||
|
||||
try {
|
||||
var res = await fetch('/audio-slicer/slice', {
|
||||
method: 'POST', headers: {'Content-Type': 'application/json'},
|
||||
body: JSON.stringify({ task_id: taskId, selected_indices: manualSegSelected })
|
||||
});
|
||||
var data = await res.json();
|
||||
if (!data.success) {
|
||||
showMessage(data.error, 'error');
|
||||
btn.disabled = false;
|
||||
btn.innerHTML = '<i class="fas fa-scissors"></i> 开始切割';
|
||||
return;
|
||||
}
|
||||
startPoll();
|
||||
} catch (e) {
|
||||
showMessage('请求失败: ' + e.message, 'error');
|
||||
btn.disabled = false;
|
||||
btn.innerHTML = '<i class="fas fa-scissors"></i> 开始切割';
|
||||
}
|
||||
}
|
||||
renderCutList();
|
||||
</script>
|
||||
</body>
|
||||
</html>
|
||||
|
||||
Reference in New Issue
Block a user