File size: 1,732 Bytes
2f49513
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
import "@testing-library/jest-dom";

// Mock AudioBuffer since it's not available in JSDOM
class MockAudioBuffer {
  length: number;
  sampleRate: number;
  numberOfChannels: number;
  private channelData: Float32Array[];

  constructor(options: {
    length: number;
    sampleRate: number;
    numberOfChannels: number;
  }) {
    this.length = options.length;
    this.sampleRate = options.sampleRate;
    this.numberOfChannels = options.numberOfChannels;
    this.channelData = Array(options.numberOfChannels)
      .fill(0)
      .map(() => new Float32Array(options.length));
  }

  getChannelData(channel: number): Float32Array {
    return this.channelData[channel];
  }
}

// Replace global AudioBuffer with our mock
global.AudioBuffer = MockAudioBuffer as unknown as typeof AudioBuffer;

// Mock the AudioContext and related APIs
class MockAudioContext {
  sampleRate: number = 44100;

  createBufferSource() {
    return {
      connect: jest.fn(),
      start: jest.fn(),
    };
  }

  createBuffer(numChannels: number, length: number, sampleRate: number) {
    return new MockAudioBuffer({
      numberOfChannels: numChannels,
      length,
      sampleRate,
    }) as unknown as AudioBuffer;
  }

  decodeAudioData() {
    return new MockAudioBuffer({
      numberOfChannels: 1,
      length: 3,
      sampleRate: this.sampleRate,
    }) as unknown as AudioBuffer;
  }
}

global.AudioContext = MockAudioContext as unknown as typeof AudioContext;

// Mock requestAnimationFrame
global.requestAnimationFrame = (callback: FrameRequestCallback): number => {
  return setTimeout(callback, 0) as unknown as number;
};

// Mock cancelAnimationFrame
global.cancelAnimationFrame = (id: number): void => {
  clearTimeout(id);
};