/* * audio - audio resource for zavai * * Copyright (C) 2009--2010 Enrico Zini * * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation; either version 2 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 General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program; if not, write to the Free Software * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA */ using GLib; namespace zavai { namespace audio { public class Audio: zavai.Service { /* protected dynamic DBus.Object audiodev; protected dynamic DBus.Object vibdev; */ public Audio() { Object(name: "audio"); /* audiodev = zavai.registry.sbus.get_object( "org.freesmartphone.odeviced", "/org/freesmartphone/Device/Audio", "org.freesmartphone.Device.Audio"); vibdev = zavai.registry.sbus.get_object( "org.freesmartphone.odeviced", "/org/freesmartphone/Device/LED/neo1973_vibrator", "org.freesmartphone.Device.LED"); */ clock.alarm_trigger_queue.triggered += on_alarm_trigger; clock.alarm_trigger_queue.acked += on_alarm_done; clock.alarm_trigger_queue.canceled += on_alarm_done; } public void on_alarm_trigger(clock.AlarmTriggerInfo info) { zavai.log.debug("Make noise for alarm"); if (zavai.led.vibrator != null) { var state = new zavai.led.LedState("alarm"); state.set_blink(255); zavai.led.vibrator.push_state(state); } soundplayer.play(config.ringtone_alarm, true); } public void on_alarm_done(clock.AlarmTriggerInfo info) { zavai.log.debug("Stop noise for alarm"); if (zavai.led.vibrator != null) zavai.led.vibrator.pop_state("alarm"); soundplayer.stop(); } } public class Player: zavai.Resource, Object { protected Gst.Element player; protected bool playing; protected Player slave; protected Player master; protected bool loop; protected string uri; public signal void state_changed(Gst.State new_state); public Player() { slave = null; master = null; player = Gst.ElementFactory.make("playbin", null); playing = false; loop = false; var bus = player.get_bus(); bus.add_signal_watch(); bus.message += on_message; } public void set_slave(Player player) { slave = player; slave.master = this; } public void play(string uri, bool loop = false) { stderr.printf("Playing %s\n", uri); this.uri = uri; if (slave != null && slave.playing) slave.pause(); player.set_property("uri", uri); player.set_state(master != null && master.playing ? Gst.State.PAUSED : Gst.State.PLAYING); playing = true; this.loop = loop; } public Gst.State get_state() { Gst.State state; Gst.State pending; player.get_state(out state, out pending, (Gst.ClockType)Gst.CLOCK_TIME_NONE); return state; } public void pause() { player.set_state(Gst.State.PAUSED); state_changed(Gst.State.PAUSED); } public void resume() { player.set_state(Gst.State.PLAYING); state_changed(Gst.State.PLAYING); } public void restart() { player.seek_simple(Gst.Format.TIME, Gst.SeekFlags.FLUSH | Gst.SeekFlags.KEY_UNIT, 0); player.set_state(Gst.State.PLAYING); state_changed(Gst.State.PLAYING); } public void stop() { playing = false; player.set_state(Gst.State.NULL); state_changed(Gst.State.NULL); // Resume slave after we are done if (slave != null && slave.playing) slave.resume(); } protected void on_message(Gst.Message message) { if (message.type == Gst.MessageType.EOS) { if (loop) restart(); else stop(); } } public void shutdown() { stop(); } } public Audio audio = null; public Player musicplayer = null; public Player soundplayer = null; public void init() { audio = new Audio(); musicplayer = new Player(); soundplayer = new Player(); soundplayer.set_slave(musicplayer); zavai.registry.register(musicplayer); zavai.registry.register(soundplayer); } } }