mirror of
https://github.com/ankitects/anki.git
synced 2025-09-22 16:02:23 -04:00
new audio handling
This commit is contained in:
parent
a6f7276b03
commit
e812f69dee
1 changed files with 72 additions and 24 deletions
|
@ -8,30 +8,10 @@ Sound support
|
||||||
"""
|
"""
|
||||||
__docformat__ = 'restructuredtext'
|
__docformat__ = 'restructuredtext'
|
||||||
|
|
||||||
import re, sys
|
import re, sys, threading, time, subprocess, os
|
||||||
|
|
||||||
try:
|
# Shared utils
|
||||||
import pygame
|
##########################################################################
|
||||||
pygame.mixer.pre_init(44100,-16,2, 1024 * 3)
|
|
||||||
pygame.mixer.init()
|
|
||||||
soundsAvailable = True
|
|
||||||
except:
|
|
||||||
soundsAvailable = False
|
|
||||||
print "Warning, pygame not available. No sounds will play."
|
|
||||||
|
|
||||||
def playPyGame(path):
|
|
||||||
"Play a sound. Expects a unicode pathname."
|
|
||||||
if not soundsAvailable:
|
|
||||||
return
|
|
||||||
path = path.encode(sys.getfilesystemencoding())
|
|
||||||
try:
|
|
||||||
if pygame.mixer.music.get_busy():
|
|
||||||
pygame.mixer.music.queue(path)
|
|
||||||
else:
|
|
||||||
pygame.mixer.music.load(path)
|
|
||||||
pygame.mixer.music.play()
|
|
||||||
except pygame.error:
|
|
||||||
return
|
|
||||||
|
|
||||||
def playFromText(text):
|
def playFromText(text):
|
||||||
for match in re.findall("\[sound:(.*?)\]", text):
|
for match in re.findall("\[sound:(.*?)\]", text):
|
||||||
|
@ -43,6 +23,71 @@ def stripSounds(text):
|
||||||
def hasSound(text):
|
def hasSound(text):
|
||||||
return re.search("\[sound:.*?\]", text) is not None
|
return re.search("\[sound:.*?\]", text) is not None
|
||||||
|
|
||||||
|
# External audio
|
||||||
|
##########################################################################
|
||||||
|
|
||||||
|
queue = []
|
||||||
|
manager = None
|
||||||
|
|
||||||
|
if sys.platform.startswith("win32"):
|
||||||
|
externalPlayer = ["c:\\program files\\windows media player\\wmplayer.exe"]
|
||||||
|
else:
|
||||||
|
externalPlayer = ["mplayer", "-really-quiet"]
|
||||||
|
|
||||||
|
# don't show box on windows
|
||||||
|
if sys.platform == "win32":
|
||||||
|
si = subprocess.STARTUPINFO()
|
||||||
|
si.dwFlags |= subprocess.STARTF_USESHOWWINDOW
|
||||||
|
else:
|
||||||
|
si = None
|
||||||
|
|
||||||
|
class QueueMonitor(threading.Thread):
|
||||||
|
|
||||||
|
def run(self):
|
||||||
|
while 1:
|
||||||
|
time.sleep(0.1)
|
||||||
|
if queue:
|
||||||
|
path = queue.pop(0)
|
||||||
|
p = subprocess.Popen(externalPlayer + [path],
|
||||||
|
startupinfo=si)
|
||||||
|
p.wait()
|
||||||
|
else:
|
||||||
|
return
|
||||||
|
|
||||||
|
def playExternal(path):
|
||||||
|
global manager
|
||||||
|
path = os.path.abspath(path).encode(sys.getfilesystemencoding())
|
||||||
|
queue.append(path)
|
||||||
|
if not manager or not manager.isAlive():
|
||||||
|
manager = QueueMonitor()
|
||||||
|
manager.start()
|
||||||
|
|
||||||
|
# Pygame
|
||||||
|
##########################################################################
|
||||||
|
|
||||||
|
# try:
|
||||||
|
# import pygame
|
||||||
|
# pygame.mixer.pre_init(44100,-16,2, 1024 * 3)
|
||||||
|
# pygame.mixer.init()
|
||||||
|
# soundsAvailable = True
|
||||||
|
# except:
|
||||||
|
# soundsAvailable = False
|
||||||
|
# print "Warning, pygame not available. No sounds will play."
|
||||||
|
|
||||||
|
# def playPyGame(path):
|
||||||
|
# "Play a sound. Expects a unicode pathname."
|
||||||
|
# if not soundsAvailable:
|
||||||
|
# return
|
||||||
|
# path = path.encode(sys.getfilesystemencoding())
|
||||||
|
# try:
|
||||||
|
# if pygame.mixer.music.get_busy():
|
||||||
|
# pygame.mixer.music.queue(path)
|
||||||
|
# else:
|
||||||
|
# pygame.mixer.music.load(path)
|
||||||
|
# pygame.mixer.music.play()
|
||||||
|
# except pygame.error:
|
||||||
|
# return
|
||||||
|
|
||||||
# Mac audio support
|
# Mac audio support
|
||||||
##########################################################################
|
##########################################################################
|
||||||
|
|
||||||
|
@ -89,7 +134,10 @@ try:
|
||||||
except ImportError:
|
except ImportError:
|
||||||
pass
|
pass
|
||||||
|
|
||||||
|
# Default audio player
|
||||||
|
##########################################################################
|
||||||
|
|
||||||
if sys.platform.startswith("darwin"):
|
if sys.platform.startswith("darwin"):
|
||||||
play = playOSX
|
play = playOSX
|
||||||
else:
|
else:
|
||||||
play = playPyGame
|
play = playExternal
|
||||||
|
|
Loading…
Reference in a new issue