Skip to content

Commit 7f4a0ec

Browse files
committed
feat(stage-shared): add an audio tapping node for troubleshooting beat-sync
1 parent 38561f3 commit 7f4a0ec

File tree

1 file changed

+28
-1
lines changed

1 file changed

+28
-1
lines changed

packages/stage-shared/src/beat-sync/browser/detector.ts

Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@ export function createBeatSyncDetector(options: CreateBeatSyncDetectorOptions):
5252
}
5353

5454
let stopSource: (() => void) | undefined
55+
let tapNode: ScriptProcessorNode | undefined // Dev
5556

5657
const listeners: { [K in keyof BeatSyncDetectorEventMap]: Array<(...args: any) => void> } = {
5758
stateChange: [],
@@ -71,6 +72,13 @@ export function createBeatSyncDetector(options: CreateBeatSyncDetectorOptions):
7172
stopSource?.()
7273
stopSource = undefined
7374

75+
// Dev
76+
if (tapNode) {
77+
tapNode.onaudioprocess = null
78+
tapNode.disconnect()
79+
tapNode = undefined
80+
}
81+
7482
source?.disconnect()
7583
source = undefined
7684

@@ -94,7 +102,26 @@ export function createBeatSyncDetector(options: CreateBeatSyncDetectorOptions):
94102
})
95103

96104
const node = await createSource(context)
97-
node.connect(analyser.workletNode)
105+
106+
if (import.meta.env.DEV) {
107+
// Dev: Use a tap node to verify there're audio samples flowing in the pipeline.
108+
tapNode = context.createScriptProcessor(256, 1, 1)
109+
tapNode.addEventListener('audioprocess', (e) => {
110+
const sample = e.inputBuffer.getChannelData(0)
111+
// Avoid log flooding
112+
if (e.timeStamp % 10 === 0) {
113+
// eslint-disable-next-line no-console
114+
console.debug('[debug:tap] sample.length =', sample.length, 'timeStamp =', e.timeStamp)
115+
}
116+
e.outputBuffer.copyToChannel(sample, 0)
117+
})
118+
node.connect(tapNode)
119+
tapNode.connect(analyser.workletNode)
120+
}
121+
else {
122+
node.connect(analyser.workletNode)
123+
}
124+
98125
source = node
99126

100127
state.isActive = true

0 commit comments

Comments
 (0)