|
| 1 | +/** |
| 2 | + * SPDX-FileCopyrightText: 2026 Nextcloud GmbH and Nextcloud contributors |
| 3 | + * SPDX-License-Identifier: AGPL-3.0-or-later |
| 4 | + */ |
| 5 | + |
| 6 | +import { |
| 7 | + destroyNoiseSuppressionWorklet, |
| 8 | + processNoiseSuppression, |
| 9 | + registerNoiseSuppressionWorklet, |
| 10 | +} from '../../suppressNoise.ts' |
| 11 | +import TrackSinkSource from './TrackSinkSource.js' |
| 12 | + |
| 13 | +/** |
| 14 | + * Processor node to enable or disable noise suppression. |
| 15 | + * |
| 16 | + * A single input track slot with the default id is accepted. A single output |
| 17 | + * track slot with the default id is provided. |
| 18 | + * |
| 19 | + * The track can be enabled and disabled by calling "setEnabled(bool)". |
| 20 | + * |
| 21 | + * When enabled, the node processes the input audio track to suppress noise and |
| 22 | + * outputs a new, processed track. When disabled, it passes the original audio |
| 23 | + * track through without modification. |
| 24 | + * |
| 25 | + * The node manages the lifecycle of the underlying noise suppression worklet, |
| 26 | + * registering a consumer when enabled and destroying it when disabled. |
| 27 | + * |
| 28 | + * ----------------- |
| 29 | + * | | |
| 30 | + * ---> | NoiseSuppressor | ---> |
| 31 | + * | | |
| 32 | + * ----------------- |
| 33 | + */ |
| 34 | +export default class NoiseSuppressor extends TrackSinkSource { |
| 35 | + constructor() { |
| 36 | + super() |
| 37 | + |
| 38 | + this._addInputTrackSlot() |
| 39 | + this._addOutputTrackSlot() |
| 40 | + |
| 41 | + /** Whether to send processed or unmodified stream to the output */ |
| 42 | + this._enabled = false |
| 43 | + /** Whether the current input audio track is enabled */ |
| 44 | + this._audioEnabled = false |
| 45 | + /** Timeout for worklets destroying and garbage collection (in case of reconnect) */ |
| 46 | + this._stopTimer = null |
| 47 | + /** Unique consumer symbol to track worklet subscribers */ |
| 48 | + this._noiseSuppressionConsumer = null |
| 49 | + /** Pending registration promise to keep _startEffect synchronous */ |
| 50 | + this._noiseSuppressionRegistrationPromise = null |
| 51 | + } |
| 52 | + |
| 53 | + setEnabled(enabled) { |
| 54 | + if (this._enabled === enabled) { |
| 55 | + return |
| 56 | + } |
| 57 | + |
| 58 | + this._enabled = enabled |
| 59 | + |
| 60 | + if (enabled) { |
| 61 | + this._startEffect() |
| 62 | + } else { |
| 63 | + this._stopEffect() |
| 64 | + } |
| 65 | + } |
| 66 | + |
| 67 | + _handleInputTrack(trackId, track) { |
| 68 | + if (this._enabled) { |
| 69 | + this._startEffect() |
| 70 | + } else { |
| 71 | + this._stopEffect() |
| 72 | + } |
| 73 | + } |
| 74 | + |
| 75 | + _handleInputTrackEnabled(trackId, enabled) { |
| 76 | + this._audioEnabled = enabled |
| 77 | + this._setOutputTrackEnabled('default', enabled) |
| 78 | + } |
| 79 | + |
| 80 | + _startEffect() { |
| 81 | + if (this._stopTimer) { |
| 82 | + clearTimeout(this._stopTimer) |
| 83 | + this._stopTimer = null |
| 84 | + } |
| 85 | + |
| 86 | + const track = this.getInputTrack() |
| 87 | + this._audioEnabled = track?.enabled ?? false |
| 88 | + if (!track) { |
| 89 | + this._setOutputTrack('default', track) |
| 90 | + return |
| 91 | + } |
| 92 | + |
| 93 | + if (!this._noiseSuppressionConsumer) { |
| 94 | + // Start initializing the worklet if not already in progress. While waiting, set default track as an output. |
| 95 | + if (!this._noiseSuppressionRegistrationPromise) { |
| 96 | + this._noiseSuppressionRegistrationPromise = registerNoiseSuppressionWorklet() |
| 97 | + .then((consumer) => { |
| 98 | + this._noiseSuppressionRegistrationPromise = null |
| 99 | + this._noiseSuppressionConsumer = consumer |
| 100 | + |
| 101 | + if (this._enabled) { |
| 102 | + this._startEffect() |
| 103 | + } else { |
| 104 | + this._stopEffect() |
| 105 | + } |
| 106 | + }) |
| 107 | + .catch((error) => { |
| 108 | + this._noiseSuppressionRegistrationPromise = null |
| 109 | + console.error(error) |
| 110 | + }) |
| 111 | + } |
| 112 | + |
| 113 | + this._setOutputTrack('default', track) |
| 114 | + return |
| 115 | + } |
| 116 | + |
| 117 | + const inputStream = new MediaStream([track]) |
| 118 | + const processedStream = processNoiseSuppression(inputStream, true) |
| 119 | + const processedTrack = processedStream.getAudioTracks()[0] |
| 120 | + processedTrack.enabled = this._audioEnabled |
| 121 | + |
| 122 | + this._setOutputTrack('default', processedTrack) |
| 123 | + } |
| 124 | + |
| 125 | + _stopEffect() { |
| 126 | + if (!this._stopTimer) { |
| 127 | + this._stopTimer = setTimeout(async () => { |
| 128 | + if (this._noiseSuppressionConsumer) { |
| 129 | + await destroyNoiseSuppressionWorklet(this._noiseSuppressionConsumer) |
| 130 | + this._noiseSuppressionConsumer = null |
| 131 | + } |
| 132 | + this._stopTimer = null |
| 133 | + }, 3_000) |
| 134 | + } |
| 135 | + |
| 136 | + const track = this.getInputTrack() |
| 137 | + this._setOutputTrack('default', track) |
| 138 | + } |
| 139 | +} |
0 commit comments