#!/usr/bin/env python # This is the MIT license: # http://www.opensource.org/licenses/mit-license.php # Copyright (c) 2009 Digital Achievement Incorporated and contributors. # Permission is hereby granted, free of charge, to any person obtaining # a copy of this software and associated documentation files (the # "Software"), to deal in the Software without restriction, including # without limitation the rights to use, copy, modify, merge, publish, # distribute, sublicense, and/or sell copies of the Software, and to # permit persons to whom the Software is furnished to do so, subject to # the following conditions: # The above copyright notice and this permission notice shall be # included in all copies or substantial portions of the Software. # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, # EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF # MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND # NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE # LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION # OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION # WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. # """ @summary: This file contains the data for SoundRecorder on Nokia N900 using Python and gstreamer. The code is based on the source by Digital Achievement and is available from: http://achievewith.us/public/articles/2009/01/28/using-gstreamer-and-python-to-record-audio @author: Janne Parkkila (the modified Object Oriented Version) @email: japskua@gmail.com @requires: gstreamer0.10-alsa, gstreamer0.10-flac, gstreamer0.10-plugins-good-extra """ from optparse import OptionParser import dbus import gst import sys import time class SoundRecorder(object): """ @summary: This is the SoundRecorder class that handles locating the microphone and capturing voice from the mic. Currently uses somewhat "dirty" method of calling gstreamer via command line, but works pretty well nonetheless :-D """ mic = None def __init__(self): """ @summary: The Constructor class, that searches for the microphone on initialization """ bus = dbus.SystemBus() hal_manager = bus.get_object("org.freedesktop.Hal", "/org/freedesktop/Hal/Manager") hal_manager = dbus.Interface(hal_manager, "org.freedesktop.Hal.Manager") print hal_manager devices = hal_manager.FindDeviceStringMatch("alsa.type", "capture") identifiers = [] for dev in devices: device = bus.get_object("org.freedesktop.Hal", dev) card = device.GetAllProperties(dbus_interface="org.freedesktop.Hal.Device") if card["alsa.card"] not in identifiers: print "%d. %s" % (card["alsa.card"], card["alsa.card_id"]) identifiers.append(card["alsa.card"]) self.mic = identifiers[0] def Record(self, destination): """ @summary: Records the audio with the microphone and saves to the given destination @param destination: The destination where to save the captured voice @type destination: String """ pipeline = gst.parse_launch("""alsasrc device=hw:%d ! audioconvert ! level name=recordlevel interval=10000000 ! audioconvert ! flacenc ! filesink location=%s""" % (self.mic, destination)) pipeline.set_state(gst.STATE_PLAYING) print "recording, press enter to stop" sys.stdin.readline() pipeline.set_state(gst.STATE_NULL) time.sleep(5) if __name__ == "__main__": # This is used if the code is run straight from the command line recorder = SoundRecorder() recorder.Record("output.wav")