feat: Audio Preview Player — browse and play WAV samples via Web Audio API
Issue: #9 Backend: - GET /api/list-wavs?dir=<path> — lists .wav files in a directory - GET /wavs/<file> — static file serving from ./output/wavs Web UI: - WAV Browser section with path input + Browse button - AudioContext-based playback (zero deps) — progress bar, time display - WAV file grid with Play buttons, active item highlighting Tests: - 3 new tests for handleListWavs (wav files, empty dir, nonexistent dir) - Fix TestFindAkaiutil: add t.Chdir(tmp) so relative path lookups don't accidentally find the repo's third_party/akaiutil/akaiutil binary
This commit is contained in:
+123
@@ -183,6 +183,129 @@ async function doExtract() {
|
||||
}
|
||||
}
|
||||
|
||||
// ─── WAV Browser ──────────────────────────────────────────────────
|
||||
|
||||
let audioCtx = null;
|
||||
let currentSource = null;
|
||||
let currentAudioBuffer = null;
|
||||
let startTime = 0;
|
||||
let pauseOffset = 0;
|
||||
let isPlaying = false;
|
||||
let animationFrame = null;
|
||||
|
||||
document.getElementById('wav-browse-btn').addEventListener('click', browseWavs);
|
||||
|
||||
async function browseWavs() {
|
||||
const dir = document.getElementById('wav-dir').value;
|
||||
const list = document.getElementById('wav-list');
|
||||
list.innerHTML = 'Loading...';
|
||||
|
||||
try {
|
||||
const resp = await fetch(`${API}/list-wavs?dir=${encodeURIComponent(dir)}`);
|
||||
const data = await resp.json();
|
||||
if (data.error) {
|
||||
list.innerHTML = '<span class="no-files">Error: ' + data.error + '</span>';
|
||||
return;
|
||||
}
|
||||
if (!Array.isArray(data) || data.length === 0) {
|
||||
list.innerHTML = '<span class="no-files">No WAV files found</span>';
|
||||
return;
|
||||
}
|
||||
list.innerHTML = data.map(f => `
|
||||
<div class="wav-item" data-file="${esc(f)}">
|
||||
<span class="wav-name" title="${esc(f)}">${esc(f)}</span>
|
||||
<button class="wav-play-btn" onclick="playWav('${esc(f)}')">Play</button>
|
||||
</div>
|
||||
`).join('');
|
||||
} catch (e) {
|
||||
list.innerHTML = 'Failed: ' + e.message;
|
||||
}
|
||||
}
|
||||
|
||||
async function playWav(filename) {
|
||||
if (!audioCtx) {
|
||||
audioCtx = new AudioContext();
|
||||
}
|
||||
|
||||
if (currentSource) {
|
||||
currentSource.stop();
|
||||
currentSource = null;
|
||||
}
|
||||
if (animationFrame) {
|
||||
cancelAnimationFrame(animationFrame);
|
||||
animationFrame = null;
|
||||
}
|
||||
|
||||
const nowPlaying = document.getElementById('wav-now-playing');
|
||||
nowPlaying.textContent = 'Loading: ' + filename;
|
||||
|
||||
try {
|
||||
const resp = await fetch(`/wavs/${encodeURIComponent(filename)}`);
|
||||
if (!resp.ok) throw new Error('HTTP ' + resp.status);
|
||||
const arrayBuffer = await resp.arrayBuffer();
|
||||
currentAudioBuffer = await audioCtx.decodeAudioData(arrayBuffer);
|
||||
|
||||
currentSource = audioCtx.createBufferSource();
|
||||
currentSource.buffer = currentAudioBuffer;
|
||||
currentSource.connect(audioCtx.destination);
|
||||
|
||||
currentSource.onended = () => {
|
||||
if (isPlaying) {
|
||||
isPlaying = false;
|
||||
pauseOffset = 0;
|
||||
nowPlaying.textContent = '';
|
||||
document.getElementById('wav-progress-fill').style.width = '0%';
|
||||
document.getElementById('wav-time').textContent = formatTime(0);
|
||||
clearActiveItem();
|
||||
}
|
||||
};
|
||||
|
||||
startTime = audioCtx.currentTime - pauseOffset;
|
||||
currentSource.start(0, pauseOffset);
|
||||
isPlaying = true;
|
||||
pauseOffset = 0;
|
||||
|
||||
nowPlaying.textContent = '▶ ' + filename;
|
||||
setActiveItem(filename);
|
||||
updateProgress();
|
||||
} catch (e) {
|
||||
nowPlaying.textContent = 'Error: ' + e.message;
|
||||
}
|
||||
}
|
||||
|
||||
function updateProgress() {
|
||||
if (!isPlaying || !currentAudioBuffer) return;
|
||||
|
||||
const elapsed = audioCtx.currentTime - startTime;
|
||||
const duration = currentAudioBuffer.duration;
|
||||
const pct = Math.min((elapsed / duration) * 100, 100);
|
||||
|
||||
document.getElementById('wav-progress-fill').style.width = pct + '%';
|
||||
document.getElementById('wav-time').textContent = formatTime(elapsed);
|
||||
|
||||
if (elapsed < duration) {
|
||||
animationFrame = requestAnimationFrame(updateProgress);
|
||||
}
|
||||
}
|
||||
|
||||
function formatTime(secs) {
|
||||
const m = Math.floor(secs / 60);
|
||||
const s = Math.floor(secs % 60);
|
||||
return m + ':' + s.toString().padStart(2, '0');
|
||||
}
|
||||
|
||||
function setActiveItem(filename) {
|
||||
document.querySelectorAll('.wav-item').forEach(el => {
|
||||
el.classList.toggle('playing', el.dataset.file === filename);
|
||||
});
|
||||
}
|
||||
|
||||
function clearActiveItem() {
|
||||
document.querySelectorAll('.wav-item').forEach(el => {
|
||||
el.classList.remove('playing');
|
||||
});
|
||||
}
|
||||
|
||||
// ─── Helpers ──────────────────────────────────────────────────────
|
||||
|
||||
function esc(s) {
|
||||
|
||||
@@ -39,6 +39,22 @@
|
||||
<div id="extract-status"></div>
|
||||
</section>
|
||||
|
||||
<section id="wav-section">
|
||||
<h2>WAV Browser</h2>
|
||||
<div class="wav-controls">
|
||||
<input type="text" id="wav-dir" placeholder="WAV directory" value="./output/wavs">
|
||||
<button id="wav-browse-btn">Browse</button>
|
||||
</div>
|
||||
<div id="wav-player" class="wav-player">
|
||||
<div id="wav-now-playing" class="now-playing"></div>
|
||||
<div class="audio-bar">
|
||||
<div class="audio-progress"><div id="wav-progress-fill" class="audio-progress-fill"></div></div>
|
||||
<span id="wav-time">0:00</span>
|
||||
</div>
|
||||
<div id="wav-list" class="wav-list"></div>
|
||||
</div>
|
||||
</section>
|
||||
|
||||
<section id="results-section">
|
||||
<h2>Results <span id="result-count"></span></h2>
|
||||
<div id="search-results"></div>
|
||||
|
||||
@@ -205,4 +205,109 @@ h2 {
|
||||
margin-top: 10px;
|
||||
font-size: 13px;
|
||||
color: var(--text2);
|
||||
}
|
||||
|
||||
.wav-controls {
|
||||
display: flex;
|
||||
gap: 8px;
|
||||
flex-wrap: wrap;
|
||||
}
|
||||
.wav-controls input {
|
||||
flex: 1;
|
||||
min-width: 200px;
|
||||
padding: 10px 14px;
|
||||
border: 1px solid var(--bg3);
|
||||
border-radius: var(--radius);
|
||||
background: var(--bg);
|
||||
color: var(--text);
|
||||
font-size: 14px;
|
||||
}
|
||||
.wav-controls button {
|
||||
padding: 10px 20px;
|
||||
border: none;
|
||||
border-radius: var(--radius);
|
||||
background: var(--accent);
|
||||
color: white;
|
||||
font-size: 14px;
|
||||
cursor: pointer;
|
||||
}
|
||||
|
||||
.wav-player {
|
||||
margin-top: 14px;
|
||||
}
|
||||
|
||||
.now-playing {
|
||||
font-size: 13px;
|
||||
color: var(--green);
|
||||
margin-bottom: 6px;
|
||||
min-height: 18px;
|
||||
}
|
||||
|
||||
.audio-bar {
|
||||
display: flex;
|
||||
align-items: center;
|
||||
gap: 10px;
|
||||
margin-bottom: 10px;
|
||||
}
|
||||
|
||||
.audio-progress {
|
||||
flex: 1;
|
||||
height: 6px;
|
||||
background: var(--bg3);
|
||||
border-radius: 3px;
|
||||
overflow: hidden;
|
||||
}
|
||||
|
||||
.audio-progress-fill {
|
||||
height: 100%;
|
||||
background: var(--green);
|
||||
border-radius: 3px;
|
||||
width: 0%;
|
||||
transition: width 0.1s;
|
||||
}
|
||||
|
||||
#wav-time {
|
||||
font-size: 12px;
|
||||
color: var(--text2);
|
||||
min-width: 40px;
|
||||
}
|
||||
|
||||
.wav-list {
|
||||
display: grid;
|
||||
grid-template-columns: repeat(auto-fill, minmax(200px, 1fr));
|
||||
gap: 8px;
|
||||
}
|
||||
|
||||
.wav-item {
|
||||
background: var(--bg);
|
||||
border: 1px solid var(--bg3);
|
||||
border-radius: var(--radius);
|
||||
padding: 10px 12px;
|
||||
display: flex;
|
||||
align-items: center;
|
||||
gap: 8px;
|
||||
cursor: pointer;
|
||||
}
|
||||
.wav-item:hover {
|
||||
border-color: var(--accent);
|
||||
}
|
||||
.wav-item.playing {
|
||||
border-color: var(--green);
|
||||
background: rgba(78, 204, 163, 0.1);
|
||||
}
|
||||
.wav-item .wav-name {
|
||||
flex: 1;
|
||||
font-size: 13px;
|
||||
overflow: hidden;
|
||||
text-overflow: ellipsis;
|
||||
white-space: nowrap;
|
||||
}
|
||||
.wav-item .wav-play-btn {
|
||||
background: var(--accent2);
|
||||
color: white;
|
||||
border: none;
|
||||
border-radius: 4px;
|
||||
padding: 4px 10px;
|
||||
font-size: 11px;
|
||||
cursor: pointer;
|
||||
}
|
||||
Reference in New Issue
Block a user