grp4 - MeHear

Esa-Petri Tirkkonen

Hilmi Abdullah

Final Report

MeHear Report

Idea

Using phone as hearing aid for old people (and not so old). Using phone's microphone and ear plugs. Basic idea is to amplify sound to make it audible for people with hearing proplems. Program will have volume control and some basic compressor and equaliser in it.

Result

We managed to do application which uses Gstreamer to stream straight from mic to ear plugs. It has also a compressor to make audio audible. Problem is just that volume or compressor is not controllable. Whole code would be needed to be rearranged to get buttons and knobs to do some thing. Propaply using gstreamers commandline version but that's other story

Screenshots

Code

#!/usr/bin/env python2.5 # # Copyright (c) 2011 # This program is free software: you can redistribute it and/or modify # it under the terms of the GNU Lesser General Public License as published by # the Free Software Foundation, either version 3 of the License, or # (at your option) any later version. # # This program is distributed in the hope that it will be useful, # but WITHOUT ANY WARRANTY; without even the implied warranty of # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the # GNU Lesser General Public License for more details. # # You should have received a copy of the GNU Lesser General Public License # along with this program. If not, see <http://www.gnu.org/licenses/>. # # ============================================================================ # Name : MeHear.py # Author : Esa-Petri and Hilmi # Version : 0.1 # Description : MeHear Maemo hearing aid program # ============================================================================ from optparse import OptionParser from time import sleep import sys, os, time import pygtk, gtk, gobject import hildon import dbus, gst, pygst Volume=100 Cratio=10 class Record(): mic = None def __init__(self, sink): """ @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] """ @attention: this is important it initializes softfare playtrought (audioconvert is not nesesity) @summary: takes stream from mic to autosink notice volume or compression ratio is not changable after program strats as they aare in constructor of main window(MeHear) """ self.pipeline = gst.parse_launch("""alsasrc device=hw:%d ! volume %d !audiodynamic ratio=%d ! autoaudiosink location=%s""" % (self.mic, Volume, Cratio, sink)) def rec(self): """ @summary: sets recording on """ self.pipeline.set_state(gst.STATE_PLAYING) print "" print "recording started" def stoprec(self): """ @summary: sets recording off """ self.pipeline.set_state(gst.STATE_NULL) print "" print "recording, stoped" class player(): def __init__(self): """ @summary: player part """ self.player = gst.element_factory_make("playbin2", "player") fakesink = gst.element_factory_make("fakesink", "fakesink") self.player.set_property("video-sink", fakesink) self.audiosink = gst.element_factory_make("autoaudiosink", "audio-output") """ @attention: intance of record class is created here and gstreamer started. """ self.recorder=Record(self.audiosink) """ @attention: next links raw data from mic to player mekanism of gstreamer """ self.player.set_property("audio-sink",self.audiosink) self.playmode = True bus = self.player.get_bus() bus.add_signal_watch() bus.connect("message", self.on_message) def on_message(self, bus, message): t = message.type if t == gst.MESSAGE_EOS: self.player.set_state(gst.STATE_NULL) self.playmode = False elif t == gst.MESSAGE_ERROR: self.player.set_state(gst.STATE_NULL) err, debug = message.parse_error() print "Error: %s" % err, debug self.playmode = False def play(self): self.recorder.rec() self.player.set_state(gst.STATE_PLAYING) print "playing" def stop(self): #self.playmode = False self.player.set_state(gst.STATE_NULL) self.recorder.stoprec() print "stoped" class MeHear(hildon.Program): def __init__(self): """ @summary: creates an intance of pleyer class and that why starts also gsteamer """ self.pl= player() def addvol_button_clicked(self, button, label, sm): #this changes the value of adjustment and thats why changes also progress bars value as it is linked to adjesment a=self.adjus.get_value() self.adjus.set_value(a+10) volume=a+10 print "volume %d"%(a+10) def decvol_button_clicked(self, button, label, sm): #this changes the value of adjustment and thats why changes also progress bars value as it is linked to adjesment a=self.adjus.get_value() self.adjus.set_value(a-10) volume=a-10 print "volume %d"%(a-10) def amplify_button_clicked(self, button, label): buttontext = button.get_label() text = buttontext if text == "AMPlify" : label.set_text("") button.set_label("Stop") self.pl.play() print "play button pressed" if text == "Stop" : label.set_text("Stopped") button.set_label("AMPlify") self.pl.stop() print "stop button pressed" def main(self): # place where fin info about pygtk http://www.pygtk.org/pygtk2tutorial/ win = hildon.StackableWindow() # Create and pack labels vbox = gtk.VBox(False, 10) hbox3 = gtk.HBox(False, 10) hbox2 = gtk.HBox(False, 10) hbox = gtk.HBox(False, 10) #interface.. buttons, labels ,... etc labelHeader = gtk.Label("MeHear") labelSubHeader = gtk.Label("voice amplifier") self.adjus = gtk.Adjustment(value=60, lower=0, upper=100, step_incr=1, page_incr=0, page_size=0) # Create the ProgressBar self.pbar = gtk.ProgressBar(adjustment=self.adjus) self. pbar.set_fraction(0.99) self.addvol = gtk.Button("+") self.addvol.connect("clicked", self.addvol_button_clicked, labelSubHeader,0.20) self.decvol = gtk.Button("-") self.decvol.connect("clicked", self.decvol_button_clicked, labelSubHeader,0.11) buttonGTK = gtk.ToggleButton("AMPlify") buttonGTK.connect("clicked", self.amplify_button_clicked, labelSubHeader) self.ratiotext= gtk.Label("Ratio:") #http://www.pygtk.org/pygtk2tutorial/sec-ComboWidget.html combo = gtk.Combo() combo.entry.set_text("Compresion") slist = [ "1", "3", "5", "10","15","20" ] combo.set_popdown_strings(slist) hbox.pack_start(buttonGTK, True, True, 0) vbox.pack_start(labelHeader, True, True, 0) vbox.pack_start(labelSubHeader, True, True, 0) self.volumetext= gtk.Label("Volume:") hbox3.pack_start(self.ratiotext, True, True, 0) hbox3.pack_start(combo, True, True, 0) hbox2.pack_start(self.decvol, True, True, 0) hbox2.pack_start(self.pbar, True, True, 0) hbox2.pack_start(self.addvol, True, True, 0) vbox.pack_start(hbox3, True, True, 0) vbox.pack_start(self.volumetext,True,False,0) vbox.pack_start(hbox2, True, True, 0) vbox.pack_start(hbox, True, True, 0) # Add label's box to window win.add(vbox) win.connect("delete_event", gtk.main_quit) win.show_all() gtk.main() if __name__ == "__main__": app = MeHear() app.main() #EOF

Cleaned code whit some examples mehear.zip

Presentation

MeHear

Conclusion

Hilmi:Developing in Maemo environment was challenging, we faced some problems during the night but at the end it works. Moreover CodeCamp is a nice environment for learning.

Esa-Petri: Coding to maemo was nightmare as libraries aren't well ported like Gstreamer Pygtk and so on. Also software restrictions make coding awful. But learning to use gstreamer and Pygtk was good experience. I just would not want to have maemo ever. Trip to Riuttasaari was a really good experience and should be made as tradition. Overall good experience