|
| 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 | + this._enabled = false |
| 42 | + this._audioEnabled = true |
| 43 | + this.noiseSuppressionConsumer = null |
| 44 | + } |
| 45 | + |
| 46 | + isEnabled() { |
| 47 | + return this._enabled |
| 48 | + } |
| 49 | + |
| 50 | + async setEnabled(enabled) { |
| 51 | + if (this._enabled === enabled) { |
| 52 | + return |
| 53 | + } |
| 54 | + |
| 55 | + this._enabled = enabled |
| 56 | + |
| 57 | + if (enabled) { |
| 58 | + this.noiseSuppressionConsumer = await registerNoiseSuppressionWorklet() |
| 59 | + } else if (this.noiseSuppressionConsumer) { |
| 60 | + await destroyNoiseSuppressionWorklet(this.noiseSuppressionConsumer) |
| 61 | + this.noiseSuppressionConsumer = null |
| 62 | + } |
| 63 | + |
| 64 | + const track = this.getInputTrack('default') |
| 65 | + this._handleInputTrack('default', track) |
| 66 | + } |
| 67 | + |
| 68 | + _handleInputTrack(trackId, track) { |
| 69 | + if (track) { |
| 70 | + this._audioEnabled = track.enabled |
| 71 | + } |
| 72 | + if (!this._enabled || !track) { |
| 73 | + this._setOutputTrack('default', track) |
| 74 | + return |
| 75 | + } |
| 76 | + |
| 77 | + const inputStream = new MediaStream([track]) |
| 78 | + const processedStream = processNoiseSuppression(inputStream, true) |
| 79 | + const processedTrack = processedStream.getAudioTracks()[0] |
| 80 | + processedTrack.enabled = this._audioEnabled |
| 81 | + |
| 82 | + this._setOutputTrack('default', processedTrack) |
| 83 | + } |
| 84 | + |
| 85 | + _handleInputTrackEnabled(trackId, enabled) { |
| 86 | + if (enabled !== this._audioEnabled) { |
| 87 | + this._audioEnabled = enabled |
| 88 | + this._setOutputTrackEnabled('default', enabled) |
| 89 | + } |
| 90 | + } |
| 91 | +} |
0 commit comments