PythonGStreamer: Difference between revisions

From XPUB & Lens-Based wiki
No edit summary
Line 29: Line 29:
== A command-line level analyzer ==
== A command-line level analyzer ==


[[Media:Level.zip]]
[[Level.py]]
 
<source lang="python">
import pygst
pygst.require("0.10")
import gst, gobject
import time, thread
 
class Level:
 
def __init__(self):
self.min = None
self.max = None
self.slider = 0.0
 
# Create a gstreamer pipeline, listen to it's messages via on_message callback
listener_desc = 'alsasrc ! level ! fakesink'
self.listener = gst.parse_launch(listener_desc)
bus = self.listener.get_bus()
bus.add_signal_watch()
bus.connect("message", self.on_message)
 
def on_message (self, bus, message):
""" callback function for gstreamer messages """
# print "message.name", s.get_name()
s = message.structure
# print "message.structure.keys", s.keys()
if s and s.get_name() == "level":
# The Level module seems to provide the following data: 'endtime', 'timestamp', 'stream-time', 'running-time', 'duration', 'rms', 'peak', 'decay'
# In this case we will just look at the "rms" value (representing relative loudness of the signal)
# It's a stereo (two-channel) value, so we just take the first one (left?)
loudness = s['rms'][0]
 
# update min and max
if (self.min==None or loudness<self.min):
self.min = loudness
if (self.max==None or loudness>self.max):
self.max = loudness
# calculate "slider" (a 0 - 1 value, from min to max)
if (self.min != self.max):
self.slider = float(loudness - self.min) / (self.max - self.min)
# if self.slider < 0.25: self.slider = 0
print self.slider
 
return True
 
def start(self):
self.listener.set_state(gst.STATE_PLAYING)
while True:
time.sleep(1)
 
 
# Run the Level object in it's own thread, then start the gobject.MainLoop as required
level = Level()
thread.start_new_thread(level.start, ())
gobject.threads_init()
loop = gobject.MainLoop()
loop.run()
</source>


== A command-line spectrum analyzer ==
== A command-line spectrum analyzer ==

Revision as of 11:36, 1 June 2009

Using Python to program GStreamer

Generating random tones

A python random oscillator stream:

import pygst
pygst.require("0.10")
import gst

pipeline = gst.Pipeline("mypipeline")
audiotestsrc = gst.element_factory_make("audiotestsrc", "audio")
pipeline.add(audiotestsrc)
sink = gst.element_factory_make("alsasink", "sink")
pipeline.add(sink)
audiotestsrc.link(sink)
audiotestsrc.set_property("freq", 800)
pipeline.set_state(gst.STATE_PLAYING)

import time, random

while 1:
	f = random.randint(200, 800)
	print "setting freq to: %d" % f 
	audiotestsrc.set_property("freq", f)
	time.sleep(1.0/random.randint(1, 10))

A command-line level analyzer

Level.py

A command-line spectrum analyzer

Media:Spectrum.zip

import pygst
pygst.require("0.10")
import gst, gobject
import time, thread

class Spectrum:

	def __init__(self):
		# Create a gstreamer pipeline, listen to it's messages via on_message callback
		# listener_desc = 'alsasrc ! spectrum ! fakesink'
		listener_desc = 'alsasrc ! spectrum bands=64 ! fakesink'
		self.listener = gst.parse_launch(listener_desc)
		bus = self.listener.get_bus()
		bus.add_signal_watch()
		bus.connect("message", self.on_message)

	def on_message (self, bus, message):
		""" callback function for gstreamer messages """
		s = message.structure
		# print "message.name", s.get_name()
		# print "message.structure.keys", s.keys()
		if s and s.get_name() == "spectrum":
			# spectrum messages have 'endtime', 'timestamp', 'stream-time', 'running-time', 'duration', 'magnitude'
			# print len(s['magnitude'])
			print "data: " + " ".join([str(x) for x in s['magnitude']])
		return True

	def start(self):
		self.listener.set_state(gst.STATE_PLAYING)
		while True:
			time.sleep(1)


# Run the Spectrum object in it's own thread, then start the gobject.MainLoop as required
spectrum = Spectrum()
thread.start_new_thread(spectrum.start, ())
gobject.threads_init()
loop = gobject.MainLoop()
loop.run()

Other Examples