|
| 1 | +import { beforeEach, describe, expect, it, vi } from 'vitest'; |
| 2 | + |
| 3 | +// ─── Minimal stubs ────────────────────────────────────────────────────────── |
| 4 | + |
| 5 | +const addModule = vi.fn().mockResolvedValue(undefined); |
| 6 | + |
| 7 | +const mockPitchParam = { value: 1.0 }; |
| 8 | +const mockSemitonesParam = { value: 0 }; |
| 9 | +const mockPlaybackRateParam = { value: 1.0 }; |
| 10 | +const mockSetStretchParameters = vi.fn(); |
| 11 | +const mockConnect = vi.fn(); |
| 12 | + |
| 13 | +const mockSourcePlaybackRate = { value: 1.0 }; |
| 14 | +const mockSourceConnect = vi.fn(); |
| 15 | +const mockSourceStart = vi.fn(); |
| 16 | + |
| 17 | +const mockCreateBufferSource = vi.fn(() => ({ |
| 18 | + playbackRate: mockSourcePlaybackRate, |
| 19 | + buffer: null as AudioBuffer | null, |
| 20 | + connect: mockSourceConnect, |
| 21 | + start: mockSourceStart, |
| 22 | +})); |
| 23 | + |
| 24 | +const mockRenderedBuffer = {} as AudioBuffer; |
| 25 | +const mockStartRendering = vi.fn().mockResolvedValue(mockRenderedBuffer); |
| 26 | + |
| 27 | +const mockDestination = {} as AudioDestinationNode; |
| 28 | + |
| 29 | +class MockOfflineAudioContext { |
| 30 | + numberOfChannels: number; |
| 31 | + length: number; |
| 32 | + sampleRate: number; |
| 33 | + audioWorklet = { addModule }; |
| 34 | + destination = mockDestination; |
| 35 | + startRendering = mockStartRendering; |
| 36 | + createBufferSource = mockCreateBufferSource; |
| 37 | + |
| 38 | + constructor(numberOfChannels: number, length: number, sampleRate: number) { |
| 39 | + this.numberOfChannels = numberOfChannels; |
| 40 | + this.length = length; |
| 41 | + this.sampleRate = sampleRate; |
| 42 | + } |
| 43 | +} |
| 44 | + |
| 45 | +class MockAudioWorkletNode { |
| 46 | + parameters: Map<string, unknown>; |
| 47 | + port = { postMessage: vi.fn() }; |
| 48 | + connect = mockConnect; |
| 49 | + |
| 50 | + constructor() { |
| 51 | + this.parameters = new Map([ |
| 52 | + ['pitch', mockPitchParam], |
| 53 | + ['pitchSemitones', mockSemitonesParam], |
| 54 | + ['playbackRate', mockPlaybackRateParam], |
| 55 | + ]); |
| 56 | + } |
| 57 | +} |
| 58 | + |
| 59 | +// ─── Setup ────────────────────────────────────────────────────────────────── |
| 60 | + |
| 61 | +beforeEach(() => { |
| 62 | + vi.resetModules(); |
| 63 | + addModule.mockClear(); |
| 64 | + mockSetStretchParameters.mockClear(); |
| 65 | + mockConnect.mockClear(); |
| 66 | + mockSourceConnect.mockClear(); |
| 67 | + mockSourceStart.mockClear(); |
| 68 | + mockStartRendering.mockResolvedValue(mockRenderedBuffer); |
| 69 | + mockPitchParam.value = 1.0; |
| 70 | + mockSemitonesParam.value = 0; |
| 71 | + mockPlaybackRateParam.value = 1.0; |
| 72 | + mockSourcePlaybackRate.value = 1.0; |
| 73 | + |
| 74 | + vi.stubGlobal('OfflineAudioContext', MockOfflineAudioContext); |
| 75 | + vi.stubGlobal('AudioWorkletNode', MockAudioWorkletNode); |
| 76 | +}); |
| 77 | + |
| 78 | +describe('processOffline', () => { |
| 79 | + function makeInput(length = 4410, channels = 2, sampleRate = 44100): AudioBuffer { |
| 80 | + return { length, numberOfChannels: channels, sampleRate } as AudioBuffer; |
| 81 | + } |
| 82 | + |
| 83 | + it('registers the processor and returns the rendered buffer', async () => { |
| 84 | + const { processOffline } = await import('./processOffline.js'); |
| 85 | + const input = makeInput(); |
| 86 | + const result = await processOffline({ |
| 87 | + input, |
| 88 | + processorUrl: '/proc.js', |
| 89 | + }); |
| 90 | + |
| 91 | + expect(addModule).toHaveBeenCalledWith('/proc.js'); |
| 92 | + expect(mockStartRendering).toHaveBeenCalled(); |
| 93 | + expect(result).toBe(mockRenderedBuffer); |
| 94 | + }); |
| 95 | + |
| 96 | + it('scales output length by 1/playbackRate', async () => { |
| 97 | + const { processOffline } = await import('./processOffline.js'); |
| 98 | + const input = makeInput(44100); |
| 99 | + let capturedLength = 0; |
| 100 | + vi.stubGlobal( |
| 101 | + 'OfflineAudioContext', |
| 102 | + class extends MockOfflineAudioContext { |
| 103 | + constructor(ch: number, len: number, sr: number) { |
| 104 | + super(ch, len, sr); |
| 105 | + capturedLength = len; |
| 106 | + } |
| 107 | + }, |
| 108 | + ); |
| 109 | + |
| 110 | + await processOffline({ input, processorUrl: '/proc.js', playbackRate: 2.0 }); |
| 111 | + expect(capturedLength).toBe(Math.ceil(44100 / 2.0)); |
| 112 | + }); |
| 113 | + |
| 114 | + it('sets pitch, pitchSemitones, and playbackRate AudioParams', async () => { |
| 115 | + const { processOffline } = await import('./processOffline.js'); |
| 116 | + const input = makeInput(); |
| 117 | + await processOffline({ |
| 118 | + input, |
| 119 | + processorUrl: '/proc.js', |
| 120 | + pitch: 1.2, |
| 121 | + pitchSemitones: -3, |
| 122 | + playbackRate: 1.5, |
| 123 | + }); |
| 124 | + |
| 125 | + expect(mockPitchParam.value).toBe(1.2); |
| 126 | + expect(mockSemitonesParam.value).toBe(-3); |
| 127 | + expect(mockPlaybackRateParam.value).toBe(1.5); |
| 128 | + expect(mockSourcePlaybackRate.value).toBe(1.5); |
| 129 | + }); |
| 130 | + |
| 131 | + it('calls setStretchParameters when stretchParameters is provided', async () => { |
| 132 | + // Patch SoundTouchNode to track setStretchParameters calls |
| 133 | + vi.stubGlobal('AudioWorkletNode', class extends MockAudioWorkletNode {}); |
| 134 | + const mod = await import('./SoundTouchNode.js'); |
| 135 | + const spy = vi |
| 136 | + .spyOn(mod.SoundTouchNode.prototype as unknown as { setStretchParameters: (p: unknown) => void }, 'setStretchParameters') |
| 137 | + .mockImplementation(() => {}); |
| 138 | + |
| 139 | + const { processOffline } = await import('./processOffline.js'); |
| 140 | + const input = makeInput(); |
| 141 | + await processOffline({ |
| 142 | + input, |
| 143 | + processorUrl: '/proc.js', |
| 144 | + stretchParameters: { overlapMs: 12 }, |
| 145 | + }); |
| 146 | + |
| 147 | + expect(spy).toHaveBeenCalledWith({ overlapMs: 12 }); |
| 148 | + spy.mockRestore(); |
| 149 | + }); |
| 150 | + |
| 151 | + it('skips setStretchParameters when not provided', async () => { |
| 152 | + const mod = await import('./SoundTouchNode.js'); |
| 153 | + const spy = vi |
| 154 | + .spyOn(mod.SoundTouchNode.prototype as unknown as { setStretchParameters: (p: unknown) => void }, 'setStretchParameters') |
| 155 | + .mockImplementation(() => {}); |
| 156 | + |
| 157 | + const { processOffline } = await import('./processOffline.js'); |
| 158 | + await processOffline({ input: makeInput(), processorUrl: '/proc.js' }); |
| 159 | + |
| 160 | + expect(spy).not.toHaveBeenCalled(); |
| 161 | + spy.mockRestore(); |
| 162 | + }); |
| 163 | + |
| 164 | + it('connects the graph: source → stNode → destination', async () => { |
| 165 | + const connectCalls: unknown[] = []; |
| 166 | + vi.stubGlobal( |
| 167 | + 'AudioWorkletNode', |
| 168 | + class extends MockAudioWorkletNode { |
| 169 | + connect(dest: unknown) { |
| 170 | + connectCalls.push({ from: 'stNode', to: dest }); |
| 171 | + } |
| 172 | + }, |
| 173 | + ); |
| 174 | + mockSourceConnect.mockImplementation((dest: unknown) => { |
| 175 | + connectCalls.push({ from: 'source', to: dest }); |
| 176 | + }); |
| 177 | + |
| 178 | + const { processOffline } = await import('./processOffline.js'); |
| 179 | + await processOffline({ input: makeInput(), processorUrl: '/proc.js' }); |
| 180 | + |
| 181 | + expect(connectCalls.some((c) => (c as { from: string }).from === 'source')).toBe(true); |
| 182 | + expect(mockSourceStart).toHaveBeenCalledWith(0); |
| 183 | + }); |
| 184 | +}); |
0 commit comments