summaryrefslogtreecommitdiff
path: root/sink.c
blob: c5134ca3f9f6d62b4b5d44bef376dd9f34ab9a99 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
#include "pa.h"


/** Makes a bit mask from a channel position. \since 0.9.16 */
#define PA_CHANNEL_POSITION_MASK(f) ((pa_channel_position_mask_t) (1ULL << (f)))


#define PA_CHANNEL_POSITION_MASK_LEFT                                   \
    (PA_CHANNEL_POSITION_MASK(PA_CHANNEL_POSITION_FRONT_LEFT)           \
     | PA_CHANNEL_POSITION_MASK(PA_CHANNEL_POSITION_REAR_LEFT)          \
     | PA_CHANNEL_POSITION_MASK(PA_CHANNEL_POSITION_FRONT_LEFT_OF_CENTER) \
     | PA_CHANNEL_POSITION_MASK(PA_CHANNEL_POSITION_SIDE_LEFT)          \
     | PA_CHANNEL_POSITION_MASK(PA_CHANNEL_POSITION_TOP_FRONT_LEFT)     \
     | PA_CHANNEL_POSITION_MASK(PA_CHANNEL_POSITION_TOP_REAR_LEFT))     \

#define PA_CHANNEL_POSITION_MASK_RIGHT                                  \
    (PA_CHANNEL_POSITION_MASK(PA_CHANNEL_POSITION_FRONT_RIGHT)          \
     | PA_CHANNEL_POSITION_MASK(PA_CHANNEL_POSITION_REAR_RIGHT)         \
     | PA_CHANNEL_POSITION_MASK(PA_CHANNEL_POSITION_FRONT_RIGHT_OF_CENTER) \
     | PA_CHANNEL_POSITION_MASK(PA_CHANNEL_POSITION_SIDE_RIGHT)         \
     | PA_CHANNEL_POSITION_MASK(PA_CHANNEL_POSITION_TOP_FRONT_RIGHT)    \
     | PA_CHANNEL_POSITION_MASK(PA_CHANNEL_POSITION_TOP_REAR_RIGHT))

#define PA_CHANNEL_POSITION_MASK_CENTER                                 \
    (PA_CHANNEL_POSITION_MASK(PA_CHANNEL_POSITION_FRONT_CENTER)         \
     | PA_CHANNEL_POSITION_MASK(PA_CHANNEL_POSITION_REAR_CENTER)        \
     | PA_CHANNEL_POSITION_MASK(PA_CHANNEL_POSITION_TOP_CENTER)         \
     | PA_CHANNEL_POSITION_MASK(PA_CHANNEL_POSITION_TOP_FRONT_CENTER)   \
     | PA_CHANNEL_POSITION_MASK(PA_CHANNEL_POSITION_TOP_REAR_CENTER))

#define PA_CHANNEL_POSITION_MASK_FRONT                                  \
    (PA_CHANNEL_POSITION_MASK(PA_CHANNEL_POSITION_FRONT_LEFT)           \
     | PA_CHANNEL_POSITION_MASK(PA_CHANNEL_POSITION_FRONT_RIGHT)        \
     | PA_CHANNEL_POSITION_MASK(PA_CHANNEL_POSITION_FRONT_CENTER)       \
     | PA_CHANNEL_POSITION_MASK(PA_CHANNEL_POSITION_FRONT_LEFT_OF_CENTER) \
     | PA_CHANNEL_POSITION_MASK(PA_CHANNEL_POSITION_FRONT_RIGHT_OF_CENTER) \
     | PA_CHANNEL_POSITION_MASK(PA_CHANNEL_POSITION_TOP_FRONT_LEFT)     \
     | PA_CHANNEL_POSITION_MASK(PA_CHANNEL_POSITION_TOP_FRONT_RIGHT)    \
     | PA_CHANNEL_POSITION_MASK(PA_CHANNEL_POSITION_TOP_FRONT_CENTER))

#define PA_CHANNEL_POSITION_MASK_REAR                                   \
    (PA_CHANNEL_POSITION_MASK(PA_CHANNEL_POSITION_REAR_LEFT)            \
     | PA_CHANNEL_POSITION_MASK(PA_CHANNEL_POSITION_REAR_RIGHT)         \
     | PA_CHANNEL_POSITION_MASK(PA_CHANNEL_POSITION_REAR_CENTER)        \
     | PA_CHANNEL_POSITION_MASK(PA_CHANNEL_POSITION_TOP_REAR_LEFT)      \
     | PA_CHANNEL_POSITION_MASK(PA_CHANNEL_POSITION_TOP_REAR_RIGHT)     \
     | PA_CHANNEL_POSITION_MASK(PA_CHANNEL_POSITION_TOP_REAR_CENTER))

