Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,12 @@ and migration instructions first. Unreleased entries do not imply npm availabili

## Unreleased

- Looping `residentSample` and `residentTakeRecorder` reads return the start
frame for a position a rounding error below the slice start, instead of the
frame at the exclusive end, which could be a stale tail after a shorter reload
or a logical recorder reset. A looping `samplePlayer` position stays inside
its slice after such a step.

## 0.2.0 — 2026-11-12

- **Breaking:** requires `@unworklet/core` **0.6.0**. Update the compiler, offline
Expand Down
6 changes: 4 additions & 2 deletions src/resident-recorder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -60,8 +60,10 @@ export const residentTakeRecorder = defineSubgraph((config: ResidentTakeRecorder
const lo = start.clamp(0, capacity - 1), hi = end.clamp(0, capacity).min(length.read());
const valid = hi.gt(lo);
const safe = select(position.eq(position), position, f64(lo)).clamp(-2147483648, 2147483647);
const span = f64(hi.sub(lo).max(1)), relative = safe.sub(f64(lo));
const wrapped = relative.sub(relative.div(span).floor().mul(span)).add(f64(lo));
const origin = f64(lo), span = f64(hi.sub(lo).max(1)), relative = safe.sub(origin);
const remainder = relative.sub(relative.div(span).floor().mul(span)).add(origin);
// Same rounding guard as residentSample: keep loop reads inside [lo,hi).
const wrapped = select(remainder.gte(origin.add(span)), origin, remainder.max(origin));
readPosition.write((loop ? wrapped : safe.clamp(f64(lo), f64(hi.sub(1).max(lo)))).mul(READ_POSITION_SCALE));
const p = readPosition.read().div(READ_POSITION_SCALE);
const whole = i32(p.floor()).clamp(0, capacity - 1), fraction = p.sub(f64(whole));
Expand Down
9 changes: 6 additions & 3 deletions src/sample.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,9 +33,12 @@ function bounded(value: Node<'f32'>, minimum: number, maximum: number, fallback
return select(value.eq(value), f64(value), f64(fallback)).clamp(minimum, maximum);
}
function wrap(position: Node<'f64'>, start: Node<'i32'>, end: Node<'i32'>) {
const length = f64(end.sub(start).max(1));
const relative = position.sub(f64(start));
return relative.sub(relative.div(length).floor().mul(length)).add(f64(start));
const origin = f64(start), length = f64(end.sub(start).max(1));
const relative = position.sub(origin);
const wrapped = relative.sub(relative.div(length).floor().mul(length)).add(origin);
// A tiny negative offset can round the remainder, or its sum with start, up
// to the exclusive end; an underflowing quotient can leave it just below start.
return select(wrapped.gte(origin.add(length)), origin, wrapped.max(origin));
}

/** A fixed-capacity mono sample, initially missing. No asset loader or streaming. */
Expand Down
21 changes: 21 additions & 0 deletions tests/resident-recorder.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,27 @@ test('logical reset leaves recorded suffix bytes intact while all reads become s
expect(result.outputs.main[2].slice(128)).toEqual(new Float32Array(128));
});

test('loop reads at and just below the slice start return the start frame, never the end frame or cleared bytes', async () => {
// 3 - 2 ** -51 and 1 - 2 ** -53 are the largest doubles below 3 and 1; their wrapped
// positions round to the exclusive end unless the reader keeps them inside the slice.
const reads = [[3, 3, 8], [3 - 2 ** -51, 3, 8], [1, 1, 4], [1 - 2 ** -53, 1, 4]] as const;
const processor = defineProcessor(() => {
const take = instantiate(residentTakeRecorder, { capacity: 8, sampleRate: 48000 }, { name: 'take' });
const input = audioInput({ channels: 3, name: 'controls' }), output = audioOutput({ channels: reads.length, name: 'main' });
return { process() { forSample(i => {
take.tick({ input: input.ch(0).at(i), record: input.ch(1).at(i).gt(0), reset: input.ch(2).at(i).gt(0) });
reads.forEach(([position, start, end], ch) => output.ch(ch).at(i).write(take.sample.read(f64(position), i32(start), i32(end), true)));
}); } };
});
// Take 10..17, a logical reset at 128, then take 1..5 over the cleared bytes 15..17.
const frames = 256, recording = (n: number) => n < 8 || n >= 129 && n < 134;
const controls = [Float32Array.from({ length: frames }, (_, n) => n < 8 ? 10 + n : recording(n) ? n - 128 : 0), Float32Array.from({ length: frames }, (_, n) => +recording(n)), Float32Array.from({ length: frames }, (_, n) => +(n === 128))];
const result = await renderOffline(processor, { sampleRate: 48000, duration: duration(frames, 48000), inputs: { controls } });
[13, 13, 11, 11].forEach((value, ch) => expect(result.outputs.main[ch].slice(8, 128)).toEqual(new Float32Array(120).fill(value)));
[4, 4, 2, 2].forEach((value, ch) => expect(result.outputs.main[ch].slice(133)).toEqual(new Float32Array(frames - 133).fill(value)));
expect(result.diagnostics.scrubbedSamples).toBe(0);
});

test('revision wraps on reset/load and append does not invalidate a growing sample player', async () => {
const processor = defineProcessor(() => {
const take = instantiate(residentTakeRecorder, { capacity: 1024, sampleRate: 48000 }, { name: 'take' });
Expand Down
23 changes: 23 additions & 0 deletions tests/sample.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -204,6 +204,29 @@ test('direct resident read sanitizes nonfinite/out-of-range positions without ex
expect(r.diagnostics.scrubbedSamples).toBe(0);
});

test('loop reads at and just below the slice start return the start frame, never the end frame or a stale tail',async()=>{
// 3-2**-51 and 1-2**-53 are the largest doubles below 3 and 1. Wrapping them
// rounds to the exclusive end, through the start addition (3,5) or the remainder (1,4).
const reads=[[3,3,5],[3-2**-51,3,5],[1,1,4],[1-2**-53,1,4],[3-2**-51,3,8]];
const p=defineProcessor(()=>{
const sample=instantiate(residentSample,{capacity:8,sourceSampleRate:48000},{name:'asset'}),load=event<{data:Float32Array}>({from:'main',name:'load',capacity:CAPACITY_16,payloadCapacity:32});load.onReceive(x=>sample.load(x.data));
const out=audioOutput({channels:reads.length,name:'main'});
return{process(){forSample(i=>reads.forEach(([position,start,end],ch)=>out.ch(ch).at(i).write(sample.read(f64(position),i32(start),i32(end),true))));}};
});
// The shorter reload leaves 20,30,40 from the first load in frames 5..7.
const r=await renderOffline(p,{sampleRate:48000,duration:duration(256,48000),messages:[{name:'load',payload:{data:Float32Array.of(1,2,3,4,10,20,30,40)}},{name:'load',payload:{data:Float32Array.of(-1,-2,-3,-4,-5)},atQuantum:1}]});
[4,4,2,2,4].forEach((v,ch)=>expect(r.outputs.main[ch].slice(0,128)).toEqual(new Float32Array(128).fill(v)));
[-4,-4,-2,-2,-4].forEach((v,ch)=>expect(r.outputs.main[ch].slice(128)).toEqual(new Float32Array(128).fill(v)));
expect(r.diagnostics.scrubbedSamples).toBe(0);
});

test('looping player position stays inside the slice after a tiny negative step from its start',async()=>{
const rate=48000,rows=Array.from({length:128},(_,n)=>({gate:true,trigger:false,reset:false,rate:n<87?-1:n===87?f(-1e-15):0}));
const r=await renderOffline(playerProcessor(rate,rate,{loop:true,startFrame:3,endFrame:91}),{sampleRate:rate,duration:duration(128,rate),inputs:{controls:playerInput(rows)},messages:[{name:'load',payload:{data:pcm}}]});
// Reverse playback starts at end-1=90 and reaches 3 at n=87; 3-1e-15 wraps to start, not to end 91.
const position=Float32Array.from({length:128},(_,n)=>Math.max(3,90-n));
expect(r.outputs.main[2]).toEqual(position);expect(r.outputs.main[0]).toEqual(Float32Array.from(position,n=>pcm[n]));expect(r.diagnostics.scrubbedSamples).toBe(0);
});

test('native offline ingress budget truncates before resident load; host must reject oversize PCM itself',async()=>{
const p=defineProcessor(()=>{
Expand Down
Loading