Here are some pedals for controlling the mpd (music player daemon) for easier transcribing of lectures. Play the audio with any mpd client, then run this to use pedals for pause and rewind-2-secs.


Uses pyserial and py-libmpdclient.

#!/usr/local/bin/python

import serial,time,mpdclient

class RetryingMpdController:
    def __init__(self,**kw):
        self.mpd_kw = kw
        self.reconnect()

    def reconnect(self):
        self.mpd = mpdclient.MpdController(**self.mpd_kw)
        print "connecting to mpd"

    def __getattr__(self,attr):
        def retrying_method(*args):
            func = getattr(self.mpd,attr)
            try:
                func(*args)
            except mpdclient.MpdError:
                self.reconnect()
                func(*args)
        return retrying_method

class Pedals:
    """
    connect RTS to DSR and CTS like so (pin numbers are for 9-pin
    serial port):

    pin 7 RTS -->-----------+
                            |
                left   /    |
    pin 6 DSR <-------/ o---+ 
                            |
                right  /    |
    pin 8 CTS <-------/ o---+
    """
    def __init__(self,*actions):
        self.ser = serial.Serial(port="/dev/ttyS0",rtscts=1)
	self.ser.setRTS(1)
        self.prev = False,False
        self.actions = actions

    def poll(self):
        pedals = self.ser.getDSR(),self.ser.getCTS()
        for which in range(2):
            if pedals[which] and not self.prev[which]:
                self.actions[which]()
        self.prev = pedals

class Playback:
    def __init__(self):
        self.mpd = RetryingMpdController(host='localhost',port=6600)

    def rewind(self,secs=2):
        pos,length,frac = self.mpd.getSongPosition()
        self.mpd.seek(seconds=pos-secs)

    def pause(self):
        self.mpd.pause()

play = Playback()

ped = Pedals(play.rewind, play.pause)

print "monitoring pedals (press ctrl-c to exit)..."
while 1:
    ped.poll()
    time.sleep(.01)

a>