posts:gstreamer:beep: refactor to create the Beep class.
authorW. Trevor King <wking@tremily.us>
Fri, 25 Nov 2011 12:52:18 +0000 (07:52 -0500)
committerW. Trevor King <wking@tremily.us>
Fri, 14 Sep 2012 16:29:15 +0000 (12:29 -0400)
posts/GStreamer/beep.py [changed mode: 0644->0755]

old mode 100644 (file)
new mode 100755 (executable)
index 172c31c..5260239
@@ -1,24 +1,42 @@
-#!/usr/bin/python
+#!/usr/bin/env python
+
+"""Simple GStreamer tone generator.
+"""
 
 import pygst
-pygst.require("0.10")
+pygst.require('0.10')
 import gst
 import pygtk
 import gtk
 
-class Main:
-       def __init__(self):
-               self.pipeline = gst.Pipeline("mypipeline")
+class Beep:
+    def __init__(self, frequency=440):
+        self.pipeline = gst.Pipeline('mypipeline')
+
+        self.audiotestsrc = gst.element_factory_make('audiotestsrc', 'audio')
+       self.audiotestsrc.set_property('freq', frequency)
+        self.pipeline.add(self.audiotestsrc)
+
+        self.sink = gst.element_factory_make('alsasink', 'sink')
+        self.pipeline.add(self.sink)
+
+        self.audiotestsrc.link(self.sink)
+
+        self.pipeline.set_state(gst.STATE_PLAYING)
+
 
-               self.audiotestsrc = gst.element_factory_make("audiotestsrc", "audio")
-               self.pipeline.add(self.audiotestsrc)
+if __name__ == '__main__':
+    import argparse
 
-               self.sink = gst.element_factory_make("alsasink", "sink")
-               self.pipeline.add(self.sink)
+    parser = argparse.ArgumentParser(description='Play a pure tone')
+    parser.add_argument(
+        '-f', '--frequency', type=float, help='tone frequency in Hz')
 
-               self.audiotestsrc.link(self.sink)
+    args = parser.parse_args()
 
-               self.pipeline.set_state(gst.STATE_PLAYING)
+    frequency = 440
+    if args.frequency:
+        frequency = args.frequency
 
-start=Main()
-gtk.main()
+    start=Beep(frequency=frequency)
+    gtk.main()