2016-05-24 17:48:22 -05:00
|
|
|
# -*- coding: utf-8 -*-
|
2018-12-19 17:33:40 -06:00
|
|
|
""" Sound utilities for socializer."""
|
2019-01-01 19:42:53 -06:00
|
|
|
from builtins import range
|
2016-05-24 17:48:22 -05:00
|
|
|
import sys
|
|
|
|
import os
|
2018-12-24 17:53:09 -06:00
|
|
|
import glob
|
|
|
|
import subprocess
|
|
|
|
import logging
|
2016-05-24 17:48:22 -05:00
|
|
|
import paths
|
|
|
|
import sound_lib
|
|
|
|
import output
|
2019-01-25 11:45:45 -06:00
|
|
|
import config
|
2018-12-24 17:53:09 -06:00
|
|
|
from sound_lib import recording
|
2016-05-24 17:48:22 -05:00
|
|
|
from mysc.repeating_timer import RepeatingTimer
|
|
|
|
from mysc.thread_utils import call_threaded
|
2018-12-07 17:53:55 -06:00
|
|
|
from sound_lib import output, input
|
2018-12-24 17:53:09 -06:00
|
|
|
|
|
|
|
log = logging.getLogger("sound")
|
|
|
|
|
2019-04-11 15:52:41 -05:00
|
|
|
def recode_audio(filename, quality=10):
|
|
|
|
subprocess.call(r'"%s" --downmix -q %r "%s"' % (os.path.join(paths.app_path(), 'oggenc2.exe'), quality, filename))
|
2018-12-24 17:53:09 -06:00
|
|
|
|
|
|
|
def get_recording(filename):
|
|
|
|
# try:
|
|
|
|
val = recording.WaveRecording(filename=filename)
|
|
|
|
# except sound_lib.main.BassError:
|
|
|
|
# sound_lib.input.Input()
|
|
|
|
# val = sound_lib.recording.WaveRecording(filename=filename)
|
|
|
|
return val
|
2016-05-24 17:48:22 -05:00
|
|
|
|
|
|
|
class soundSystem(object):
|
|
|
|
|
|
|
|
def check_soundpack(self):
|
|
|
|
""" Checks if the folder where live the current soundpack exists."""
|
|
|
|
self.soundpack_OK = False
|
2018-12-14 15:27:20 -06:00
|
|
|
if os.path.exists(os.path.join(paths.sound_path(), self.config["current_soundpack"])):
|
|
|
|
self.path = os.path.join(paths.sound_path(), self.config["current_soundpack"])
|
2016-05-24 17:48:22 -05:00
|
|
|
self.soundpack_OK = True
|
2018-12-14 15:27:20 -06:00
|
|
|
elif os.path.exists(os.path.join(paths.sound_path(), "default")):
|
2016-05-24 17:48:22 -05:00
|
|
|
log.error("The soundpack does not exist, using default...")
|
2018-12-14 15:27:20 -06:00
|
|
|
self.path = os.path.join(paths.sound_path(), "default")
|
2016-05-24 17:48:22 -05:00
|
|
|
self.soundpack_OK = True
|
|
|
|
else:
|
|
|
|
log.error("The current soundpack could not be found and the default soundpack has been deleted, Socializer will not play sounds.")
|
|
|
|
self.soundpack_OK = False
|
|
|
|
|
|
|
|
def __init__(self, soundConfig):
|
|
|
|
""" Sound Player."""
|
|
|
|
self.config = soundConfig
|
|
|
|
# Set the output and input default devices.
|
|
|
|
try:
|
2018-12-07 17:53:55 -06:00
|
|
|
self.output = output.Output()
|
|
|
|
self.input = input.Input()
|
2016-05-24 17:48:22 -05:00
|
|
|
except:
|
|
|
|
pass
|
|
|
|
# Try to use the selected device from the configuration. It can fail if the machine does not has a mic.
|
|
|
|
try:
|
|
|
|
log.debug("Setting input and output devices...")
|
|
|
|
self.output.set_device(self.output.find_device_by_name(self.config["output_device"]))
|
|
|
|
self.input.set_device(self.input.find_device_by_name(self.config["input_device"]))
|
|
|
|
except:
|
|
|
|
log.error("Error in input or output devices, using defaults...")
|
|
|
|
self.config["output_device"] = "Default"
|
|
|
|
self.config["input_device"] = "Default"
|
2019-01-25 11:45:45 -06:00
|
|
|
# Set proxy for audio.
|
|
|
|
if config.app["app-settings"]["use_proxy"]:
|
|
|
|
self.output.set_proxy(b"socializer:socializer@socializer.su:3128")
|
2016-05-24 17:48:22 -05:00
|
|
|
self.files = []
|
|
|
|
self.cleaner = RepeatingTimer(60, self.clear_list)
|
|
|
|
self.cleaner.start()
|
|
|
|
self.check_soundpack()
|
|
|
|
|
|
|
|
def clear_list(self):
|
|
|
|
if len(self.files) == 0: return
|
|
|
|
try:
|
2019-01-01 19:42:53 -06:00
|
|
|
for i in range(0, len(self.files)):
|
2016-05-24 17:48:22 -05:00
|
|
|
if self.files[i].is_playing == False:
|
|
|
|
self.files[i].free()
|
|
|
|
self.files.pop(i)
|
|
|
|
except IndexError:
|
|
|
|
pass
|
|
|
|
|
|
|
|
def play(self, sound, argument=False):
|
|
|
|
if self.soundpack_OK == False: return
|
|
|
|
if self.config["session_mute"] == True: return
|
|
|
|
sound_object = sound_lib.stream.FileStream(file="%s/%s" % (self.path, sound))
|
2019-01-25 11:45:45 -06:00
|
|
|
# sound_object.volume = self.config["volume"]/100
|
2016-05-24 17:48:22 -05:00
|
|
|
self.files.append(sound_object)
|
|
|
|
sound_object.play()
|