Skip to content

Commit 352d3f6

Browse files
authored
Merge pull request #21 from ryohey/fix-call-stack-size-exceeded-error
Fix call stack size exceeded error
2 parents 7c2722a + 6b50bab commit 352d3f6

File tree

7 files changed

+398
-392
lines changed

7 files changed

+398
-392
lines changed

example/package.json

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,9 +21,9 @@
2121
"@rollup/plugin-node-resolve": "^15.2.3",
2222
"@rollup/plugin-typescript": "^11.1.6",
2323
"@types/wav-encoder": "^1.3.3",
24-
"rollup": "^4.18.0",
24+
"rollup": "^4.21.2",
2525
"rollup-plugin-serve": "^2.0.2",
26-
"tslib": "^2.6.3",
27-
"typescript": "^5.4.5"
26+
"tslib": "^2.7.0",
27+
"typescript": "^5.6.2"
2828
}
2929
}

lib/package.json

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@ryohey/wavelet",
3-
"version": "0.7.3",
3+
"version": "0.7.4",
44
"description": "A wavetable synthesizer that never stops the UI thread created by AudioWorklet.",
55
"main": "dist/index.js",
66
"types": "dist/index.d.ts",
@@ -20,12 +20,12 @@
2020
"@rollup/plugin-commonjs": "^26.0.1",
2121
"@rollup/plugin-node-resolve": "^15.2.3",
2222
"@rollup/plugin-typescript": "^11.1.6",
23-
"@types/audioworklet": "^0.0.55",
23+
"@types/audioworklet": "^0.0.60",
2424
"@types/jest": "^29.5.12",
2525
"jest": "^29.7.0",
26-
"rollup": "^4.18.0",
27-
"ts-jest": "^29.1.4",
28-
"tslib": "^2.6.3",
29-
"typescript": "^5.4.5"
26+
"rollup": "^4.21.2",
27+
"ts-jest": "^29.2.5",
28+
"tslib": "^2.7.0",
29+
"typescript": "^5.6.2"
3030
}
3131
}

lib/src/helper/math.ts

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
/**
2+
* This is a custom implementation of Math.max to prevent call stack size exceeded error
3+
* when using Math.max(...arr).
4+
*/
5+
export function max(arr: number[]): number | undefined {
6+
if (arr.length === 0) {
7+
return undefined
8+
}
9+
let max = arr[0]
10+
for (let i = 1; i < arr.length; i++) {
11+
if (arr[i] > max) {
12+
max = arr[i]
13+
}
14+
}
15+
return max
16+
}

lib/src/processor/SynthProcessor.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { SynthEvent } from ".."
1+
import { SynthMessage } from ".."
22
import { SynthProcessorCore } from "./SynthProcessorCore"
33

44
export class SynthProcessor extends AudioWorkletProcessor {
@@ -10,7 +10,7 @@ export class SynthProcessor extends AudioWorkletProcessor {
1010
constructor() {
1111
super()
1212

13-
this.port.onmessage = (e: MessageEvent<SynthEvent>) => {
13+
this.port.onmessage = (e: MessageEvent<SynthMessage>) => {
1414
this.synth.addEvent(e.data)
1515
}
1616
}

lib/src/renderer/renderAudio.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,12 @@ import {
44
SampleParameterEvent,
55
SynthEvent,
66
} from ".."
7+
import { max } from "../helper/math"
78
import { SynthProcessorCore } from "../processor/SynthProcessorCore"
89

910
// returns in frame unit
1011
const getSongLength = (events: SynthEvent[]) =>
11-
Math.max(...events.map((e) => (e.type === "midi" ? e.delayTime : 0)))
12+
max(events.map((e) => (e.type === "midi" ? e.delayTime : 0))) ?? 0
1213

1314
// Maximum time to wait for the note release sound to become silent
1415
const silentTimeoutSec = 5

0 commit comments

Comments
 (0)