#define PA_CHANNEL_POSITION_MASK_SIDE_OR_TOP_CENTER                     \
    (PA_CHANNEL_POSITION_MASK(PA_CHANNEL_POSITION_SIDE_LEFT)            \
     | PA_CHANNEL_POSITION_MASK(PA_CHANNEL_POSITION_SIDE_RIGHT)         \
     | PA_CHANNEL_POSITION_MASK(PA_CHANNEL_POSITION_TOP_CENTER))

#define PA_CHANNEL_POSITION_MASK_TOP                                    \
    (PA_CHANNEL_POSITION_MASK(PA_CHANNEL_POSITION_TOP_CENTER)           \
     | PA_CHANNEL_POSITION_MASK(PA_CHANNEL_POSITION_TOP_FRONT_LEFT)     \
     | PA_CHANNEL_POSITION_MASK(PA_CHANNEL_POSITION_TOP_FRONT_RIGHT)    \
     | PA_CHANNEL_POSITION_MASK(PA_CHANNEL_POSITION_TOP_FRONT_CENTER)   \
     | PA_CHANNEL_POSITION_MASK(PA_CHANNEL_POSITION_TOP_REAR_LEFT)      \
     | PA_CHANNEL_POSITION_MASK(PA_CHANNEL_POSITION_TOP_REAR_RIGHT)     \
     | PA_CHANNEL_POSITION_MASK(PA_CHANNEL_POSITION_TOP_REAR_CENTER))

#define PA_CHANNEL_POSITION_MASK_ALL            \
    ((pa_channel_position_mask_t) (PA_CHANNEL_POSITION_MASK(PA_CHANNEL_POSITION_MAX)-1))

const char *const channel_name_table[PA_CHANNEL_POSITION_MAX] = {
    [PA_CHANNEL_POSITION_MONO] = _("Mono"),

    [PA_CHANNEL_POSITION_FRONT_CENTER] = _("Front Center"),
    [PA_CHANNEL_POSITION_FRONT_LEFT] = _("Front Left"),
    [PA_CHANNEL_POSITION_FRONT_RIGHT] = _("Front Right"),

    [PA_CHANNEL_POSITION_REAR_CENTER] = _("Rear Center"),
    [PA_CHANNEL_POSITION_REAR_LEFT] = _("Rear Left"),
    [PA_CHANNEL_POSITION_REAR_RIGHT] = _("Rear Right"),

    [PA_CHANNEL_POSITION_LFE] = _("Subwoofer"),

    [PA_CHANNEL_POSITION_FRONT_LEFT_OF_CENTER] = _("Front Left-of-center"),
    [PA_CHANNEL_POSITION_FRONT_RIGHT_OF_CENTER] = _("Front Right-of-center"),

    [PA_CHANNEL_POSITION_SIDE_LEFT] = _("Side Left"),
    [PA_CHANNEL_POSITION_SIDE_RIGHT] = _("Side Right"),

    [PA_CHANNEL_POSITION_AUX0] = _("Auxiliary 0"),
    [PA_CHANNEL_POSITION_AUX1] = _("Auxiliary 1"),
    [PA_CHANNEL_POSITION_AUX2] = _("Auxiliary 2"),
    [PA_CHANNEL_POSITION_AUX3] = _("Auxiliary 3"),
    [PA_CHANNEL_POSITION_AUX4] = _("Auxiliary 4"),
    [PA_CHANNEL_POSITION_AUX5] = _("Auxiliary 5"),
    [PA_CHANNEL_POSITION_AUX6] = _("Auxiliary 6"),
    [PA_CHANNEL_POSITION_AUX7] = _("Auxiliary 7"),
    [PA_CHANNEL_POSITION_AUX8] = _("Auxiliary 8"),
    [PA_CHANNEL_POSITION_AUX9] = _("Auxiliary 9"),
    [PA_CHANNEL_POSITION_AUX10] = _("Auxiliary 10"),
    [PA_CHANNEL_POSITION_AUX11] = _("Auxiliary 11"),
    [PA_CHANNEL_POSITION_AUX12] = _("Auxiliary 12"),
    [PA_CHANNEL_POSITION_AUX13] = _("Auxiliary 13"),
    [PA_CHANNEL_POSITION_AUX14] = _("Auxiliary 14"),
    [PA_CHANNEL_POSITION_AUX15] = _("Auxiliary 15"),
    [PA_CHANNEL_POSITION_AUX16] = _("Auxiliary 16"),
    [PA_CHANNEL_POSITION_AUX17] = _("Auxiliary 17"),
    [PA_CHANNEL_POSITION_AUX18] = _("Auxiliary 18"),
    [PA_CHANNEL_POSITION_AUX19] = _("Auxiliary 19"),
    [PA_CHANNEL_POSITION_AUX20] = _("Auxiliary 20"),
    [PA_CHANNEL_POSITION_AUX21] = _("Auxiliary 21"),
    [PA_CHANNEL_POSITION_AUX22] = _("Auxiliary 22"),
    [PA_CHANNEL_POSITION_AUX23] = _("Auxiliary 23"),
    [PA_CHANNEL_POSITION_AUX24] = _("Auxiliary 24"),
    [PA_CHANNEL_POSITION_AUX25] = _("Auxiliary 25"),
    [PA_CHANNEL_POSITION_AUX26] = _("Auxiliary 26"),
    [PA_CHANNEL_POSITION_AUX27] = _("Auxiliary 27"),
    [PA_CHANNEL_POSITION_AUX28] = _("Auxiliary 28"),
    [PA_CHANNEL_POSITION_AUX29] = _("Auxiliary 29"),
    [PA_CHANNEL_POSITION_AUX30] = _("Auxiliary 30"),
    [PA_CHANNEL_POSITION_AUX31] = _("Auxiliary 31"),

    [PA_CHANNEL_POSITION_TOP_CENTER] = _("Top Center"),

    [PA_CHANNEL_POSITION_TOP_FRONT_CENTER] = _("Top Front Center"),
    [PA_CHANNEL_POSITION_TOP_FRONT_LEFT] = _("Top Front Left"),
    [PA_CHANNEL_POSITION_TOP_FRONT_RIGHT] = _("Top Front Right"),

    [PA_CHANNEL_POSITION_TOP_REAR_CENTER] = _("Top Rear Center"),
    [PA_CHANNEL_POSITION_TOP_REAR_LEFT] = _("Top Rear Left"),
    [PA_CHANNEL_POSITION_TOP_REAR_RIGHT] = _("Top Rear Right")
};

static Eina_Bool on_left(pa_channel_position_t p) {
    return !!(PA_CHANNEL_POSITION_MASK(p) & PA_CHANNEL_POSITION_MASK_LEFT);
}

static Eina_Bool on_right(pa_channel_position_t p) {
    return !!(PA_CHANNEL_POSITION_MASK(p) & PA_CHANNEL_POSITION_MASK_RIGHT);
}
#if 0
static Eina_Bool on_center(pa_channel_position_t p) {
    return !!(PA_CHANNEL_POSITION_MASK(p) & PA_CHANNEL_POSITION_MASK_CENTER);
}

static Eina_Bool on_lfe(pa_channel_position_t p) {
    return p == PA_CHANNEL_POSITION_LFE;
}
#endif
static Eina_Bool on_front(pa_channel_position_t p) {
    return !!(PA_CHANNEL_POSITION_MASK(p) & PA_CHANNEL_POSITION_MASK_FRONT);
}

static Eina_Bool on_rear(pa_channel_position_t p) {
    return !!(PA_CHANNEL_POSITION_MASK(p) & PA_CHANNEL_POSITION_MASK_REAR);
}

static void
pulse_sink_port_info_free(Pulse_Sink_Port_Info *pi)
{
   if (!pi) return;
   eina_stringshare_del(pi->name);
   eina_stringshare_del(pi->description);
   free(pi);
}

void
pulse_sink_free(Pulse_Sink *sink)
{
   Pulse_Sink_Port_Info *pi;
   if (!sink) return;
   if (!sink->deleted)
     {
        sink->deleted = EINA_TRUE;
        if (sink->source)
          {
             eina_hash_del_by_key(pulse_sources, (uintptr_t*)&sink->index);
             return;
          }
        else
          {
             eina_hash_del_by_key(pulse_sinks, (uintptr_t*)&sink->index);
             return;
          }
     }
   eina_stringshare_del(sink->name);
   eina_stringshare_del(sink->description);
   EINA_LIST_FREE(sink->ports, pi)
     pulse_sink_port_info_free(pi);
   eina_stringshare_del(sink->active_port);
   free(sink);
}

const Eina_List *
pulse_sink_ports_get(Pulse_Sink *sink)
{
   EINA_SAFETY_ON_NULL_RETURN_VAL(sink, NULL);
   return sink->ports;
}

const char *
pulse_sink_port_active_get(Pulse_Sink *sink)
{
   EINA_SAFETY_ON_NULL_RETURN_VAL(sink, NULL);
   return sink->active_port;
}

const char *
pulse_sink_name_get(Pulse_Sink *sink)
{
   EINA_SAFETY_ON_NULL_RETURN_VAL(sink, NULL);

   return sink->name;
}

const char *
pulse_sink_desc_get(Pulse_Sink *sink)
{
   EINA_SAFETY_ON_NULL_RETURN_VAL(sink, NULL);

   return sink->description;
}

