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