daily pastebin goal
92%
SHARE
TWEET

Untitled

a guest Apr 16th, 2018 50 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #!/usr/bin/env python
  2. import pygst
  3. pygst.require('0.10')
  4. import gst, sys
  5. import gobject
  6. import errno
  7. from struct import pack, unpack
  8. gobject.threads_init ()
  9.  
  10. class SomaAudioEventFilter(gst.Element):
  11.  
  12.     _sinkpadtemplate = gst.PadTemplate ("sinkpadtemplate",
  13.                                         gst.PAD_SINK,
  14.                                         gst.PAD_ALWAYS,
  15.                                         gst.caps_new_any())
  16.  
  17.     _srcpadtemplate = gst.PadTemplate ("srcpadtemplate",
  18.                                         gst.PAD_SRC,
  19.                                         gst.PAD_ALWAYS,
  20.                                         gst.caps_new_any())
  21.  
  22.     def __init__(self):
  23.         gst.Element.__init__(self)
  24.  
  25.         gst.info('creating srcpadpad')
  26.         self.srcpad = gst.Pad(self._srcpadtemplate, "src")
  27.         gst.info('adding srcpad to self')
  28.         self.add_pad(self.srcpad)
  29.         self.srcpad.set_getcaps_function(self.src_getcaps_function)
  30.         self.srcpad.set_setcaps_function(self.src_setcaps_function)
  31.  
  32.         gst.info('creating sinkpad')
  33.         self.sinkpad = gst.Pad(self._sinkpadtemplate, "sink")
  34.         gst.info('adding sinkpad to self')
  35.         self.add_pad(self.sinkpad)
  36.         self.sinkpad.set_getcaps_function(self.sink_getcaps_function)
  37.         self.sinkpad.set_setcaps_function(self.sink_setcaps_function)
  38.  
  39.         gst.info('setting chain/event functions')
  40.         self.sinkpad.set_chain_function(self.chainfunc)
  41.         self.sinkpad.set_event_function(self.eventfunc)
  42.         self.srccaps =  gst.caps_from_string('audio/x-raw-int,rate=8000,channels=1,endianness=1234,width=16,depth=16,signed=true')
  43.         self.audioCMD = 0x30;
  44.         self.lastvalue = 0
  45.        
  46.     def set_audio_src(self, newSrc):
  47.         self.audioSRC = newSrc
  48.  
  49.     def src_getcaps_function(self, *foo):
  50.         print "src getcaps"
  51.         gst.info('src getcaps')
  52.         return self.srccaps
  53.  
  54.     def src_setcaps_function(self, pad, caps):
  55.         print "src setcaps"
  56.         gst.info("attempting to src  setcaps:%s" % caps.to_string())
  57.         self.srccaps = caps
  58.         return True
  59.    
  60.     def sink_getcaps_function(self, *foo):
  61.  
  62.         gst.info('sink getcaps')
  63.         capsString = "soma/event,src=%s"  % (self.audioSRC)
  64.  
  65.         caps = gst.caps_from_string(capsString)
  66.         return caps
  67.    
  68.     def sink_setcaps_function(self, *foo):
  69.         print "SomaAudioFilter setcaps"
  70.         gst.info('sink setcaps')
  71.         return True
  72.        
  73.     def chainfunc(self, pad, buffer):
  74.         #self.info("%s timestamp(buffer):%d" % (pad, buffer.timestamp))
  75.         buffer_out = ""
  76.         nEvents = len(buffer)/12;  
  77.         #gst.info("somaEventFilter.chainfunc length of Buffer IN:" +  str(len(buffer)) + ' with nEvents:' + str(nEvents))
  78.        
  79. ##         if len(buffer) % 12 != 0:
  80. ##             gst.warn( "Error: nEvents%12 !=0")
  81. ##             return ""
  82.  
  83.         for i in range(nEvents):
  84.             buffer_out = buffer_out + (self.parseEvent( buffer[ (i*12) : (i*12)+12 ] ))
  85.        
  86.         buf = gst.Buffer(buffer_out)
  87.         caps = self.srccaps
  88.         buf.set_caps(caps)
  89.         #buf.stamp(buffer)
  90.         #gst.info("somaEventFilter.chainfunc() returning buffer of len: " + str(len(buffer_out)))
  91.  
  92.         return  self.srcpad.push(buf)
  93.  
  94.     def eventfunc(self, pad, event):
  95.         print "FILTER ELEMENT GOT EVENT", event.type
  96.         self.info("%s event:%r" % (pad, event.type))
  97.    
  98.         return self.srcpad.push_event(event)
  99.    
  100.     def parseEvent(self, buffer):
  101. ##         if len(buffer)<12:
  102. ##             gst.info("ParsingEvents, buffer len <12")
  103. ##             return ''
  104.  
  105.         CMD = 'B'
  106.         SRC = 'B'
  107.         DATA_IN = '>h'
  108.         DATA_OUT = '<h'
  109.  
  110.         data = ''
  111.  
  112.         cmd = unpack(CMD, buffer[0:1])[0]
  113. ##         if cmd != self.audioCMD:
  114. ##             gst.info("Parsing Events, CMD IS NOT :" +  str(self.audioCMD) + '  CMD IS:' +  str(cmd))
  115. ##             return ""
  116.  
  117.  
  118.         buffer_out = buffer[4:]
  119.         #print unpack(">hhhh", buffer[4:])
  120.        
  121. ##      buffer_out = ""        
  122. ##         for i in range(4):
  123. ##             buffer_out += pack(">h",  self.lastvalue)
  124.  
  125. ##             if self.lastvalue == 2**15 -1:
  126. ##                 self.lastvalue = -2**15
  127. ##             else:
  128. ##                  self.lastvalue += 1
  129.                  
  130.         return buffer_out
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