Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fade at the End #24

Open
PyFlat-JR opened this issue Jan 2, 2023 · 4 comments
Open

Fade at the End #24

PyFlat-JR opened this issue Jan 2, 2023 · 4 comments

Comments

@PyFlat-JR
Copy link

Is there a way to fade the music out at the end of the file like in Pygame Mixer. And if there's no way is it possible for you to code it? Otherwise i can try on my own, but i think it would be faster if you do it.

Btw i'm a big fan of this module, i searched for sth like this for like 2 days and then i found this.

Thanks in advance,
Johannes

@PrajwalVandana
Copy link

PrajwalVandana commented Jan 2, 2023

I'm not a contributor, but the feature you requested is not built in, as far as I know. You could try something like this:

import threading
import time

from just_playback import Playback


class PlaybackFadeable(Playback):
    def _fade(self, start_fade, init_vol, dest_vol):
        self.set_volume(init_vol)
        while True:
            if not self.active:
                break

            secs_remaining = self.duration - self.curr_pos
            if secs_remaining < start_fade:
                self.set_volume(  # simple linear fade
                    (init_vol - dest_vol) * secs_remaining / start_fade
                    + dest_vol
                )
                time.sleep(0.1)  # might not need this, IDK
        self.set_volume(init_vol)

    def play(self, start_fade=2, init_vol=1, dest_vol=0):
        super().play()
        p = threading.Thread(
            target=self._fade,
            args=(start_fade, init_vol, dest_vol),
            daemon=True,
        )
        p.start()


playback = PlaybackFadeable()
playback.load_file("some audio file.wav")
playback.play(start_fade=2, init_vol=1, dest_vol=0)  # fade from full volume to silence, starting 2 seconds before the end

@PyFlat-JR
Copy link
Author

This helps me a lot, thank you. I had the same idea but didn't deal with the module enough.

@cheofusi
Copy link
Owner

cheofusi commented Jan 3, 2023

Hi everyone,

Support for fading in/out would be great to have in the next release.

Any ideas on how the API could look like ??

@PrajwalVandana
Copy link

PrajwalVandana commented Jan 4, 2023

How about having them as (probably keyword-only) arguments in the load_file method? Another option would be to have two new methods, set_fade_in and set_fade_out ... but it would have to be made clear that calling either method would not affect already playing audio (i.e. previous calls to play).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants