Web   ·   Wiki   ·   Activities   ·   Blog   ·   Lists   ·   Chat   ·   Meeting   ·   Bugs   ·   Git   ·   Translate   ·   Archive   ·   People   ·   Donate
summaryrefslogtreecommitdiffstats
path: root/sound.py
blob: 25b254eca40b01fb9edbb8e1c98118b649f105b3 (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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
# This program 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 2 of the License, or
# (at your option) any later version.
#
# This program 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 this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA

import os
import gtk
import gst
from glob import glob
from gettext import gettext as _

import theme
from utils import *
from sugar.activity.activity import get_bundle_path

def load():
    from document import Document

    if Document.sound and Document.sound.custom():
        THEMES.insert(-1, Document.sound)

class Sound:
    playing = False
    current = None
    player = None

    def __init__(self, name, sound, type):
        self.name = name
        self._type = type

        def _tmpname():
            l = sorted(glob(os.path.join(theme.SESSION_PATH, 'sound*')))
            return os.path.join(theme.SESSION_PATH, 'sound.%03d' % (len(l)+1))

        if type == theme.RESTORED:
            self._thumb = theme.pixbuf(theme.SOUND_CUSTOM, theme.THUMB_SIZE)
            self._soundfile = _tmpname()
            file(self._soundfile, 'w').write(sound)
        elif type == theme.CUSTOM:
            self._thumb = theme.pixbuf(theme.SOUND_MUTE, theme.THUMB_SIZE)
            self._soundfile = ''
        elif type == theme.JOURNAL:
            self._thumb = theme.pixbuf(theme.SOUND_CUSTOM, theme.THUMB_SIZE)
            self._soundfile = _tmpname()
            os.rename(sound, self._soundfile) 
        else:
            self._thumb = theme.pixbuf(theme.SOUND_SPEAKER, theme.THUMB_SIZE)
            self._soundfile = sound

    def custom(self):
        return self._type != theme.PREINSTALLED

    def read(self):
        if not self._soundfile:
            return ''
        else:
            return file(self._soundfile, 'r').read()

    def filename(self):
        return self._soundfile

    def thumb(self):
        return self._thumb

    def change(self):
        out = self

        if self._type == theme.CUSTOM:
            out = theme.choose(
                    lambda title, file: Sound(title, file, theme.JOURNAL))
            if not out:
                return None

        Sound.current = self
        if not Sound.playing: return out
        Sound.player.set_state(gst.STATE_NULL)
        if len(out._soundfile) == 0: return out

        Sound.player.set_property('uri', 'file://' + theme.path(out._soundfile))
        Sound.player.set_state(gst.STATE_NULL)
        Sound.player.set_state(gst.STATE_PLAYING)

        return out

THEMES = [
    Sound(_('Gobble'),  'sounds/gobble.wav', theme.PREINSTALLED),
    Sound(_('Funk'),    'sounds/funk.wav', theme.PREINSTALLED),
    Sound(_('Giggle'),  'sounds/giggle.wav', theme.PREINSTALLED),
    Sound(_('Jungle'),  'sounds/jungle.wav', theme.PREINSTALLED),
    Sound(_('Mute'),    '', theme.PREINSTALLED),
    None,
    Sound(_('Custom'),  None, theme.CUSTOM) ]

Sound.current = THEMES[0]

def play():
    Sound.playing = True
    Sound.current.change()

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

# GSTREAMER STUFF

def _gstmessage_cb(bus, message):
    type = message.type

    if type == gst.MESSAGE_EOS:
        # END OF SOUND FILE
        Sound.player.set_state(gst.STATE_NULL)
        Sound.player.set_state(gst.STATE_PLAYING)
    elif type == gst.MESSAGE_ERROR:
        Sound.player.set_state(gst.STATE_NULL)

Sound.player = gst.element_factory_make("playbin", "player")
fakesink = gst.element_factory_make('fakesink', "my-fakesink")
Sound.player.set_property("video-sink", fakesink)

bus = Sound.player.get_bus()
bus.add_signal_watch()
bus.connect('message', _gstmessage_cb)