Advertisement
Guest User

Untitled

a guest
Jul 20th, 2017
65
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.59 KB | None | 0 0
  1. import gst
  2. import gtk
  3. import cv
  4. import rb
  5. from struct import unpack
  6. from numpy import average,mean,sqrt,array,ndarray
  7.  
  8.  
  9. class SWave(rb.Plugin):
  10.     def __init__(self):
  11.         rb.Plugin.__init__(self)
  12.         self.width,self.height = 800,30
  13.         self.winWidth = 300
  14.         self.player = None
  15.         #self.img = cv.CreateImage((self.width,self.height), cv.IPL_DEPTH_8U,1)
  16.         self.img = gtk.gdk.Pixbuf(gtk.gdk.COLORSPACE_RGB, False, 8, self.width, self.height)
  17.         self.scaledimg = None
  18.         self.gtkimage = gtk.Image()
  19.         self.clearImage()
  20.         self.fake = gst.element_factory_make("fakesink")
  21.         self.fake.set_property("signal-handoffs", True)
  22.         self.fake.connect("handoff", self.handler)
  23.         self.m=0
  24.         self.lastpos=None
  25.         self.playingEntry=None
  26.  
  27.  
  28.     def activate(self, shell):
  29.         self.b=gtk.EventBox()
  30.         self.b.set_events(gtk.gdk.BUTTON_PRESS_MASK)
  31.         #self.gtkimage.set_from_pixbuf(self.img)
  32.         self.clearImage()
  33.         self.gtkimage.set_visible(True)
  34.         self.b.add(self.gtkimage)
  35.         self.b.set_visible(True)
  36.         self.b.connect('button-press-event', self.imgPressed)
  37.         shell.add_widget(self.b,rb.SHELL_UI_LOCATION_MAIN_TOP,False,True)
  38.  
  39.         self.player = shell.get_player()
  40.         self.player.props.player.add_tee(self.fake)
  41.         self.player.connect('playing-changed',self.playchanged)
  42.  
  43.  
  44.         shell.props.window.connect('expose-event', self.resizeImage)
  45.         print "added"
  46.  
  47.     def deactivate(self, shell):
  48.         shell.get_player().props.player.remove_tee(self.fake)
  49.         shell.remove_widget(self.b,rb.SHELL_UI_LOCATION_MAIN_TOP)
  50.  
  51.     def imgPressed(self, eventbox, event):
  52.         pos = int(1e-9 * (event.x * self.duration / self.winWidth) + 0.5)
  53.         self.player.set_playing_time(pos)
  54.        
  55.  
  56.     def playchanged(self, RBSplayer, status):
  57.         if status:
  58.             if self.player.get_playing_entry() == self.playingEntry:
  59.                 return
  60.             self.duration = 1e9*self.player.get_playing_song_duration()
  61.             self.clearImage()
  62.  
  63.  
  64.     def clearImage(self):
  65.         #cv.Rectangle(self.img,(0,0),(self.width,self.height),cv.RGB(0,0,0),-1)
  66.         self.img.pixel_array[:] *= 0
  67.         self.gtkimage.set_from_pixbuf(self.img.scale_simple(self.winWidth, self.height, gtk.gdk.INTERP_BILINEAR))
  68.         self.gtkimage.queue_draw()
  69.  
  70.     def drawLine(self, time, data):
  71.         imgpos = int(self.width * time/self.duration)
  72.         if self.lastpos == imgpos:
  73.             return
  74.         self.lastpos = imgpos
  75.         avg = int(0.5+ self.height*sqrt(mean(pow(array(data),2))))
  76.         avg = max(1,avg)
  77.         #cv.Line(self.img,(imgpos, self.height/2-avg),(imgpos, self.height/2+avg), cv.RGB(255,255,255),1)
  78.         self.img.pixel_array[self.height/2-avg:self.height/2+avg, imgpos:imgpos+1] = 254
  79.         self.gtkimage.set_from_pixbuf(self.img.scale_simple(self.winWidth, self.height, gtk.gdk.INTERP_BILINEAR))
  80.         self.gtkimage.queue_draw()
  81.  
  82.     def resizeImage(self,widget, event):
  83.         if widget.allocation[2] == self.winWidth:
  84.             return
  85.         self.winWidth = widget.allocation[2]
  86.         self.gtkimage.set_from_pixbuf(self.img.scale_simple(self.winWidth, self.height, gtk.gdk.INTERP_BILINEAR))
  87.         #self.img.scale(self.scaledimg, 0, 0, self.winWidth, self.height, 0,0,self.width/self.winWidth,1, gtk.gdk.INTERP_BILINEAR)
  88.         self.gtkimage.queue_draw()
  89.  
  90.     def handler(self, fakesink, gstbuffer, pad, *args):
  91.         l = len(gstbuffer.data)/4
  92.         self.drawLine(gstbuffer.timestamp, unpack('<%if'%l, gstbuffer.data))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement