#! /usr/bin/python

###############################################
# "Phrozen's PyWav player" v.0.1a
#  by PhrozenSmoke@yahoo.com
#  February 2002
#  A BASIC, lightweight wav player, ideal
#  for quick previewing of wav files from a 
#  file manager. It's really a 
#  frontend to the playwav program.
#
#  License: Open Source, free for non-commercial use
#  Please leave these credits intact.
#
#  This program and it's source may be freely 
#  modified and redistributed for non-commercial, 
#  non-profit use only.
#
#  This software comes with NO WARRANTY 
#  whatsoever!
#
#  This program requires Python and TCL.
#  Tested only on: Mandrake 8.1, Kernel 2.4.8, 
#  Glibc 2.2, Python 2.2, TCL 8.3.4 
#  and the Linux  wavplay utility version 1.4
###############################################

import os
from Tkinter import *
import tkMessageBox
import sys
import tkFileDialog

class WavPlayer(Frame):
  def __init__(self, *args, **params):
    apply(Frame.__init__, (self,) + args, params)
    self._parent = None
    if len(args) != 0: self._parent = args[0]
    self.wav_file=""
    self.pid=3987
    self.addWidgets()
    self.killed=1

  def addWidgets(self):
    self._widgets = {}
    self.bgcolor="#DEE2EE"
    self["bg"]=self.bgcolor
    self._widgets['title'] = Label(self,background=self.bgcolor,text="PyWav: "+self.wav_file,font='-*-MS Sans Serif-Bold-R-Normal-*-*-140-*-*-*-*-*-*')
    self._widgets['title'].grid(column=1, row=1,sticky='new',pady=5)

    self._widgets['buttframe'] = Frame(self,background=self.bgcolor)
    self._widgets['buttframe'].grid(column=1, row=2,sticky='new',pady=5)

    self._widgets['play'] = Button(self._widgets['buttframe'],background="#12A62D",foreground="#FFFFFF",text="PLAY",command=self.playwav)
    self._widgets['play'].grid(column=1, row=1,sticky='new',pady=5)

    self._widgets['stop'] = Button(self._widgets['buttframe'],background="#A25843",foreground="#FFFFFF",text="STOP",command=self.stopwav)
    self._widgets['stop'].grid(column=2, row=1,sticky='new',pady=5)

    self._widgets['about'] = Button(self._widgets['buttframe'],background=self.bgcolor,text="About",command=self.about)
    self._widgets['about'].grid(column=3, row=1,sticky='new',pady=5,padx=12)

    self.bind("<Destroy>",self.stopwav)

    #Menus
    self.men=Menu(self,background="#BDC6CD")
    self.dropdown=Menu(self.men,background="#BDC6CD")
    self.dropdown.add_command(label="Open...",command=self.doOpen,background="#BDC6CD")
    self.dropdown.add_command(label="Exit",command=self.doClose,background="#BDC6CD")
    self.men.add_cascade(label="File", menu=self.dropdown,background="#BDC6CD")
    self._parent.config(menu=self.men)




  def playwav(self):
    if self.wav_file:
      try:
        self.stopwav()
      except:
        pass
      try:

        ## CHANGE THE Line below if you do want the 
        ## the wav  to loop more times or if you don't want it to loop at all

        LOOP_COUNT=5      # must be set to at least 1
          
        l=[]
        if LOOP_COUNT < 1:
          LOOP_COUNT=1    # if LOOP_COUNT is less than 1, nothing will play!

        zz=0
        while zz < (LOOP_COUNT+1):
           l.append(self.wav_file)
           zz=zz+1


        l=[self.wav_file,self.wav_file,self.wav_file]
        self.pid=os.spawnvp(os.P_NOWAIT,"wavplay",l)
        self.killed=0
      except:
        tkMessageBox.showinfo("PyWav","Error playing the selected wav file!")
    else:
      self.doOpen()

  def stopwav(self,xarg=None):
    try:
      if self.killed: 
        return
      f=os.popen("kill "+str(self.pid))
      self.killed=1
      f.close()
    except:
      pass

  def about(self):
    tkMessageBox.showinfo("About PyWav","PyWav v.0.1a\nA BASIC, lightweight frontend to the Linux wavplay utility.\nWritten with Python/Tkinter.\nIdeal for quick previewing of wav files from a file manager.\nWritten: February 2002\nBy: PhrozenSmoke@yahoo.com\nLicense: Open souce, free for non-commercial use.\nThis software comes with NO WARRANTY whatsoever.") 
 
  def doClose(self,xargs=None):
    self.stopwav()
    sys.exit(0)

  def doOpen(self,xargs=None):
    self.setWavFile(tkFileDialog.askopenfilename())
    self.playwav()

  def setWavFile(self,wavf):
    self.wav_file=str(wavf).strip()
    m=str(wavf).strip()
    if m.find("/") > -1:
      m=m[m.rfind("/")+1:len(m)]
    self._widgets['title']["text"]=" PyWav:  \""+m+"\" "


def openWindow(wavf,doopen=1):
  root=Tk()
  m=WavPlayer(root)  
  m.pack(fill=BOTH,expand=1)
  if doopen:
    m.setWavFile(wavf)
    m.playwav()
  root.title("PyWav")
  root["bg"]="#DEE2EE"
  root.mainloop()

if __name__ == "__main__":
  if not len(sys.argv) == 2:
    openWindow("",0)
  else:
    wav=str(sys.argv[1])
    openWindow(wav)