|
1 | | -class MockGainNode { |
2 | | - gain = { value: 1 }; |
3 | | - disconnect() {} |
4 | | - connect() { |
| 1 | +/* eslint-disable @typescript-eslint/no-explicit-any */ |
| 2 | + |
| 3 | +declare const global: any; |
| 4 | + |
| 5 | +const MockGainNode: any = { |
| 6 | + gain: { value: 1 }, |
| 7 | + // eslint-disable-next-line @typescript-eslint/no-unused-vars |
| 8 | + connect(..._args: any[]): any { |
5 | 9 | return this; |
6 | | - } |
7 | | -} |
| 10 | + }, |
| 11 | + disconnect(): void {}, |
| 12 | +}; |
8 | 13 |
|
9 | | -class MockAudioContext { |
10 | | - sampleRate = 44100; |
11 | | - baseLatency = 0; |
12 | | - close() { |
13 | | - return Promise.resolve(); |
14 | | - } |
15 | | - createGain() { |
16 | | - return new MockGainNode(); |
17 | | - } |
18 | | - createBufferSource() { |
19 | | - return { |
20 | | - buffer: null, |
21 | | - connect: () => {}, |
22 | | - start: () => {}, |
23 | | - stop: () => {}, |
24 | | - onended: null, |
25 | | - disconnect: () => {}, |
26 | | - }; |
27 | | - } |
28 | | - createBuffer(channels: number, length: number, sampleRate: number) { |
29 | | - return { |
30 | | - numberOfChannels: channels, |
31 | | - length, |
32 | | - sampleRate, |
33 | | - duration: length / sampleRate, |
34 | | - getChannelData: () => new Float32Array(length), |
35 | | - }; |
36 | | - } |
37 | | - decodeAudioData(buffer: ArrayBuffer) { |
38 | | - return Promise.resolve(this.createBuffer(2, 44100, 44100)); |
39 | | - } |
40 | | - destination = {}; |
| 14 | +function MockAudioContext(this: any): void { |
| 15 | + this.sampleRate = 44100; |
| 16 | + this.baseLatency = 0; |
| 17 | + this.destination = {}; |
41 | 18 | } |
42 | 19 |
|
43 | | -class MockAudio { |
44 | | - src = ""; |
45 | | - currentTime = 0; |
46 | | - duration = 0; |
47 | | - paused = true; |
48 | | - volume = 1; |
49 | | - playbackRate = 1; |
50 | | - readyState = 4; |
51 | | - HAVE_ENOUGH_DATA = 4; |
| 20 | +MockAudioContext.prototype.close = function(): Promise<void> { |
| 21 | + return Promise.resolve(); |
| 22 | +}; |
| 23 | + |
| 24 | +MockAudioContext.prototype.createGain = function(): any { |
| 25 | + return MockGainNode; |
| 26 | +}; |
| 27 | + |
| 28 | +MockAudioContext.prototype.createBuffer = function( |
| 29 | + channels: number, |
| 30 | + length: number, |
| 31 | + sampleRate: number |
| 32 | +): any { |
| 33 | + return { |
| 34 | + numberOfChannels: channels, |
| 35 | + length, |
| 36 | + sampleRate, |
| 37 | + duration: length / sampleRate, |
| 38 | + getChannelData: () => new Float32Array(length), |
| 39 | + }; |
| 40 | +}; |
| 41 | + |
| 42 | +MockAudioContext.prototype.createBufferSource = function(): any { |
| 43 | + return { |
| 44 | + buffer: null, |
| 45 | + connect: () => {}, |
| 46 | + disconnect: () => {}, |
| 47 | + start: () => {}, |
| 48 | + stop: () => {}, |
| 49 | + }; |
| 50 | +}; |
52 | 51 |
|
53 | | - play() { |
54 | | - return Promise.resolve(); |
55 | | - } |
56 | | - pause() {} |
57 | | - load() {} |
58 | | - addEventListener(_event: string, _callback: () => void) {} |
59 | | - removeEventListener(_event: string, _callback: () => void) {} |
60 | | - dispatchEvent(_event: unknown) { |
61 | | - return true; |
62 | | - } |
| 52 | +MockAudioContext.prototype.decodeAudioData = function( |
| 53 | + buffer: ArrayBuffer, |
| 54 | + successCallback?: (decoded: any) => void, |
| 55 | + errorCallback?: (error: any) => void |
| 56 | +): Promise<any> { |
| 57 | + const decoded = this.createBuffer(2, 44100, 44100); |
| 58 | + successCallback?.(decoded); |
| 59 | + return Promise.resolve(decoded).catch((err) => { |
| 60 | + errorCallback?.(err); |
| 61 | + throw err; |
| 62 | + }); |
| 63 | +}; |
| 64 | + |
| 65 | +const listeners = new Map<string, Set<any>>(); |
| 66 | + |
| 67 | +function MockAudio(this: any): void { |
| 68 | + this.src = ""; |
| 69 | + this.currentTime = 0; |
| 70 | + this.duration = 0; |
| 71 | + this.paused = true; |
| 72 | + this.volume = 1; |
| 73 | + this.playbackRate = 1; |
| 74 | + this.readyState = 4; |
| 75 | + this.HAVE_ENOUGH_DATA = 4; |
63 | 76 | } |
64 | 77 |
|
65 | | -(global as unknown as { AudioContext: typeof AudioContext }).AudioContext = MockAudioContext as unknown as typeof AudioContext; |
66 | | -(global as unknown as { webkitAudioContext: typeof AudioContext }).webkitAudioContext = MockAudioContext as unknown as typeof AudioContext; |
67 | | -(global as unknown as { Audio: typeof Audio }).Audio = MockAudio as unknown as typeof Audio; |
| 78 | +MockAudio.prototype.play = function(): Promise<void> { |
| 79 | + return Promise.resolve(); |
| 80 | +}; |
| 81 | +MockAudio.prototype.pause = function(): void {}; |
| 82 | +MockAudio.prototype.load = function(): void {}; |
| 83 | + |
| 84 | +MockAudio.prototype.addEventListener = function(event: string, callback: any): void { |
| 85 | + if (!listeners.has(event)) listeners.set(event, new Set()); |
| 86 | + listeners.get(event)!.add(callback); |
| 87 | +}; |
| 88 | + |
| 89 | +MockAudio.prototype.removeEventListener = function(event: string, callback: any): void { |
| 90 | + listeners.get(event)?.delete(callback); |
| 91 | +}; |
| 92 | + |
| 93 | +MockAudio.prototype.dispatchEvent = function(event: any): boolean { |
| 94 | + listeners.get(event.type)?.forEach((listener) => listener()); |
| 95 | + return true; |
| 96 | +}; |
| 97 | + |
| 98 | +(global as any).AudioContext = MockAudioContext; |
| 99 | +(global as any).webkitAudioContext = MockAudioContext; |
| 100 | +(global as any).Audio = MockAudio; |
0 commit comments