Valhalla Legends Forums Archive | General Programming | [Python] Winamp controller class

AuthorMessageTime
Topaz
My first Python program! Small stuff, but I see bigger things on the horizon ;)

[code]
__version__ = '1.0.0'
__author__ = 'topaz'
__copyright__ = 'BSD (Berkeley Software Distribution) License'

import win32api
import win32gui
import string
import os

WM_COMMAND = 0x400
WM_MSG = 273

class winamp:

    def __init__(me):
        #grabs handle at class initialization for further use
        me.handle = win32gui.FindWindow('Winamp v1.x', None)

    def execute():
        #function for executing winamp if not already open
        os.startfile('C:\Program Files\Winamp\winamp.exe')

    def play(me):
        win32api.SendMessage(me.handle, WM_MSG, 40045, 0)

    def setvol(me, volume):
        #Converts 1-100 scale to 1-255, as read by Winamp
        volume = volume * 2.55
        print volume

        win32api.SendMessage(me.handle, WM_COMMAND, volume, 122)
       
    def next(me):
        win32api.SendMessage(me.handle, WM_MSG, 40048, 0)

    def previous(me):
        win32api.SendMessage(me.handle, WM_MSG, 40044, 0)

    def close(me):
        win32api.SendMessage(me.handle, WM_MSG, 40001, 0)

    def stop(me):
        win32api.SendMessage(me.handle, WM_MSG, 40047, 0)

    def pause(me):
        #Note: this function is both for pausing and unpausing
        win32api.SendMessage(me.handle, WM_MSG, 40046, 0)

    def song(me):
        raw = win32gui.GetWindowText(me.handle)
        song = string.replace(raw, ' - Winamp', '')

        return song
       
    def bitrate(me):
        bitrate = win32api.SendMessage(me.handle, WM_COMMAND, 1, 126)
       
        return bitrate
   
    def playstatus(me):
        playstatus = win32api.SendMessage(me.handle, WM_COMMAND, 0, 104)
           
        if playstatus == 0:
            return 'Winamp is stopped.'
        elif playstatus == 1:
            return 'Winamp is playing.'
        elif playstatus == 3:
            return 'Winamp is paused.'
        else:
            return 'Unrecognized Winamp status.'
[/code]

usage:

[quote]import winamp
winamp = winamp.winamp()
winamp.play()
[/quote]

Thanks to Banana fanna fo fanna for his help in learning Python.
May 20, 2006, 12:39 AM
St0rm.iD
A little bit of advice:

- Naming conventions: CamelCase class names, lowercase_underscored method, function, and variable names
- Properties: it might be a little bit nicer to say winamp.volume = 100 rather than winamp.setvol(100). A simple example of properties should serve you nicely:

[code]
class TestProperties(object): # must extend object, new-style classes
    def get_prop(self):
        return "get_prop()"
    def set_prop(self, value):
        print "setting",value
    prop = property(get_prop, set_prop)
[/code]

Also, the convention is to use "self" instead of "me", but it doesn't really matter.
May 20, 2006, 1:29 PM

Search