Web   ·   Wiki   ·   Activities   ·   Blog   ·   Lists   ·   Chat   ·   Meeting   ·   Bugs   ·   Git   ·   Translate   ·   Archive   ·   People   ·   Donate
summaryrefslogtreecommitdiffstats
path: root/Sound.py
blob: f6262e2d2359a395c9230bd096967ba4a3b3922a (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
# 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 gettext import gettext as _

import Theme
from sugar.activity.activity import get_bundle_path

sound_icon = Theme.pixmap('icons/sound_icon.png')

THEMES = (
    { 'name'  : _('Custom'),
      'pixbuf': sound_icon,
      'sound' : None },

    { 'name'  :  _('Gobble'),
      'pixbuf': sound_icon,
      'sound' : 'sounds/gobble.wav' },

    { 'name'  : _('Funk'),
      'pixbuf': sound_icon,
      'sound' : 'sounds/funk.wav' },

    { 'name'  : _('Giggle'),
      'pixbuf': sound_icon,
      'sound' : 'sounds/giggle.wav' },

    { 'name'  : _('Jungle'),
      'pixbuf': sound_icon,
      'sound' : 'sounds/jungle.wav' },

    { 'name'  : _('Mute'),
      'pixbuf': sound_icon,
      'sound' : None } )

class FileInstanceVariable:
    def __init__(self, value = None):
        self.value = value

    def get(self):
        return self.value

    def set(self, value):
        self.value = value

    def __getitem__(self, key):
        return self.value[key]

theme = FileInstanceVariable(THEMES[0])
playing = FileInstanceVariable(False)

def play():
    playing.set(True)
    switch(theme.get())

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

def switch(a_theme):
    if not a_theme: return
    theme.set(a_theme)

    if not playing.get(): return

    player.set_state(gst.STATE_NULL)

    sound = theme['sound']
    if not sound: return

    player.set_property('uri', 'file://' + Theme.path(sound))
    player.set_state(gst.STATE_NULL)
    player.set_state(gst.STATE_PLAYING)

# GSTREAMER STUFF

def _gstmessage_cb(bus, message):
    t = message.type
    if t == gst.MESSAGE_EOS:
        # END OF SOUND FILE
        player.set_state(gst.STATE_NULL)
        player.set_state(gst.STATE_PLAYING)
    elif t == gst.MESSAGE_ERROR:
        player.set_state(gst.STATE_NULL)

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

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