]> code.delx.au - pulseaudio/blob - src/modules/module-position-event-sounds.c
90e693a315c1d019b6f0c32314ed5e4c0f33cdd5
[pulseaudio] / src / modules / module-position-event-sounds.c
1 /***
2 This file is part of PulseAudio.
3
4 Copyright 2006 Lennart Poettering
5
6 PulseAudio is free software; you can redistribute it and/or modify
7 it under the terms of the GNU Lesser General Public License as published
8 by the Free Software Foundation; either version 2 of the License,
9 or (at your option) any later version.
10
11 PulseAudio is distributed in the hope that it will be useful, but
12 WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 General Public License for more details.
15
16 You should have received a copy of the GNU Lesser General Public License
17 along with PulseAudio; if not, write to the Free Software
18 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
19 USA.
20 ***/
21
22 #ifdef HAVE_CONFIG_H
23 #include <config.h>
24 #endif
25
26 #include <unistd.h>
27 #include <string.h>
28 #include <errno.h>
29 #include <sys/types.h>
30 #include <stdio.h>
31 #include <stdlib.h>
32 #include <ctype.h>
33
34 #include <pulse/xmalloc.h>
35 #include <pulse/volume.h>
36 #include <pulse/channelmap.h>
37
38 #include <pulsecore/core-error.h>
39 #include <pulsecore/module.h>
40 #include <pulsecore/core-util.h>
41 #include <pulsecore/modargs.h>
42 #include <pulsecore/log.h>
43 #include <pulsecore/sink-input.h>
44
45 #include "module-position-event-sounds-symdef.h"
46
47 PA_MODULE_AUTHOR("Lennart Poettering");
48 PA_MODULE_DESCRIPTION("Position event sounds between L and R depending on the position on screen of the widget triggering them.");
49 PA_MODULE_VERSION(PACKAGE_VERSION);
50 PA_MODULE_LOAD_ONCE(TRUE);
51
52 static const char* const valid_modargs[] = {
53 NULL
54 };
55
56 struct userdata {
57 pa_core *core;
58 pa_hook_slot *sink_input_fixate_hook_slot;
59 };
60
61 static pa_bool_t is_left(pa_channel_position_t p) {
62 return
63 p == PA_CHANNEL_POSITION_FRONT_LEFT ||
64 p == PA_CHANNEL_POSITION_REAR_LEFT ||
65 p == PA_CHANNEL_POSITION_FRONT_LEFT_OF_CENTER ||
66 p == PA_CHANNEL_POSITION_SIDE_LEFT ||
67 p == PA_CHANNEL_POSITION_TOP_FRONT_LEFT ||
68 p == PA_CHANNEL_POSITION_TOP_REAR_LEFT;
69 }
70
71 static pa_bool_t is_right(pa_channel_position_t p) {
72 return
73 p == PA_CHANNEL_POSITION_FRONT_RIGHT ||
74 p == PA_CHANNEL_POSITION_REAR_RIGHT||
75 p == PA_CHANNEL_POSITION_FRONT_RIGHT_OF_CENTER ||
76 p == PA_CHANNEL_POSITION_SIDE_RIGHT ||
77 p == PA_CHANNEL_POSITION_TOP_FRONT_RIGHT ||
78 p == PA_CHANNEL_POSITION_TOP_REAR_RIGHT;
79 }
80
81 static pa_hook_result_t sink_input_fixate_hook_callback(pa_core *core, pa_sink_input_new_data *data, struct userdata *u) {
82 const char *hpos;
83 double f;
84 unsigned c;
85 char t[PA_CVOLUME_SNPRINT_MAX];
86
87 pa_assert(data);
88
89 if (!(hpos = pa_proplist_gets(data->proplist, PA_PROP_EVENT_MOUSE_HPOS)))
90 return PA_HOOK_OK;
91
92 if (pa_atod(hpos, &f) < 0) {
93 pa_log_warn("Failed to parse "PA_PROP_EVENT_MOUSE_HPOS" property '%s'.", hpos);
94 return PA_HOOK_OK;
95 }
96
97 if (f < 0.0 || f > 1.0) {
98 pa_log_warn("Property "PA_PROP_EVENT_MOUSE_HPOS" out of range %0.2f", f);
99 return PA_HOOK_OK;
100 }
101
102 pa_log_debug("Positioning event sound '%s' at %0.2f.", pa_strnull(pa_proplist_gets(data->proplist, PA_PROP_EVENT_ID)), f);
103
104 if (!data->volume_is_set) {
105 pa_cvolume_reset(&data->volume, data->sample_spec.channels);
106 data->volume_is_set = TRUE;
107 }
108
109 for (c = 0; c < data->sample_spec.channels; c++) {
110
111 if (is_left(data->channel_map.map[c]))
112 data->volume.values[c] =
113 pa_sw_volume_multiply(data->volume.values[c], (pa_volume_t) (PA_VOLUME_NORM * (1.0 - f)));
114
115 if (is_right(data->channel_map.map[c]))
116 data->volume.values[c] =
117 pa_sw_volume_multiply(data->volume.values[c], (pa_volume_t) (PA_VOLUME_NORM * f));
118 }
119
120 pa_log_debug("Final volume %s.", pa_cvolume_snprint(t, sizeof(t), &data->volume));
121
122 return PA_HOOK_OK;
123 }
124
125 int pa__init(pa_module*m) {
126 pa_modargs *ma = NULL;
127 struct userdata *u;
128
129 pa_assert(m);
130
131 if (!(ma = pa_modargs_new(m->argument, valid_modargs))) {
132 pa_log("Failed to parse module arguments");
133 goto fail;
134 }
135
136 m->userdata = u = pa_xnew(struct userdata, 1);
137 u->core = m->core;
138 u->sink_input_fixate_hook_slot = pa_hook_connect(&m->core->hooks[PA_CORE_HOOK_SINK_INPUT_FIXATE], PA_HOOK_EARLY, (pa_hook_cb_t) sink_input_fixate_hook_callback, u);
139
140 pa_modargs_free(ma);
141
142 return 0;
143
144 fail:
145 pa__done(m);
146
147 if (ma)
148 pa_modargs_free(ma);
149
150 return -1;
151 }
152
153 void pa__done(pa_module*m) {
154 struct userdata* u;
155
156 pa_assert(m);
157
158 if (!(u = m->userdata))
159 return;
160
161 if (u->sink_input_fixate_hook_slot)
162 pa_hook_slot_free(u->sink_input_fixate_hook_slot);
163
164 pa_xfree(u);
165 }