uint32_t
pulse_sink_idx_get(Pulse_Sink *sink)
{
   EINA_SAFETY_ON_NULL_RETURN_VAL(sink, 0);

   return sink->index;
}

Eina_Bool
pulse_sink_muted_get(Pulse_Sink *sink)
{
   EINA_SAFETY_ON_NULL_RETURN_VAL(sink, EINA_FALSE);

   return sink->mute;
}

uint8_t
pulse_sink_channels_count(Pulse_Sink *sink)
{
   EINA_SAFETY_ON_NULL_RETURN_VAL(sink, 0);

   return sink->volume.channels;
}

double
pulse_sink_avg_get_pct(Pulse_Sink *sink)
{
   double avg;
   int x;

   EINA_SAFETY_ON_NULL_RETURN_VAL(sink, -1.0);

   for (avg = 0, x = 0; x < sink->volume.channels; x++)
     avg += sink->volume.values[x];
   avg /= sink->volume.channels;

   if (avg <= PA_VOLUME_MUTED) return 0.0;

   if (avg == PA_VOLUME_NORM) return 100.0;

   return (avg * 100 + PA_VOLUME_NORM / 2) / PA_VOLUME_NORM;
}

Eina_List *
pulse_sink_channel_names_get(Pulse_Sink *sink)
{
   Eina_List *ret = NULL;
   unsigned int x;

   EINA_SAFETY_ON_NULL_RETURN_VAL(sink, NULL);
   for (x = 0; x < sink->volume.channels; x++)
      ret = eina_list_append(ret, pulse_sink_channel_id_get_name(sink, x));
   return ret;
}

unsigned int
pulse_sink_channel_name_get_id(Pulse_Sink *sink, const char *name)
{
   unsigned int x;

   EINA_SAFETY_ON_NULL_RETURN_VAL(sink, UINT_MAX);
   EINA_SAFETY_ON_NULL_RETURN_VAL(name, UINT_MAX);
   for (x = 0; x < sink->channel_map.channels; x++)
     {
        if (!strcmp(name, channel_name_table[sink->channel_map.map[x]]))
           return x;
     }
   return UINT_MAX;
}

const char *
pulse_sink_channel_id_get_name(Pulse_Sink *sink, unsigned int id)
{
   EINA_SAFETY_ON_NULL_RETURN_VAL(sink, NULL);
   EINA_SAFETY_ON_TRUE_RETURN_VAL(id >= sink->channel_map.channels, NULL);
   return eina_stringshare_add(channel_name_table[sink->channel_map.map[id]]);
}

double
pulse_sink_channel_volume_get(Pulse_Sink *sink, unsigned int id)
{
   EINA_SAFETY_ON_NULL_RETURN_VAL(sink, -1);
   EINA_SAFETY_ON_TRUE_RETURN_VAL(id >= sink->channel_map.channels, -1);
   return (sink->volume.values[id] * 100 + PA_VOLUME_NORM / 2) / PA_VOLUME_NORM;
}

float
pulse_sink_channel_balance_get(Pulse_Sink *sink, unsigned int id)
{
   EINA_SAFETY_ON_NULL_RETURN_VAL(sink, -1.0);
   EINA_SAFETY_ON_TRUE_RETURN_VAL(id >= sink->channel_map.channels, -1.0);

   if (on_left(sink->channel_map.map[id])) return 0.0;
   if (on_right(sink->channel_map.map[id])) return 1.0;
   return 0.5;
}

float
pulse_sink_channel_depth_get(Pulse_Sink *sink, unsigned int id)
{
   EINA_SAFETY_ON_NULL_RETURN_VAL(sink, -1.0);
   EINA_SAFETY_ON_TRUE_RETURN_VAL(id >= sink->channel_map.channels, -1.0);

   if (on_rear(sink->channel_map.map[id])) return 0.0;
   if (on_front(sink->channel_map.map[id])) return 1.0;
   return 0.5;
}

float
pulse_sink_balance_get(Pulse_Sink *sink)
{
    int c;
    float l, r;
    pa_volume_t left = 0, right = 0;
    unsigned n_left = 0, n_right = 0;

    for (c = 0; c < sink->channel_map.channels; c++) {
        if (on_left(sink->channel_map.map[c])) {
            left += sink->volume.values[c];
            n_left++;
        } else if (on_right(sink->channel_map.map[c])) {
            right += sink->volume.values[c];
            n_right++;
        }
    }

    if (n_left <= 0)
        l = PA_VOLUME_NORM;
    else
        l = left / n_left;

    if (n_right <= 0)
        r = PA_VOLUME_NORM;
    else
        r = right / n_right;
    l /= (float)PA_VOLUME_NORM;
    r /= (float)PA_VOLUME_NORM;
    return r - l;
}