| 1 |
ninoborges |
8 |
###########################################################
|
| 2 |
|
|
## winamp.py
|
| 3 |
|
|
## Python Winamp Controller
|
| 4 |
|
|
##
|
| 5 |
|
|
## Version 1.0, 1-Feb-2001
|
| 6 |
|
|
##
|
| 7 |
|
|
## About:
|
| 8 |
|
|
## Class to control running instance
|
| 9 |
|
|
## or Winamp mp3 player on current
|
| 10 |
|
|
## machine.
|
| 11 |
|
|
##
|
| 12 |
|
|
## Requirements:
|
| 13 |
|
|
## Winamp and Win32 (of course).
|
| 14 |
|
|
## ActivePython or Win32 Python extensions.
|
| 15 |
|
|
## Tested with Winamp 2.71 and ActivePython 2.0
|
| 16 |
|
|
## on WinNT4.
|
| 17 |
|
|
##
|
| 18 |
|
|
## Usage:
|
| 19 |
|
|
## Copy this file anywhere in your
|
| 20 |
|
|
## pythonpath.
|
| 21 |
|
|
## Create an instance of winamp class
|
| 22 |
|
|
## and call methods on it to control
|
| 23 |
|
|
## a running winamp program on current
|
| 24 |
|
|
## machine.
|
| 25 |
|
|
##
|
| 26 |
|
|
## Example:
|
| 27 |
|
|
## >>> import winamp
|
| 28 |
|
|
## >>> w = winamp.winamp()
|
| 29 |
|
|
## >>> w.getPlayingStatus()
|
| 30 |
|
|
## 'stopped'
|
| 31 |
|
|
## >>> w.play()
|
| 32 |
|
|
## >>> w.getPlayingStatus()
|
| 33 |
|
|
## 'playing'
|
| 34 |
|
|
## >>>
|
| 35 |
|
|
##
|
| 36 |
|
|
## Uses the winamp api http://www.winamp.com
|
| 37 |
|
|
## /nsdn/winamp2x/dev/sdk/api.jhtml
|
| 38 |
|
|
## and windows messaging to control winamp.
|
| 39 |
|
|
##
|
| 40 |
|
|
##
|
| 41 |
|
|
## Copyright (c) 2001, Shalabh Chaturvedi
|
| 42 |
|
|
##
|
| 43 |
|
|
## Permission is hereby granted, free of charge, to any
|
| 44 |
|
|
## person obtaining a copy of this software and associated
|
| 45 |
|
|
## documentation files (the "Software"), to deal in the
|
| 46 |
|
|
## Software without restriction, including without
|
| 47 |
|
|
## limitation the rights to use, copy, modify, merge,
|
| 48 |
|
|
## publish, distribute, sublicense, and/or sell copies of
|
| 49 |
|
|
## the Software, and to permit persons to whom the Software
|
| 50 |
|
|
## is furnished to do so, subject to the following
|
| 51 |
|
|
## conditions:
|
| 52 |
|
|
##
|
| 53 |
|
|
## The above copyright notice and this permission notice
|
| 54 |
|
|
## shall be included in all copies or substantial portions
|
| 55 |
|
|
## of the Software.
|
| 56 |
|
|
##
|
| 57 |
|
|
## THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY
|
| 58 |
|
|
## KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO
|
| 59 |
|
|
## THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A
|
| 60 |
|
|
## PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
|
| 61 |
|
|
## THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
|
| 62 |
|
|
## DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF
|
| 63 |
|
|
## CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
|
| 64 |
|
|
## CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
|
| 65 |
|
|
## IN THE SOFTWARE.
|
| 66 |
|
|
##
|
| 67 |
|
|
###########################################################
|
| 68 |
|
|
|
| 69 |
|
|
import win32gui
|
| 70 |
|
|
import win32api
|
| 71 |
|
|
|
| 72 |
|
|
# wonder why win32 imports dont define these
|
| 73 |
|
|
WM_COMMAND = 0x0111
|
| 74 |
|
|
WM_USER = 0x400
|
| 75 |
|
|
|
| 76 |
|
|
def voidfunc():
|
| 77 |
|
|
pass
|
| 78 |
|
|
|
| 79 |
|
|
class winamp:
|
| 80 |
|
|
|
| 81 |
|
|
winamp_commands = { 'prev' :40044,
|
| 82 |
|
|
'next' :40048,
|
| 83 |
|
|
'play' :40045,
|
| 84 |
|
|
'pause' :40046,
|
| 85 |
|
|
'stop' :40047,
|
| 86 |
|
|
'fadeout' :40157,
|
| 87 |
|
|
'forward' :40148,
|
| 88 |
|
|
'rewind' :40144,
|
| 89 |
|
|
'raisevol':40058,
|
| 90 |
|
|
'lowervol':40059}
|
| 91 |
|
|
|
| 92 |
|
|
def __init__(self):
|
| 93 |
|
|
#self.hWinamp = win32gui.FindWindow('Winamp v3.x', None)
|
| 94 |
|
|
self.hWinamp = win32gui.FindWindow('Studio', None)
|
| 95 |
|
|
|
| 96 |
|
|
iVersionNumber = self.usercommand(0)
|
| 97 |
|
|
sVersionString = hex(iVersionNumber)
|
| 98 |
|
|
sVersionString = sVersionString[2:3] + '.' + sVersionString[3:]
|
| 99 |
|
|
self.sVersion = sVersionString
|
| 100 |
|
|
|
| 101 |
|
|
def command(self, sCommand):
|
| 102 |
|
|
if winamp.winamp_commands.has_key(sCommand):
|
| 103 |
|
|
return win32api.SendMessage(self.hWinamp, WM_COMMAND, winamp.winamp_commands[sCommand], 0)
|
| 104 |
|
|
else:
|
| 105 |
|
|
raise 'NoSuchWinampCommand'
|
| 106 |
|
|
|
| 107 |
|
|
def __getattr__(self, attr):
|
| 108 |
|
|
self.command(attr)
|
| 109 |
|
|
return voidfunc
|
| 110 |
|
|
|
| 111 |
|
|
def usercommand(self, id, data=0):
|
| 112 |
|
|
return win32api.SendMessage(self.hWinamp, WM_USER, data, id)
|
| 113 |
|
|
|
| 114 |
|
|
def getVersion(self):
|
| 115 |
|
|
"returns the version number of winamp"
|
| 116 |
|
|
return self.sVersion
|
| 117 |
|
|
|
| 118 |
|
|
def getPlayingStatus(self):
|
| 119 |
|
|
"returns the current status string which is one of 'playing', 'paused' or 'stopped'"
|
| 120 |
|
|
iStatus = self.usercommand(104)
|
| 121 |
|
|
if iStatus == 1:
|
| 122 |
|
|
return 'playing'
|
| 123 |
|
|
elif iStatus == 3:
|
| 124 |
|
|
return 'paused'
|
| 125 |
|
|
else:
|
| 126 |
|
|
return 'stopped'
|
| 127 |
|
|
|
| 128 |
|
|
def getTrackStatus(self):
|
| 129 |
|
|
"returns a tuple (total_length, current_position) where both are in msecs"
|
| 130 |
|
|
iTotalLength = self.usercommand(105, 1) * 1000 # the usercommand returns the number in seconds
|
| 131 |
|
|
iCurrentPos = self.usercommand(105, 0)
|
| 132 |
|
|
return (iTotalLength, iCurrentPos)
|
| 133 |
|
|
|
| 134 |
|
|
def setCurrentTrack(self, iTrackNumber):
|
| 135 |
|
|
"changes the track selection to the number specified"
|
| 136 |
|
|
return self.usercommand(121, iTrackNumber)
|
| 137 |
|
|
|
| 138 |
|
|
def getCurrentTrack(self):
|
| 139 |
|
|
return self.usercommand(125)
|
| 140 |
|
|
|
| 141 |
|
|
def getCurrentTrackName(self):
|
| 142 |
|
|
return win32gui.GetWindowText(self.hWinamp)
|
| 143 |
|
|
|
| 144 |
|
|
def seekWithinTrack(self, iPositionMsecs):
|
| 145 |
|
|
"seeks within currently playing track to specified milliseconds since start"
|
| 146 |
|
|
return self.usercommand(106, iPositionMsecs)
|
| 147 |
|
|
|
| 148 |
|
|
def setVolume(self, iVolumeLevel):
|
| 149 |
|
|
"sets the volume to number specified (range is 0 to 255)"
|
| 150 |
|
|
return self.usercommand(122, iVolumeLevel)
|
| 151 |
|
|
|
| 152 |
|
|
def getNumTracks(self):
|
| 153 |
|
|
"returns number of tracks in current playlist"
|
| 154 |
|
|
return self.usercommand(124)
|
| 155 |
|
|
|
| 156 |
|
|
def getTrackInfo(self):
|
| 157 |
|
|
"returns a tuple (samplerate, bitrate, number of channels)"
|
| 158 |
|
|
iSampleRate = self.usercommand(126,0)
|
| 159 |
|
|
iBitRate = self.usercommand(126,1)
|
| 160 |
|
|
iNumChannels = self.usercommand(126,2)
|
| 161 |
|
|
return (iSampleRate, iBitrate, iNumChannels)
|
| 162 |
|
|
|
| 163 |
|
|
def dumpList(self):
|
| 164 |
|
|
"dumps the current playlist into WINAMPDIR/winamp.m3u"
|
| 165 |
|
|
return self.usercommand(120)
|
| 166 |
|
|
|
| 167 |
|
|
|
| 168 |
|
|
def getTrackList(sPlaylistFilepath):
|
| 169 |
|
|
playlistfile = open(sPlaylistFilepath, "r")
|
| 170 |
|
|
lines = playlistfile.readlines()
|
| 171 |
|
|
playlistfile.close()
|
| 172 |
|
|
playlist = []
|
| 173 |
|
|
for line in lines:
|
| 174 |
|
|
if not line[0]=='#':
|
| 175 |
|
|
playlist.append(line[:-1])
|
| 176 |
|
|
return playlist
|
| 177 |
|
|
|
| 178 |
|
|
|