mirror of
https://github.com/MCV-Software/TWBlue.git
synced 2025-07-21 07:16:07 -04:00
The next generation branch has been added
This commit is contained in:
92
src/sound.py
92
src/sound.py
@@ -1,22 +1,4 @@
|
||||
# -*- coding: utf-8 -*-
|
||||
""" Sound utilities."""
|
||||
############################################################
|
||||
# Copyright (c) 2013, 2014 Manuel Eduardo Cortéz Vallejo <manuel@manuelcortez.net>
|
||||
#
|
||||
# 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, see <http://www.gnu.org/licenses/>.
|
||||
#
|
||||
############################################################
|
||||
import sys
|
||||
import url_shortener
|
||||
import audio_services
|
||||
@@ -28,15 +10,18 @@ import paths
|
||||
import sound_lib
|
||||
import subprocess
|
||||
import platform
|
||||
import output
|
||||
system = platform.system()
|
||||
from mysc.repeating_timer import RepeatingTimer
|
||||
|
||||
player = None
|
||||
player = URLPlayer = None
|
||||
|
||||
def setup():
|
||||
global player
|
||||
global player, URLPlayer
|
||||
if not player:
|
||||
player = soundSystem()
|
||||
if not URLPlayer:
|
||||
URLPlayer = URLStream()
|
||||
|
||||
def recode_audio(filename, quality=4.5):
|
||||
global system
|
||||
@@ -55,8 +40,8 @@ class soundSystem(object):
|
||||
def check_soundpack(self):
|
||||
""" Checks if the folder where live the current soundpack exists."""
|
||||
self.soundpack_OK = False
|
||||
if os.path.exists(paths.sound_path(config.main["sound"]["current_soundpack"])):
|
||||
self.path = paths.sound_path(config.main["sound"]["current_soundpack"])
|
||||
if os.path.exists(paths.sound_path(config.app["app-settings"]["current_soundpack"])):
|
||||
self.path = paths.sound_path(config.app["app-settings"]["current_soundpack"])
|
||||
self.soundpack_OK = True
|
||||
elif os.path.exists(paths.sound_path("default")):
|
||||
self.path = paths.sound_path("default")
|
||||
@@ -71,11 +56,11 @@ class soundSystem(object):
|
||||
self.input = sound_lib.input.Input()
|
||||
# Try to use the selected device from the configuration. It can fail if the machine does not has a mic.
|
||||
try:
|
||||
self.output.set_device(self.output.find_device_by_name(config.main["sound"]["output_device"]))
|
||||
self.input.set_device(self.input.find_device_by_name(config.main["sound"]["input_device"]))
|
||||
self.output.set_device(self.output.find_device_by_name(config.app["app-settings"]["output_device"]))
|
||||
self.input.set_device(self.input.find_device_by_name(config.app["app-settings"]["input_device"]))
|
||||
except:
|
||||
config.main["sound"]["output_device"] = "Default"
|
||||
config.main["sound"]["input_device"] = "Default"
|
||||
config.app["app-settings"]["output_device"] = "Default"
|
||||
config.app["app-settings"]["input_device"] = "Default"
|
||||
|
||||
self.files = []
|
||||
self.cleaner = RepeatingTimer(60, self.clear_list)
|
||||
@@ -84,42 +69,49 @@ class soundSystem(object):
|
||||
|
||||
def clear_list(self):
|
||||
log.debug("Cleaning sounds... Total sounds found: %i" % len(self.files))
|
||||
if self.files == []: return
|
||||
for i in xrange(0, len(self.files)):
|
||||
if self.files[i].is_playing == False:
|
||||
self.files[i].free()
|
||||
self.files.pop(i)
|
||||
if len(self.files) == 0: return
|
||||
try:
|
||||
for i in range(0, len(self.files)):
|
||||
if self.files[i].is_playing == False:
|
||||
self.files[i].free()
|
||||
self.files.pop(i)
|
||||
except IndexError:
|
||||
pass
|
||||
log.debug("Files used now: %i" % len(self.files))
|
||||
|
||||
def play(self, sound, argument=False):
|
||||
if self.soundpack_OK == False: return
|
||||
if config.main["sound"]["global_mute"] == True: return
|
||||
if config.app["app-settings"]["global_mute"] == True: return
|
||||
sound_object = sound_lib.stream.FileStream(file="%s/%s" % (self.path, sound))
|
||||
sound_object.volume = float(config.main["sound"]["volume"])
|
||||
sound_object.volume = float(config.app["app-settings"]["volume"])
|
||||
self.files.append(sound_object)
|
||||
sound_object.play()
|
||||
|
||||
class urlStream(object):
|
||||
def __init__(self, url):
|
||||
self.url = url
|
||||
log.debug(u"URL: %s" % url)
|
||||
class URLStream(object):
|
||||
def __init__(self):
|
||||
self.url = None
|
||||
self.prepared = False
|
||||
|
||||
def prepare(self):
|
||||
def prepare(self, url):
|
||||
self.prepared = False
|
||||
url = url_shortener.unshorten(self.url)
|
||||
log.debug("url desacortada: "+str(url))
|
||||
if url != None:
|
||||
self.url = url
|
||||
transformer = audio_services.find_url_transformer(self.url)
|
||||
self.url = transformer(self.url)
|
||||
log.debug(u"Url transformada: %s" % self.url)
|
||||
prepare = True
|
||||
self.url = url_shortener.unshorten(url)
|
||||
if self.url != None:
|
||||
transformer = audio_services.find_url_transformer(self.url)
|
||||
self.url = transformer(self.url)
|
||||
self.prepared = True
|
||||
|
||||
def play(self):
|
||||
self.stream = sound_lib.stream.URLStream(url=self.url)
|
||||
self.stream.volume = float(config.main["sound"]["volume"])
|
||||
self.stream.play()
|
||||
def play(self, url):
|
||||
if hasattr(self, "stream") and self.stream.is_playing:
|
||||
output.speak(_(u"Stopped"))
|
||||
self.stream.stop()
|
||||
del self.stream
|
||||
else:
|
||||
output.speak(_(u"Playing..."))
|
||||
self.prepare(url)
|
||||
if self.prepared == True:
|
||||
self.stream = sound_lib.stream.URLStream(url=self.url)
|
||||
self.stream.volume = float(config.app["app-settings"]["volume"])
|
||||
self.stream.play()
|
||||
|
||||
@staticmethod
|
||||
def delete_old_tempfiles():
|
||||
|
Reference in New Issue
Block a user