habe folgendes Grundproblem:
Möchte meinen AV-Receiver (HDMI) und Lautsprecher (Klinke) gleichzeitig in der Lautstärke ändern. Dass die beiden Ausgänge gleichzeitig angesprochen werden habe ich mit Virtual Audio Cable hinbekommen, wenn ich jedoch dieses als Standardgerät setze und die Lautstärke ändere, bleibt die Lautstärke der Einzelgeräte unverändert. Jetzt möchte ich mit Python ein Kommandozeilentool scripten, womit ich die einzelnen Geräte ansprechen kann (zB "setvolume {deviceID} {volume}")
Nach einem ähnlichen Tool habe ich schon gesucht, aber nichts gefunden (benutze Win7). Auch mit nircmd schaffe ich es nicht, die Lautstärke einzelner Devices zu ändern.
Jetzt habe ich im Net folgenden Code gefunden:
Code: Alles auswählen
#!/usr/bin/env python
#Boa:PyApp:main
modules = {}
import ctypes
mixerSetControlDetails = (
ctypes.windll.winmm.mixerSetControlDetails)
mixerGetControlDetails = (
ctypes.windll.winmm.mixerGetControlDetailsA)
# Some constants
MIXER_OBJECTF_MIXER = 0 # mmsystem.h
VOLUME_CONTROL_ID = 0 # Same on all machines?
SPEAKER_LINE_FADER_ID = 0 # "Identifier <identifier> in OID value does not resolve to a positive integer"
MINIMUM_VOLUME = 0 # fader control (MSDN Library)
MAXIMUM_VOLUME = 65535 # fader control (MSDN Library)
class MIXERCONTROLDETAILS(ctypes.Structure):
_pack_ = 1
_fields_ = [('cbStruct', ctypes.c_ulong),
('dwControlID', ctypes.c_ulong),
('cChannels', ctypes.c_ulong),
('cMultipleItems', ctypes.c_ulong),
('cbDetails', ctypes.c_ulong),
('paDetails', ctypes.POINTER(ctypes.c_ulong))]
def setVolume(volume):
"""Set the speaker volume on the 'Volume Control' mixer"""
if not (MINIMUM_VOLUME <= volume <= MAXIMUM_VOLUME):
raise ValueError, "Volume out of range"
cd = MIXERCONTROLDETAILS(ctypes.sizeof(MIXERCONTROLDETAILS),
SPEAKER_LINE_FADER_ID,
1, 0,
ctypes.sizeof(ctypes.c_ulong),
ctypes.pointer(ctypes.c_ulong(volume)))
ret = mixerSetControlDetails(VOLUME_CONTROL_ID,
ctypes.byref(cd),
MIXER_OBJECTF_MIXER)
if ret != 0:
print WindowsError, "Error %d while setting volume" % ret
ret = mixerGetControlDetails(VOLUME_CONTROL_ID,
ctypes.byref(cd),
MIXER_OBJECTF_MIXER)
if ret != 0:
print WindowsError, "Error %d while setting volume" % ret
else:
print 'cbStruct', cd.cbStruct
print 'dwControlID', cd.dwControlID
print 'cChannels', cd.cChannels
print 'cMultipleItems', cd.cMultipleItems
print 'cbDetails', cd.cbDetails
print 'paDetails', cd.paDetails.contents
return
setVolume((2**16-1)/2)
from ctypes import *
from struct import *
winmm= windll.winmm
print
print 'waveOutGetNumDevs=',winmm.waveOutGetNumDevs()
print 'mixerGetNumDevs', winmm.mixerGetNumDevs()
wvcps= ' '*52
print 'res:', winmm.waveOutGetDevCapsA(0,wvcps,len(wvcps))
res = unpack('IIL32cLI', wvcps)
wMid=res[0]
wPid=res[1]
vDriverVersion=res[2]
szPname=''.join(res[3:35])
dwFormats=res[35]
wChannels=res[36]
print 'wMid=',wMid
print 'wPid=',wPid
print 'vDriverVersion=',vDriverVersion
print 'szPname=',szPname
print 'dwFormats=',dwFormats
print 'wChannels=',wChannels
vol=c_ulong()
print 'res', winmm.waveOutGetVolume(0, byref(vol))
print 'l:', vol.value & 0xffff, 'r:',vol.value >> 16 # left, right
Code: Alles auswählen
<<type 'exceptions.WindowsError'> Error 11 while setting volume
<type 'exceptions.WindowsError'> Error 11 while setting volume
waveOutGetNumDevs= 3
mixerGetNumDevs 4
res: 0
wMid= 6553601
wPid= 1537
vDriverVersion= 2037083727
szPname= o (AMD High Definition Audi
dwFormats= 2
wChannels= 36
res 0
l: 65535 r: 0