Web   ·   Wiki   ·   Activities   ·   Blog   ·   Lists   ·   Chat   ·   Meeting   ·   Bugs   ·   Git   ·   Translate   ·   Archive   ·   People   ·   Donate
summaryrefslogtreecommitdiffstats
path: root/sound.py
blob: 5fef049be84ae808237c96e7f190218fff4c160f (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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
# 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
import shutil
from glob import glob
from gettext import gettext as _

import toolkit.chooser as chooser

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.append(Document.sound)

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

    def __init__(self, name, id, soundfile, thumb):
        self.name = name
        self.id = id
        self._soundfile = soundfile
        self._thumb = theme.pixbuf(thumb, theme.THUMB_SIZE)

    def custom(self):
        return True

    def serialize(self):
        return file(self._soundfile, 'r').read()

    def thumb(self):
        return self._thumb

    def select(self):
        if Sound.current != self:
            Sound.current = self
            Sound.player.set_state(gst.STATE_NULL)
            Sound.player.set_property('uri',
                    'file://' + theme.path(self._soundfile))
        if Sound.playing:
            Sound.player.set_state(gst.STATE_PLAYING)
        return self

class PreinstalledSound(Sound):
    def __init__(self, name, filename):
        Sound.__init__(self, name, filename, filename, theme.SOUND_SPEAKER)

    def custom(self):
        return False

class MuteSound(Sound):
    def __init__(self, name):
        Sound.__init__(self, name, 'mute', None, theme.SOUND_MUTE)

    def custom(self):
        return False

    def serialize(self):
        return ''

    def select(self):
        Sound.current = self
        Sound.player.set_state(gst.STATE_PAUSED)
        return self

class CustomSound(Sound):
    def __init__(self, name):
        Sound.__init__(self, name, None, None, theme.SOUND_CUSTOM)

    def select(self):
        sound = chooser.pick(lambda jobject: JournalSound(jobject),
                what=chooser.AUDIO)
        if sound:
            sound.select()
        return sound

class RestoredSound(Sound):
    def __init__(self, name, id, data):
        soundfile = os.path.join(theme.SESSION_PATH, id)
        Sound.__init__(self, name, id, soundfile, theme.SOUND_CUSTOM)
        file(soundfile, 'w').write(data)

class JournalSound(Sound):
    def __init__(self, jobject):
        soundfile = os.path.join(theme.SESSION_PATH, jobject.object_id)
        Sound.__init__(self, jobject.metadata['title'],
                jobject.object_id, soundfile, theme.SOUND_CUSTOM)
        shutil.copy(jobject.file_path, soundfile) 
        THEMES.append(self)

THEMES = [
    PreinstalledSound(_('Gobble'),  'sounds/gobble.wav'),
    PreinstalledSound(_('Funk'),    'sounds/funk.wav'),
    PreinstalledSound(_('Giggle'),  'sounds/giggle.wav'),
    PreinstalledSound(_('Jungle'),  'sounds/jungle.wav'),
    MuteSound(_('Mute')),
    None,
    CustomSound(_('Custom')) ]

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

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

# GSTREAMER STUFF

def _reload_cb(bus, message):
    Sound.player.set_state(gst.STATE_READY)
    Sound.player.set_state(gst.STATE_PLAYING)

def _error_cb(bus, message):
    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::eos', _reload_cb)
bus.connect('message::error', _error_cb)