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 VibratorState
29 public int brightness;
30 public string trigger;
34 public VibratorState(string name)
40 public void set_constant(int power)
48 public void set_blink(int power, int delay_on=200, int delay_off=300)
52 this.delay_on = delay_on;
53 this.delay_off = delay_off;
56 public void to_omhacks(ref Omhacks.Led led)
58 led.brightness = brightness;
59 Memory.copy(led.trigger, trigger, trigger.size());
65 public class Vibrator : zavai.Resource, Object
67 protected Omhacks.Led vibrator;
69 protected List<VibratorState> states;
71 public Vibrator() throws FileError
73 if (vibrator.init("neo1973:vibrator") != 0)
74 throw new FileError.NOENT("vibrator not found");
76 states = new List<VibratorState>();
79 public void turn_off()
81 vibrator.brightness = 0;
82 Memory.copy(vibrator.trigger, "none", 5);
86 public void push_state(VibratorState state)
88 states.prepend(state);
89 state.to_omhacks(ref vibrator);
93 public void pop_state(string name)
96 if (states == null) return;
98 // Track if the list head changed
99 weak List<VibratorState> old_top = states;
101 // Remove state "name" from the stack
102 for (weak List<VibratorState> i = states; i != null; i = i.next)
103 if (i.data.name == name)
105 states.delete_link(i);
109 // If the list head changed, put into action the new top state
110 if (states != old_top)
114 // Activate the new top
115 states.data.to_omhacks(ref vibrator);
120 public void shutdown()
122 while (states != null)
123 states.delete_link(states);
128 public class Audio: zavai.Service
131 protected dynamic DBus.Object audiodev;
132 protected dynamic DBus.Object vibdev;
137 Object(name: "audio");
140 audiodev = zavai.registry.sbus.get_object(
141 "org.freesmartphone.odeviced",
142 "/org/freesmartphone/Device/Audio",
143 "org.freesmartphone.Device.Audio");
144 vibdev = zavai.registry.sbus.get_object(
145 "org.freesmartphone.odeviced",
146 "/org/freesmartphone/Device/LED/neo1973_vibrator",
147 "org.freesmartphone.Device.LED");
149 clock.alarm_trigger_queue.triggered += on_alarm_trigger;
150 clock.alarm_trigger_queue.acked += on_alarm_done;
151 clock.alarm_trigger_queue.canceled += on_alarm_done;
154 public void on_alarm_trigger(clock.AlarmTriggerInfo info)
156 zavai.log.debug("Make noise for alarm");
157 if (vibrator != null)
159 var state = new VibratorState("alarm");
160 state.set_blink(255);
161 vibrator.push_state(state);
163 soundplayer.play(config.ringtone_alarm, true);
166 public void on_alarm_done(clock.AlarmTriggerInfo info)
168 zavai.log.debug("Stop noise for alarm");
169 if (vibrator != null)
170 vibrator.pop_state("alarm");
175 public class Player: zavai.Resource, Object
177 protected Gst.Element player;
178 protected bool playing;
179 protected Player slave;
180 protected Player master;
182 protected string uri;
183 public signal void state_changed(Gst.State new_state);
189 player = Gst.ElementFactory.make("playbin", null);
192 var bus = player.get_bus();
193 bus.add_signal_watch();
194 bus.message += on_message;
197 public void set_slave(Player player)
203 public void play(string uri, bool loop = false)
205 stderr.printf("Playing %s\n", uri);
208 if (slave != null && slave.playing)
211 player.set_property("uri", uri);
212 player.set_state(master != null && master.playing ? Gst.State.PAUSED : Gst.State.PLAYING);
217 public Gst.State get_state()
222 player.get_state(out state, out pending, (Gst.ClockType)Gst.CLOCK_TIME_NONE);
229 player.set_state(Gst.State.PAUSED);
230 state_changed(Gst.State.PAUSED);
235 player.set_state(Gst.State.PLAYING);
236 state_changed(Gst.State.PLAYING);
239 public void restart()
241 player.seek_simple(Gst.Format.TIME, Gst.SeekFlags.FLUSH | Gst.SeekFlags.KEY_UNIT, 0);
242 player.set_state(Gst.State.PLAYING);
243 state_changed(Gst.State.PLAYING);
249 player.set_state(Gst.State.NULL);
250 state_changed(Gst.State.NULL);
252 // Resume slave after we are done
253 if (slave != null && slave.playing)
257 protected void on_message(Gst.Message message)
259 if (message.type == Gst.MessageType.EOS)
268 public void shutdown()
274 public Vibrator vibrator = null;
275 public Audio audio = null;
276 public Player musicplayer = null;
277 public Player soundplayer = null;
282 vibrator = new Vibrator();
283 zavai.registry.register(vibrator);
285 zavai.log.info("No vibrator found");
289 musicplayer = new Player();
290 soundplayer = new Player();
291 soundplayer.set_slave(musicplayer);
292 zavai.registry.register(musicplayer);
293 zavai.registry.register(soundplayer);