Commit 01c3336f authored by Jeff nan's avatar Jeff nan

feat

parent 5665c6f3
......@@ -383,12 +383,46 @@
.waveform-tune-handle--end span {
background: #f56c6c;
}
.waveform-tune-playhead {
position: absolute;
top: 0;
bottom: 0;
width: 2px;
margin-left: -1px;
background: #e6a23c;
box-shadow: 0 0 6px rgba(230, 162, 60, 0.85);
z-index: 3;
pointer-events: none;
}
.waveform-tune-playhead span {
position: absolute;
bottom: 6px;
left: 50%;
transform: translateX(-50%);
padding: 1px 6px;
border-radius: 3px;
font-size: 12px;
color: #fff;
background: #e6a23c;
white-space: nowrap;
pointer-events: none;
}
.waveform-tune-error {
margin-top: 12px;
color: #f56c6c;
font-size: 13px;
line-height: 1.5;
}
.waveform-tune-actions {
display: flex;
align-items: center;
gap: 10px;
margin-top: 12px;
}
.waveform-tune-actions__tip {
color: #909399;
font-size: 12px;
}
/* 提示包裹,保证禁用按钮仍可触发 tooltip */
.disabled-tip {
......
......@@ -393,6 +393,22 @@
class="waveform-tune-selection"
:style="{ left: waveformStartPct + '%', width: Math.max(0, waveformEndPct - waveformStartPct) + '%' }"
></div>
<div
v-if="waveformPlayheadPct != null"
class="waveform-tune-playhead"
:style="{ left: waveformPlayheadPct + '%' }"
>
<span>{{ formatTime(waveformDialog.playheadSec) }}</span>
</div>
</div>
<div class="waveform-tune-actions">
<el-button
size="small"
type="success"
:disabled="!!waveformDialog.loading || !!waveformDialog.error"
@click="toggleWaveformPreview"
>{{ waveformDialog.playing ? '停止' : '播放' }}</el-button>
<span class="waveform-tune-actions__tip">试听当前选中区间</span>
</div>
<div v-if="waveformDialog.error" class="waveform-tune-error">{{ waveformDialog.error }}</div>
</div>
......
......@@ -109,6 +109,8 @@ function initVueApp(savedData) {
endSec: 0,
peaks: [],
dragging: null, // 'start' | 'end'
playing: false,
playheadSec: null, // 播放头当前位置(绝对秒)
},
},
computed: {
......@@ -124,6 +126,14 @@ function initVueApp(savedData) {
if (!span || span <= 0) return 100;
return ((d.endSec - d.windowStart) / span) * 100;
},
waveformPlayheadPct() {
const d = this.waveformDialog;
if (d.playheadSec == null || isNaN(d.playheadSec)) return null;
const span = d.windowEnd - d.windowStart;
if (!span || span <= 0) return null;
const pct = ((d.playheadSec - d.windowStart) / span) * 100;
return Math.max(0, Math.min(100, pct));
},
},
mounted() {
// 如果有保存的数据,恢复状态
......@@ -726,11 +736,18 @@ function initVueApp(savedData) {
return '';
},
// 停止句子试听
stopSentencePreview() {
// 只停引擎,不改 UI 状态(供切换播放时内部调用)
_stopPreviewEngine() {
this.stopPlayheadTracking();
if (this._previewBufferSource) {
try { this._previewBufferSource.onended = null; } catch (e) {}
try { this._previewBufferSource.stop(); } catch (e2) {}
try { this._previewBufferSource.disconnect(); } catch (e3) {}
this._previewBufferSource = null;
}
const audio = this.$refs.sentencePreviewAudio;
if (audio) {
try { audio.pause(); } catch (e) {}
try { audio.pause(); } catch (e4) {}
if (this._previewTimeUpdate) {
audio.removeEventListener('timeupdate', this._previewTimeUpdate);
this._previewTimeUpdate = null;
......@@ -740,33 +757,129 @@ function initVueApp(savedData) {
this._previewEnded = null;
}
}
},
// 停止试听(HTML Audio + Web Audio)
stopSentencePreview() {
this._stopPreviewEngine();
this.previewPlayingIndex = -1;
if (this.waveformDialog) {
this.waveformDialog.playing = false;
this.waveformDialog.playheadSec = null;
}
},
stopPlayheadTracking() {
if (this._previewRaf != null) {
cancelAnimationFrame(this._previewRaf);
this._previewRaf = null;
}
this._previewRafActive = false;
this._playheadMode = null;
this._playheadOffset = 0;
this._playheadCtxStart = 0;
this._playheadEndSec = 0;
},
// 启动播放头跟踪(波形弹窗可见时才更新 UI)
startPlayheadTracking(mode, startSec, endSec) {
this.stopPlayheadTracking();
this._playheadMode = mode; // 'webaudio' | 'html'
this._playheadOffset = startSec;
this._playheadEndSec = endSec;
if (mode === 'webaudio' && this._previewAudioCtx) {
this._playheadCtxStart = this._previewAudioCtx.currentTime;
}
if (this.waveformDialog && this.waveformDialog.visible) {
this.waveformDialog.playheadSec = startSec;
}
this._previewRafActive = true;
const tick = () => {
if (!this._previewRafActive) return;
let now = startSec;
if (this._playheadMode === 'webaudio' && this._previewAudioCtx) {
now = this._playheadOffset + (this._previewAudioCtx.currentTime - this._playheadCtxStart);
} else if (this._playheadMode === 'html') {
const audio = this.$refs.sentencePreviewAudio;
now = audio ? audio.currentTime : now;
}
if (this.waveformDialog && this.waveformDialog.visible) {
this.waveformDialog.playheadSec = Math.min(now, this._playheadEndSec);
}
if (now >= this._playheadEndSec - 0.001) {
// Web Audio 会自己 onended;HTML 路径在这里停
if (this._playheadMode === 'html') {
const audio = this.$refs.sentencePreviewAudio;
if (audio) {
try { audio.pause(); } catch (e) {}
try { audio.currentTime = this._playheadEndSec; } catch (e2) {}
}
this.stopSentencePreview();
return;
}
}
this._previewRaf = requestAnimationFrame(tick);
};
this._previewRaf = requestAnimationFrame(tick);
},
// 试听某一句:播放 start ~ end
async previewSentence(idx) {
if (this.previewPlayingIndex === idx) {
this.stopSentencePreview();
return;
// 毫秒级精准播放区间:优先 Web Audio start(offset, duration)
async playPreciseRange(startSec, endSec) {
if (!(endSec > startSec)) {
this.$message.warning('播放区间无效');
return false;
}
const item = this.subtitleEditor.mergedList && this.subtitleEditor.mergedList[idx];
if (!item) return;
const startSec = this.parseTimeToSeconds(item.start);
const endSec = this.parseTimeToSeconds(item.end);
if (isNaN(startSec) || isNaN(endSec) || endSec <= startSec) {
this.$message.warning('该句时间无效,无法试听');
return;
if (!this.getPreviewMediaSrc()) {
this.$message.warning('暂无可用音视频,无法播放');
return false;
}
const src = this.getPreviewMediaSrc();
if (!src) {
this.$message.warning('暂无可用音视频,无法试听');
return;
this._stopPreviewEngine();
// 先尝试 Web Audio(采样级精度)
try {
const buffer = await this.ensurePreviewAudioBuffer();
const ctx = this._previewAudioCtx;
if (!ctx) throw new Error('no audio ctx');
if (ctx.state === 'suspended') await ctx.resume();
const offset = Math.max(0, Math.min(startSec, buffer.duration));
const stopAt = Math.max(offset, Math.min(endSec, buffer.duration));
const duration = stopAt - offset;
if (!(duration > 0)) {
this.$message.warning('播放区间无效');
return false;
}
const source = ctx.createBufferSource();
source.buffer = buffer;
source.connect(ctx.destination);
this._previewBufferSource = source;
source.onended = () => {
if (this._previewBufferSource === source) {
this._previewBufferSource = null;
this.previewPlayingIndex = -1;
if (this.waveformDialog) {
this.waveformDialog.playing = false;
this.waveformDialog.playheadSec = null;
}
this.stopPlayheadTracking();
}
};
// when=0 立即播;offset/duration 单位秒,按采样率裁剪,可到毫秒级
source.start(0, offset, duration);
this.startPlayheadTracking('webaudio', offset, stopAt);
return true;
} catch (e) {
console.warn('Web Audio 精准播放失败,回退 HTMLAudio+rAF', e);
}
this.stopSentencePreview();
// 回退:HTMLAudio + requestAnimationFrame
const audio = this.$refs.sentencePreviewAudio;
if (!audio) return;
if (!audio) {
this.$message.error('播放失败');
return false;
}
const src = this.getPreviewMediaSrc();
const needReload = !audio.src || audio.getAttribute('data-src') !== src;
if (needReload) {
audio.src = src;
......@@ -783,35 +896,55 @@ function initVueApp(savedData) {
audio.addEventListener('error', onErr);
audio.load();
});
} catch (e) {
} catch (err) {
this.$message.error('音频加载失败');
return;
return false;
}
}
this.previewPlayingIndex = idx;
try {
audio.currentTime = Math.max(0, startSec);
} catch (e) {}
this._previewTimeUpdate = () => {
if (audio.currentTime >= endSec - 0.03) {
this.stopSentencePreview();
}
};
this._previewEnded = () => {
this.stopSentencePreview();
};
audio.addEventListener('timeupdate', this._previewTimeUpdate);
audio.addEventListener('ended', this._previewEnded);
} catch (e5) {}
try {
await audio.play();
} catch (e) {
console.error(e);
this.startPlayheadTracking('html', startSec, endSec);
return true;
} catch (e8) {
console.error(e8);
this.$message.error('播放失败');
this.stopSentencePreview();
return false;
}
},
// 波形弹窗:播放 / 停止当前选中区间
async toggleWaveformPreview() {
if (this.waveformDialog.playing) {
this.stopSentencePreview();
return;
}
const d = this.waveformDialog;
if (d.loading || d.error) return;
const ok = await this.playPreciseRange(d.startSec, d.endSec);
d.playing = !!ok;
if (!ok) d.playheadSec = null;
},
// 试听某一句:播放 start ~ end
async previewSentence(idx) {
if (this.previewPlayingIndex === idx) {
this.stopSentencePreview();
return;
}
const item = this.subtitleEditor.mergedList && this.subtitleEditor.mergedList[idx];
if (!item) return;
const startSec = this.parseTimeToSeconds(item.start);
const endSec = this.parseTimeToSeconds(item.end);
if (isNaN(startSec) || isNaN(endSec) || endSec <= startSec) {
this.$message.warning('该句时间无效,无法试听');
return;
}
const ok = await this.playPreciseRange(startSec, endSec);
this.previewPlayingIndex = ok ? idx : -1;
},
// 在当前句后面插入一组空句子
......@@ -1171,8 +1304,11 @@ function initVueApp(savedData) {
},
onWaveformDialogClosed() {
this.stopSentencePreview();
this.unbindWaveformDragListeners();
this.waveformDialog.dragging = null;
this.waveformDialog.playing = false;
this.waveformDialog.playheadSec = null;
this.waveformDialog.peaks = [];
this.waveformDialog.error = '';
this.waveformDialog.loading = false;
......@@ -1959,72 +2095,53 @@ function initVueApp(savedData) {
return inter / uni;
},
// 应用修改回原始数组(保存/重新生成时才真正按句子 end 对齐并写回)
// 应用修改回原始数组(保存/重新生成时才真正按句子时间写回)
// 不再输出 words:界面已改为句子级编辑,单词颗粒度会与文本不一致
applyEditorChanges() {
const merged = this.subtitleEditor.mergedList || [];
// 关键修复:根据 mergedList 重新构建数组,确保“拆分后的第二句”被真实写回
const newSList = [];
const newTList = [];
// 强制使用“数值秒 Number + 三位小数”的格式
const sFmt = { kind: 'number', unit: 'sec', decimals: 3 };
const tFmt = { kind: 'number', unit: 'sec', decimals: 3 };
for (let i = 0; i < merged.length; i++) {
const m = merged[i];
if (!m) continue;
const sWords = Array.isArray(m.sWordsRef) ? m.sWordsRef.slice() : [];
const tSegs = Array.isArray(m.tItemsRef) ? m.tItemsRef.slice() : [];
// 以界面句子时间为准;保存时强制首/末词、翻译首/末段与之对齐
let sStartSec = this.parseTimeToSeconds(m.start);
if (isNaN(sStartSec) && sWords.length) {
sStartSec = this.parseTimeToSeconds(sWords[0].start);
}
let sEndSec = this.parseTimeToSeconds(m.end);
if (isNaN(sEndSec) && sWords.length) {
sEndSec = this.parseTimeToSeconds(sWords[sWords.length - 1].end);
if (isNaN(sStartSec) && Array.isArray(m.sWordsRef) && m.sWordsRef[0]) {
sStartSec = this.parseTimeToSeconds(m.sWordsRef[0].start);
}
if (!isNaN(sStartSec)) {
if (sWords.length) {
const first = sWords[0];
sWords[0] = Object.assign({}, first, {
start: this.serializeTimeByFormat(sStartSec, this.timeWriteFormat(first)),
});
}
if (tSegs.length) {
const firstT = tSegs[0];
tSegs[0] = Object.assign({}, firstT, {
start: this.serializeTimeByFormat(sStartSec, this.timeWriteFormat(firstT)),
});
}
if (isNaN(sEndSec) && Array.isArray(m.sWordsRef) && m.sWordsRef.length) {
sEndSec = this.parseTimeToSeconds(m.sWordsRef[m.sWordsRef.length - 1].end);
}
if (!isNaN(sEndSec)) {
if (sWords.length) {
const last = sWords[sWords.length - 1];
sWords[sWords.length - 1] = Object.assign({}, last, {
end: this.serializeTimeByFormat(sEndSec, this.timeWriteFormat(last)),
});
}
if (tSegs.length) {
const lastT = tSegs[tSegs.length - 1];
tSegs[tSegs.length - 1] = Object.assign({}, lastT, {
end: this.serializeTimeByFormat(sEndSec, this.timeWriteFormat(lastT)),
});
}
// 翻译首/末段时间与句子对齐(仅内存展示用;写回仍带 translatedItems)
if (!isNaN(sStartSec) && tSegs.length) {
const firstT = tSegs[0];
tSegs[0] = Object.assign({}, firstT, {
start: this.serializeTimeByFormat(sStartSec, this.timeWriteFormat(firstT)),
});
}
if (!isNaN(sEndSec) && tSegs.length) {
const lastT = tSegs[tSegs.length - 1];
tSegs[tSegs.length - 1] = Object.assign({}, lastT, {
end: this.serializeTimeByFormat(sEndSec, this.timeWriteFormat(lastT)),
});
}
const sStartOut = this.serializeTimeByFormat(sStartSec, sFmt);
const sEndOut = this.serializeTimeByFormat(sEndSec, sFmt);
const enText = (typeof m.subtitleText === 'string')
? m.subtitleText
: this.buildSentenceFromWords(sWords);
: this.buildSentenceFromWords(m.sWordsRef);
newSList.push({
start: sStartOut,
end: sEndOut,
words: sWords,
text: enText,
});
// 翻译条目 start/end 与句子 start/end 实际对齐
const tStartSec = !isNaN(sStartSec) ? sStartSec : this.parseTimeToSeconds(tSegs.length ? tSegs[0].start : sStartSec);
const tEndSec = !isNaN(sEndSec) ? sEndSec : this.parseTimeToSeconds(tSegs.length ? tSegs[tSegs.length - 1].end : sEndSec);
const tStartOut = this.serializeTimeByFormat(tStartSec, tFmt);
......@@ -2039,10 +2156,8 @@ function initVueApp(savedData) {
translated: zhText,
});
}
// 用新数组覆盖旧数组(确保下一步导出/覆盖时包含拆分后的完整结果)
this.subtitleEditor.subtitleList = newSList;
this.subtitleEditor.translatedList = newTList;
// 同步清空历史标记
this.subtitleEditor.clearedSIndices = [];
this.subtitleEditor.clearedTIndices = [];
this.$message.success('修改已应用到内存');
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment