Implemented GPX + Audio recording
authorEnrico Zini <enrico@enricozini.org>
Mon, 15 Jun 2009 15:49:10 +0000 (23:49 +0800)
committerEnrico Zini <enrico@enricozini.org>
Mon, 15 Jun 2009 15:49:10 +0000 (23:49 +0800)
plugins/55_gpx_trace.py
zavai/__init__.py
zavai/audio.py

index 28b00f8ab8010294a93dd4e79fd46514942ffc50..d85f41de52708c79a5fbf54531231f6039cf4c9c 100644 (file)
@@ -25,7 +25,7 @@ class GPXTracer(gtk.ToggleAction):
 
     def __init__(self, registry, **kw):
         self.state = 0
-        super(GPXTracer, self).__init__("menu.main.gps.toggle", self.states[self.state], None, None)
+        super(GPXTracer, self).__init__("menu.main.gps.gpx", self.states[self.state], None, None)
 
         self.registry = registry
         self.set_active(False)
@@ -61,7 +61,49 @@ class GPXWaypoint(gtk.Action):
     def on_activate(self, *args):
         self.gpx.waypoint()
 
+class GPXAudioTracer(gtk.ToggleAction):
+    states = [_("Start GPX and audio trace"), _("Stop GPX and audio trace")]
+
+    def __init__(self, registry, **kw):
+        self.state = 0
+        super(GPXAudioTracer, self).__init__("menu.main.gps.gpxaudio", self.states[self.state], None, None)
+
+        self.registry = registry
+        self.recorder = zavai.Recorder(registry)
+        self.set_active(False)
+
+        self.connect("toggled", self.on_toggle)
+
+    def shutdown(self):
+        self.recorder.stop()
+        super(GPXAudioTracer, self).shutdown()
+
+    def on_toggle(self, *args):
+        self.state = (self.state + 1) % len(self.states)
+        self.set_property("label", self.states[self.state])
+        if self.get_active():
+            self.start()
+        else:
+            self.stop()
+
+    def start(self):
+        zavai.info("GPX trace started")
+        self.registry.resource("gpx").connect("gpx", self)
+        self.gpx.add_activity_monitor(self.on_gpx_activity_changed)
+
+    def stop(self):
+        zavai.info("GPX trace ended")
+        self.registry.resource("gpx").disconnect("gpx", self)
+        self.recorder.stop()
+        self.gpx.del_activity_monitor(self.on_gpx_activity_changed)
+
+    def on_gpx_activity_changed(self, gpx, state):
+        if state:
+            self.recorder.start(gpx.basename + ".wav")
+        else:
+            self.recorder.stop()
 
 def init(conf = None, registry = None, **kw):
     registry.register(GPXTracer(registry))
+    registry.register(GPXAudioTracer(registry))
     registry.register(GPXWaypoint(registry))
index 6c4c82b6cf52d7919bd2eb3fbe875d228e6bcf59..147c5819f555d047ab08a023700ada9498b8cbea 100644 (file)
@@ -23,7 +23,7 @@ from registry import Registry, Resource, Service, get_parent, default_label
 from menu import Menu, MenuButton, LinkButton, ToggleButton
 from app import Zavai, Applet
 from gps import GPS, GPX
-from audio import Audio
+from audio import Audio, Recorder
 
 def warn(*args):
     import sys
index 37b483f72724c7b75df4101ad56dbcb7b40ed61a..b7345d6db1b0fd146842e9cac90ad5768f4d36a7 100755 (executable)
@@ -29,6 +29,31 @@ import gobject
 #import dbus.mainloop.glib
 #import subprocess
 
+class Recorder(zavai.Resource):
+    def __init__(self, registry):
+        super(Recorder, self).__init__()
+        self.registry = registry
+
+    def shutdown(self):
+        self.stop()
+
+    def start(self, filename):
+        if self.recorder is not None: return
+
+        self.registry.resource("audio").connect("audio", self)
+
+        self.recorder = subprocess.Popen(
+            ["arecord", "-D", "hw", "-f", "cd", "-r", "8000", "-t", "wav", filename])
+
+    def stop(self):
+        if self.recorder is None: return
+
+        os.kill(self.recorder.pid, signal.SIGINT)
+        self.recorder.wait()
+        self.recorder = None
+
+        self.registry.resource("audio").disconnect("audio", self)
+
 
 # TODO:
 #  - hook into the headset plugged/unplugged event
@@ -84,10 +109,6 @@ class Audio(zavai.Service):
         self.input_fd.close()
         self.input_fd = None
 
-#        if self.recorder is not None:
-#            os.kill(self.recorder.pid, signal.SIGINT)
-#            self.recorder.wait()
-
         # Restore mixer settings
         self.load_scenario(self.saved_scenario)
 
@@ -145,12 +166,6 @@ class Audio(zavai.Service):
 #    def set_basename(self, basename):
 #        self.basename = basename
 #
-#    def start_recording(self):
-#        if self.basename is None:
-#            raise RuntimeError("Recording requested but basename not set")
-#        self.recorder = subprocess.Popen(
-#            ["arecord", "-D", "hw", "-f", "cd", "-r", "8000", "-t", "wav", self.basename + ".wav"])
-#
 #    def start_levels(self):
 #        self.recorder = subprocess.Popen(
 #            ["arecord", "-D", "hw", "-f", "cd", "-r", "8000", "-t", "wav", "-V", "stereo", "/dev/null"])