home *** CD-ROM | disk | FTP | other *** search
/ PC Welt 2006 November (DVD) / PCWELT_11_2006.ISO / casper / filesystem.squashfs / usr / share / doc / python-gst0.10 / examples / vumeter.py < prev   
Encoding:
Python Source  |  2006-07-31  |  3.4 KB  |  109 lines

  1. #!/usr/bin/env python
  2. # -*- Mode: Python -*-
  3. # vi:si:et:sw=4:sts=4:ts=4
  4.  
  5. # gst-python
  6. # Copyright (C) 2005 Andy Wingo <wingo@pobox.com>
  7. #
  8. # This library is free software; you can redistribute it and/or
  9. # modify it under the terms of the GNU Library General Public
  10. # License as published by the Free Software Foundation; either
  11. # version 2 of the License, or (at your option) any later version.
  12. #
  13. # This library is distributed in the hope that it will be useful,
  14. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  16. # Library General Public License for more details.
  17. #
  18. # You should have received a copy of the GNU Library General Public
  19. # License along with this library; if not, write to the
  20. # Free Software Foundation, Inc., 59 Temple Place - Suite 330,
  21. # Boston, MA 02111-1307, USA.
  22.  
  23.  
  24. # A test more of gst-plugins than of gst-python.
  25.  
  26.  
  27. import pygtk
  28. pygtk.require('2.0')
  29. import gtk
  30. import gobject
  31.  
  32. import pygst
  33. pygst.require('0.10')
  34. import gst
  35.  
  36. import fvumeter
  37.  
  38.  
  39. def clamp(x, min, max):
  40.     if x < min:
  41.         return min
  42.     elif x > max:
  43.         return max
  44.     return x
  45.  
  46.  
  47. class Window(gtk.Dialog):
  48.     def __init__(self):
  49.         gtk.Dialog.__init__(self, 'Volume Level')
  50.         self.prepare_ui()
  51.  
  52.     def prepare_ui(self):
  53.         self.set_default_size(200,60)
  54.         self.set_title('Volume Level')
  55.         self.connect('delete-event', lambda *x: gtk.main_quit())
  56.         self.vus = []
  57.         self.vus.append(fvumeter.FVUMeter())
  58.         self.vus.append(fvumeter.FVUMeter())
  59.         self.vbox.add(self.vus[0])
  60.         self.vbox.add(self.vus[1])
  61.         self.vus[0].show()
  62.         self.vus[1].show()
  63.  
  64.     def error(self, message, secondary=None):
  65.         m = gtk.MessageDialog(self,
  66.                               gtk.DIALOG_MODAL | gtk.DIALOG_DESTROY_WITH_PARENT,
  67.                               gtk.MESSAGE_ERROR,
  68.                               gtk.BUTTONS_OK,
  69.                               message)
  70.         if secondary:
  71.             m.format_secondary_text(secondary)
  72.         m.run()
  73.                               
  74.     def on_message(self, bus, message):
  75.         if  message.structure.get_name() == 'level':
  76.             s = message.structure
  77.             for i in range(0, len(s['peak'])):
  78.                 self.vus[i].freeze_notify()
  79.                 decay = clamp(s['decay'][i], -90.0, 0.0)
  80.                 peak = clamp(s['peak'][i], -90.0, 0.0)
  81.                 if peak > decay:
  82.                     print "ERROR: peak bigger than decay!"
  83.             
  84.                 self.vus[i].set_property('decay', decay)
  85.                 self.vus[i].set_property('peak', peak)
  86.         return True
  87.  
  88.     def run(self):
  89.         try:
  90.             self.set_sensitive(False)
  91.             s = 'alsasrc ! level message=true ! fakesink'
  92.             pipeline = gst.parse_launch(s)
  93.             self.set_sensitive(True)
  94.             pipeline.get_bus().add_signal_watch()
  95.             i = pipeline.get_bus().connect('message::element', self.on_message)
  96.             pipeline.set_state(gst.STATE_PLAYING)
  97.             gtk.Dialog.run(self)
  98.             pipeline.get_bus().disconnect(i)
  99.             pipeline.get_bus().remove_signal_watch()
  100.             pipeline.set_state(gst.STATE_NULL)
  101.         except gobject.GError, e:
  102.             self.set_sensitive(True)
  103.             self.error('Could not create pipeline', e.__str__)
  104.         
  105. if __name__ == '__main__':
  106.     w = Window()
  107.     w.show_all()
  108.     w.run()
  109.