fix(audio-compressor): real-time behavior and duplicated audio bug #3786

Merged
Benjas333 merged 5 commits from compressor-fix into master 2025-09-05 20:15:47 +00:00
Benjas333 commented 2025-08-29 05:43:14 +00:00 (Migrated from github.com)

I noticed the compressor doesn't activate in real-time, but until the next song starts or when restarting the whole app.

The easy fix would be to add restartNeeded: true,
But I challenged myself to make it enable and disable in real-time.

My approximation was to make the ensureAudioContextLoad function because the 'ytmd:audio-can-play' event is only sent when loading a video. So it was impossible to know what song was playing at the moment the plugin was enabled.

By the way, it would have been easier if the audioSource and audioContext were sent when enabling a plugin, or the 'ytmd:audio-can-play' was sent again to plugins when enabled. Nevertheless, my pull request tries to modify only the plugin code and nothing from the source code.

I hope you like it.

Note

All the scenarios I mentioned are more notorious if you have the 'Resume last song when app starts' option enabled.


P.S. While making the fix, I realized the Exponential Volume plugin has a bug when starting the app, where it changes the volume to the max and only updates to the right value when changing the volume manually (just clicking the volume bar counts). #3787

Edit: TL;DR

Just realized this text is huge for no reason. In summary, my pull request offers:

ADDED

  • Enabling and disabling the compressor in real-time.

FIXED

  • Audio duplication.
I noticed the compressor doesn't activate in real-time, but until the next song starts or when restarting the whole app. The easy fix would be to add `restartNeeded: true,` But I challenged myself to make it enable and disable in real-time. My approximation was to make the `ensureAudioContextLoad` function because the 'ytmd:audio-can-play' event is only sent when loading a video. So it was impossible to know what song was playing at the moment the plugin was enabled. By the way, it would have been easier if the audioSource and audioContext were sent when enabling a plugin, or the 'ytmd:audio-can-play' was sent again to plugins when enabled. Nevertheless, my pull request tries to modify only the plugin code and nothing from the source code. I hope you like it. > [!NOTE] > All the scenarios I mentioned are more notorious if you have the 'Resume last song when app starts' option enabled. <br> > P.S. While making the fix, I realized the Exponential Volume plugin has a bug when starting the app, where it changes the volume to the max and only updates to the right value when changing the volume manually (just clicking the volume bar counts). #3787 ## Edit: TL;DR Just realized this text is huge for no reason. In summary, my pull request offers: ### ADDED - Enabling and disabling the compressor in real-time. ### FIXED - Audio duplication.
Benjas333 commented 2025-08-29 09:09:28 +00:00 (Migrated from github.com)

Oh, I totally forgot it. I made the fix at first because the compressor was duplicating the signal (the audioSource was still connected to the audioContext.destination). So yeah, I fixed that too.
renderer.ts
imagen

audio-compressor.ts
imagen

Edit: that was the main bug and, in fact, the original reason for the Pull Request. Everything else came later.

Oh, I totally forgot it. I made the fix at first because the compressor was duplicating the signal (the audioSource was still connected to the audioContext.destination). So yeah, I fixed that too. [renderer.ts](https://github.com/th-ch/youtube-music/blob/dddfa581edde2fac7a53a5e36426594c30fd5628/src/renderer.ts#L311) <img width="517" height="85" alt="imagen" src="https://github.com/user-attachments/assets/5a92eb8a-fc64-462f-8aae-f061f32b9a1d" /> [audio-compressor.ts](https://github.com/th-ch/youtube-music/blob/dddfa581edde2fac7a53a5e36426594c30fd5628/src/plugins/audio-compressor.ts#L21) <img width="414" height="42" alt="imagen" src="https://github.com/user-attachments/assets/9223896c-7f62-4aa2-8ace-82de1353333b" /> Edit: that was the main bug and, in fact, the original reason for the Pull Request. Everything else came later.
JellyBrick (Migrated from github.com) requested changes 2025-09-05 18:22:09 +00:00
@ -1,26 +1,133 @@
import { createPlugin } from '@/utils';
JellyBrick (Migrated from github.com) commented 2025-09-05 18:19:32 +00:00

Use camelCase instead of snake_case

Use camelCase instead of snake_case
copilot-pull-request-reviewer[bot] (Migrated from github.com) reviewed 2025-09-05 19:55:43 +00:00
copilot-pull-request-reviewer[bot] (Migrated from github.com) left a comment

Pull Request Overview

This PR fixes real-time behavior and audio duplication issues in the audio compressor plugin. Previously, the compressor would only activate when a new song started or the app restarted, requiring users to manually trigger audio events to enable compression.

  • Refactored the plugin to support real-time enable/disable functionality without requiring app restart
  • Added proper connection/disconnection logic to prevent audio duplication when toggling the plugin
  • Implemented fallback mechanism to trigger audio context loading when the plugin is enabled mid-playback

Tip: Customize your code reviews with copilot-instructions.md. Create the file or learn how to get started.

## Pull Request Overview This PR fixes real-time behavior and audio duplication issues in the audio compressor plugin. Previously, the compressor would only activate when a new song started or the app restarted, requiring users to manually trigger audio events to enable compression. - Refactored the plugin to support real-time enable/disable functionality without requiring app restart - Added proper connection/disconnection logic to prevent audio duplication when toggling the plugin - Implemented fallback mechanism to trigger audio context loading when the plugin is enabled mid-playback --- <sub>**Tip:** Customize your code reviews with copilot-instructions.md. <a href="/th-ch/youtube-music/new/master/.github?filename=copilot-instructions.md" class="Link--inTextBlock" target="_blank" rel="noopener noreferrer">Create the file</a> or <a href="https://docs.github.com/en/copilot/customizing-copilot/adding-repository-custom-instructions-for-github-copilot" class="Link--inTextBlock" target="_blank" rel="noopener noreferrer">learn how to get started</a>.</sub>
@ -1,26 +1,133 @@
import { createPlugin } from '@/utils';
copilot-pull-request-reviewer[bot] (Migrated from github.com) commented 2025-09-05 19:55:43 +00:00

The event listener is added with once: true, which means it will only trigger once and then be automatically removed. This conflicts with the manual removal in the stop() method at line 130, and will prevent the compressor from working on subsequent songs.


The event listener is added with `once: true`, which means it will only trigger once and then be automatically removed. This conflicts with the manual removal in the `stop()` method at line 130, and will prevent the compressor from working on subsequent songs. ```suggestion ```
@ -3,0 +103,4 @@
playerApi.getCurrentTime(),
playerApi.getUserPlaybackQualityPreference(),
);
};
copilot-pull-request-reviewer[bot] (Migrated from github.com) commented 2025-09-05 19:55:43 +00:00

The magic number 1 should be replaced with a named constant. This appears to represent a specific player state but lacks clarity for future maintainers.

The magic number `1` should be replaced with a named constant. This appears to represent a specific player state but lacks clarity for future maintainers.
JellyBrick (Migrated from github.com) approved these changes 2025-09-05 20:15:38 +00:00
Sign in to join this conversation.
No reviewers
No milestone
No project
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Dependencies

No dependencies set.

Reference: YTMD/youtube-music#3786
No description provided.