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
28 protected Omhacks.Led vibrator;
29 protected bool has_vibrator;
32 protected dynamic DBus.Object audiodev;
33 protected dynamic DBus.Object vibdev;
38 Object(name: "audio");
40 has_vibrator = (vibrator.init("neo1973:vibrator") == 0);
43 audiodev = zavai.registry.sbus.get_object(
44 "org.freesmartphone.odeviced",
45 "/org/freesmartphone/Device/Audio",
46 "org.freesmartphone.Device.Audio");
47 vibdev = zavai.registry.sbus.get_object(
48 "org.freesmartphone.odeviced",
49 "/org/freesmartphone/Device/LED/neo1973_vibrator",
50 "org.freesmartphone.Device.LED");
52 clock.alarm_trigger_queue.triggered += on_alarm_trigger;
53 clock.alarm_trigger_queue.acked += on_alarm_done;
54 clock.alarm_trigger_queue.canceled += on_alarm_done;
57 public void on_alarm_trigger(clock.AlarmTriggerInfo info)
59 zavai.log.debug("Make noise for alarm");
62 vibrator.brightness = 256;
63 // FIXME: is there a better way? I hope there is a better way. Please
64 // tell me there is a better way.
66 for (int i = 0; ; ++i)
68 vibrator.trigger[i] = (char)trig[i];
69 if (trig[i] == 0) break;
71 vibrator.delay_on = 200;
72 vibrator.delay_off = 300;
75 soundplayer.play(config.ringtone_alarm, true);
78 public void on_alarm_done(clock.AlarmTriggerInfo info)
80 zavai.log.debug("Stop noise for alarm");
84 for (int i = 0; ; ++i)
86 vibrator.trigger[i] = (char)trig[i];
87 if (trig[i] == 0) break;
89 vibrator.brightness = 0;
96 public void notify_alarm(zavai.clock.Alarm a)
98 // Wiggle screen to turn on backlight
99 zavai.ui.power.backlight.wiggle();
101 // Method does not exist in this frameworkd
102 vibdev.BlinkSeconds(5, 500, 200);
104 zavai.log.error("Cannot blink vibrator: " + e.message);
111 public class Player: zavai.Resource, Object
113 protected Gst.Element player;
114 protected bool playing;
115 protected Player slave;
116 protected Player master;
118 protected string uri;
119 public signal void state_changed(Gst.State new_state);
125 player = Gst.ElementFactory.make("playbin", null);
128 var bus = player.get_bus();
129 bus.add_signal_watch();
130 bus.message += on_message;
133 public void set_slave(Player player)
139 public void play(string uri, bool loop = false)
141 stderr.printf("Playing %s\n", uri);
144 if (slave != null && slave.playing)
147 player.set_property("uri", uri);
148 player.set_state(master != null && master.playing ? Gst.State.PAUSED : Gst.State.PLAYING);
153 public Gst.State get_state()
158 player.get_state(out state, out pending, (Gst.ClockType)Gst.CLOCK_TIME_NONE);
165 player.set_state(Gst.State.PAUSED);
166 state_changed(Gst.State.PAUSED);
171 player.set_state(Gst.State.PLAYING);
172 state_changed(Gst.State.PLAYING);
175 public void restart()
177 player.seek_simple(Gst.Format.TIME, Gst.SeekFlags.FLUSH | Gst.SeekFlags.KEY_UNIT, 0);
178 player.set_state(Gst.State.PLAYING);
179 state_changed(Gst.State.PLAYING);
185 player.set_state(Gst.State.NULL);
186 state_changed(Gst.State.NULL);
188 // Resume slave after we are done
189 if (slave != null && slave.playing)
193 protected void on_message(Gst.Message message)
195 if (message.type == Gst.MessageType.EOS)
204 public void shutdown()
210 public Audio audio = null;
211 public Player musicplayer = null;
212 public Player soundplayer = null;
217 musicplayer = new Player();
218 soundplayer = new Player();
219 soundplayer.set_slave(musicplayer);
220 zavai.registry.register(musicplayer);
221 zavai.registry.register(soundplayer);