|
|
(2 intermediate revisions by the same user not shown) |
Line 27: |
Line 27: |
| </source> | | </source> |
|
| |
|
| == A command-line level analyzer == | | == Other Examples == |
| | |
| [[Media:Level.zip]]
| |
| | |
| <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 ==
| |
| | |
| [[Media:Spectrum.zip]]
| |
| | |
| <source lang="python">
| |
| import pygst
| |
| pygst.require("0.10")
| |
| import gst, gobject
| |
| import time, thread
| |
|
| |
|
| class Spectrum:
| | * [[Level.py | Level analyzer]] |
| | | * [[Spectrum.py | Spectral analysis]] |
| 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()
| |
| </source>
| |
| | |
| == Other Examples ==
| |
| * [[ListeningAndWatching]] | | * [[ListeningAndWatching]] |