Let a looped sample play out past its loop when the note ends #331
With "Mode" and "Loop" as two fields, One-shot plus Loop ran until Stop was pressed: letting go of the key did nothing. The two fields are now one, "Play", with four entries that each say what happens while the note is held and when it is let go. - Gate and One-shot play the sound once, as before. Loop repeats it and fades out with the note. - "Loop, then release" is new: the sampler's sustain loop. Let go, it finishes the round it is in and plays what comes after "Loop end" to the end of the sound. - A buffer source stops looping and plays on by itself. The pitch worklet gets a "leave" message. The phase vocoder then lets the round finish that its newest frame has read into, so nothing read already changes under the frames still sounding. - Samples saved with "Mode" and "Loop" are turned into "Play" on load. Gate and One-shot stay, either with Loop becomes Loop, since playing until stopped is gone. - The keys of the Instrument tab stay half lit while a note let go is still heard. Esc on the keys stops everything, like Stop. - Striking a ringing key again plays a new note beside it rather than stopping the old one, so a drum can be hit quickly. This differs from what was proposed in the issue discussion. Tests: - instrument + pitch-player + sound-library + node-catalog: 477 tests, green (12 new). - e2e 34 (melektaus-atx): ALL PASSED, 45 checks (+1). Let go after a loop of 0.05 s, the note played out for 0.31 s on the audio clock, the key half lit meanwhile. - Counter-check: a release that does not leave the loop fails the 2 new player tests. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
8a4e4a6762
commit
144dee0a97
14 files changed
+285
-29
No files matched your search
@@ -29,8 +29,9 @@ export interface FxDelay {
|
||||
|
||||
/**
|
||||
* Where a way of an instrument's note gets its sound (#331): a file of its own, how loud and how it
|
||||
* loops, and whether it ends with the note (`gate`) or plays through. A way of a sound FX has none –
|
||||
* it plays the sound the voice was started with. `loopEnd` 0 is the end of the sound.
|
||||
* loops, and whether it ends with the note (`gate`) or plays through – one that loops leaves its
|
||||
* loop then and plays what comes after it. A way of a sound FX has none: it plays the sound the
|
||||
* voice was started with. `loopEnd` 0 is the end of the sound.
|
||||
*/
|
||||
export interface FxSource {
|
||||
file: string;
|
||||
|
||||
@@ -55,10 +55,15 @@ export class PitchPlayer {
|
||||
/** Whether `lastPhase` holds the frame before: a frame at 0 semitones does not need its phases. */
|
||||
private phased = false;
|
||||
private primed = false;
|
||||
/** How many samples of the output the sound lasts; a looping one never ends. */
|
||||
private readonly end: number;
|
||||
/** How many samples of the output the sound lasts; a looping one never ends until it leaves its loop. */
|
||||
private end: number;
|
||||
/** Where a loop runs in the samples of the sound; null for a sound that does not loop. */
|
||||
private readonly loop: { start: number; end: number } | null;
|
||||
/**
|
||||
* Once the loop is left, how far the sound's samples lag behind the output from the end of the
|
||||
* last round on: every round played before then. Infinity while it loops on.
|
||||
*/
|
||||
private unwound = Infinity;
|
||||
|
||||
/**
|
||||
* `channels` are the samples of the sound, `step` how many of them pass for one sample of the
|
||||
@@ -129,6 +134,21 @@ export class PitchPlayer {
|
||||
return this.frameAt + this.given < this.end;
|
||||
}
|
||||
|
||||
/**
|
||||
* Lets the loop finish the round it is in and then plays on into what comes after it, to the end
|
||||
* of the sound (#331). The round is the one the newest frame has read up to, so nothing read
|
||||
* already changes under the frames still sounding.
|
||||
*/
|
||||
leaveLoop(): void {
|
||||
const { loop } = this;
|
||||
if (!loop || this.unwound !== Infinity) return;
|
||||
const span = loop.end - loop.start;
|
||||
const read = (this.frameAt + this.size) * this.step;
|
||||
const rounds = read < loop.end ? 0 : Math.floor((read - loop.end) / span) + 1;
|
||||
this.unwound = rounds * span;
|
||||
this.end = Math.ceil(((this.channels[0]?.length ?? 0) + this.unwound) / this.step);
|
||||
}
|
||||
|
||||
/** Adds the next frame, a hop on from the last, and hands out the hop it finishes. */
|
||||
private frame(semitones: number): void {
|
||||
this.frameAt += this.hop;
|
||||
@@ -259,14 +279,22 @@ export class PitchPlayer {
|
||||
|
||||
/**
|
||||
* Channel `c` for the frame starting at output sample `from`, each sample read between two of the
|
||||
* sound's; outside the sound it is silence, or the loop again.
|
||||
* sound's; outside the sound it is silence, or the loop again – or past the loop once it is left.
|
||||
*/
|
||||
private readFrame(c: number, from: number): Float64Array {
|
||||
const { reading, size, step, loop } = this;
|
||||
const { reading, size, step, loop, unwound } = this;
|
||||
const samples = this.channels[c]!;
|
||||
const end = loop ? loop.end : samples.length;
|
||||
for (let j = 0; j < size; j++) {
|
||||
let position = (from + j) * step;
|
||||
if (loop && position >= loop.end + unwound) {
|
||||
position -= unwound;
|
||||
const below = Math.floor(position);
|
||||
const fraction = position - below;
|
||||
const next = below + 1 < samples.length ? samples[below + 1]! : 0;
|
||||
reading[j] = below < samples.length ? samples[below]! * (1 - fraction) + next * fraction : 0;
|
||||
continue;
|
||||
}
|
||||
if (position < 0 || position >= end) {
|
||||
if (!loop || position < 0) {
|
||||
reading[j] = 0;
|
||||
|
||||
Reference in new issue
Block a user