Skip to content

Commit

Permalink
Merge pull request #21 from ryohey/fix-call-stack-size-exceeded-error
Browse files Browse the repository at this point in the history
Fix call stack size exceeded error
  • Loading branch information
ryohey authored Sep 10, 2024
2 parents 7c2722a + 6b50bab commit 352d3f6
Show file tree
Hide file tree
Showing 7 changed files with 398 additions and 392 deletions.
6 changes: 3 additions & 3 deletions example/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,9 @@
"@rollup/plugin-node-resolve": "^15.2.3",
"@rollup/plugin-typescript": "^11.1.6",
"@types/wav-encoder": "^1.3.3",
"rollup": "^4.18.0",
"rollup": "^4.21.2",
"rollup-plugin-serve": "^2.0.2",
"tslib": "^2.6.3",
"typescript": "^5.4.5"
"tslib": "^2.7.0",
"typescript": "^5.6.2"
}
}
12 changes: 6 additions & 6 deletions lib/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@ryohey/wavelet",
"version": "0.7.3",
"version": "0.7.4",
"description": "A wavetable synthesizer that never stops the UI thread created by AudioWorklet.",
"main": "dist/index.js",
"types": "dist/index.d.ts",
Expand All @@ -20,12 +20,12 @@
"@rollup/plugin-commonjs": "^26.0.1",
"@rollup/plugin-node-resolve": "^15.2.3",
"@rollup/plugin-typescript": "^11.1.6",
"@types/audioworklet": "^0.0.55",
"@types/audioworklet": "^0.0.60",
"@types/jest": "^29.5.12",
"jest": "^29.7.0",
"rollup": "^4.18.0",
"ts-jest": "^29.1.4",
"tslib": "^2.6.3",
"typescript": "^5.4.5"
"rollup": "^4.21.2",
"ts-jest": "^29.2.5",
"tslib": "^2.7.0",
"typescript": "^5.6.2"
}
}
16 changes: 16 additions & 0 deletions lib/src/helper/math.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
/**
* This is a custom implementation of Math.max to prevent call stack size exceeded error
* when using Math.max(...arr).
*/
export function max(arr: number[]): number | undefined {
if (arr.length === 0) {
return undefined
}
let max = arr[0]
for (let i = 1; i < arr.length; i++) {
if (arr[i] > max) {
max = arr[i]
}
}
return max
}
4 changes: 2 additions & 2 deletions lib/src/processor/SynthProcessor.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { SynthEvent } from ".."
import { SynthMessage } from ".."
import { SynthProcessorCore } from "./SynthProcessorCore"

export class SynthProcessor extends AudioWorkletProcessor {
Expand All @@ -10,7 +10,7 @@ export class SynthProcessor extends AudioWorkletProcessor {
constructor() {
super()

this.port.onmessage = (e: MessageEvent<SynthEvent>) => {
this.port.onmessage = (e: MessageEvent<SynthMessage>) => {
this.synth.addEvent(e.data)
}
}
Expand Down
3 changes: 2 additions & 1 deletion lib/src/renderer/renderAudio.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,12 @@ import {
SampleParameterEvent,
SynthEvent,
} from ".."
import { max } from "../helper/math"
import { SynthProcessorCore } from "../processor/SynthProcessorCore"

// returns in frame unit
const getSongLength = (events: SynthEvent[]) =>
Math.max(...events.map((e) => (e.type === "midi" ? e.delayTime : 0)))
max(events.map((e) => (e.type === "midi" ? e.delayTime : 0))) ?? 0

// Maximum time to wait for the note release sound to become silent
const silentTimeoutSec = 5
Expand Down
Loading

0 comments on commit 352d3f6

Please sign in to comment.