| 1 |
# Copyright (C) 2015 Dustin Spicuzza |
| 2 |
# |
| 3 |
# This program is free software; you can redistribute it and/or modify |
| 4 |
# it under the terms of the GNU General Public License as published by |
| 5 |
# the Free Software Foundation; either version 2, or (at your option) |
| 6 |
# any later version. |
| 7 |
# |
| 8 |
# This program is distributed in the hope that it will be useful, |
| 9 |
# but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 10 |
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 11 |
# GNU General Public License for more details. |
| 12 |
# |
| 13 |
# You should have received a copy of the GNU General Public License |
| 14 |
# along with this program; if not, write to the Free Software |
| 15 |
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. |
| 16 |
# |
| 17 |
# |
| 18 |
# The developers of the Exaile media player hereby grant permission |
| 19 |
# for non-GPL compatible GStreamer and Exaile plugins to be used and |
| 20 |
# distributed together with GStreamer and Exaile. This permission is |
| 21 |
# above and beyond the permissions granted by the GPL license by which |
| 22 |
# Exaile is covered. If you modify this code, you may extend this |
| 23 |
# exception to your version of the code, but you are not obligated to |
| 24 |
# do so. If you do not wish to do so, delete this exception statement |
| 25 |
# from your version. |
| 26 |
|
| 27 |
from typing import Tuple |
| 28 |
|
| 29 |
from gi.repository import GLib |
| 30 |
|
| 31 |
from xl import common |
| 32 |
|
| 33 |
import logging |
| 34 |
|
| 35 |
FadeState = common.enum(NoFade=1, FadingIn=2, Normal=3, FadingOut=4) |
| 36 |
|
| 37 |
|
| 38 |
class TrackFader: |
| 39 |
""" |
| 40 |
This object manages the volume of a track, and fading it in/out via |
| 41 |
volume. As a bonus, this object can manage the start/stop offset of |
| 42 |
a track also. |
| 43 |
|
| 44 |
* Fade in only happens once per track play |
| 45 |
* Fade out can happen multiple times |
| 46 |
|
| 47 |
This fader can be used on any engine, as long as it implements the |
| 48 |
following functions: |
| 49 |
|
| 50 |
* get_position: return current track position in nanoseconds |
| 51 |
* get_volume/set_volume: volume is 0 to 1.0 |
| 52 |
* stop: stops playback |
| 53 |
|
| 54 |
There is a user volume and a fade volume. The fade volume is used |
| 55 |
internally and the output volume is set by multiplying both of |
| 56 |
them together. |
| 57 |
|
| 58 |
.. note:: Only intended to be used by engine implementations |
| 59 |
""" |
| 60 |
|
| 61 |
SECOND = 1000000000.0 |
| 62 |
|
| 63 |
def __init__(self, stream, on_fade_out, name): |
| 64 |
self.name = name |
| 65 |
|
| 66 |
self.logger = logging.getLogger('%s [%s]' % (__name__, name)) |
| 67 |
|
| 68 |
self.stream = stream |
| 69 |
self.state = FadeState.NoFade |
| 70 |
self.on_fade_out = on_fade_out |
| 71 |
self.timer_id = None |
| 72 |
|
| 73 |
self.fade_volume = 1.0 |
| 74 |
self.user_volume = 1.0 |
| 75 |
|
| 76 |
self.fade_in_start = None |
| 77 |
self.fade_out_start = None |
| 78 |
|
| 79 |
def calculate_fades(self, track, fade_in, fade_out): |
| 80 |
'''duration is in seconds''' |
| 81 |
|
| 82 |
start_offset = track.get_tag_raw('__startoffset') or 0 |
| 83 |
stop_offset = track.get_tag_raw('__stopoffset') or 0 |
| 84 |
tracklen = track.get_tag_raw('__length') or 0 |
| 85 |
|
| 86 |
if tracklen == 0: |
| 87 |
return (None,) * 4 |
| 88 |
|
| 89 |
if stop_offset < 1: |
| 90 |
stop_offset = tracklen |
| 91 |
|
| 92 |
# Deal with bad values.. |
| 93 |
start_offset = min(start_offset, tracklen) |
| 94 |
stop_offset = min(stop_offset, tracklen) |
| 95 |
|
| 96 |
# Give up |
| 97 |
if stop_offset <= start_offset: |
| 98 |
return (tracklen,) * 4 |
| 99 |
|
| 100 |
if fade_in is None: |
| 101 |
fade_in = 0 |
| 102 |
else: |
| 103 |
fade_in = max(0, fade_in) |
| 104 |
|
| 105 |
if fade_out is None: |
| 106 |
fade_out = 0 |
| 107 |
else: |
| 108 |
fade_out = max(0, fade_out) |
| 109 |
|
| 110 |
playlen = stop_offset - start_offset |
| 111 |
|
| 112 |
total_fade = max(fade_in + fade_out, 0.1) |
| 113 |
|
| 114 |
if total_fade > playlen: |
| 115 |
fade_in = playlen * (float(fade_in) / total_fade) |
| 116 |
fade_out = playlen * (float(fade_out) / total_fade) |
| 117 |
|
| 118 |
return ( |
| 119 |
start_offset, |
| 120 |
start_offset + fade_in, |
| 121 |
stop_offset - fade_out, |
| 122 |
stop_offset, |
| 123 |
) |
| 124 |
|
| 125 |
def calculate_user_volume(self, real_volume) -> Tuple[float, bool]: |
| 126 |
"""Given the 'real' output volume, calculate what the user |
| 127 |
volume should be and whether they are identical""" |
| 128 |
|
| 129 |
vol = self.user_volume * self.fade_volume |
| 130 |
real_is_same = abs(real_volume - vol) < 0.01 |
| 131 |
|
| 132 |
if real_is_same: |
| 133 |
return real_volume, True |
| 134 |
|
| 135 |
if self.fade_volume < 0.01: |
| 136 |
return real_volume, True |
| 137 |
else: |
| 138 |
user_vol = real_volume / self.fade_volume |
| 139 |
is_same = abs(user_vol - self.user_volume) < 0.01 |
| 140 |
return user_vol, is_same |
| 141 |
|
| 142 |
def fade_out_on_play(self): |
| 143 |
if self.fade_out_start is None: |
| 144 |
self.logger.debug("foop: no fade out defined, stopping") |
| 145 |
self.stream.stop() |
| 146 |
return |
| 147 |
|
| 148 |
self.now = self.stream.get_position() / self.SECOND - 0.010 |
| 149 |
fade_len = self.fade_out_end - self.fade_out_start |
| 150 |
|
| 151 |
# If playing, and is not fading out, then force a fade out |
| 152 |
if self.state == FadeState.Normal: |
| 153 |
start = self.now |
| 154 |
|
| 155 |
elif self.state == FadeState.FadingIn: |
| 156 |
# Calculate an optimal fadeout given the current volume |
| 157 |
volume = self.fade_volume |
| 158 |
start = -((volume * fade_len) - self.now) |
| 159 |
self.state = FadeState.FadingOut |
| 160 |
|
| 161 |
else: |
| 162 |
return |
| 163 |
|
| 164 |
self.logger.debug( |
| 165 |
"foop: starting fade (now: %s, start: %s, len: %s)", |
| 166 |
self.now, |
| 167 |
start, |
| 168 |
fade_len, |
| 169 |
) |
| 170 |
|
| 171 |
self._cancel() |
| 172 |
if self._execute_fade(start, fade_len): |
| 173 |
self.timer_id = GLib.timeout_add(10, self._execute_fade, start, fade_len) |
| 174 |
|
| 175 |
def get_user_volume(self): |
| 176 |
return self.user_volume |
| 177 |
|
| 178 |
def is_fading_out(self): |
| 179 |
return self.state == FadeState.FadingOut |
| 180 |
|
| 181 |
def setup_track(self, track, fade_in, fade_out, is_update=False, now=None): |
| 182 |
""" |
| 183 |
Call this function either when a track first starts, or if the |
| 184 |
crossfade period has been updated. |
| 185 |
|
| 186 |
As a bonus, calling this function with crossfade_duration=None |
| 187 |
will automatically stop the track at its stop_offset |
| 188 |
|
| 189 |
:param fade_in: Set to None or fade duration in seconds |
| 190 |
:param fade_out: Set to None or fade duration in seconds |
| 191 |
:param is_update: Set True if this is a settings update |
| 192 |
""" |
| 193 |
|
| 194 |
# If user disables crossfade during a transition, then don't |
| 195 |
# cancel the transition |
| 196 |
|
| 197 |
has_fade = fade_in is not None or fade_out is not None |
| 198 |
|
| 199 |
if is_update and has_fade: |
| 200 |
if self.state == FadeState.FadingOut: |
| 201 |
# Don't cancel the current fade out |
| 202 |
return |
| 203 |
|
| 204 |
elif self.state == FadeState.FadingIn: |
| 205 |
# Don't cancel the current fade in, but adjust the |
| 206 |
# current fade out |
| 207 |
_, _, self.fade_out_start, self.fade_out_end = self.calculate_fades( |
| 208 |
track, fade_in, fade_out |
| 209 |
) |
| 210 |
|
| 211 |
self._next(now=now) |
| 212 |
return |
| 213 |
|
| 214 |
if has_fade: |
| 215 |
self.play(*self.calculate_fades(track, fade_in, fade_out)) |
| 216 |
else: |
| 217 |
stop_offset = track.get_tag_raw('__stopoffset') or 0 |
| 218 |
if stop_offset > 0: |
| 219 |
self.play(None, None, stop_offset, stop_offset, now=now) |
| 220 |
else: |
| 221 |
self.play(now=now) |
| 222 |
|
| 223 |
def play( |
| 224 |
self, |
| 225 |
fade_in_start=None, |
| 226 |
fade_in_end=None, |
| 227 |
fade_out_start=None, |
| 228 |
fade_out_end=None, |
| 229 |
now=None, |
| 230 |
): |
| 231 |
'''Don't call this when doing crossfading''' |
| 232 |
|
| 233 |
self.fade_in_start = fade_in_start |
| 234 |
self.fade_in_end = fade_in_end |
| 235 |
|
| 236 |
self.fade_out_start = fade_out_start |
| 237 |
self.fade_out_end = fade_out_end |
| 238 |
|
| 239 |
if self.fade_in_start is not None: |
| 240 |
self.state = FadeState.FadingIn |
| 241 |
elif self.fade_out_start is None: |
| 242 |
self.state = FadeState.NoFade |
| 243 |
else: |
| 244 |
self.state = FadeState.Normal |
| 245 |
|
| 246 |
self._next(now=now) |
| 247 |
|
| 248 |
def pause(self): |
| 249 |
self._cancel() |
| 250 |
|
| 251 |
def seek(self, to): |
| 252 |
self._next(now=to) |
| 253 |
|
| 254 |
def set_user_volume(self, volume): |
| 255 |
self.user_volume = volume |
| 256 |
self.stream.set_volume(self.user_volume * self.fade_volume) |
| 257 |
|
| 258 |
def set_fade_volume(self, volume): |
| 259 |
self.fade_volume = volume |
| 260 |
self.stream.set_volume(self.user_volume * self.fade_volume) |
| 261 |
|
| 262 |
def unpause(self): |
| 263 |
self._next() |
| 264 |
|
| 265 |
def stop(self): |
| 266 |
self.state = FadeState.NoFade |
| 267 |
self._cancel() |
| 268 |
|
| 269 |
def _next(self, now=None): |
| 270 |
self._cancel() |
| 271 |
|
| 272 |
if self.state == FadeState.NoFade: |
| 273 |
self.set_fade_volume(1.0) |
| 274 |
return |
| 275 |
|
| 276 |
if now is None: |
| 277 |
now = self.stream.get_position() / self.SECOND |
| 278 |
|
| 279 |
msg = "Fade data: now: %.2f; in: %s,%s; out: %s,%s" |
| 280 |
self.logger.debug( |
| 281 |
msg, |
| 282 |
now, |
| 283 |
self.fade_in_start, |
| 284 |
self.fade_in_end, |
| 285 |
self.fade_out_start, |
| 286 |
self.fade_out_end, |
| 287 |
) |
| 288 |
|
| 289 |
if self.state == FadeState.FadingIn: |
| 290 |
if now < self.fade_in_end: |
| 291 |
self._on_fade_start(now=now) |
| 292 |
return |
| 293 |
|
| 294 |
self.state = FadeState.Normal |
| 295 |
|
| 296 |
if self.fade_out_start is None: |
| 297 |
self.state = FadeState.NoFade |
| 298 |
self.set_fade_volume(1.0) |
| 299 |
return |
| 300 |
|
| 301 |
fade_tm = int((self.fade_out_start - now) * 1000) |
| 302 |
if fade_tm > 0: |
| 303 |
self.logger.debug("- Will fade out in %.2f seconds", fade_tm / 1000.0) |
| 304 |
self.timer_id = GLib.timeout_add(fade_tm, self._on_fade_start) |
| 305 |
self.set_fade_volume(1.0) |
| 306 |
else: |
| 307 |
# do the fade now |
| 308 |
self._on_fade_start(now=now) |
| 309 |
|
| 310 |
def _cancel(self): |
| 311 |
if self.timer_id: |
| 312 |
GLib.source_remove(self.timer_id) |
| 313 |
self.timer_id = None |
| 314 |
|
| 315 |
def _on_fade_start(self, now=None): |
| 316 |
if now is None: |
| 317 |
now = self.stream.get_position() / self.SECOND |
| 318 |
|
| 319 |
self.now = now - 0.010 |
| 320 |
|
| 321 |
if self.state == FadeState.FadingIn: |
| 322 |
self.logger.debug("Fade in begins") |
| 323 |
end = self.fade_in_end |
| 324 |
start = self.fade_in_start |
| 325 |
else: |
| 326 |
end = self.fade_out_end |
| 327 |
start = self.fade_out_start |
| 328 |
|
| 329 |
if self.state != FadeState.FadingOut: |
| 330 |
self.logger.debug("Fade out begins at %s", self.now) |
| 331 |
self.state = FadeState.FadingOut |
| 332 |
self.on_fade_out() |
| 333 |
|
| 334 |
fade_len = float(end - start) |
| 335 |
|
| 336 |
if self._execute_fade(start, fade_len): |
| 337 |
self.timer_id = GLib.timeout_add(10, self._execute_fade, start, fade_len) |
| 338 |
return False |
| 339 |
|
| 340 |
def _execute_fade(self, fade_start, fade_len): |
| 341 |
""" |
| 342 |
Executes a fade for a period of time, then ends |
| 343 |
|
| 344 |
:param fade_start: When the fade should have started |
| 345 |
:param fade_len: _total_ length of fade, regardless of start |
| 346 |
""" |
| 347 |
|
| 348 |
# Don't query the stream, just assume this is close enough |
| 349 |
self.now += 0.010 |
| 350 |
fading_in = self.state == FadeState.FadingIn |
| 351 |
|
| 352 |
if fade_len < 0.01: |
| 353 |
volume = 0.0 |
| 354 |
else: |
| 355 |
volume = (self.now - fade_start) / fade_len |
| 356 |
|
| 357 |
if not fading_in: |
| 358 |
volume = 1.0 - volume |
| 359 |
|
| 360 |
volume = min(max(0.0, volume), 1.0) |
| 361 |
self.set_fade_volume(volume) |
| 362 |
|
| 363 |
if self.now > fade_start + fade_len: |
| 364 |
self.timer_id = None |
| 365 |
|
| 366 |
if fading_in: |
| 367 |
self.logger.debug("Fade in ends") |
| 368 |
self.state = FadeState.Normal |
| 369 |
self._next() |
| 370 |
else: |
| 371 |
self.logger.debug("Fade out ends") |
| 372 |
self.state = FadeState.NoFade |
| 373 |
self.stream.stop() |
| 374 |
|
| 375 |
return False |
| 376 |
|
| 377 |
return True |