#! /usr/bin/python

from liblo import *

import pcop, pydcop

import os, threading

class ArdourHelper( Server ):

    def run( self ):
        while True:
            self.recv()

    # Handlers
    @make_method( '/session/loaded', 'ss' )
    def sessionLoaded( self, oscPath, args ):
        ( path, name ) = args[:2]

        rose = pydcop.anyAppCalled( "rosegarden" )
        rose.RosegardenIface.openFile( '%s/%s.rg' % ( path, name ) )

    @make_method( '/session/exported', 'ss' )
    def sessionExported( self, oscPath, args ):
        ( path, title ) = args[:2]

        artist = album = ''

        # Call lame
        print 'Encoding %s' % title
        self.mp3Encode( path, artist, album, title )

    def mp3Encode( self, path, artist='', album='', title='' ):
        filename = os.path.dirname( path )
        if artist:
            filename += '%s - ' % artist
        filename = '%s/%s.mp3' % ( filename, title )

        print 'lame --ta "%s" --tl "%s" --tt "%s" "%s" "%s"' % ( artist, album, title, path, filename )
        
        print os.system( 'lame --ta "%s" --tl "%s" --tt "%s" "%s" "%s"' % ( artist, album, title, path, filename ) )

#############################

helper = ArdourHelper( 7770 )
helper.run()
