AO2XP/audio.py

113 lines
2.6 KiB
Python
Raw Normal View History

2021-06-20 01:29:40 -04:00
# platform-dependent audio module using BASS
import platform, ctypes, os
from pybass import constants_only as ctts
import ini
DOWNLOADPROC = ctypes.CFUNCTYPE(ctypes.c_void_p, ctypes.c_void_p, ctypes.c_ulong, ctypes.c_void_p)
dll = None
dllf = ""
opus = ""
use_ctypes = False
if platform.system() == "Windows":
dllf = "bass.dll"
opus = "bassopus.dll"
elif platform.system() == "Darwin":
dllf = "libbass.dylib"
opus = "libbassopus.dylib"
else:
dllf = "libbass.so"
opus = "libbassopus.so"
def checkAvailable():
"""
Check if the DLLs exist
Returns string with dll name if it's missing, empty if all DLLs are in place
"""
one = os.path.exists(os.path.abspath(dllf))
two = os.path.exists(os.path.abspath(opus))
if not one: return dllf
if not two: return two
return ""
def init(freq=48000):
"""
Initialize BASS and the opus plugin
"""
global dll, use_ctypes
if platform.system() == "Darwin":
2021-06-20 01:31:45 -04:00
dll = ctypes.CDLL(dllf)
2021-06-20 01:29:40 -04:00
use_ctypes = True
else:
import pybass as dll
dll.BASS_Free()
dll.BASS_Init(ini.read_ini_int("AO2XP.ini", "Audio", "device", -1), freq, 0, 0, 0)
dll.BASS_PluginLoad(os.path.abspath(opus), 0)
def free():
"""
Free BASS
"""
dll.BASS_Free()
def getcurrdevice():
return dll.BASS_GetDevice()
def getdevices():
"""
Get BASS devices
"""
info = ctts.BASS_DEVICEINFO() if use_ctypes else dll.BASS_DEVICEINFO()
ind = 0
devices = []
while dll.BASS_GetDeviceInfo(ind, ctypes.addressof(info) if use_ctypes else info):
devices.append(info.name)
ind += 1
return devices
def loadhandle(mem, file, offset=0, length=0, flags=0):
"""
Load a BASS stream handle
"""
return dll.BASS_StreamCreateFile(mem, file, ctts.QWORD(offset), ctts.QWORD(length), flags)
def loadURLhandle(url, offset, flags, proc=DOWNLOADPROC(), user=0):
"""
Load a BASS stream handle from an URL
"""
return dll.BASS_StreamCreateURL(url, offset, flags, proc, user)
def freehandle(handle):
"""
Free a handle
"""
return dll.BASS_StreamFree(handle)
def playhandle(handle, restart):
"""
Play a handle
"""
return dll.BASS_ChannelPlay(handle, restart)
def stophandle(handle):
"""
Stop handle playback
"""
return dll.BASS_ChannelStop(handle)
def handleisactive(handle):
"""
Get handle playback status
"""
return dll.BASS_ChannelIsActive(handle)
def sethandleattr(handle, attr, value):
"""
Set handle attribute
"""
if use_ctypes and type(value) == float: value = ctypes.c_float(value)
return dll.BASS_ChannelSetAttribute(handle, attr, value)