def on_audio(audio_data):
    if is_listening:
	# TODO identify audio segments and batch up the data into a new AudioData message
	# publish the new message using the publisher
	size = len(audio_data.data)
	buffer = gst.buffer_new_and_alloc(size)
	buffer[0:size] = audio_data.data
	src.emit("push-buffer", buffer)
예제 #2
0
    def push_gap(self, src, gap_duration_s):
        gap_start = self.next_output_timestamp / gst.SECOND
        gap_duration = gap_duration_s * gst.SECOND
        gap_samples = gap_duration_s * self.rate
        gap_end = gap_start + self.waitTime
        # FIXME
        # For a real gap buffer, you would want zero samples. (I think.)
        #gap_samples = 0
        gap_vals = np.zeros(gap_samples)
        gap_vals = gap_vals.astype(np.float32)
        buffer_len = gap_vals.nbytes
        buf = gst.buffer_new_and_alloc(buffer_len)
        buf[:buffer_len - 1] = np.getbuffer(gap_vals)
        # Again, for a real gap buffer, you'd want to set this flag.
        #buf.flag_set(gst.BUFFER_FLAG_GAP)
        buf.timestamp = self.next_output_timestamp
        buf.duration = gap_duration
        buf.offset = self.current_offset
        buf.offset_end = self.current_offset + gap_samples
        src.emit("push-buffer", buf)
        src.info("gst clock = %d" % int(src.get_clock().get_time()))
        src.info("No files! Pushed gap with start=%d, duration=%d latency=%d" %
                 (buf.timestamp / gst.SECOND, gap_duration / gst.SECOND,
                  (gpstime.GpsSecondsFromPyUTC(time.time()) - gap_end)))

        if self.logger:
            outDict = {}
            outDict['type'] = 'buffer'
            outDict['time'] = datetime.datetime.now().isoformat()
            outDict['is_gap'] = True
            outDict['gpsstart'] = buf.timestamp / gst.SECOND
            outDict['duration'] = gap_duration / gst.SECOND
            outDict['latency'] = gpstime.GpsSecondsFromPyUTC(
                time.time()) - gap_end
            self.logger.info(json.dumps(outDict))
        self.next_output_timestamp += buf.duration
        self.current_offset = buf.offset_end
        return
예제 #3
0
    def push_gap(self, src, gap_duration_s):
        gap_start = self.next_output_timestamp / gst.SECOND
        gap_duration = gap_duration_s * gst.SECOND
        gap_samples = gap_duration_s * self.rate
        gap_end = gap_start + self.waitTime
        # FIXME
        # For a real gap buffer, you would want zero samples. (I think.)
        #gap_samples = 0
        gap_vals = np.zeros(gap_samples)
        gap_vals = gap_vals.astype(np.float32)
        buffer_len = gap_vals.nbytes
        buf = gst.buffer_new_and_alloc(buffer_len)
        buf[:buffer_len-1] = np.getbuffer(gap_vals)
        # Again, for a real gap buffer, you'd want to set this flag.
        #buf.flag_set(gst.BUFFER_FLAG_GAP)
        buf.timestamp = self.next_output_timestamp
        buf.duration = gap_duration
        buf.offset = self.current_offset
        buf.offset_end = self.current_offset + gap_samples
        src.emit("push-buffer", buf)
        src.info("gst clock = %d" % int(src.get_clock().get_time()))
        src.info("No files! Pushed gap with start=%d, duration=%d latency=%d" %
            (buf.timestamp/gst.SECOND,gap_duration/gst.SECOND,(gpstime.GpsSecondsFromPyUTC(time.time()) - gap_end)))

        if self.logger:
            outDict = {}
            outDict['type'] = 'buffer'
            outDict['time'] = datetime.datetime.now().isoformat()
            outDict['is_gap'] = True
            outDict['gpsstart'] = buf.timestamp/gst.SECOND
            outDict['duration'] = gap_duration/gst.SECOND
            outDict['latency'] = gpstime.GpsSecondsFromPyUTC(time.time()) - gap_end
            self.logger.info(json.dumps(outDict))
        self.next_output_timestamp += buf.duration
        self.current_offset = buf.offset_end
        return
