1
0
mirror of https://github.com/aidygus/LinVAM.git synced 2024-11-23 09:18:04 +11:00
LinVAM/soundfiles.py
smirgol 7b4ae4d3e7 Fixes & implementation of playing audio files. See changes.md for details.
- hacked profileexcutor to reload command list when changing it. it did not do that,
  so changing the profile had no effect at all.
- hacked profileexecutor to somewhat make use of "Enable listening".
  It was an option without functionality at all until now.
- created new directory 'voicepacks', copy HCS voicepacks here
- new command to play a sound
- new gui to select sounds from voicepacks
- created playsound class that reads voicepack files and plays audio files with ffplay
- added alternate keypress handling using xdotool, which won't require root privileges.
  might not work for any key existing in profiles, as some need to be remapped. added some remappings to profileexecutor.py pressKey()
- added basic command line argument reading to set some configs. right now there:
  -noroot - will enable xdotool usage for keypresses
  -xdowindowid <windowid> - will send keypresses to this window only, makes it more relyable when window is not focused for any reason
- added auto-detection of Elite Dangerous client window id if -noroot is used and no -xdowindowid is supplied.
  for this to work, start this script AFTER the client is already running.
- monkey-wrenched a volume slider to the main window
- added confirmation dialog for removing a profile (!)
- copy existing profile. added a copy button to the main menu
- properly shutdown audio recording stream
- updated gitignore to ignore audio files
2020-05-16 20:51:24 +02:00

87 lines
2.8 KiB
Python

import time
import threading
import os
import shutil
from os import path
import subprocess
import shlex
import signal
# I've tried a couple of libs that are capable of playing mp3:
# pysound - offers no way to stop sounds. can't play files with whitespace in path
# pygame - has problems with certain mp3 files from voice packs
# mpg123 - does not offer volume control. need to be threaded as it's a system binary
#
# ffplay - need to be threaded as it's a system binary. i picked this one as it uses ffmpeg which
# is an excellent tool and should be installed on any system already
# I needed some process stuff to be able to stop an already playing sound (kill ffplay subprocess)
class SoundFiles():
def __init__(self):
print("SoundFiles: init")
self.m_sounds = {}
self.scanSoundFiles()
self.thread_play = False
self.volume = 100
def scanSoundFiles(self):
print("SoundFiles: scanning")
if not path.exists('./voicepacks'):
print("No folder 'voicepacks' found. Please create one and copy all your voicepacks in there.")
return
for root, dirs, files in os.walk("./voicepacks"):
for file in files:
if file.endswith(".mp3"):
# we expect a path like this:
# voicepacks/VOICEPACKNAME/COMMANDGROUP/(FURTHER_OPTIONAL_FOLDERS/)FILE
path_parts = root.split('/')
if len(path_parts) < 4:
continue
if not path_parts[2] in self.m_sounds:
self.m_sounds[path_parts[2]] = {}
category = path_parts[3]
# there might be subfolders, so we have more than just 4 split results...
# for the ease of my mind, we concat the voicepack subfolders to 1 category name
# like voicepacks/hcspack/Characters/Astra/blah.mp3 will become:
#
# voicepack = hcspack
# category = Characters/Astra
# file = blah.mp4
if len(path_parts) > 4:
for i in range(4, len(path_parts)):
category = category + '/' + path_parts[i]
if not category in self.m_sounds[path_parts[2]]:
self.m_sounds[path_parts[2]][category] = []
self.m_sounds[path_parts[2]][category].append(file)
def play(self, sound_file):
if not os.path.isfile(sound_file):
print("ERROR - Sound file not found: ", sound_file)
return
self.stop()
# construct shell command. use shlex to split it up into valid args for Popen.
cmd = "ffplay -nodisp -autoexit -loglevel quiet -volume " + str(self.volume) + " \"" + sound_file + "\"";
args = shlex.split(cmd)
self.thread_play = subprocess.Popen(args)
def stop(self):
if not self.thread_play == False:
# that aint no nice, but it's the only way i got the subprocess reliably killed.
# self.thread_play.terminate() or kill() should do the trick, but it won't
try:
os.kill(self.thread_play.pid, signal.SIGKILL)
except OSError:
pass
def setVolume(self, volume):
self.volume = volume;