Commit | Line | Data |
---|---|---|
460b9539 | 1 | /* |
2 | * This file is part of DisOrder. | |
3849817f | 3 | * Copyright (C) 2006-2008 Richard Kettlewell |
460b9539 | 4 | * |
5 | * This program is free software; you can redistribute it and/or modify | |
6 | * it under the terms of the GNU General Public License as published by | |
7 | * the Free Software Foundation; either version 2 of the License, or | |
8 | * (at your option) any later version. | |
9 | * | |
10 | * This program is distributed in the hope that it will be useful, but | |
11 | * WITHOUT ANY WARRANTY; without even the implied warranty of | |
12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |
13 | * General Public License for more details. | |
14 | * | |
15 | * You should have received a copy of the GNU General Public License | |
16 | * along with this program; if not, write to the Free Software | |
17 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 | |
18 | * USA | |
19 | */ | |
219dc95c RK |
20 | /** @file disobedience/control.c |
21 | * @brief Volume control and buttons | |
22 | */ | |
460b9539 | 23 | |
24 | #include "disobedience.h" | |
321f7536 | 25 | #include "mixer.h" |
460b9539 | 26 | |
e9b70a84 | 27 | /* Forward declarations ---------------------------------------------------- */ |
28 | ||
29 | WT(adjustment); | |
30 | WT(hscale); | |
31 | WT(hbox); | |
e9b70a84 | 32 | WT(button); |
33 | WT(image); | |
34 | WT(label); | |
35 | WT(vbox); | |
460b9539 | 36 | |
37 | struct icon; | |
38 | ||
3849817f | 39 | static void update_icon(const struct icon *icon); |
460b9539 | 40 | static void clicked_icon(GtkButton *, gpointer); |
ac6bf2ba | 41 | static void clicked_menu(GtkMenuItem *, gpointer userdata); |
2d504956 | 42 | static void toggled_menu(GtkCheckMenuItem *, gpointer userdata); |
460b9539 | 43 | |
a99c4e9a RK |
44 | static int enable_rtp(disorder_eclient *c, |
45 | disorder_eclient_no_response *completed, | |
46 | void *v); | |
47 | static int disable_rtp(disorder_eclient *c, | |
48 | disorder_eclient_no_response *completed, | |
49 | void *v); | |
50 | ||
460b9539 | 51 | static double left(double v, double b); |
52 | static double right(double v, double b); | |
53 | static double volume(double l, double r); | |
54 | static double balance(double l, double r); | |
55 | ||
56 | static void volume_adjusted(GtkAdjustment *a, gpointer user_data); | |
57 | static gchar *format_volume(GtkScale *scale, gdouble value); | |
58 | static gchar *format_balance(GtkScale *scale, gdouble value); | |
59 | ||
1f868ca3 RK |
60 | static void volume_changed(const char *event, |
61 | void *eventdata, | |
62 | void *callbackdata); | |
63 | ||
460b9539 | 64 | /* Control bar ------------------------------------------------------------- */ |
65 | ||
fb44b59e RK |
66 | /** @brief Guard against feedback */ |
67 | int suppress_actions = 1; | |
460b9539 | 68 | |
219dc95c RK |
69 | /** @brief Definition of an icon |
70 | * | |
3849817f RK |
71 | * We have two kinds of icon: |
72 | * - action icons, which just do something but don't have a state as such | |
73 | * - toggle icons, which toggle between two states ("on" and "off"). | |
74 | * | |
75 | * The scratch button is an action icon; currently all the others are toggle | |
76 | * icons. | |
77 | * | |
78 | * (All icons can be sensitive or insensitive, separately to the above.) | |
219dc95c RK |
79 | */ |
80 | struct icon { | |
3849817f RK |
81 | /** @brief Filename for 'on' image */ |
82 | const char *icon_on; | |
83 | ||
84 | /** @brief Text for 'on' tooltip */ | |
85 | const char *tip_on; | |
219dc95c | 86 | |
3849817f RK |
87 | /** @brief Filename for 'off' image or NULL for an action icon */ |
88 | const char *icon_off; | |
89 | ||
90 | /** @brief Text for 'off tooltip */ | |
91 | const char *tip_off; | |
219dc95c | 92 | |
ac6bf2ba RK |
93 | /** @brief Associated menu item or NULL */ |
94 | const char *menuitem; | |
219dc95c | 95 | |
3849817f RK |
96 | /** @brief @ref eclient.h function to call to go from off to on |
97 | * | |
98 | * For action buttons, this should be NULL. | |
99 | */ | |
100 | int (*action_go_on)(disorder_eclient *c, | |
101 | disorder_eclient_no_response *completed, | |
102 | void *v); | |
219dc95c | 103 | |
3849817f RK |
104 | /** @brief @ref eclient.h function to call to go from on to off |
105 | * | |
106 | * For action buttons, this action is used. | |
107 | */ | |
108 | int (*action_go_off)(disorder_eclient *c, | |
109 | disorder_eclient_no_response *completed, | |
110 | void *v); | |
219dc95c | 111 | |
3849817f RK |
112 | /** @brief Get button state |
113 | * @return 1 for on, 0 for off | |
114 | */ | |
115 | int (*on)(void); | |
116 | ||
117 | /** @brief Get button sensitivity | |
118 | * @return 1 for sensitive, 0 for insensitive | |
119 | * | |
120 | * Can be NULL for always sensitive. | |
121 | */ | |
122 | int (*sensitive)(void); | |
2d504956 | 123 | |
219dc95c | 124 | /** @brief Pointer to button */ |
460b9539 | 125 | GtkWidget *button; |
ac6bf2ba RK |
126 | |
127 | /** @brief Pointer to menu item */ | |
128 | GtkWidget *item; | |
3849817f RK |
129 | |
130 | GtkWidget *image_on; | |
131 | GtkWidget *image_off; | |
219dc95c RK |
132 | }; |
133 | ||
3849817f RK |
134 | /* TODO: Add rights into the mix below */ |
135 | ||
136 | static int pause_resume_on(void) { | |
137 | return !(last_state & DISORDER_TRACK_PAUSED); | |
138 | } | |
139 | ||
140 | static int pause_resume_sensitive(void) { | |
6f09d54d RK |
141 | return !!(last_state & DISORDER_PLAYING) |
142 | && (last_rights & RIGHT_PAUSE); | |
3849817f RK |
143 | } |
144 | ||
145 | static int scratch_sensitive(void) { | |
6f09d54d RK |
146 | return !!(last_state & DISORDER_PLAYING) |
147 | && (last_rights & RIGHT_SCRATCH__MASK); | |
148 | /* TODO: it's more complicated than that... */ | |
149 | } | |
150 | ||
151 | static int random_sensitive(void) { | |
152 | return !!(last_rights & RIGHT_GLOBAL_PREFS); | |
3849817f RK |
153 | } |
154 | ||
155 | static int random_enabled(void) { | |
156 | return !!(last_state & DISORDER_RANDOM_ENABLED); | |
157 | } | |
158 | ||
6f09d54d RK |
159 | static int playing_sensitive(void) { |
160 | return !!(last_rights & RIGHT_GLOBAL_PREFS); | |
161 | } | |
162 | ||
3849817f RK |
163 | static int playing_enabled(void) { |
164 | return !!(last_state & DISORDER_PLAYING_ENABLED); | |
165 | } | |
2d504956 | 166 | |
3849817f RK |
167 | static int rtp_enabled(void) { |
168 | return rtp_is_running; | |
169 | } | |
170 | ||
171 | static int rtp_sensitive(void) { | |
172 | return rtp_supported; | |
173 | } | |
2d504956 | 174 | |
219dc95c RK |
175 | /** @brief Table of all icons */ |
176 | static struct icon icons[] = { | |
2d504956 | 177 | { |
3849817f RK |
178 | icon_on: "pause.png", |
179 | tip_on: "Pause playing track", | |
180 | icon_off: "play.png", | |
181 | tip_off: "Resume playing track", | |
182 | menuitem: "<GdisorderMain>/Control/Playing", | |
183 | on: pause_resume_on, | |
184 | sensitive: pause_resume_sensitive, | |
185 | action_go_on: disorder_eclient_resume, | |
186 | action_go_off: disorder_eclient_pause, | |
2d504956 RK |
187 | }, |
188 | { | |
3849817f RK |
189 | icon_on: "cross.png", |
190 | tip_on: "Cancel playing track", | |
191 | menuitem: "<GdisorderMain>/Control/Scratch", | |
192 | sensitive: scratch_sensitive, | |
193 | action_go_off: disorder_eclient_scratch_playing, | |
2d504956 RK |
194 | }, |
195 | { | |
3849817f RK |
196 | icon_on: "randomcross.png", |
197 | tip_on: "Disable random play", | |
198 | icon_off: "random.png", | |
199 | tip_off: "Enable random play", | |
200 | menuitem: "<GdisorderMain>/Control/Random play", | |
201 | on: random_enabled, | |
6f09d54d | 202 | sensitive: random_sensitive, |
3849817f RK |
203 | action_go_on: disorder_eclient_random_enable, |
204 | action_go_off: disorder_eclient_random_disable, | |
2d504956 RK |
205 | }, |
206 | { | |
3849817f RK |
207 | icon_on: "notescross.png", |
208 | tip_on: "Disable play", | |
209 | icon_off: "notes.png", | |
210 | tip_off: "Enable play", | |
211 | on: playing_enabled, | |
6f09d54d | 212 | sensitive: playing_sensitive, |
3849817f RK |
213 | action_go_on: disorder_eclient_enable, |
214 | action_go_off: disorder_eclient_disable, | |
2d504956 RK |
215 | }, |
216 | { | |
3849817f RK |
217 | icon_on: "speakercross.png", |
218 | tip_on: "Stop playing network stream", | |
219 | icon_off: "speaker.png", | |
220 | tip_off: "Play network stream", | |
221 | menuitem: "<GdisorderMain>/Control/Network player", | |
222 | on: rtp_enabled, | |
223 | sensitive: rtp_sensitive, | |
224 | action_go_on: enable_rtp, | |
225 | action_go_off: disable_rtp | |
2d504956 | 226 | }, |
460b9539 | 227 | }; |
219dc95c RK |
228 | |
229 | /** @brief Count of icons */ | |
460b9539 | 230 | #define NICONS (int)(sizeof icons / sizeof *icons) |
231 | ||
219dc95c RK |
232 | static GtkAdjustment *volume_adj; |
233 | static GtkAdjustment *balance_adj; | |
3c499fe7 RK |
234 | static GtkWidget *volume_widget; |
235 | static GtkWidget *balance_widget; | |
460b9539 | 236 | |
3849817f RK |
237 | /** @brief Called whenever last_state changes in any way |
238 | * | |
239 | * TODO we should only update things that have changed. | |
240 | */ | |
6c7a654c RK |
241 | static void control_changed(const char attribute((unused)) *event, |
242 | void attribute((unused)) *evendata, | |
243 | void attribute((unused)) *callbackdata) { | |
6d1302f0 | 244 | int n; |
3c499fe7 | 245 | gboolean volume_supported; |
6d1302f0 | 246 | |
6c7a654c | 247 | D(("control_changed")); |
3849817f | 248 | /* Update all the icons */ |
6d1302f0 | 249 | for(n = 0; n < NICONS; ++n) |
3849817f | 250 | update_icon(&icons[n]); |
3c499fe7 RK |
251 | /* Only display volume/balance controls if they will work */ |
252 | if(!rtp_supported | |
253 | || (rtp_supported && mixer_supported(DEFAULT_BACKEND))) | |
254 | volume_supported = TRUE; | |
255 | else | |
256 | volume_supported = FALSE; | |
257 | (volume_supported ? gtk_widget_show : gtk_widget_hide)(volume_widget); | |
258 | (volume_supported ? gtk_widget_show : gtk_widget_hide)(balance_widget); | |
6d1302f0 RK |
259 | } |
260 | ||
219dc95c | 261 | /** @brief Create the control bar */ |
e9b70a84 | 262 | GtkWidget *control_widget(void) { |
460b9539 | 263 | GtkWidget *hbox = gtk_hbox_new(FALSE, 1), *vbox; |
460b9539 | 264 | int n; |
265 | ||
e9b70a84 | 266 | NW(hbox); |
460b9539 | 267 | D(("control_widget")); |
ac6bf2ba | 268 | assert(mainmenufactory); /* ordering must be right */ |
460b9539 | 269 | for(n = 0; n < NICONS; ++n) { |
3849817f | 270 | /* Create the button */ |
e9b70a84 | 271 | NW(button); |
3849817f RK |
272 | icons[n].button = gtk_button_new(); |
273 | gtk_widget_set_style(icons[n].button, tool_style); | |
274 | icons[n].image_on = gtk_image_new_from_pixbuf(find_image(icons[n].icon_on)); | |
275 | gtk_widget_set_style(icons[n].image_on, tool_style); | |
276 | g_object_ref(icons[n].image_on); | |
277 | /* If it's a toggle icon, create the 'off' half too */ | |
278 | if(icons[n].icon_off) { | |
279 | icons[n].image_off = gtk_image_new_from_pixbuf(find_image(icons[n].icon_off)); | |
280 | gtk_widget_set_style(icons[n].image_off, tool_style); | |
281 | g_object_ref(icons[n].image_off); | |
282 | } | |
460b9539 | 283 | g_signal_connect(G_OBJECT(icons[n].button), "clicked", |
ac6bf2ba | 284 | G_CALLBACK(clicked_icon), &icons[n]); |
460b9539 | 285 | /* pop the icon in a vbox so it doesn't get vertically stretch if there are |
286 | * taller things in the control bar */ | |
e9b70a84 | 287 | NW(vbox); |
460b9539 | 288 | vbox = gtk_vbox_new(FALSE, 0); |
289 | gtk_box_pack_start(GTK_BOX(vbox), icons[n].button, TRUE, FALSE, 0); | |
290 | gtk_box_pack_start(GTK_BOX(hbox), vbox, FALSE, FALSE, 0); | |
ac6bf2ba | 291 | if(icons[n].menuitem) { |
3849817f | 292 | /* Find the menu item */ |
ac6bf2ba RK |
293 | icons[n].item = gtk_item_factory_get_widget(mainmenufactory, |
294 | icons[n].menuitem); | |
3849817f | 295 | if(icons[n].icon_off) |
2d504956 RK |
296 | g_signal_connect(G_OBJECT(icons[n].item), "toggled", |
297 | G_CALLBACK(toggled_menu), &icons[n]); | |
3849817f | 298 | else |
2d504956 RK |
299 | g_signal_connect(G_OBJECT(icons[n].item), "activate", |
300 | G_CALLBACK(clicked_menu), &icons[n]); | |
ac6bf2ba | 301 | } |
460b9539 | 302 | } |
303 | /* create the adjustments for the volume control */ | |
e9b70a84 | 304 | NW(adjustment); |
460b9539 | 305 | volume_adj = GTK_ADJUSTMENT(gtk_adjustment_new(0, 0, goesupto, |
306 | goesupto / 20, goesupto / 20, | |
307 | 0)); | |
e9b70a84 | 308 | NW(adjustment); |
460b9539 | 309 | balance_adj = GTK_ADJUSTMENT(gtk_adjustment_new(0, -1, 1, |
310 | 0.2, 0.2, 0)); | |
311 | /* the volume control */ | |
e9b70a84 | 312 | NW(hscale); |
3c499fe7 | 313 | volume_widget = gtk_hscale_new(volume_adj); |
e9b70a84 | 314 | NW(hscale); |
3c499fe7 RK |
315 | balance_widget = gtk_hscale_new(balance_adj); |
316 | gtk_widget_set_style(volume_widget, tool_style); | |
317 | gtk_widget_set_style(balance_widget, tool_style); | |
318 | gtk_scale_set_digits(GTK_SCALE(volume_widget), 10); | |
319 | gtk_scale_set_digits(GTK_SCALE(balance_widget), 10); | |
320 | gtk_widget_set_size_request(volume_widget, 192, -1); | |
321 | gtk_widget_set_size_request(balance_widget, 192, -1); | |
322 | gtk_tooltips_set_tip(tips, volume_widget, "Volume", ""); | |
323 | gtk_tooltips_set_tip(tips, balance_widget, "Balance", ""); | |
324 | gtk_box_pack_start(GTK_BOX(hbox), volume_widget, FALSE, TRUE, 0); | |
325 | gtk_box_pack_start(GTK_BOX(hbox), balance_widget, FALSE, TRUE, 0); | |
460b9539 | 326 | /* space updates rather than hammering the server */ |
3c499fe7 RK |
327 | gtk_range_set_update_policy(GTK_RANGE(volume_widget), GTK_UPDATE_DELAYED); |
328 | gtk_range_set_update_policy(GTK_RANGE(balance_widget), GTK_UPDATE_DELAYED); | |
460b9539 | 329 | /* notice when the adjustments are changed */ |
330 | g_signal_connect(G_OBJECT(volume_adj), "value-changed", | |
331 | G_CALLBACK(volume_adjusted), 0); | |
332 | g_signal_connect(G_OBJECT(balance_adj), "value-changed", | |
333 | G_CALLBACK(volume_adjusted), 0); | |
334 | /* format the volume/balance values ourselves */ | |
3c499fe7 | 335 | g_signal_connect(G_OBJECT(volume_widget), "format-value", |
460b9539 | 336 | G_CALLBACK(format_volume), 0); |
3c499fe7 | 337 | g_signal_connect(G_OBJECT(balance_widget), "format-value", |
460b9539 | 338 | G_CALLBACK(format_balance), 0); |
6c7a654c RK |
339 | event_register("enabled-changed", control_changed, 0); |
340 | event_register("random-changed", control_changed, 0); | |
341 | event_register("pause-changed", control_changed, 0); | |
342 | event_register("playing-changed", control_changed, 0); | |
343 | event_register("rtp-changed", control_changed, 0); | |
6f09d54d | 344 | event_register("rights-changed", control_changed, 0); |
1f868ca3 | 345 | event_register("volume-changed", volume_changed, 0); |
460b9539 | 346 | return hbox; |
347 | } | |
348 | ||
6d1302f0 | 349 | /** @brief Update the volume control when it changes */ |
1f868ca3 RK |
350 | static void volume_changed(const char attribute((unused)) *event, |
351 | void attribute((unused)) *eventdata, | |
352 | void attribute((unused)) *callbackdata) { | |
460b9539 | 353 | double l, r; |
354 | ||
1f868ca3 | 355 | D(("volume_changed")); |
460b9539 | 356 | l = volume_l / 100.0; |
357 | r = volume_r / 100.0; | |
fb44b59e | 358 | ++suppress_actions; |
460b9539 | 359 | gtk_adjustment_set_value(volume_adj, volume(l, r) * goesupto); |
360 | gtk_adjustment_set_value(balance_adj, balance(l, r)); | |
fb44b59e | 361 | --suppress_actions; |
460b9539 | 362 | } |
363 | ||
ad58ebcc | 364 | /** @brief Update the state of one of the control icons |
00959300 | 365 | * @param icon Target icon |
ad58ebcc | 366 | */ |
3849817f RK |
367 | static void update_icon(const struct icon *icon) { |
368 | int on = icon->on ? icon->on() : 1; | |
369 | int sensitive = icon->sensitive ? icon->sensitive() : 1; | |
370 | GtkWidget *child, *newchild; | |
371 | ||
372 | ++suppress_actions; | |
ad58ebcc | 373 | /* If the connection is down nothing is ever usable */ |
00959300 | 374 | if(!(last_state & DISORDER_CONNECTED)) |
3849817f RK |
375 | sensitive = 0; |
376 | /* Replace the child */ | |
377 | newchild = on ? icon->image_on : icon->image_off; | |
378 | child = gtk_bin_get_child(GTK_BIN(icon->button)); | |
379 | if(child != newchild) { | |
380 | if(child) | |
381 | gtk_container_remove(GTK_CONTAINER(icon->button), child); | |
382 | gtk_container_add(GTK_CONTAINER(icon->button), newchild); | |
383 | gtk_widget_show(newchild); | |
384 | } | |
385 | if(icon->tip_on) | |
386 | gtk_tooltips_set_tip(tips, icon->button, | |
387 | on ? icon->tip_on : icon->tip_off, ""); | |
388 | gtk_widget_set_sensitive(icon->button, sensitive); | |
389 | /* Icons with an associated menu item */ | |
ac6bf2ba | 390 | if(icon->item) { |
3849817f RK |
391 | if(icon->icon_off) |
392 | gtk_check_menu_item_set_active(GTK_CHECK_MENU_ITEM(icon->item), on); | |
393 | gtk_widget_set_sensitive(icon->item, sensitive); | |
ac6bf2ba | 394 | } |
3849817f | 395 | --suppress_actions; |
a99c4e9a RK |
396 | } |
397 | ||
53fa91bb RK |
398 | static void icon_action_completed(void attribute((unused)) *v, |
399 | const char *error) { | |
400 | if(error) | |
401 | popup_protocol_error(0, error); | |
402 | } | |
403 | ||
460b9539 | 404 | static void clicked_icon(GtkButton attribute((unused)) *button, |
405 | gpointer userdata) { | |
406 | const struct icon *icon = userdata; | |
407 | ||
3849817f RK |
408 | if(suppress_actions) |
409 | return; | |
410 | if(!icon->on || icon->on()) | |
411 | icon->action_go_off(client, icon_action_completed, 0); | |
412 | else | |
413 | icon->action_go_on(client, icon_action_completed, 0); | |
460b9539 | 414 | } |
415 | ||
ac6bf2ba RK |
416 | static void clicked_menu(GtkMenuItem attribute((unused)) *menuitem, |
417 | gpointer userdata) { | |
3849817f | 418 | clicked_icon(NULL, userdata); |
ac6bf2ba RK |
419 | } |
420 | ||
3849817f | 421 | static void toggled_menu(GtkCheckMenuItem attribute((unused)) *menuitem, |
2d504956 | 422 | gpointer userdata) { |
3849817f | 423 | clicked_icon(NULL, userdata); |
2d504956 RK |
424 | } |
425 | ||
699517af RK |
426 | /** @brief Called when a volume command completes */ |
427 | static void volume_completed(void attribute((unused)) *v, | |
428 | const char *error, | |
429 | int attribute((unused)) l, | |
430 | int attribute((unused)) r) { | |
431 | if(error) | |
432 | popup_protocol_error(0, error); | |
433 | /* We don't set the UI's notion of the volume here, it is set from the log | |
434 | * regardless of the reason it changed */ | |
435 | } | |
436 | ||
219dc95c | 437 | /** @brief Called when the volume has been adjusted */ |
460b9539 | 438 | static void volume_adjusted(GtkAdjustment attribute((unused)) *a, |
439 | gpointer attribute((unused)) user_data) { | |
440 | double v = gtk_adjustment_get_value(volume_adj) / goesupto; | |
441 | double b = gtk_adjustment_get_value(balance_adj); | |
442 | ||
fb44b59e | 443 | if(suppress_actions) |
460b9539 | 444 | /* This is the result of an update from the server, not a change from the |
445 | * user. Don't feedback! */ | |
446 | return; | |
447 | D(("volume_adjusted")); | |
448 | /* force to 'stereotypical' values */ | |
449 | v = nearbyint(100 * v) / 100; | |
450 | b = nearbyint(5 * b) / 5; | |
451 | /* Set the volume. We don't want a reply, we'll get the actual new volume | |
452 | * from the log. */ | |
321f7536 RK |
453 | if(rtp_supported) { |
454 | int l = nearbyint(left(v, b) * 100), r = nearbyint(right(v, b) * 100); | |
3c499fe7 | 455 | mixer_control(DEFAULT_BACKEND, &l, &r, 1); |
321f7536 | 456 | } else |
699517af | 457 | disorder_eclient_volume(client, volume_completed, |
321f7536 RK |
458 | nearbyint(left(v, b) * 100), |
459 | nearbyint(right(v, b) * 100), | |
460 | 0); | |
460b9539 | 461 | } |
462 | ||
219dc95c | 463 | /** @brief Formats the volume value */ |
460b9539 | 464 | static gchar *format_volume(GtkScale attribute((unused)) *scale, |
465 | gdouble value) { | |
466 | char s[32]; | |
467 | ||
468 | snprintf(s, sizeof s, "%.1f", (double)value); | |
2fc4475d | 469 | return g_strdup(s); |
460b9539 | 470 | } |
471 | ||
219dc95c | 472 | /** @brief Formats the balance value */ |
460b9539 | 473 | static gchar *format_balance(GtkScale attribute((unused)) *scale, |
474 | gdouble value) { | |
475 | char s[32]; | |
476 | ||
477 | if(fabs(value) < 0.1) | |
2fc4475d | 478 | return g_strdup("0"); |
460b9539 | 479 | snprintf(s, sizeof s, "%+.1f", (double)value); |
2fc4475d | 480 | return g_strdup(s); |
460b9539 | 481 | } |
482 | ||
483 | /* Volume mapping. We consider left, right, volume to be in [0,1] | |
484 | * and balance to be in [-1,1]. | |
485 | * | |
486 | * First, we just have volume = max(left, right). | |
487 | * | |
488 | * Balance we consider to linearly represent the amount by which the quieter | |
489 | * channel differs from the louder. In detail: | |
490 | * | |
491 | * if right > left then balance > 0: | |
492 | * balance = 0 => left = right (as an endpoint, not an instance) | |
493 | * balance = 1 => left = 0 | |
494 | * fitting to linear, left = right * (1 - balance) | |
495 | * so balance = 1 - left / right | |
496 | * (right > left => right > 0 so no division by 0.) | |
497 | * | |
498 | * if left > right then balance < 0: | |
499 | * balance = 0 => right = left (same caveat as above) | |
500 | * balance = -1 => right = 0 | |
501 | * again fitting to linear, right = left * (1 + balance) | |
502 | * so balance = right / left - 1 | |
503 | * (left > right => left > 0 so no division by 0.) | |
504 | * | |
505 | * if left = right then we just have balance = 0. | |
506 | * | |
507 | * Thanks to Clive and Andrew. | |
508 | */ | |
509 | ||
219dc95c | 510 | /** @brief Return the greater of @p x and @p y */ |
460b9539 | 511 | static double max(double x, double y) { |
512 | return x > y ? x : y; | |
513 | } | |
514 | ||
219dc95c | 515 | /** @brief Compute the left channel volume */ |
460b9539 | 516 | static double left(double v, double b) { |
517 | if(b > 0) /* volume = right */ | |
518 | return v * (1 - b); | |
519 | else /* volume = left */ | |
520 | return v; | |
521 | } | |
522 | ||
219dc95c | 523 | /** @brief Compute the right channel volume */ |
460b9539 | 524 | static double right(double v, double b) { |
525 | if(b > 0) /* volume = right */ | |
526 | return v; | |
527 | else /* volume = left */ | |
528 | return v * (1 + b); | |
529 | } | |
530 | ||
219dc95c | 531 | /** @brief Compute the overall volume */ |
460b9539 | 532 | static double volume(double l, double r) { |
533 | return max(l, r); | |
534 | } | |
535 | ||
219dc95c | 536 | /** @brief Compute the balance */ |
460b9539 | 537 | static double balance(double l, double r) { |
538 | if(l > r) | |
539 | return r / l - 1; | |
540 | else if(r > l) | |
541 | return 1 - l / r; | |
542 | else /* left = right */ | |
543 | return 0; | |
544 | } | |
545 | ||
a99c4e9a RK |
546 | /** @brief Called to enable RTP play |
547 | * | |
548 | * Rather odd signature is to fit in with the other icons which all call @ref | |
549 | * lib/eclient.h functions. | |
550 | */ | |
551 | static int enable_rtp(disorder_eclient attribute((unused)) *c, | |
552 | disorder_eclient_no_response attribute((unused)) *completed, | |
553 | void attribute((unused)) *v) { | |
554 | start_rtp(); | |
555 | return 0; | |
556 | } | |
557 | ||
558 | /** @brief Called to disable RTP play | |
559 | * | |
560 | * Rather odd signature is to fit in with the other icons which all call @ref | |
561 | * lib/eclient.h functions. | |
562 | */ | |
563 | static int disable_rtp(disorder_eclient attribute((unused)) *c, | |
564 | disorder_eclient_no_response attribute((unused)) *completed, | |
565 | void attribute((unused)) *v) { | |
566 | stop_rtp(); | |
567 | return 0; | |
568 | } | |
569 | ||
460b9539 | 570 | /* |
571 | Local Variables: | |
572 | c-basic-offset:2 | |
573 | comment-column:40 | |
574 | fill-column:79 | |
575 | indent-tabs-mode:nil | |
576 | End: | |
577 | */ |