daily pastebin goal
25%
SHARE
TWEET

Untitled

a guest Dec 28th, 2014 200 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import Tkinter
  2. import Tkconstants
  3. import tkFileDialog
  4. import tkMessageBox
  5. import Nsound as ns
  6.  
  7.  
  8. class ResampleGUI(Tkinter.Frame):
  9.  
  10.     def __init__(self, root):
  11.         Tkinter.Frame.__init__(self, root)
  12.  
  13.         stepOne = Tkinter.LabelFrame(self, text=" WAV Resample: ")
  14.         stepOne.grid(row=0, columnspan=7, sticky='W', padx=5, pady=5, ipadx=5, ipady=5)
  15.  
  16.         inFileLbl = Tkinter.Label(stepOne, text="Input File Path:")
  17.         inFileLbl.grid(row=0, column=0, sticky='E', padx=5, pady=2)
  18.  
  19.         self.inFileTxt = Tkinter.Entry(stepOne)
  20.         self.inFileTxt.grid(row=0, column=1, columnspan=7, sticky="WE", pady=3)
  21.  
  22.  
  23.         inFileBtn = Tkinter.Button(stepOne, text="Open Wav file ... ", command=self.askopenfilename)
  24.         inFileBtn.grid(row=0, column=8, sticky='W', padx=5, pady=2)
  25.  
  26.         outFileLbl = Tkinter.Label(stepOne, text="Output File Path:")
  27.         outFileLbl.grid(row=1, column=0, sticky='E', padx=5, pady=2)
  28.  
  29.         self.outFileTxt = Tkinter.Entry(stepOne)
  30.         self.outFileTxt.grid(row=1, column=1, columnspan=7, sticky="WE", pady=2)
  31.  
  32.         outFileBtn = Tkinter.Button(stepOne, text="      Resample       ", command=self.asksaveasfilename)
  33.         outFileBtn.grid(row=1, column=8, sticky='W', padx=5, pady=2)
  34.  
  35.         inEncLbl = Tkinter.Label(stepOne, text="Input File Frequency:")
  36.         inEncLbl.grid(row=2, column=0, sticky='E', padx=5, pady=2)
  37.  
  38.         self.inFTxt = Tkinter.Entry(stepOne)
  39.         self.inFTxt.grid(row=2, column=1, sticky='E', pady=2)
  40.  
  41.         outEncLbl = Tkinter.Label(stepOne, text="Output File Frequency:")
  42.         outEncLbl.grid(row=2, column=5, padx=5, pady=2)
  43.  
  44.         self.outFTxt = Tkinter.Entry(stepOne)
  45.         self.outFTxt.grid(row=2, column=7, pady=2)
  46.  
  47.  
  48.  
  49.  
  50.  
  51.         # options for buttons
  52.         button_opt = {'fill': Tkconstants.BOTH, 'padx': 5, 'pady': 5}
  53.  
  54.         self.file_opt = options = {}
  55.  
  56.         options['defaultextension'] = '.wav'
  57.         options['filetypes'] = [('WAV', '.wav')]
  58.         options['initialdir'] = 'C:\\'
  59.         options['initialfile'] = 'sound'
  60.         options['parent'] = root
  61.         options['title'] = 'Open Wav file'
  62.  
  63.  
  64.     def askopenfilename(self):
  65.  
  66.         filename = tkFileDialog.askopenfilename(**self.file_opt)
  67.  
  68.         if filename:
  69.             self.in_field = Tkinter.StringVar()
  70.             self.in_field.set(filename)
  71.             self.inFileTxt['textvariable'] = self.in_field
  72.  
  73.             fl = str(filename.decode("utf-8"))
  74.             a1 = ns.AudioStream(fl)
  75.             source = a1.getSampleRate()
  76.  
  77.             self.inF_filed = Tkinter.StringVar()
  78.             self.inF_filed.set(str(float(source)))
  79.             self.inFTxt['textvariable'] = self.inF_filed
  80.  
  81.             return True
  82.  
  83.     def asksaveasfilename(self):
  84.  
  85.         outF = self.outFTxt.get()
  86.  
  87.         save_filename = tkFileDialog.asksaveasfilename(**self.file_opt)
  88.  
  89.         #TODO if file exis
  90.  
  91.  
  92.         target = float(outF)
  93.  
  94.         a1 = ns.AudioStream(str(self.inFileTxt.get()))
  95.         source = a1.getSampleRate()
  96.  
  97.         print "source: %d" %(source)
  98.         print "target: %d" %(target)
  99.  
  100.         ratio = target / source
  101.  
  102.         print "ratio:  %f" %(ratio)
  103.  
  104.         a2 = a1.getResample(ratio)
  105.  
  106.         a2.setSampleRate(int(target))
  107.  
  108.         a2 >> str(save_filename)
  109.  
  110.         tkMessageBox.showinfo("Success! ", "completed!")
  111.  
  112.         return True
  113.  
  114. if __name__ == '__main__':
  115.  
  116.         root = Tkinter.Tk()
  117.         root.wm_title(u"WAV Resampler (C) MKobilev")
  118.         ResampleGUI(root).pack()
  119.         root.mainloop()
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top