예제 #4
0
    def need_data(self, src, need_bytes=None):
        src.info("----------------------------------------------------")
        src.info("Received need-data signal, %s." % time.asctime())

        self.check_for_new_files(self.next_output_timestamp)
        try:
            filePath = self.fileQueue.popleft()
        except IndexError:
            if self.idq_log:
                # Check to see what is the latest stride that iDQ is working on.
                stride_start, stride_end = idq.most_recent_realtime_stride(
                    self.idq_log)

                if stride_start * gst.SECOND > self.next_output_timestamp:
                    # The iDQ process has moved on. Push a gap.
                    gap_duration_s = stride_start - self.next_output_timestamp / gst.SECOND
                    self.push_gap(src, gap_duration_s)

            # If no iDQ log file is available, or if the stride we want is still being
            # worked on by iDQ, we wait.
            time.sleep(self.waitTime)
            # Try it again.
            self.check_for_new_files(self.next_output_timestamp)
            try:
                filePath = self.fileQueue.popleft()
            except IndexError:
                # Push gap equivalent to the wait time and return.
                self.push_gap(src, self.waitTime)
                return True

        # Ah, we have a file.
        # Get the gpsstart time from the filename.
        rest = filePath[len(self.fullCurrentPrefix):]
        gpsstart = int(rest.split('-')[2])
        # Let's re-derive the duration.  maybe it changed?
        rest = rest.split('-')[3]
        duration = int(rest[:rest.find(self.inputExt)])

        # Is this file later than the one we were expecting?
        # If so, we can be sure that the one we wanted will never show up.
        # We might as well push a gap and then continue to process
        # the file that we have available.
        if gpsstart * gst.SECOND > self.next_output_timestamp:
            gap_duration_s = gpsstart - self.next_output_timestamp / gst.SECOND
            self.push_gap(src, gap_duration_s)

        # Down to business. We must read in data from the npy file in order to
        # create the buffer. First load the numpy array.
        src.info("processing %s" % filePath)
        veto_vals = wrapNpLoad(filePath)
        veto_vals = veto_vals.astype(np.float32)

        # Build the buffer.
        buffer_len = veto_vals.nbytes
        buf = gst.buffer_new_and_alloc(buffer_len)
        buf[:buffer_len - 1] = np.getbuffer(veto_vals)
        buf.timestamp = gpsstart * gst.SECOND
        # gst buffers require:
        # buffer_duration * rate / gst.SECOND = (offset_end - offset)
        # The offset is zero since our data begin at the beginning
        # of the buffer.
        buf_end = gpsstart + duration
        buf.duration = duration * gst.SECOND
        buf.offset = self.current_offset
        buf.offset_end = self.current_offset + duration * self.rate
        buf.caps = self.caps
        # Push the buffer into the stream (a side effect of
        # emitting this signal).
        src.emit("push-buffer", buf)
        # XXX FIXME Use a real gstreamer clock for latency instead? Make sure to fix it in
        # the other places too. Okay?
        #src.info("pushed buffer with start=%d, duration=%d, latency=%d" %
        #    (gpsstart,duration, (int(src.get_clock().get_time())-buf.timestamp)/gst.SECOND))
        src.info("pushed buffer with start=%d, duration=%d, latency=%d" %
                 (gpsstart, duration,
                  (gpstime.GpsSecondsFromPyUTC(time.time()) - buf_end)))
        if self.logger:
            outDict = {}
            outDict['type'] = 'buffer'
            outDict['time'] = datetime.datetime.now().isoformat()
            outDict['is_gap'] = False
            outDict['gpsstart'] = gpsstart
            outDict['duration'] = duration
            outDict['latency'] = gpstime.GpsSecondsFromPyUTC(
                time.time()) - buf_end
            self.logger.info(json.dumps(outDict))

        self.next_output_timestamp += buf.duration
        self.current_offset = buf.offset_end
        return True
