mirror of
https://github.com/ankitects/anki.git
synced 2025-09-20 15:02:21 -04:00
use mplayer slave mode
This commit is contained in:
parent
2d32e1aaa1
commit
3085ce7328
1 changed files with 56 additions and 34 deletions
|
@ -49,17 +49,6 @@ processingChain = [
|
||||||
["lame", "tmp3.wav", processingDst, "--noreplaygain", "--quiet"],
|
["lame", "tmp3.wav", processingDst, "--noreplaygain", "--quiet"],
|
||||||
]
|
]
|
||||||
|
|
||||||
queue = []
|
|
||||||
manager = None
|
|
||||||
|
|
||||||
if sys.platform.startswith("win32"):
|
|
||||||
externalPlayer = ["mplayer.exe", "-ao", "win32", "-really-quiet", "-noconsolecontrols"]
|
|
||||||
dir = os.path.dirname(os.path.abspath(sys.argv[0]))
|
|
||||||
os.environ['PATH'] += ";" + dir
|
|
||||||
os.environ['PATH'] += ";" + dir + "\\..\\dist" # for testing
|
|
||||||
else:
|
|
||||||
externalPlayer = ["mplayer", "-really-quiet", "-noconsolecontrols"]
|
|
||||||
|
|
||||||
# don't show box on windows
|
# don't show box on windows
|
||||||
if sys.platform == "win32":
|
if sys.platform == "win32":
|
||||||
si = subprocess.STARTUPINFO()
|
si = subprocess.STARTUPINFO()
|
||||||
|
@ -111,35 +100,68 @@ def generateNoiseProfile():
|
||||||
processingChain[0] = ["sox", processingSrc, "tmp2.wav",
|
processingChain[0] = ["sox", processingSrc, "tmp2.wav",
|
||||||
"noisered", noiseProfile, NOISE_AMOUNT]
|
"noisered", noiseProfile, NOISE_AMOUNT]
|
||||||
|
|
||||||
# External playing
|
# Mplayer
|
||||||
##########################################################################
|
##########################################################################
|
||||||
|
|
||||||
class QueueMonitor(threading.Thread):
|
if sys.platform.startswith("win32"):
|
||||||
|
mplayerCmd = ["mplayer.exe", "-ao", "win32", "-really-quiet",
|
||||||
|
"-slave", "-idle"]
|
||||||
|
dir = os.path.dirname(os.path.abspath(sys.argv[0]))
|
||||||
|
os.environ['PATH'] += ";" + dir
|
||||||
|
os.environ['PATH'] += ";" + dir + "\\..\\dist" # for testing
|
||||||
|
else:
|
||||||
|
mplayerCmd = ["mplayer", "-really-quiet", "-slave", "-idle"]
|
||||||
|
|
||||||
|
mplayerQueue = []
|
||||||
|
mplayerManager = None
|
||||||
|
mplayerCond = threading.Condition()
|
||||||
|
|
||||||
|
class MplayerMonitor(threading.Thread):
|
||||||
|
|
||||||
def run(self):
|
def run(self):
|
||||||
|
self.mplayer = None
|
||||||
while 1:
|
while 1:
|
||||||
if queue:
|
mplayerCond.acquire()
|
||||||
path = queue.pop(0)
|
while not mplayerQueue:
|
||||||
|
mplayerCond.wait()
|
||||||
|
if not self.mplayer:
|
||||||
|
self.startProcess()
|
||||||
|
nextClears = False
|
||||||
|
while mplayerQueue:
|
||||||
|
item = mplayerQueue.pop(0)
|
||||||
|
if item is None:
|
||||||
|
nextClears = True
|
||||||
|
continue
|
||||||
|
if nextClears:
|
||||||
|
nextClears = False
|
||||||
|
extra = ""
|
||||||
|
else:
|
||||||
|
extra = " 1"
|
||||||
|
cmd = "loadfile %s%s\n" % (item, extra)
|
||||||
|
self.mplayer.stdin.write(cmd)
|
||||||
|
mplayerCond.release()
|
||||||
|
|
||||||
|
def startProcess(self):
|
||||||
try:
|
try:
|
||||||
retryWait(subprocess.Popen(
|
self.mplayer = subprocess.Popen(
|
||||||
externalPlayer + [path], startupinfo=si))
|
mplayerCmd, startupinfo=si, stdin=subprocess.PIPE)
|
||||||
except OSError:
|
except OSError:
|
||||||
raise Exception("Audio player not found")
|
raise Exception("Audio player not found")
|
||||||
else:
|
|
||||||
return
|
|
||||||
time.sleep(0.1)
|
|
||||||
|
|
||||||
def playExternal(path):
|
def queueMplayer(path):
|
||||||
global manager
|
|
||||||
path = path.encode(sys.getfilesystemencoding())
|
path = path.encode(sys.getfilesystemencoding())
|
||||||
queue.append(path)
|
mplayerCond.acquire()
|
||||||
if not manager or not manager.isAlive():
|
mplayerQueue.append(path)
|
||||||
manager = QueueMonitor()
|
mplayerCond.notify()
|
||||||
manager.start()
|
mplayerCond.release()
|
||||||
|
|
||||||
def clearQueueExternal():
|
def clearMplayerQueue():
|
||||||
global queue
|
mplayerCond.acquire()
|
||||||
queue = []
|
mplayerQueue.append(None)
|
||||||
|
mplayerCond.release()
|
||||||
|
|
||||||
|
mplayerManager = MplayerMonitor()
|
||||||
|
mplayerManager.start()
|
||||||
|
|
||||||
# PyAudio recording
|
# PyAudio recording
|
||||||
##########################################################################
|
##########################################################################
|
||||||
|
@ -232,7 +254,7 @@ class PyAudioRecorder(_Recorder):
|
||||||
# Default audio player
|
# Default audio player
|
||||||
##########################################################################
|
##########################################################################
|
||||||
|
|
||||||
play = playExternal
|
play = queueMplayer
|
||||||
clearAudioQueue = clearQueueExternal
|
clearAudioQueue = clearMplayerQueue
|
||||||
|
|
||||||
Recorder = PyAudioRecorder
|
Recorder = PyAudioRecorder
|
||||||
|
|
Loading…
Reference in a new issue