feat: added an option to allow user choose whether to trim leading silence when bouncing audio
This commit is contained in:
@@ -1,7 +1,37 @@
|
||||
import { describe, it, expect } from 'vitest';
|
||||
import { applyOfflinePitchBendAutomation, encodeWav, getOfflineTrackGain, getOfflineTrackVolumeDb } from './KGOfflineRenderer';
|
||||
import { beforeEach, describe, expect, it, vi } from 'vitest';
|
||||
import { createMockMidiNote, createMockMidiRegion, createMockMidiTrack, createMockProject } from '../../test/utils/mock-data';
|
||||
import { bakeMidiAutomationPointsInWindow } from '../../util/midiAutomationUtil';
|
||||
|
||||
const { offlineMock, configGetMock } = vi.hoisted(() => ({
|
||||
offlineMock: vi.fn(),
|
||||
configGetMock: vi.fn(),
|
||||
}));
|
||||
|
||||
vi.mock('tone', () => ({
|
||||
Offline: offlineMock,
|
||||
}));
|
||||
|
||||
vi.mock('./KGAudioInterface', () => ({
|
||||
KGAudioInterface: {
|
||||
instance: vi.fn(() => ({
|
||||
getTrackVolume: vi.fn().mockReturnValue(0),
|
||||
getTrackMuted: vi.fn().mockReturnValue(false),
|
||||
getTrackSolo: vi.fn().mockReturnValue(false),
|
||||
getAudioBuffer: vi.fn().mockReturnValue(null),
|
||||
})),
|
||||
},
|
||||
}));
|
||||
|
||||
vi.mock('../config/ConfigManager', () => ({
|
||||
ConfigManager: {
|
||||
instance: vi.fn(() => ({
|
||||
get: configGetMock,
|
||||
})),
|
||||
},
|
||||
}));
|
||||
|
||||
import { KGOfflineRenderer, applyOfflinePitchBendAutomation, encodeWav, getOfflineTrackGain, getOfflineTrackVolumeDb } from './KGOfflineRenderer';
|
||||
|
||||
/**
|
||||
* Create a minimal AudioBuffer-like object for testing.
|
||||
* In the jsdom test environment, AudioBuffer is not available,
|
||||
@@ -228,3 +258,77 @@ describe('offline pitch bend automation', () => {
|
||||
expect(calls[0][1]).toBe(0.26);
|
||||
});
|
||||
});
|
||||
|
||||
describe('renderToBuffer bounce range', () => {
|
||||
beforeEach(() => {
|
||||
vi.clearAllMocks();
|
||||
configGetMock.mockImplementation((key: string) => {
|
||||
if (key === 'audio.bounce_starts_from_beat_1') return true;
|
||||
if (key === 'audio.midi_automation_interpolation_interval_ms') return 10;
|
||||
return null;
|
||||
});
|
||||
offlineMock.mockResolvedValue({
|
||||
duration: 0,
|
||||
numberOfChannels: 2,
|
||||
get: vi.fn(),
|
||||
});
|
||||
;(KGOfflineRenderer as unknown as { _instance: KGOfflineRenderer | null })._instance = null;
|
||||
});
|
||||
|
||||
it('starts non-looping bounce at beat 0 when configured to include leading silence', async () => {
|
||||
const region = createMockMidiRegion({
|
||||
startFromBeat: 8,
|
||||
notes: [createMockMidiNote({ startBeat: 0, endBeat: 4 })],
|
||||
});
|
||||
const track = createMockMidiTrack({ id: 1, regions: [region] });
|
||||
const project = createMockProject({ bpm: 120, tracks: [track] });
|
||||
|
||||
await KGOfflineRenderer.instance().renderToBuffer(project, { tailSeconds: 0 });
|
||||
|
||||
expect(offlineMock).toHaveBeenCalledWith(expect.any(Function), 6, 2, 44100);
|
||||
});
|
||||
|
||||
it('starts non-looping bounce at first content when the setting is disabled', async () => {
|
||||
configGetMock.mockImplementation((key: string) => {
|
||||
if (key === 'audio.bounce_starts_from_beat_1') return false;
|
||||
if (key === 'audio.midi_automation_interpolation_interval_ms') return 10;
|
||||
return null;
|
||||
});
|
||||
|
||||
const region = createMockMidiRegion({
|
||||
startFromBeat: 8,
|
||||
notes: [createMockMidiNote({ startBeat: 0, endBeat: 4 })],
|
||||
});
|
||||
const track = createMockMidiTrack({ id: 1, regions: [region] });
|
||||
const project = createMockProject({ bpm: 120, tracks: [track] });
|
||||
|
||||
await KGOfflineRenderer.instance().renderToBuffer(project, { tailSeconds: 0 });
|
||||
|
||||
expect(offlineMock).toHaveBeenCalledWith(expect.any(Function), 2, 2, 44100);
|
||||
});
|
||||
|
||||
it('keeps looping bounce bounds regardless of the beat-1 setting', async () => {
|
||||
configGetMock.mockImplementation((key: string) => {
|
||||
if (key === 'audio.bounce_starts_from_beat_1') return false;
|
||||
if (key === 'audio.midi_automation_interpolation_interval_ms') return 10;
|
||||
return null;
|
||||
});
|
||||
|
||||
const project = createMockProject({ bpm: 120, tracks: [] });
|
||||
project.setIsLooping(true);
|
||||
project.setLoopingRange([2, 5]);
|
||||
|
||||
await KGOfflineRenderer.instance().renderToBuffer(project, { tailSeconds: 0 });
|
||||
|
||||
expect(offlineMock).toHaveBeenCalledWith(expect.any(Function), 8, 2, 44100);
|
||||
});
|
||||
|
||||
it('falls back to the full project length when there is no renderable content', async () => {
|
||||
const project = createMockProject({ bpm: 120, tracks: [] });
|
||||
project.setMaxBars(16);
|
||||
|
||||
await KGOfflineRenderer.instance().renderToBuffer(project, { tailSeconds: 0 });
|
||||
|
||||
expect(offlineMock).toHaveBeenCalledWith(expect.any(Function), 32, 2, 44100);
|
||||
});
|
||||
});
|
||||
|
||||
@@ -99,6 +99,7 @@ export class KGOfflineRenderer {
|
||||
|
||||
let renderStartBeat = 0;
|
||||
let renderEndBeat: number;
|
||||
const bounceStartsFromBeat1 = (ConfigManager.instance().get('audio.bounce_starts_from_beat_1') as boolean) ?? true;
|
||||
|
||||
const isLooping = project.getIsLooping();
|
||||
// Looping range is determined up-front; non-looping range is computed
|
||||
@@ -272,7 +273,7 @@ export class KGOfflineRenderer {
|
||||
}
|
||||
|
||||
if (contentEnd > 0) {
|
||||
renderStartBeat = contentStart;
|
||||
renderStartBeat = bounceStartsFromBeat1 ? 0 : contentStart;
|
||||
renderEndBeat = contentEnd;
|
||||
}
|
||||
// else: no content found, keep the full project range as fallback
|
||||
|
||||
@@ -3,6 +3,7 @@ import { CONFIG_UPGRADER_CONSTANTS } from '../../constants/coreConstants';
|
||||
import { upgradeConfigToV1 } from './upgradeConfigToV1';
|
||||
import { upgradeConfigToV2 } from './upgradeConfigToV2';
|
||||
import { upgradeConfigToV3 } from './upgradeConfigToV3';
|
||||
import { upgradeConfigToV4 } from './upgradeConfigToV4';
|
||||
|
||||
/**
|
||||
* KGConfigUpgrader — Orchestrates app-level migrations (e.g., storage backend changes).
|
||||
@@ -43,6 +44,10 @@ export class KGConfigUpgrader {
|
||||
await upgradeConfigToV3();
|
||||
break;
|
||||
}
|
||||
case 4: {
|
||||
await upgradeConfigToV4();
|
||||
break;
|
||||
}
|
||||
default: {
|
||||
throw new Error(`No config upgrader found for version ${nextVersion}`);
|
||||
}
|
||||
|
||||
@@ -0,0 +1,64 @@
|
||||
import { beforeEach, describe, expect, it, vi } from 'vitest';
|
||||
|
||||
const getRawMock = vi.fn();
|
||||
const saveRawMock = vi.fn();
|
||||
|
||||
vi.mock('../io/KGConfigStorage', () => ({
|
||||
KGConfigStorage: {
|
||||
getInstance: vi.fn(() => ({
|
||||
getRaw: getRawMock,
|
||||
saveRaw: saveRawMock,
|
||||
})),
|
||||
},
|
||||
}));
|
||||
|
||||
import { upgradeConfigToV4 } from './upgradeConfigToV4';
|
||||
|
||||
describe('upgradeConfigToV4', () => {
|
||||
beforeEach(() => {
|
||||
vi.clearAllMocks();
|
||||
});
|
||||
|
||||
it('creates the audio config and defaults bounce_starts_from_beat_1 to true when audio is missing', async () => {
|
||||
const config: Record<string, unknown> = {
|
||||
general: {},
|
||||
};
|
||||
getRawMock.mockResolvedValue(config);
|
||||
|
||||
await upgradeConfigToV4();
|
||||
|
||||
expect(config.audio).toEqual({ bounce_starts_from_beat_1: true });
|
||||
expect(saveRawMock).toHaveBeenCalledWith('userConfig', config);
|
||||
});
|
||||
|
||||
it('defaults bounce_starts_from_beat_1 to true when the key is missing', async () => {
|
||||
const config: Record<string, unknown> = {
|
||||
audio: {
|
||||
playback_delay: 0.2,
|
||||
},
|
||||
};
|
||||
getRawMock.mockResolvedValue(config);
|
||||
|
||||
await upgradeConfigToV4();
|
||||
|
||||
expect(config.audio).toEqual({
|
||||
playback_delay: 0.2,
|
||||
bounce_starts_from_beat_1: true,
|
||||
});
|
||||
expect(saveRawMock).toHaveBeenCalledWith('userConfig', config);
|
||||
});
|
||||
|
||||
it('preserves an explicit false value', async () => {
|
||||
const config: Record<string, unknown> = {
|
||||
audio: {
|
||||
bounce_starts_from_beat_1: false,
|
||||
},
|
||||
};
|
||||
getRawMock.mockResolvedValue(config);
|
||||
|
||||
await upgradeConfigToV4();
|
||||
|
||||
expect(saveRawMock).not.toHaveBeenCalled();
|
||||
expect((config.audio as Record<string, unknown>).bounce_starts_from_beat_1).toBe(false);
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,30 @@
|
||||
import { KGConfigStorage } from '../io/KGConfigStorage';
|
||||
|
||||
const CONFIG_KEY = 'userConfig';
|
||||
|
||||
export async function upgradeConfigToV4(): Promise<void> {
|
||||
const storage = KGConfigStorage.getInstance();
|
||||
const rawConfig = await storage.getRaw(CONFIG_KEY);
|
||||
if (!rawConfig || typeof rawConfig !== 'object') {
|
||||
return;
|
||||
}
|
||||
|
||||
const config = rawConfig as Record<string, unknown>;
|
||||
const audio = config.audio;
|
||||
|
||||
if (!audio || typeof audio !== 'object') {
|
||||
config.audio = {
|
||||
bounce_starts_from_beat_1: true,
|
||||
};
|
||||
await storage.saveRaw(CONFIG_KEY, config);
|
||||
return;
|
||||
}
|
||||
|
||||
const audioRecord = audio as Record<string, unknown>;
|
||||
if ('bounce_starts_from_beat_1' in audioRecord) {
|
||||
return;
|
||||
}
|
||||
|
||||
audioRecord.bounce_starts_from_beat_1 = true;
|
||||
await storage.saveRaw(CONFIG_KEY, config);
|
||||
}
|
||||
@@ -80,6 +80,7 @@ interface AppConfig {
|
||||
default_open: boolean;
|
||||
};
|
||||
audio: {
|
||||
bounce_starts_from_beat_1: boolean;
|
||||
enable_audio_capture_for_screen_sharing: boolean;
|
||||
input_device_id: string;
|
||||
lookahead_time: number;
|
||||
@@ -260,6 +261,7 @@ export class ConfigManager {
|
||||
default_open: true
|
||||
},
|
||||
audio: {
|
||||
bounce_starts_from_beat_1: true,
|
||||
enable_audio_capture_for_screen_sharing: false,
|
||||
input_device_id: 'default',
|
||||
lookahead_time: 0.05,
|
||||
|
||||
Reference in New Issue
Block a user