예제 #5
0
    def need_data(self, src, need_bytes=None):
        src.info("----------------------------------------------------")
        src.info("Received need-data signal, %s." % time.asctime())

        self.check_for_new_files(self.next_output_timestamp)
        try:
            filePath = self.fileQueue.popleft()
        except IndexError:
            if self.idq_log:
                # Check to see what is the latest stride that iDQ is working on.
                stride_start, stride_end = idq.most_recent_realtime_stride(self.idq_log)

                if stride_start * gst.SECOND > self.next_output_timestamp:
                    # The iDQ process has moved on. Push a gap.
                    gap_duration_s = stride_start - self.next_output_timestamp / gst.SECOND
                    self.push_gap(src, gap_duration_s)
            
            # If no iDQ log file is available, or if the stride we want is still being 
            # worked on by iDQ, we wait.
            time.sleep(self.waitTime)
            # Try it again.
            self.check_for_new_files(self.next_output_timestamp)
            try:
                filePath = self.fileQueue.popleft()
            except IndexError: 
                # Push gap equivalent to the wait time and return.
                self.push_gap(src, self.waitTime)
                return True
                    
        # Ah, we have a file.
        # Get the gpsstart time from the filename.
        rest = filePath[len(self.fullCurrentPrefix):]
        gpsstart = int(rest.split('-')[2])
        # Let's re-derive the duration.  maybe it changed?
        rest = rest.split('-')[3]
        duration = int(rest[:rest.find(self.inputExt)])

        # Is this file later than the one we were expecting?  
        # If so, we can be sure that the one we wanted will never show up.
        # We might as well push a gap and then continue to process
        # the file that we have available.
        if gpsstart * gst.SECOND > self.next_output_timestamp:
            gap_duration_s = gpsstart - self.next_output_timestamp / gst.SECOND
            self.push_gap(src, gap_duration_s)

        # Down to business. We must read in data from the npy file in order to
        # create the buffer. First load the numpy array.
        src.info("processing %s" % filePath)
        veto_vals = wrapNpLoad(filePath)
        veto_vals = veto_vals.astype(np.float32)

        # Build the buffer.
        buffer_len = veto_vals.nbytes
        buf = gst.buffer_new_and_alloc(buffer_len)
        buf[:buffer_len-1] = np.getbuffer(veto_vals)
        buf.timestamp = gpsstart * gst.SECOND
        # gst buffers require:
        # buffer_duration * rate / gst.SECOND = (offset_end - offset)
        # The offset is zero since our data begin at the beginning 
        # of the buffer.
        buf_end = gpsstart + duration
        buf.duration = duration * gst.SECOND
        buf.offset = self.current_offset
        buf.offset_end = self.current_offset + duration * self.rate
        buf.caps = self.caps
        # Push the buffer into the stream (a side effect of 
        # emitting this signal).
        src.emit("push-buffer", buf)
        # XXX FIXME Use a real gstreamer clock for latency instead? Make sure to fix it in 
        # the other places too. Okay?
        #src.info("pushed buffer with start=%d, duration=%d, latency=%d" % 
        #    (gpsstart,duration, (int(src.get_clock().get_time())-buf.timestamp)/gst.SECOND))
        src.info("pushed buffer with start=%d, duration=%d, latency=%d" % 
            (gpsstart,duration,(gpstime.GpsSecondsFromPyUTC(time.time()) - buf_end)))
        if self.logger:
            outDict = {}
            outDict['type'] = 'buffer'
            outDict['time'] = datetime.datetime.now().isoformat()
            outDict['is_gap'] = False
            outDict['gpsstart'] = gpsstart
            outDict['duration'] = duration
            outDict['latency'] = gpstime.GpsSecondsFromPyUTC(time.time()) - buf_end
            self.logger.info(json.dumps(outDict))        
    
        self.next_output_timestamp += buf.duration
        self.current_offset = buf.offset_end
        return True