Web   ·   Wiki   ·   Activities   ·   Blog   ·   Lists   ·   Chat   ·   Meeting   ·   Bugs   ·   Git   ·   Translate   ·   Archive   ·   People   ·   Donate
summaryrefslogtreecommitdiffstats
path: root/olpcfr/tools/sound.py
blob: 7ac6a57fcb22e395dc456f12dcda1927d4f9b5cc (plain)
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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111

# python import
import gst
# ..
from datetime import timedelta


class Player(object):

    def __init__(self, loop=False):
        # playing flag
        self.playing = False
        self.loop = loop
        # player object
        self.player = None
        self._init_player()
        # file to play
        self._soundfile = None

    def _reload_cb(self, bus, message):
        if self.loop is True:
            self.player.set_state(gst.STATE_READY)
            self.player.set_state(gst.STATE_PLAYING)
        else:
            pass

    def _error_cb(self, bus, message):
        self.player.set_state(gst.STATE_NULL)

    def _init_player(self):
        # make player
        self.player = gst.element_factory_make("playbin", "player")
        # video fake
        _fakesink = gst.element_factory_make('fakesink', "my-fakesink")
        self.player.set_property("video-sink", _fakesink)
        # bus ..
        bus = self.player.get_bus()
        bus.add_signal_watch()
        bus.connect('message::eos', self._reload_cb)
        bus.connect('message::error', self._error_cb)

    def serialize(self):
        # little check
        if self._soundfile is None:
            return None
        else:
            return file(self._soundfile, 'r').read()

    def load(self, soundfile):
        # file to play
        self._soundfile = soundfile
        # little check
        if self._soundfile is None:
            pass
        else:
            # load sound file
            self.player.set_state(gst.STATE_NULL)
            self.player.set_property('uri', 'file://' + self._soundfile)

    def get_position(self):
        # little check
        if self._soundfile is None:
            return None
        else:
            # ...
            _position = self.player.query_duration(gst.FORMAT_TIME)[0]
            # ...
            return timedelta(seconds=(_position / gst.SECOND))

    def get_duration(self):
        # little check
        if self._soundfile is None:
            return None
        else:
            # _duration = self.player.query_duration(gst.FORMAT_TIME)[0]
            # ..
            _parser = gst.parse_launch("filesrc name=source ! decodebin2 ! fakesink")
            # ..
            _source = _parser.get_by_name("source")
            _source.set_property("location", self._soundfile)
            # ..
            _parser.set_state(gst.STATE_PLAYING)
            _parser.get_state()
            # ..
            _format = gst.Format(gst.FORMAT_TIME)
            _duration = _parser.query_duration(_format)[0]
            _parser.set_state(gst.STATE_NULL)
            # ..
            return timedelta(seconds=(_duration / gst.SECOND))

    def seek(self, time):
        # little check
        if self._soundfile is None:
            return
        else:
            # format time
            _seek = time * 1000000000
            # do seek
            self.player.seek_simple(gst.FORMAT_TIME, gst.SEEK_FLAG_FLUSH, _seek)

    def play(self):
        self.playing = True
        self.player.set_state(gst.STATE_PLAYING)

    def pause(self):
        self.playing = False
        self.player.set_state(gst.STATE_PAUSED)

    def stop(self):
        self.playing = False
        self.player.set_state(gst.STATE_NULL)