[Tutor] Interactive control with keys

Tim Ronning tim.ronning at start.no
Tue Nov 4 14:51:35 EST 2003


Greetings all

I continue my mp3 player learning project. I'm at a stage where I would 
like to add interactive controll like "stop", "next" and "previous" 
assigned to keys "S", "N" and "P" I have no clue on how to implement this 
except that I think it have to be placed or called from somewere within the 
play function. It would be great if someone could give me a hint on how to 
start. Not the solution but some hints to get me digging.

I include the full source as is right now.

Best regards
Tim R

#!/usr/bin/env python
############################################
##               DSPmP                    ##
##    Dead.Simple.Python.mp3.Player       ##
##                                        ##
##    Author: Tim Ronning                 ##
##            tim.ronning at pasecurity.org  ##
##    License: GPL                        ##
##    Platform: POSIX                     ##
##    Version: 0.0.3                      ##
##    Date: 03.11.03                      ##
##    Req. libraries:                     ##
##           - libao                      ##
##           - libmad                     ##
##    Req. wrappers:                      ##
##           - pyao                       ##
##           - pymad                      ##
##                                        ##
############################################

import sys, ao, mad, os

# Global configuration #
global_aod = "alsa09" # valid ; alsa05 / alsa09 / oss
global_plist = "playlist" # your choice for playlist file

os.system("clear")

def makeaList(s):
        anothermp3 = s.split()
        return anothermp3
    def append(mp3):
    if choice == "3":
        app = open(global_plist,"a")
        app.write(mp3 + "\n")
        app.close()
    else:
        app = open("playlist.new","a")
        app.write(mp3 + "\n")
        app.close

def play(t):                    # plays a file and returns key data about 
mp3
    mf = mad.MadFile(t)
    os.system("clear")
    if mf.layer() == mad.LAYER_I:
        print "MPEG Layer I"
    elif mf.layer() == mad.LAYER_II:
        print "MPEG Layer II"
    elif mf.layer() == mad.LAYER_III:
        print "MPEG Layer III"
    else:
        print "unexpected layer value"
    if mf.mode() == mad.MODE_SINGLE_CHANNEL:
        print "single channel"
    elif mf.mode() == mad.MODE_DUAL_CHANNEL:
        print "dual channel"
    elif mf.mode() == mad.MODE_JOINT_STEREO:
        print "joint (MS/intensity) stereo"
    elif mf.mode() == mad.MODE_STEREO:
        print "normal L/R stereo"
    else:
        print "unexpected mode value"
    if mf.emphasis() == mad.EMPHASIS_NONE:
        print "no emphasis"
    elif mf.emphasis() == mad.EMPHASIS_50_15_US:
        print "50/15us emphasis"
    elif mf.emphasis() == mad.EMPHASIS_CCITT_J_17:
        print "CCITT J.17 emphasis"
    else:
        print "unexpected emphasis value"
    print "bitrate %lu bps" % mf.bitrate()
    print "samplerate %d Hz" % mf.samplerate()
    millis = mf.total_time()
    secs = millis / 1000
    print "total time %d ms (%dm%2ds)" % (millis, secs / 60, secs % 60)
    print "Now playing: %s" % (t)
    dev = ao.AudioDevice(global_aod, rate=mf.samplerate())
    while 1:
        buf = mf.read()
        if buf is None:
            break
        dev.play(buf, len(buf))

choice = '1'

while choice != '6':
    print """
    ************************
    *         DSPmP        *
    ************************
    * 1)....Play List      *
    * 2)....Play Directory *
    * 3)....Show List      *
    * 4)....Add MP3's      *
    * 5)....Remove MP3's   *
    * 6)....Quit           *
    ************************
    ************************
    """
    choice = raw_input("Choice: ")
    if choice == '1':
        playlist = open(global_plist,"r")
        mp3list = []
        for line in playlist.readlines():
            mp3list = mp3list + makeaList(line)
        playlist.close()
        items = len(mp3list)
        j=0
        while j < items:
            tune = mp3list[j]
            play(tune)          # calls function play with arg. tune
            j = j + 1

    elif choice == '2':
        dirpath = raw_input("Directory: ")
        raw_list = os.listdir(dirpath)      # makes a list of an entire 
directory
        os.chdir(dirpath)
        items = len(raw_list)
        j=0
        while j < items:
            tune = raw_list[j]
            play(tune)
            j = j + 1

    elif choice == '3':
        os.system("clear")
        playlist = open(global_plist,"r")
        for line in playlist.readlines():
            print line
        playlist.close()

    elif choice == '4':
        newmp3 = raw_input("New mp3: ")
        append(newmp3)
        os.system("clear")

    elif choice == '5':
        playlist = open(global_plist,"r")
        mp3list = []
        for line in playlist.readlines():
            mp3list = mp3list + makeaList(line)
        playlist.close()
        delnr = int(raw_input("Which number? "))
        del mp3list[delnr]
        listlen = len(mp3list)
        n = 0
        while n < listlen:
            append(mp3list[n])
            n = n + 1
        os.system("mv -f playlist.new playlist")
        os.system("clear")

    else:
        sys.exit()


-- 
Using M2, Opera's revolutionary e-mail client: http://www.opera.com/m2/



More information about the Tutor mailing list