File: speed.py

package info (click to toggle)
linux-show-player 0.5.3-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 4,896 kB
  • sloc: python: 12,408; sh: 154; makefile: 17; xml: 8
file content (87 lines) | stat: -rw-r--r-- 3,050 bytes parent folder | download | duplicates (4)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
# -*- coding: utf-8 -*-
#
# This file is part of Linux Show Player
#
# Copyright 2012-2016 Francesco Ceruti <ceppofrancy@gmail.com>
#
# Linux Show Player is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# Linux Show Player is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with Linux Show Player.  If not, see <http://www.gnu.org/licenses/>.

from PyQt5.QtCore import QT_TRANSLATE_NOOP

from lisp.backend.media_element import ElementType, MediaType
from lisp.core.has_properties import Property
from lisp.modules.gst_backend.gi_repository import Gst
from lisp.modules.gst_backend.gst_element import GstMediaElement


class Speed(GstMediaElement):
    ElementType = ElementType.Plugin
    MediaType = MediaType.Audio
    Name = QT_TRANSLATE_NOOP('MediaElementName', 'Speed')

    speed = Property(default=1.0)

    def __init__(self, pipeline):
        super().__init__()

        self.pipeline = pipeline
        self.scale_tempo = Gst.ElementFactory.make("scaletempo", None)
        self.audio_convert = Gst.ElementFactory.make("audioconvert", None)

        self.pipeline.add(self.scale_tempo)
        self.pipeline.add(self.audio_convert)

        self.scale_tempo.link(self.audio_convert)

        bus = self.pipeline.get_bus()
        bus.add_signal_watch()
        self._handler = bus.connect("message", self.__on_message)

        self._old_speed = self.speed
        self.changed('speed').connect(self.__prepare_speed)

    def __prepare_speed(self, value):
        if self._old_speed != value:
            self._old_speed = value

            if self.pipeline.current_state == Gst.State.PLAYING:
                self.__change_speed()

    def sink(self):
        return self.scale_tempo

    def src(self):
        return self.audio_convert

    def dispose(self):
        bus = self.pipeline.get_bus()
        bus.remove_signal_watch()
        bus.disconnect(self._handler)

    def __on_message(self, bus, message):
        if (message.type == Gst.MessageType.STATE_CHANGED and
                    message.src == self.scale_tempo and
                    message.parse_state_changed()[1] == Gst.State.PLAYING):
            self.__change_speed()

    def __change_speed(self):
        current_position = self.scale_tempo.query_position(Gst.Format.TIME)[1]

        self.scale_tempo.seek(self.speed,
                              Gst.Format.TIME,
                              Gst.SeekFlags.FLUSH | Gst.SeekFlags.ACCURATE,
                              Gst.SeekType.SET,
                              current_position,
                              Gst.SeekType.NONE,
                              0)