Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

change volume of single audio device

16 views
Skip to first unread message

molde...@gmail.com

unread,
Nov 13, 2013, 9:40:02 AM11/13/13
to
hi,

i'd like to change the volume of my av-receiver (HDMI) and speakers (analogue) simultaneously. in order to use both outputs i'm using virtual audio cable but if i set it as default device and change volume, hdmi and analogue remains unchanged.
i intend to script a command line tool in python providing to change the volume of a single device (eg "setvolume {deviceID} {volume}")

i've been looking for a similar tool, but none of them worked (eg nircmd); i'm using Win7.

while looking for a solution i found that snippet of code

[CODE]
#!/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]


on executing i get
[CODE]
<<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
[/CODE]

Could anyone tell me why this error occurs?

Jean-Michel Pichavant

unread,
Nov 13, 2013, 10:43:50 AM11/13/13
to molde...@gmail.com, pytho...@python.org

I'm afraid there's nothing related to python here.
It looks like the win function mixerSetControlDetails returned the error MMSYSERR_INVALPARAM.
You've set your devices IDs to 0. Are you sure this is correct ?


Cheers,


JM


-- IMPORTANT NOTICE:

The contents of this email and any attachments are confidential and may also be privileged. If you are not the intended recipient, please notify the sender immediately and do not disclose the contents to any other person, use it for any purpose, or store or copy the information in any medium. Thank you.

Steven D'Aprano

unread,
Nov 13, 2013, 11:05:00 AM11/13/13
to
On Wed, 13 Nov 2013 06:40:02 -0800, moldevort87 wrote:

> on executing i get
> [CODE]
> <<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
> [/CODE]
>
> Could anyone tell me why this error occurs?

It's a Windows error, not a Python error. You'll need to look up the
documentation for the Windows API used (I think it is
mixerSetControlDetails) and see what return code 11 means. I don't even
know if it's documented.

Start by googling for "mixerSetControlDetails return value 11" and go on
from there.



--
Steven

Tim Chase

unread,
Nov 13, 2013, 11:12:31 AM11/13/13
to pytho...@python.org
On 2013-11-13 16:05, Steven D'Aprano wrote:
> Start by googling for "mixerSetControlDetails return value 11" and
> go on from there.

Sounds like Nigel at work.[1]

-tkc


http://en.wikipedia.org/wiki/Up_to_eleven
0 new messages