samplepack-builder: slice one WAV (e.g. a GarageBand export) across pads by beats

Tempo (default 120 BPM) and beats per pad (default 4); the file is cut from 0:00
into equal slices filling pads 1..16, silent slices at the end are dropped and
slices past 16 reported. Boundaries are rounded per slice, so odd tempos don't drift.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
Reactorcoremeltdown
2026-11-17 11:43:23 +01:00
co-authored by Claude Opus 5.5
parent 7041f762e5
commit 6939dccbf0
+97 -2
View File
@@ -20,9 +20,12 @@
.bar{display:flex;flex-wrap:wrap;gap:10px;align-items:flex-end;
background:var(--surface);border:1px solid var(--grid);padding:12px;margin-bottom:16px}
label{display:block;font-size:10px;color:var(--dim);margin-bottom:4px;text-transform:uppercase;letter-spacing:.5px}
input[type=text]{background:var(--bg);border:1px solid var(--grid);color:var(--text);
input[type=text],input[type=number]{background:var(--bg);border:1px solid var(--grid);color:var(--text);
font-family:inherit;font-size:14px;padding:8px 10px;width:220px;outline:none}
input[type=text]:focus{border-color:var(--accent)}
input[type=number]{width:96px}
input[type=text]:focus,input[type=number]:focus{border-color:var(--accent)}
.bar .note{font-size:11px;color:var(--dim);align-self:center;max-width:300px;line-height:1.4}
button.over{border-color:var(--play);color:var(--play)}
button{font-family:inherit;font-size:12px;background:var(--surface);color:var(--text);
border:1px solid var(--grid);padding:9px 14px;cursor:pointer;letter-spacing:.5px}
button:hover{border-color:var(--accent);color:var(--accent)}
@@ -78,6 +81,21 @@
<button id="download" class="primary" disabled>⤓ Download .zip</button>
</div>
<!-- One long file (e.g. a GarageBand export) cut into equal slices, one per pad. -->
<div class="bar">
<div>
<label for="bpm">Tempo (BPM)</label>
<input id="bpm" type="number" value="120" min="20" max="400" step="any">
</div>
<div>
<label for="beats">Beats per pad</label>
<input id="beats" type="number" value="4" min="0.25" max="64" step="any">
</div>
<span class="note" id="sliceInfo"></span>
<div class="spacer"></div>
<button id="sliceBtn" title="Or drop the file on this button">✂ Slice one WAV across pads…</button>
</div>
<div class="grid" id="grid"></div>
<div class="status" id="status"></div>
@@ -88,8 +106,16 @@
Pads holding the <strong>same sample</strong> as another pad are tinted <span style="color:var(--danger)">red</span> (matched by audio content, not file name).
Import the downloaded zip via the Sampler's preset toolbar (<code>Import</code>).
</p>
<p class="hint">
<strong>Slicing one file:</strong> export the song from GarageBand as one WAV (Share › Export Song to Disk, WAV),
set its tempo and how many beats each pad gets, then drop the file on <code>✂ Slice one WAV</code> (or click it).
The file is cut at those beat boundaries from its very start (bar 1 must be at 0:00) and the slices fill pads
1, 2, 3… replacing what's there. Silent slices at the end (the export's tail) are left out; a file longer than
16 slices keeps the first 16. Turn on <code>ONE-SHOT</code> in the Sampler so each pad plays its whole slice.
</p>
<input id="filepick" type="file" accept="audio/*,.wav" multiple style="display:none">
<input id="slicepick" type="file" accept="audio/*,.wav" style="display:none">
<script>
"use strict";
@@ -258,6 +284,62 @@ async function loadFiles(pad, fileList){
}
}
// ---- slicing one file into beat-length pads ----
const bpmInput = document.getElementById("bpm");
const beatsInput = document.getElementById("beats");
const sliceInfo = document.getElementById("sliceInfo");
const SILENT = 0.001; // peak below this (-60 dBFS) counts as silence
function sliceSeconds(){
const bpm = parseFloat(bpmInput.value), beats = parseFloat(beatsInput.value);
if (!(bpm >= 20 && bpm <= 400) || !(beats > 0 && beats <= 64)) return 0;
return beats * 60 / bpm;
}
function updateSliceInfo(){
const s = sliceSeconds();
sliceInfo.textContent = s ? ("= " + s.toFixed(3).replace(/\.?0+$/, "") + " s per pad") : "tempo 20-400, beats above 0";
}
// Cut [ab] (decoded) into slices of sliceSeconds(), dropping silent ones at the end,
// and put them on pads 1.. (at most 16). Boundaries are rounded per slice from the
// exact length, so odd tempos don't drift.
function sliceIntoPads(fileName, ab){
const secs = sliceSeconds();
if (!secs){ setStatus("Set a tempo (20-400 BPM) and beats per pad first.", "err"); return 0; }
const mono = toMono(ab), sr = ab.sampleRate, exact = secs * sr;
const total = Math.ceil(mono.length / exact - 1e-9);
const slices = [];
for (let i = 0; i < total; i++){
const a = Math.round(i * exact), b = Math.min(mono.length, Math.round((i + 1) * exact));
if (b > a) slices.push(mono.subarray(a, b));
}
const loud = sl => { for (let i = 0; i < sl.length; i++) if (Math.abs(sl[i]) >= SILENT) return true; return false; };
while (slices.length && !loud(slices[slices.length - 1])) slices.pop();
if (!slices.length){ setStatus("“" + fileName + "” is silent.", "err"); return 0; }
const used = Math.min(PADS, slices.length);
const base = fileName.replace(/\.[^.]+$/, "");
for (let p = 0; p < used; p++){
const data = new Float32Array(slices[p]); // own copy: the WAV writer and the signature read it
const buffer = ctx().createBuffer(1, data.length, sr);
buffer.copyToChannel(data, 0);
pads[p] = { fileName: base + " · " + (p + 1) + "/" + slices.length, mono: data, sampleRate: sr, buffer, sig: sigOf(data, sr) };
}
refresh();
const more = slices.length > PADS ? " (it holds " + slices.length + "; the first 16 were used)" : "";
setStatus("Sliced “" + fileName + "” into " + used + " pad" + (used === 1 ? "" : "s") +
" of " + beatsInput.value + " beats at " + bpmInput.value + " BPM" + more + ".", more ? "err" : "ok");
return used;
}
async function sliceFile(file){
try{
const ab = await ctx().decodeAudioData(await file.arrayBuffer());
sliceIntoPads(file.name, ab);
}catch(e){
setStatus("Could not decode “" + file.name + "” — not a supported audio file.", "err");
}
}
function play(pad){
const s = pads[pad];
if (!s) return;
@@ -309,6 +391,19 @@ for (let p=0;p<PADS;p++){
filepick.addEventListener("change", () => { if (filepick.files.length) loadFiles(pickTarget, filepick.files); });
const sliceBtn = document.getElementById("sliceBtn");
const slicepick = document.getElementById("slicepick");
sliceBtn.addEventListener("click", () => { slicepick.value = ""; slicepick.click(); });
slicepick.addEventListener("change", () => { if (slicepick.files.length) sliceFile(slicepick.files[0]); });
["dragenter","dragover"].forEach(ev => sliceBtn.addEventListener(ev, (e) => { e.preventDefault(); sliceBtn.classList.add("over"); }));
["dragleave","dragend"].forEach(ev => sliceBtn.addEventListener(ev, () => sliceBtn.classList.remove("over")));
sliceBtn.addEventListener("drop", (e) => {
e.preventDefault(); sliceBtn.classList.remove("over");
if (e.dataTransfer && e.dataTransfer.files && e.dataTransfer.files.length) sliceFile(e.dataTransfer.files[0]);
});
[bpmInput, beatsInput].forEach(i => i.addEventListener("input", updateSliceInfo));
updateSliceInfo();
// Prevent the browser from navigating when a file is dropped outside a pad.
["dragover","drop"].forEach(ev => window.addEventListener(ev, (e) => e.preventDefault()));