Skip to content

Commit 97dca7b

Browse files
committed
fix(audio): apply playlist clip fades
1 parent a859d85 commit 97dca7b

3 files changed

Lines changed: 24 additions & 10 deletions

File tree

src/audio/core/useTransportScheduler.js

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,10 @@ import { useEffect, useRef } from "react";
1010
import { clamp } from "../../store/utils";
1111
import { computeSamplePlaybackParams } from "./computeSamplePlaybackParams";
1212
import { createSamplePlaybackNodes } from "./createSamplePlaybackNodes";
13+
import {
14+
applySampleGainAutomation,
15+
computeSampleFadeParams,
16+
} from "./sampleGainAutomation";
1317
import {
1418
DEFAULT_SAMPLE_MIDI_PITCH,
1519
midiPitchToPlaybackRate,
@@ -323,7 +327,6 @@ export function useTransportScheduler({
323327
),
324328
);
325329

326-
const fadeOutAt = time + Math.max(0, playDuration - 0.012);
327330
const clipGain = Math.max(
328331
MIN_DURATION_SEC,
329332
Number(channel?.volume ?? 0.75) *
@@ -336,9 +339,13 @@ export function useTransportScheduler({
336339

337340
source.buffer = scheduledBuffer;
338341
source.playbackRate.setValueAtTime(playbackRate, time);
339-
gain.gain.setValueAtTime(clipGain, time);
340-
gain.gain.setValueAtTime(clipGain, fadeOutAt);
341-
gain.gain.linearRampToValueAtTime(0.0001, time + playDuration);
342+
applySampleGainAutomation(
343+
gain.gain,
344+
time,
345+
playDuration,
346+
clipGain,
347+
computeSampleFadeParams(playDuration, settings),
348+
);
342349
panner.pan.setValueAtTime(clipPan, time);
343350

344351
source.connect(gain);

src/audio/exportProjectAudio.js

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,10 @@ import { applyInsertSettings } from "./core/applyInsertSettings";
66
import { createMixerInsertNodes } from "./core/createMixerInsertNodes";
77
import { computeSamplePlaybackParams } from "./core/computeSamplePlaybackParams";
88
import { createSamplePlaybackNodes } from "./core/createSamplePlaybackNodes";
9+
import {
10+
applySampleGainAutomation,
11+
computeSampleFadeParams,
12+
} from "./core/sampleGainAutomation";
913
import { DEFAULT_SAMPLE_MIDI_PITCH, midiPitchToPlaybackRate } from "./domain/pitch";
1014
import { getSafeSampleSettings } from "./domain/sampleSettings";
1115
import { getTimeStretchProfile } from "./domain/timeStretch";
@@ -747,7 +751,6 @@ export async function renderPlaylistArrangementToFile(options) {
747751
playDuration * playbackRate,
748752
),
749753
);
750-
const fadeOutAt = clipStartTime + Math.max(0, playDuration - 0.012);
751754
const clipGain = Math.max(
752755
MIN_DURATION_SEC,
753756
Number(channel?.volume ?? 0.75) *
@@ -764,9 +767,13 @@ export async function renderPlaylistArrangementToFile(options) {
764767

765768
source.buffer = scheduledBuffer;
766769
source.playbackRate.setValueAtTime(playbackRate, clipStartTime);
767-
gain.gain.setValueAtTime(clipGain, clipStartTime);
768-
gain.gain.setValueAtTime(clipGain, fadeOutAt);
769-
gain.gain.linearRampToValueAtTime(0.0001, clipStartTime + playDuration);
770+
applySampleGainAutomation(
771+
gain.gain,
772+
clipStartTime,
773+
playDuration,
774+
clipGain,
775+
computeSampleFadeParams(playDuration, settings),
776+
);
770777
panner.pan.setValueAtTime(clipPan, clipStartTime);
771778

772779
source.connect(gain);

src/test/setup.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ globalThis.AudioContext = class AudioContext {
1111
return { duration: length / sampleRate, length, sampleRate, numberOfChannels: channels };
1212
}
1313
createGain() {
14-
return { connect: vi.fn(), gain: { value: 1, setValueAtTime: vi.fn(), linearRampToValueAtTime: vi.fn() } };
14+
return { connect: vi.fn(), gain: { value: 1, setValueAtTime: vi.fn(), linearRampToValueAtTime: vi.fn(), exponentialRampToValueAtTime: vi.fn() } };
1515
}
1616
createBufferSource() {
1717
return { connect: vi.fn(), start: vi.fn(), stop: vi.fn(), playbackRate: { value: 1 } };
@@ -44,7 +44,7 @@ globalThis.OfflineAudioContext = class OfflineAudioContext {
4444
return { connect: vi.fn(), start: vi.fn(), stop: vi.fn(), playbackRate: { value: 1 } };
4545
}
4646
createGain() {
47-
return { connect: vi.fn(), gain: { value: 1 } };
47+
return { connect: vi.fn(), gain: { value: 1, setValueAtTime: vi.fn(), linearRampToValueAtTime: vi.fn(), exponentialRampToValueAtTime: vi.fn() } };
4848
}
4949
createStereoPanner() {
5050
return { connect: vi.fn(), pan: { value: 0 } };

0 commit comments

Comments
 (0)