2 * audio - audio resource for zavai
4 * Copyright (C) 2009--2010 Enrico Zini <enrico@enricozini.org>
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
26 public class Audio: zavai.Service
29 protected dynamic DBus.Object audiodev;
30 protected dynamic DBus.Object vibdev;
35 Object(name: "audio");
38 audiodev = zavai.registry.sbus.get_object(
39 "org.freesmartphone.odeviced",
40 "/org/freesmartphone/Device/Audio",
41 "org.freesmartphone.Device.Audio");
42 vibdev = zavai.registry.sbus.get_object(
43 "org.freesmartphone.odeviced",
44 "/org/freesmartphone/Device/LED/neo1973_vibrator",
45 "org.freesmartphone.Device.LED");
47 clock.alarm_trigger_queue.triggered += on_alarm_trigger;
48 clock.alarm_trigger_queue.acked += on_alarm_done;
49 clock.alarm_trigger_queue.canceled += on_alarm_done;
52 public void on_alarm_trigger(clock.AlarmTriggerInfo info)
54 zavai.log.debug("Make noise for alarm");
55 if (zavai.led.vibrator != null)
57 var state = new zavai.led.LedState("alarm");
59 zavai.led.vibrator.push_state(state);
61 soundplayer.play(config.ringtone_alarm, true);
64 public void on_alarm_done(clock.AlarmTriggerInfo info)
66 zavai.log.debug("Stop noise for alarm");
67 if (zavai.led.vibrator != null)
68 zavai.led.vibrator.pop_state("alarm");
73 public class Player: zavai.Resource, Object
75 protected Gst.Element player;
76 protected bool playing;
77 protected Player slave;
78 protected Player master;
81 public signal void state_changed(Gst.State new_state);
87 player = Gst.ElementFactory.make("playbin", null);
90 var bus = player.get_bus();
91 bus.add_signal_watch();
92 bus.message += on_message;
95 public void set_slave(Player player)
101 public void play(string uri, bool loop = false)
103 stderr.printf("Playing %s\n", uri);
106 if (slave != null && slave.playing)
109 player.set_property("uri", uri);
110 player.set_state(master != null && master.playing ? Gst.State.PAUSED : Gst.State.PLAYING);
115 public Gst.State get_state()
120 player.get_state(out state, out pending, (Gst.ClockType)Gst.CLOCK_TIME_NONE);
127 player.set_state(Gst.State.PAUSED);
128 state_changed(Gst.State.PAUSED);
133 player.set_state(Gst.State.PLAYING);
134 state_changed(Gst.State.PLAYING);
137 public void restart()
139 player.seek_simple(Gst.Format.TIME, Gst.SeekFlags.FLUSH | Gst.SeekFlags.KEY_UNIT, 0);
140 player.set_state(Gst.State.PLAYING);
141 state_changed(Gst.State.PLAYING);
147 player.set_state(Gst.State.NULL);
148 state_changed(Gst.State.NULL);
150 // Resume slave after we are done
151 if (slave != null && slave.playing)
155 protected void on_message(Gst.Message message)
157 if (message.type == Gst.MessageType.EOS)
166 public void shutdown()
172 public Audio audio = null;
173 public Player musicplayer = null;
174 public Player soundplayer = null;
179 musicplayer = new Player();
180 soundplayer = new Player();
181 soundplayer.set_slave(musicplayer);
182 zavai.registry.register(musicplayer);
183 zavai.registry.register(soundplayer);