File: Sound.py

package info (click to toggle)
onboard 1.4.1-5
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, bullseye
  • size: 31,548 kB
  • sloc: python: 29,215; cpp: 5,965; ansic: 5,735; xml: 1,026; sh: 163; makefile: 39
file content (118 lines) | stat: -rw-r--r-- 3,564 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
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
# -*- coding: utf-8 -*-

# Copyright © 2013 Gerd Kohlberger <lowfi@chello.at>
# Copyright © 2013, 2016 marmuta <marmvta@gmail.com>
#
# This file is part of Onboard.
#
# Onboard 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.
#
# Onboard 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, see <http://www.gnu.org/licenses/>.

from __future__ import division, print_function, unicode_literals

from Onboard.utils  import unicode_str

import Onboard.osk as osk
import logging

_logger = logging.getLogger(__name__)


class Sound(object):
    """
    Sound singleton.
    Wrapper for osk.Audio.
    """

    """ Event id for key feedback """
    key_feedback = "onboard-key-feedback"

    def __new__(cls, *args, **kwargs):
        """
        Singleton magic.
        """
        if not hasattr(cls, "self"):
            cls.self = object.__new__(cls, *args, **kwargs)
            cls.self.construct()
        return cls.self

    def __init__(self):
        """
        Called multiple times, do not use.
        """
        pass

    def construct(self):
        self._osk_audio = None

        try:
            self._osk_audio = osk.Audio()
        except Exception as ex:
            _logger.warning("Failed to create osk.Audio: " + unicode_str(ex))

        self.enable()
        self.cache_sample(self.key_feedback)

    def is_valid(self):
        """ Check initialization of the audio backend """
        return self._osk_audio is not None

    def play(self, event_id, x, y, xs, ys):
        """ Play a sound """
        try:
            if self.is_valid():
                self._osk_audio.play(event_id, x, y, xs, ys)
        except Exception as ex:
            _logger.warning("Failed to play sound: " + unicode_str(ex))

    def cancel(self):
        """ Cancel all playing sounds """
        try:
            if self.is_valid():
                self._osk_audio.cancel()
        except Exception as ex:
            _logger.warning("Failed to cancel sound: " + unicode_str(ex))

    def set_theme(self, name):
        """ Set the XDG sound theme """
        try:
            if self.is_valid():
                self._osk_audio.set_theme(name)
        except Exception as ex:
            _logger.warning("Failed to set sound theme: " + unicode_str(ex))

    def enable(self):
        """ Enable canberra audio output """
        try:
            if self.is_valid():
                self._osk_audio.enable()
        except Exception as ex:
            _logger.warning("Failed to enable audio: " + unicode_str(ex))

    def disable(self):
        """ Disable canberra audio output """
        try:
            if self.is_valid():
                self._osk_audio.disable()
        except Exception as ex:
            _logger.warning("Failed to disable audio: " + unicode_str(ex))

    def cache_sample(self, event_id):
        """ Upload sample to the sound server. Blocking call. """
        try:
            if self.is_valid():
                self._osk_audio.cache_sample(event_id)
        except Exception as ex:
            _logger.warning("Failed to cache sample: " + unicode_str(ex))