[python-win32] Control Volume on Windows

2014-03-11 Thread Trevor Haba
I'm looking for a way to use python to change the volume on windows. After
some googling the best I could come up with was this script I got from a
relatively ancient entry on this mailing list (I think it was from 2005 or
something). I'm using windows 7, and I would like it to work on windows 8
if possible, though its not necessary. I don't need to support any previous
versions of windows.

Here is the 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 = 1 # Same on all machines?
SPEAKER_LINE_FADER_ID = 1 # 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)
setVolume(0)   ## added by me, neither value does anything

If I run this, the ret value is 0 and it prints out all the data from the
cd struct. But it doesn't change the volume at all. If I change the
SPEAKER_LINE_FADER_ID to 0 the program crashes with: windows error 1025 -
error setting the volume. I've tried googling that error, winmm,
ctypes.windll, and a bunch of other things but I seem unable to find any
resources with any information at all. The original poster of this script
said the constants might need to be changed, and that finding the right
constants could take some work but never elaborated on how to actually do
it.

I've looked at the relevant MSDN C++ docs that windows provides, and while
they are somewhat informative I would really like to keep this project to
python only. I have been unable to find much of any documentation on
pywin32 itself. Is it hidden somewhere?

Anyone have any ideas?
___
python-win32 mailing list
python-win32@python.org
https://mail.python.org/mailman/listinfo/python-win32


Re: [python-win32] Control Volume on Windows

2014-03-11 Thread Tim Roberts
Trevor Haba wrote:
 I'm looking for a way to use python to change the volume on windows.
 After some googling the best I could come up with was this script I
 got from a relatively ancient entry on this mailing list (I think it
 was from 2005 or something). I'm using windows 7, and I would like it
 to work on windows 8 if possible, though its not necessary. I don't
 need to support any previous versions of windows.

Alas, the news is not good.

The simple and easy-to-use winmm APIs are no longer supported.  They
still work in many cases, but the mapping from the winmm device
numbering to the new audio API device numbering is mysterious, as you
have learned, and there is no good recipe to find them that works on
every system.

The blessed method of adjusting the volume in the Vista-and-beyond
audio world requires the use of COM.  You fetch an IMMDeviceEnumerator
interface, use that to fetch an IMMDevice for your device, then fetch an
IAudioEndpoint interface and call SetMasterVolumeLevel to adjust the
volume.  That's not really very much code in C++ terms, BUT none of
those interfaces have late-binding support, which means Python cannot
use them.

So, your only real choice is to write a Python extension in C++. 
Fortunately, using boost/python.hpp, that's not as hard as it used to be.

-- 
Tim Roberts, t...@probo.com
Providenza  Boekelheide, Inc.

___
python-win32 mailing list
python-win32@python.org
https://mail.python.org/mailman/listinfo/python-win32


[python-win32] win32api on Windows 8

2014-03-11 Thread Werner

Hi,

I sent this to pywin32-b...@lists.sourceforge.net a while ago but it 
doesn't show up in the archive, so I try here.


I just started working with Windows 8.1 and run into problems with above.

2.7.6 (default, Nov 10 2013, 19:24:18) [MSC v.1500 32 bit (Intel)]
Python Type help, copyright, credits or license for more 
information.

import win32api
Traceback (most recent call last):
  File string, line 1, in fragment
ImportError: DLL load failed: %1 is not a valid Win32 application.

I tried both pywin32-218.win32-py2.7.exe and 
pywin32-216.win32-py2.7.exe.


I had no problems with pywin32 on Windows 7.

Appreciate any tip on how to fix this.

Werner
___
python-win32 mailing list
python-win32@python.org
https://mail.python.org/mailman/listinfo/python-win32


Re: [python-win32] win32api on Windows 8

2014-03-11 Thread Vernon D. Cole
I updated the Python version on my Windows 8.1 box to match yours exactly,
and here is what I see:

Microsoft Windows [Version 6.3.9600]
 (c) 2013 Microsoft Corporation. All rights reserved.

 C:\Users\vernonpy
 Python 2.7.6 (default, Nov 10 2013, 19:24:18) [MSC v.1500 32 bit (Intel)]
 on win32
 Type help, copyright, credits or license for more information.
  import win32api
  win32api.GetVersion()
 602931718
 

 The difference I see is that my Python says on win32 but yours does not,
so there is some difference in the installations.
I used the Python x86 msi installer, UAC on, installed for all users from
an administrator account, taking all defaults.
I am running on an administrator account.

How might your installation be different?
--
Vernon Cole

On Tue, Mar 11, 2014 at 6:17 PM, Werner werner...@gmx.ch wrote:

 Hi,

 I sent this to pywin32-b...@lists.sourceforge.net a while ago but it
 doesn't show up in the archive, so I try here.

 I just started working with Windows 8.1 and run into problems with above.

 2.7.6 (default, Nov 10 2013, 19:24:18) [MSC v.1500 32 bit (Intel)]
 Python Type help, copyright, credits or license for more
 information.
 import win32api
 Traceback (most recent call last):
   File string, line 1, in fragment
 ImportError: DLL load failed: %1 is not a valid Win32 application.

 I tried both pywin32-218.win32-py2.7.exe and
 pywin32-216.win32-py2.7.exe.

 I had no problems with pywin32 on Windows 7.

 Appreciate any tip on how to fix this.

 Werner
 ___
 python-win32 mailing list
 python-win32@python.org
 https://mail.python.org/mailman/listinfo/python-win32

___
python-win32 mailing list
python-win32@python.org
https://mail.python.org/mailman/listinfo/python-win32


Re: [python-win32] Control Volume on Windows [SEC=UNCLASSIFIED]

2014-03-11 Thread Andrew MacIntyre
Trevor Haba wrote:
 I'm looking for a way to use python to change the volume on windows.
 After some googling the best I could come up with was this script I 
 got from a relatively ancient entry on this mailing list (I think it 
 was from 2005 or something). I'm using windows 7, and I would like it 

 to work on windows 8 if possible, though its not necessary. I don't 
 need to support any previous versions of windows.

To which Tim Roberts replied:
 Alas, the news is not good.
 
 The simple and easy-to-use winmm APIs are no longer supported.  They
 still work in many cases, but the mapping from the winmm device
 numbering to the new audio API device numbering is mysterious, as you
 have learned, and there is no good recipe to find them that works on
 every system.
 
 The blessed method of adjusting the volume in the Vista-and-beyond
 audio world requires the use of COM.  You fetch an IMMDeviceEnumerator
 interface, use that to fetch an IMMDevice for your device, then fetch an
 IAudioEndpoint interface and call SetMasterVolumeLevel to adjust the
 volume.  That's not really very much code in C++ terms, BUT none of those
 interfaces have late-binding support, which means Python cannot use them.

I think that comtypes (https://pypi.python.org/pypi/comtypes) can deal with 
early binding interfaces?

- These thoughts are mine alone! -
Andrew MacIntyre   Operations and Services Branch
tel:   +61 2 6219 5356 Communications Infrastructure Division
fax:   +61 2 6219 5347 Australian Communications  Media Authority
email: andrew.macint...@acma.gov.auhttp://www.acma.gov.au/


NOTICE: This email message is for the sole use of the intended recipient(s) 
 and may contain confidential and privileged information. Any unauthorized 
 review, use, disclosure or distribution is prohibited. If you are not the 
 intended recipient, please contact the sender by reply email and destroy all 
 copies of the original message.
___
python-win32 mailing list
python-win32@python.org
https://mail.python.org/mailman/listinfo/python-win32