socializer/src/sound.py

87 lines
2.8 KiB
Python
Raw Normal View History

2016-05-24 17:48:22 -05:00
# -*- coding: utf-8 -*-
2018-12-19 17:33:40 -06:00
""" Sound utilities for socializer."""
from __future__ import unicode_literals
2016-05-24 17:48:22 -05:00
import sys
import os
import glob
import subprocess
import logging
2016-05-24 17:48:22 -05:00
import paths
import sound_lib
import output
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
from sound_lib import output, input
log = logging.getLogger("sound")
def recode_audio(filename, quality=4.5):
subprocess.call(r'"%s" -q %r "%s"' % (os.path.join(paths.app_path(), 'oggenc2.exe'), quality, filename))
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
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
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...")
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:
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"
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:
for i in xrange(0, len(self.files)):
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))
sound_object.volume = float(self.config["volume"])
self.files.append(sound_object)
sound_object.play()