2 This file is part of PulseAudio.
4 This module is based off Lennart Poettering's LADSPA sink and swaps out
5 LADSPA functionality for a dbus-aware STFT OLA based digital equalizer.
6 All new work is published under Pulseaudio's original license.
7 Copyright 2009 Jason Newton <nevion@gmail.com>
10 Copyright 2004-2008 Lennart Poettering
12 PulseAudio is free software; you can redistribute it and/or modify
13 it under the terms of the GNU Lesser General Public License as published
14 by the Free Software Foundation; either version 2.1 of the License,
15 or (at your option) any later version.
17 PulseAudio is distributed in the hope that it will be useful, but
18 WITHOUT ANY WARRANTY; without even the implied warranty of
19 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
20 General Public License for more details.
22 You should have received a copy of the GNU Lesser General Public License
23 along with PulseAudio; if not, write to the Free Software
24 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
39 #include <pulse/xmalloc.h>
40 #include <pulse/i18n.h>
41 #include <pulse/timeval.h>
43 #include <pulsecore/core-rtclock.h>
44 #include <pulsecore/aupdate.h>
45 #include <pulsecore/core-error.h>
46 #include <pulsecore/namereg.h>
47 #include <pulsecore/sink.h>
48 #include <pulsecore/module.h>
49 #include <pulsecore/core-util.h>
50 #include <pulsecore/modargs.h>
51 #include <pulsecore/log.h>
52 #include <pulsecore/thread.h>
53 #include <pulsecore/thread-mq.h>
54 #include <pulsecore/rtpoll.h>
55 #include <pulsecore/sample-util.h>
56 #include <pulsecore/shared.h>
57 #include <pulsecore/idxset.h>
58 #include <pulsecore/strlist.h>
59 #include <pulsecore/database.h>
60 #include <pulsecore/protocol-dbus.h>
61 #include <pulsecore/dbus-util.h>
69 #include <xmmintrin.h>
70 #include <emmintrin.h>
75 #include "module-equalizer-sink-symdef.h"
77 PA_MODULE_AUTHOR("Jason Newton");
78 PA_MODULE_DESCRIPTION(_("General Purpose Equalizer"));
79 PA_MODULE_VERSION(PACKAGE_VERSION
);
80 PA_MODULE_LOAD_ONCE(FALSE
);
81 PA_MODULE_USAGE(_("sink=<sink to connect to> "));
83 #define MEMBLOCKQ_MAXLENGTH (16*1024*1024)
89 pa_sink_input
*sink_input
;
93 size_t fft_size
;//length (res) of fft
96 *effectively chooses R
98 size_t R
;/* the hop size between overlapping windows
99 * the latency of the filter, calculated from window_size
100 * based on constraints of COLA and window function
102 //for twiddling with pulseaudio
103 size_t overlap_size
;//window_size-R
104 size_t samples_gathered
;
105 size_t input_buffer_max
;
107 float *W
;//windowing function (time domain)
108 float *work_buffer
, **input
, **overlap_accum
;
109 fftwf_complex
*output_window
;
110 fftwf_plan forward_plan
, inverse_plan
;
114 float ***Hs
;//thread updatable copies of the freq response filters (magintude based)
116 pa_memchunk conv_buffer
;
117 pa_memblockq
*input_q
;
118 pa_bool_t first_iteration
;
120 pa_dbus_protocol
*dbus_protocol
;
122 pa_bool_t set_default
;
124 pa_database
*database
;
125 char **base_profiles
;
128 static const char* const valid_modargs
[] = {
142 #define SINKLIST "equalized_sinklist"
143 #define EQDB "equalizer_db"
144 #define EQ_STATE_DB "equalizer-state"
145 #define FILTER_SIZE (u->fft_size / 2 + 1)
146 #define CHANNEL_PROFILE_SIZE (FILTER_SIZE + 1)
147 #define FILTER_STATE_SIZE (CHANNEL_PROFILE_SIZE * u->channels)
148 static void dbus_init(struct userdata
*u
);
149 static void dbus_done(struct userdata
*u
);
151 static void hanning_window(float *W
, size_t window_size
){
152 //h=.5*(1-cos(2*pi*j/(window_size+1)), COLA for R=(M+1)/2
153 for(size_t i
=0; i
< window_size
;++i
){
154 W
[i
] = (float).5*(1-cos(2*M_PI
*i
/(window_size
+1)));
158 static void fix_filter(float *H
, size_t fft_size
){
159 //divide out the fft gain
160 for(size_t i
= 0; i
< fft_size
/ 2 + 1; ++i
){
165 static void interpolate(float *signal
, size_t length
, uint32_t *xs
, float *ys
, size_t n_points
){
166 //Note that xs must be monotonically increasing!
167 float x_range_lower
, x_range_upper
, c0
;
168 pa_assert_se(n_points
>=2);
169 pa_assert_se(xs
[0] == 0);
170 pa_assert_se(xs
[n_points
- 1] == length
- 1);
171 for(size_t x
= 0, x_range_lower_i
= 0; x
< length
-1; ++x
){
172 pa_assert(x_range_lower_i
< n_points
-1);
173 x_range_lower
= (float) (xs
[x_range_lower_i
]);
174 x_range_upper
= (float) (xs
[x_range_lower_i
+1]);
175 pa_assert_se(x_range_lower
< x_range_upper
);
176 pa_assert_se(x
>= x_range_lower
);
177 pa_assert_se(x
<= x_range_upper
);
178 //bilinear-interpolation of coefficients specified
179 c0
= (x
-x_range_lower
)/(x_range_upper
-x_range_lower
);
180 pa_assert_se(c0
>= 0&&c0
<= 1.0);
181 signal
[x
] = ((1.0f
- c0
) * ys
[x_range_lower_i
] + c0
* ys
[x_range_lower_i
+ 1]);
182 while(x
>= xs
[x_range_lower_i
+ 1]){
186 signal
[length
-1]=ys
[n_points
-1];
189 static int is_monotonic(const uint32_t *xs
,size_t length
){
193 for(size_t i
= 1; i
< length
; ++i
){
201 //ensure's memory allocated is a multiple of v_size
203 static void * alloc(size_t x
,size_t s
){
204 size_t f
= PA_ROUND_UP(x
*s
, sizeof(float)*v_size
);
212 static void alloc_input_buffers(struct userdata
*u
, size_t min_buffer_length
){
213 if(min_buffer_length
<= u
->input_buffer_max
){
216 pa_assert(min_buffer_length
>= u
->window_size
);
217 for(size_t c
= 0; c
< u
->channels
; ++c
){
218 float *tmp
= alloc(min_buffer_length
, sizeof(float));
220 if(!u
->first_iteration
){
221 memcpy(tmp
, u
->input
[c
], u
->overlap_size
* sizeof(float));
227 u
->input_buffer_max
= min_buffer_length
;
230 /* Called from I/O thread context */
231 static int sink_process_msg_cb(pa_msgobject
*o
, int code
, void *data
, int64_t offset
, pa_memchunk
*chunk
) {
232 struct userdata
*u
= PA_SINK(o
)->userdata
;
236 case PA_SINK_MESSAGE_GET_LATENCY
: {
237 //size_t fs=pa_frame_size(&u->sink->sample_spec);
239 /* The sink is _put() before the sink input is, so let's
240 * make sure we don't access it in that time. Also, the
241 * sink input is first shut down, the sink second. */
242 if (!PA_SINK_IS_LINKED(u
->sink
->thread_info
.state
) ||
243 !PA_SINK_INPUT_IS_LINKED(u
->sink_input
->thread_info
.state
)) {
244 *((pa_usec_t
*) data
) = 0;
248 *((pa_usec_t
*) data
) =
249 /* Get the latency of the master sink */
250 pa_sink_get_latency_within_thread(u
->sink_input
->sink
) +
252 /* Add the latency internal to our sink input on top */
253 pa_bytes_to_usec(pa_memblockq_get_length(u
->sink_input
->thread_info
.render_memblockq
), &u
->sink_input
->sink
->sample_spec
);
254 // pa_bytes_to_usec(u->samples_gathered * fs, &u->sink->sample_spec);
255 //+ pa_bytes_to_usec(u->latency * fs, ss)
256 //+ pa_bytes_to_usec(pa_memblockq_get_length(u->input_q), ss);
261 return pa_sink_process_msg(o
, code
, data
, offset
, chunk
);
265 /* Called from main context */
266 static int sink_set_state_cb(pa_sink
*s
, pa_sink_state_t state
) {
269 pa_sink_assert_ref(s
);
270 pa_assert_se(u
= s
->userdata
);
272 if (!PA_SINK_IS_LINKED(state
) ||
273 !PA_SINK_INPUT_IS_LINKED(pa_sink_input_get_state(u
->sink_input
)))
276 pa_sink_input_cork(u
->sink_input
, state
== PA_SINK_SUSPENDED
);
280 /* Called from I/O thread context */
281 static void sink_request_rewind_cb(pa_sink
*s
) {
284 pa_sink_assert_ref(s
);
285 pa_assert_se(u
= s
->userdata
);
287 if (!PA_SINK_IS_LINKED(u
->sink
->thread_info
.state
) ||
288 !PA_SINK_INPUT_IS_LINKED(u
->sink_input
->thread_info
.state
))
291 /* Just hand this one over to the master sink */
292 pa_sink_input_request_rewind(u
->sink_input
, s
->thread_info
.rewind_nbytes
+pa_memblockq_get_length(u
->input_q
), TRUE
, FALSE
, FALSE
);
295 /* Called from I/O thread context */
296 static void sink_update_requested_latency_cb(pa_sink
*s
) {
299 pa_sink_assert_ref(s
);
300 pa_assert_se(u
= s
->userdata
);
302 if (!PA_SINK_IS_LINKED(u
->sink
->thread_info
.state
) ||
303 !PA_SINK_INPUT_IS_LINKED(u
->sink_input
->thread_info
.state
))
306 /* Just hand this one over to the master sink */
307 pa_sink_input_set_requested_latency_within_thread(
309 pa_sink_get_requested_latency_within_thread(s
));
312 /* Called from main context */
313 static void sink_set_volume_cb(pa_sink
*s
) {
316 pa_sink_assert_ref(s
);
317 pa_assert_se(u
= s
->userdata
);
319 if (!PA_SINK_IS_LINKED(pa_sink_get_state(s
)) ||
320 !PA_SINK_INPUT_IS_LINKED(pa_sink_input_get_state(u
->sink_input
)))
323 pa_sink_input_set_volume(u
->sink_input
, &s
->real_volume
, s
->save_volume
, TRUE
);
326 /* Called from main context */
327 static void sink_set_mute_cb(pa_sink
*s
) {
330 pa_sink_assert_ref(s
);
331 pa_assert_se(u
= s
->userdata
);
333 if (!PA_SINK_IS_LINKED(pa_sink_get_state(s
)) ||
334 !PA_SINK_INPUT_IS_LINKED(pa_sink_input_get_state(u
->sink_input
)))
337 pa_sink_input_set_mute(u
->sink_input
, s
->muted
, s
->save_muted
);
341 //reference implementation
342 static void dsp_logic(
343 float * restrict dst
,//used as a temp array too, needs to be fft_length!
344 float * restrict src
,/*input data w/ overlap at start,
345 *automatically cycled in routine
347 float * restrict overlap
,
348 const float X
,//multipliar
349 const float * restrict H
,//The freq. magnitude scalers filter
350 const float * restrict W
,//The windowing function
351 fftwf_complex
* restrict output_window
,//The transformed window'd src
353 //use a linear-phase sliding STFT and overlap-add method (for each channel)
355 for(size_t j
= 0; j
< u
->window_size
; ++j
){
356 dst
[j
] = X
* W
[j
] * src
[j
];
358 //zero padd the the remaining fft window
359 memset(dst
+ u
->window_size
, 0, (u
->fft_size
- u
->window_size
) * sizeof(float));
360 //Processing is done here!
362 fftwf_execute_dft_r2c(u
->forward_plan
, dst
, output_window
);
364 for(size_t j
= 0; j
< FILTER_SIZE
; ++j
){
365 u
->output_window
[j
][0] *= H
[j
];
366 u
->output_window
[j
][1] *= H
[j
];
369 fftwf_execute_dft_c2r(u
->inverse_plan
, output_window
, dst
);
370 ////debug: tests overlaping add
371 ////and negates ALL PREVIOUS processing
372 ////yields a perfect reconstruction if COLA is held
373 //for(size_t j = 0; j < u->window_size; ++j){
374 // u->work_buffer[j] = u->W[j] * u->input[c][j];
377 //overlap add and preserve overlap component from this window (linear phase)
378 for(size_t j
= 0; j
< u
->overlap_size
; ++j
){
379 u
->work_buffer
[j
] += overlap
[j
];
380 overlap
[j
] = dst
[u
->R
+ j
];
382 ////debug: tests if basic buffering works
383 ////shouldn't modify the signal AT ALL (beyond roundoff)
384 //for(size_t j = 0; j < u->window_size;++j){
385 // u->work_buffer[j] = u->input[c][j];
388 //preseve the needed input for the next window's overlap
389 memmove(src
, src
+ u
->R
,
390 (u
->samples_gathered
- u
->R
) * sizeof(float)
394 typedef float v4sf
__attribute__ ((__aligned__(v_size
* sizeof(float))));
395 typedef union float_vector
{
401 //regardless of sse enabled, the loops in here assume
402 //16 byte aligned addresses and memory allocations divisible by v_size
403 static void dsp_logic(
404 float * restrict dst
,//used as a temp array too, needs to be fft_length!
405 float * restrict src
,/*input data w/ overlap at start,
406 *automatically cycled in routine
408 float * restrict overlap
,//The size of the overlap
409 const float X
,//multipliar
410 const float * restrict H
,//The freq. magnitude scalers filter
411 const float * restrict W
,//The windowing function
412 fftwf_complex
* restrict output_window
,//The transformed window'd src
413 struct userdata
*u
){//Collection of constants
414 const size_t overlap_size
= PA_ROUND_UP(u
->overlap_size
, v_size
);
417 //assert(u->samples_gathered >= u->R);
418 //use a linear-phase sliding STFT and overlap-add method
419 for(size_t j
= 0; j
< u
->window_size
; j
+= v_size
){
420 //dst[j] = W[j] * src[j];
421 float_vector_t
*d
= (float_vector_t
*) (dst
+ j
);
422 float_vector_t
*w
= (float_vector_t
*) (W
+ j
);
423 float_vector_t
*s
= (float_vector_t
*) (src
+ j
);
425 d
->m
= _mm_mul_ps(w
->m
, s
->m
);
427 // d->v = w->v * s->v;
430 //zero padd the the remaining fft window
431 memset(dst
+ u
->window_size
, 0, (u
->fft_size
- u
->window_size
) * sizeof(float));
433 //Processing is done here!
435 fftwf_execute_dft_r2c(u
->forward_plan
, dst
, output_window
);
436 //perform filtering - purely magnitude based
437 for(size_t j
= 0; j
< FILTER_SIZE
; j
+= v_size
/ 2){
438 //output_window[j][0]*=H[j];
439 //output_window[j][1]*=H[j];
440 float_vector_t
*d
= (float_vector_t
*)( ((float *) output_window
) + 2 * j
);
442 h
.f
[0] = h
.f
[1] = H
[j
];
443 h
.f
[2] = h
.f
[3] = H
[j
+ 1];
445 d
->m
= _mm_mul_ps(d
->m
, h
.m
);
447 // d->v = d->v * h.v;
452 fftwf_execute_dft_c2r(u
->inverse_plan
, output_window
, dst
);
454 ////debug: tests overlaping add
455 ////and negates ALL PREVIOUS processing
456 ////yields a perfect reconstruction if COLA is held
457 //for(size_t j = 0; j < u->window_size; ++j){
458 // dst[j] = W[j] * src[j];
461 //overlap add and preserve overlap component from this window (linear phase)
462 for(size_t j
= 0; j
< overlap_size
; j
+= v_size
){
463 //dst[j]+=overlap[j];
464 //overlap[j]+=dst[j+R];
465 float_vector_t
*d
= (float_vector_t
*)(dst
+ j
);
466 float_vector_t
*o
= (float_vector_t
*)(overlap
+ j
);
468 d
->m
= _mm_add_ps(d
->m
, o
->m
);
469 o
->m
= ((float_vector_t
*)(dst
+ u
->R
+ j
))->m
;
471 // d->v = d->v + o->v;
472 // o->v = ((float_vector_t*)(dst + u->R + j))->v;
475 //memcpy(overlap, dst+u->R, u->overlap_size * sizeof(float)); //overlap preserve (debug)
476 //zero out the bit beyond the real overlap so we don't add garbage next iteration
477 memset(overlap
+ u
->overlap_size
, 0, overlap_size
- u
->overlap_size
);
479 ////debug: tests if basic buffering works
480 ////shouldn't modify the signal AT ALL (beyond roundoff)
481 //for(size_t j = 0; j < u->window_size; ++j){
485 //preseve the needed input for the next window's overlap
486 memmove(src
, src
+ u
->R
,
487 (u
->samples_gathered
- u
->R
) * sizeof(float)
492 static void process_samples(struct userdata
*u
, pa_memchunk
*tchunk
){
493 size_t fs
= pa_frame_size(&(u
->sink
->sample_spec
));
497 size_t iterations
, offset
;
498 pa_assert(u
->samples_gathered
>= u
->window_size
);
499 iterations
= (u
->samples_gathered
- u
->overlap_size
) / u
->R
;
501 tchunk
->length
= iterations
* u
->R
* fs
;
502 tchunk
->memblock
= pa_memblock_new(u
->sink
->core
->mempool
, tchunk
->length
);
503 dst
= ((float*) pa_memblock_acquire(tchunk
->memblock
));
504 for(size_t iter
= 0; iter
< iterations
; ++iter
){
505 offset
= iter
* u
->R
* fs
;
506 for(size_t c
= 0;c
< u
->channels
; c
++) {
507 a_i
= pa_aupdate_read_begin(u
->a_H
[c
]);
520 pa_aupdate_read_end(u
->a_H
[c
]);
521 if(u
->first_iteration
){
522 /* The windowing function will make the audio ramped in, as a cheap fix we can
523 * undo the windowing (for non-zero window values)
525 for(size_t i
= 0; i
< u
->overlap_size
; ++i
){
526 u
->work_buffer
[i
] = u
->W
[i
] <= FLT_EPSILON
? u
->work_buffer
[i
] : u
->work_buffer
[i
] / u
->W
[i
];
529 pa_sample_clamp(PA_SAMPLE_FLOAT32NE
, (uint8_t *) (dst
+ c
) + offset
, fs
, u
->work_buffer
, sizeof(float), u
->R
);
531 if(u
->first_iteration
){
532 u
->first_iteration
= FALSE
;
534 u
->samples_gathered
-= u
->R
;
536 pa_memblock_release(tchunk
->memblock
);
539 static void input_buffer(struct userdata
*u
, pa_memchunk
*in
){
540 size_t fs
= pa_frame_size(&(u
->sink
->sample_spec
));
541 size_t samples
= in
->length
/fs
;
542 float *src
= (float*) ((uint8_t*) pa_memblock_acquire(in
->memblock
) + in
->index
);
543 pa_assert(u
->samples_gathered
+ samples
<= u
->input_buffer_max
);
544 for(size_t c
= 0; c
< u
->channels
; c
++) {
545 //buffer with an offset after the overlap from previous
548 u
->input
[c
] + u
->samples_gathered
+ samples
<= u
->input
[c
] + u
->input_buffer_max
550 pa_sample_clamp(PA_SAMPLE_FLOAT32NE
, u
->input
[c
] + u
->samples_gathered
, sizeof(float), src
+ c
, fs
, samples
);
552 u
->samples_gathered
+= samples
;
553 pa_memblock_release(in
->memblock
);
556 /* Called from I/O thread context */
557 static int sink_input_pop_cb(pa_sink_input
*i
, size_t nbytes
, pa_memchunk
*chunk
) {
559 size_t fs
, target_samples
;
560 struct timeval start
, end
;
562 pa_sink_input_assert_ref(i
);
563 pa_assert_se(u
= i
->userdata
);
566 fs
= pa_frame_size(&(u
->sink
->sample_spec
));
567 target_samples
= PA_ROUND_UP(nbytes
/ fs
, u
->R
);
568 if(u
->first_iteration
){
569 //allocate request_size
570 target_samples
= PA_MAX(target_samples
, u
->window_size
);
572 //allocate request_size + overlap
573 target_samples
+= u
->overlap_size
;
574 alloc_input_buffers(u
, target_samples
);
576 alloc_input_buffers(u
, target_samples
);
577 chunk
->memblock
= NULL
;
579 /* Hmm, process any rewind request that might be queued up */
580 pa_sink_process_rewind(u
->sink
, 0);
582 //pa_log_debug("start output-buffered %ld, input-buffered %ld, requested %ld",buffered_samples,u->samples_gathered,samples_requested);
583 pa_rtclock_get(&start
);
585 size_t input_remaining
= target_samples
- u
->samples_gathered
;
586 pa_assert(input_remaining
> 0);
587 while(pa_memblockq_peek(u
->input_q
, &tchunk
) < 0){
588 //pa_sink_render(u->sink, input_remaining * fs, &tchunk);
589 pa_sink_render_full(u
->sink
, input_remaining
* fs
, &tchunk
);
590 pa_assert(tchunk
.memblock
);
591 pa_memblockq_push(u
->input_q
, &tchunk
);
592 pa_memblock_unref(tchunk
.memblock
);
594 pa_assert(tchunk
.memblock
);
595 tchunk
.length
= PA_MIN(input_remaining
* fs
, tchunk
.length
);
596 pa_memblockq_drop(u
->input_q
, tchunk
.length
);
597 //pa_log_debug("asked for %ld input samples, got %ld samples",input_remaining,buffer->length/fs);
599 //pa_rtclock_get(start);
600 input_buffer(u
, &tchunk
);
601 //pa_rtclock_get(&end);
602 //pa_log_debug("Took %0.5f seconds to setup", pa_timeval_diff(end, start) / (double) PA_USEC_PER_SEC);
603 pa_memblock_unref(tchunk
.memblock
);
604 }while(u
->samples_gathered
< target_samples
);
606 pa_rtclock_get(&end
);
607 pa_log_debug("Took %0.6f seconds to get data", (double) pa_timeval_diff(&end
, &start
) / PA_USEC_PER_SEC
);
609 pa_assert(u
->fft_size
>= u
->window_size
);
610 pa_assert(u
->R
< u
->window_size
);
611 /* set the H filter */
612 pa_rtclock_get(&start
);
613 /* process a block */
614 process_samples(u
, chunk
);
615 pa_rtclock_get(&end
);
616 pa_log_debug("Took %0.6f seconds to process", (double) pa_timeval_diff(&end
, &start
) / PA_USEC_PER_SEC
);
618 pa_assert(chunk
->memblock
);
619 //pa_log_debug("gave %ld", chunk->length/fs);
620 //pa_log_debug("end pop");
624 /* Called from main context */
625 static void sink_input_volume_changed_cb(pa_sink_input
*i
) {
628 pa_sink_input_assert_ref(i
);
629 pa_assert_se(u
= i
->userdata
);
631 pa_sink_volume_changed(u
->sink
, &i
->volume
);
634 /* Called from main context */
635 static void sink_input_mute_changed_cb(pa_sink_input
*i
) {
638 pa_sink_input_assert_ref(i
);
639 pa_assert_se(u
= i
->userdata
);
641 pa_sink_mute_changed(u
->sink
, i
->muted
);
644 static void reset_filter(struct userdata
*u
){
645 size_t fs
= pa_frame_size(&u
->sink
->sample_spec
);
647 u
->samples_gathered
= 0;
648 for(size_t i
= 0; i
< u
->channels
; ++i
){
649 memset(u
->overlap_accum
[i
], 0, u
->overlap_size
* sizeof(float));
651 u
->first_iteration
= TRUE
;
652 //set buffer size to max request, no overlap copy
653 max_request
= PA_ROUND_UP(pa_sink_input_get_max_request(u
->sink_input
) / fs
, u
->R
);
654 max_request
= PA_MAX(max_request
, u
->window_size
);
655 pa_sink_set_max_request_within_thread(u
->sink
, max_request
* fs
);
658 /* Called from I/O thread context */
659 static void sink_input_process_rewind_cb(pa_sink_input
*i
, size_t nbytes
) {
663 pa_log_debug("Rewind callback!");
664 pa_sink_input_assert_ref(i
);
665 pa_assert_se(u
= i
->userdata
);
667 if (u
->sink
->thread_info
.rewind_nbytes
> 0) {
670 //max_rewrite = nbytes;
671 max_rewrite
= nbytes
+ pa_memblockq_get_length(u
->input_q
);
672 //PA_MIN(pa_memblockq_get_length(u->input_q), nbytes);
673 amount
= PA_MIN(u
->sink
->thread_info
.rewind_nbytes
, max_rewrite
);
674 u
->sink
->thread_info
.rewind_nbytes
= 0;
677 //invalidate the output q
678 pa_memblockq_seek(u
->input_q
, - (int64_t) amount
, PA_SEEK_RELATIVE
, TRUE
);
679 pa_log("Resetting filter");
680 //reset_filter(u); //this is the "proper" thing to do...
684 pa_sink_process_rewind(u
->sink
, amount
);
685 pa_memblockq_rewind(u
->input_q
, nbytes
);
688 /* Called from I/O thread context */
689 static void sink_input_update_max_rewind_cb(pa_sink_input
*i
, size_t nbytes
) {
692 pa_sink_input_assert_ref(i
);
693 pa_assert_se(u
= i
->userdata
);
695 pa_memblockq_set_maxrewind(u
->input_q
, nbytes
);
696 pa_sink_set_max_rewind_within_thread(u
->sink
, nbytes
);
699 /* Called from I/O thread context */
700 static void sink_input_update_max_request_cb(pa_sink_input
*i
, size_t nbytes
) {
703 pa_sink_input_assert_ref(i
);
704 pa_assert_se(u
= i
->userdata
);
705 //if(u->first_iteration){
708 fs
= pa_frame_size(&(u
->sink
->sample_spec
));
709 pa_sink_set_max_request_within_thread(u
->sink
, PA_ROUND_UP(nbytes
/ fs
, u
->R
) * fs
);
712 /* Called from I/O thread context */
713 static void sink_input_update_sink_latency_range_cb(pa_sink_input
*i
) {
716 pa_sink_input_assert_ref(i
);
717 pa_assert_se(u
= i
->userdata
);
719 pa_sink_set_latency_range_within_thread(u
->sink
, i
->sink
->thread_info
.min_latency
, i
->sink
->thread_info
.max_latency
);
722 /* Called from I/O thread context */
723 static void sink_input_update_sink_fixed_latency_cb(pa_sink_input
*i
) {
726 pa_sink_input_assert_ref(i
);
727 pa_assert_se(u
= i
->userdata
);
729 pa_sink_set_fixed_latency_within_thread(u
->sink
, i
->sink
->thread_info
.fixed_latency
);
732 /* Called from I/O thread context */
733 static void sink_input_detach_cb(pa_sink_input
*i
) {
736 pa_sink_input_assert_ref(i
);
737 pa_assert_se(u
= i
->userdata
);
739 pa_sink_detach_within_thread(u
->sink
);
741 pa_sink_set_rtpoll(u
->sink
, NULL
);
744 /* Called from I/O thread context */
745 static void sink_input_attach_cb(pa_sink_input
*i
) {
747 size_t fs
, max_request
;
748 pa_sink_input_assert_ref(i
);
749 pa_assert_se(u
= i
->userdata
);
751 pa_sink_set_rtpoll(u
->sink
, i
->sink
->thread_info
.rtpoll
);
752 pa_sink_set_latency_range_within_thread(u
->sink
, i
->sink
->thread_info
.min_latency
, i
->sink
->thread_info
.max_latency
);
754 pa_sink_set_fixed_latency_within_thread(u
->sink
, i
->sink
->thread_info
.fixed_latency
);
755 fs
= pa_frame_size(&u
->sink
->sample_spec
);
756 //set buffer size to max request, no overlap copy
757 max_request
= PA_ROUND_UP(pa_sink_input_get_max_request(u
->sink_input
) / fs
, u
->R
);
758 max_request
= PA_MAX(max_request
, u
->window_size
);
759 pa_sink_set_max_request_within_thread(u
->sink
, max_request
* fs
);
760 pa_sink_set_max_rewind_within_thread(u
->sink
, pa_sink_input_get_max_rewind(i
));
761 pa_sink_attach_within_thread(u
->sink
);
763 pa_log_debug("Setting default sink to %s", u
->sink
->name
);
764 pa_namereg_set_default_sink(u
->module
->core
, u
->sink
);
768 /* Called from main context */
769 static void sink_input_kill_cb(pa_sink_input
*i
) {
772 pa_sink_input_assert_ref(i
);
773 pa_assert_se(u
= i
->userdata
);
775 /* The order here matters! We first kill the sink input, followed
776 * by the sink. That means the sink callbacks must be protected
777 * against an unconnected sink input! */
778 pa_sink_input_unlink(u
->sink_input
);
779 pa_sink_unlink(u
->sink
);
781 pa_sink_input_unref(u
->sink_input
);
782 u
->sink_input
= NULL
;
784 pa_sink_unref(u
->sink
);
787 pa_module_unload_request(u
->module
, TRUE
);
790 /* Called from IO thread context */
791 static void sink_input_state_change_cb(pa_sink_input
*i
, pa_sink_input_state_t state
) {
794 pa_sink_input_assert_ref(i
);
795 pa_assert_se(u
= i
->userdata
);
797 /* If we are added for the first time, ask for a rewinding so that
798 * we are heard right-away. */
799 if (PA_SINK_INPUT_IS_LINKED(state
) &&
800 i
->thread_info
.state
== PA_SINK_INPUT_INIT
) {
801 pa_log_debug("Requesting rewind due to state change.");
802 pa_sink_input_request_rewind(i
, 0, FALSE
, TRUE
, TRUE
);
806 static void pack(char **strs
, size_t len
, char **packed
, size_t *length
){
808 size_t headers
= (1+len
) * sizeof(uint16_t);
810 for(size_t i
= 0; i
< len
; ++i
){
811 t_len
+= strlen(strs
[i
]);
813 *length
= headers
+ t_len
;
814 p
= *packed
= pa_xmalloc0(*length
);
815 *((uint16_t *) p
) = (uint16_t) len
;
816 p
+= sizeof(uint16_t);
817 for(size_t i
= 0; i
< len
; ++i
){
818 uint16_t l
= strlen(strs
[i
]);
819 *((uint16_t *) p
) = (uint16_t) l
;
820 p
+= sizeof(uint16_t);
821 memcpy(p
, strs
[i
], l
);
825 static void unpack(char *str
, size_t length
, char ***strs
, size_t *len
){
827 *len
= *((uint16_t *) p
);
828 p
+= sizeof(uint16_t);
829 *strs
= pa_xnew(char *, *len
);
831 for(size_t i
= 0; i
< *len
; ++i
){
832 size_t l
= *((uint16_t *) p
);
833 p
+= sizeof(uint16_t);
834 (*strs
)[i
] = pa_xnew(char, l
+ 1);
835 memcpy((*strs
)[i
], p
, l
);
836 (*strs
)[i
][l
] = '\0';
840 static void save_profile(struct userdata
*u
, size_t channel
, char *name
){
842 const size_t profile_size
= CHANNEL_PROFILE_SIZE
* sizeof(float);
843 float *H_n
, *profile
;
846 profile
= pa_xnew0(float, profile_size
);
847 a_i
= pa_aupdate_read_begin(u
->a_H
[channel
]);
848 profile
[0] = u
->Xs
[a_i
][channel
];
849 H
= u
->Hs
[channel
][a_i
];
851 for(size_t i
= 0 ; i
<= FILTER_SIZE
; ++i
){
852 H_n
[i
] = H
[i
] * u
->fft_size
;
855 pa_aupdate_read_end(u
->a_H
[channel
]);
857 key
.size
= strlen(key
.data
);
859 data
.size
= profile_size
;
860 pa_database_set(u
->database
, &key
, &data
, TRUE
);
861 pa_database_sync(u
->database
);
862 if(u
->base_profiles
[channel
]){
863 pa_xfree(u
->base_profiles
[channel
]);
865 u
->base_profiles
[channel
] = pa_xstrdup(name
);
868 static void save_state(struct userdata
*u
){
870 const size_t filter_state_size
= FILTER_STATE_SIZE
* sizeof(float);
874 pa_database
*database
;
876 char *state_name
= u
->name
;
878 size_t packed_length
;
880 pack(u
->base_profiles
, u
->channels
, &packed
, &packed_length
);
881 state
= (float *) pa_xmalloc0(filter_state_size
+ packed_length
);
882 memcpy(state
+ FILTER_STATE_SIZE
, packed
, packed_length
);
885 for(size_t c
= 0; c
< u
->channels
; ++c
){
886 a_i
= pa_aupdate_read_begin(u
->a_H
[c
]);
887 state
[c
* CHANNEL_PROFILE_SIZE
] = u
->Xs
[c
][a_i
];
889 H_n
= &state
[c
* CHANNEL_PROFILE_SIZE
+ 1];
890 memcpy(H_n
, H
, FILTER_SIZE
* sizeof(float));
891 pa_aupdate_read_end(u
->a_H
[c
]);
894 key
.data
= state_name
;
895 key
.size
= strlen(key
.data
);
897 data
.size
= filter_state_size
+ packed_length
;
898 //thread safety for 0.9.17?
899 pa_assert_se(dbname
= pa_state_path(EQ_STATE_DB
, FALSE
));
900 pa_assert_se(database
= pa_database_open(dbname
, TRUE
));
903 pa_database_set(database
, &key
, &data
, TRUE
);
904 pa_database_sync(database
);
905 pa_database_close(database
);
909 static void remove_profile(pa_core
*c
, char *name
){
911 pa_database
*database
;
913 key
.size
= strlen(key
.data
);
914 pa_assert_se(database
= pa_shared_get(c
, EQDB
));
915 pa_database_unset(database
, &key
);
916 pa_database_sync(database
);
919 static const char* load_profile(struct userdata
*u
, size_t channel
, char *name
){
922 const size_t profile_size
= CHANNEL_PROFILE_SIZE
* sizeof(float);
924 key
.size
= strlen(key
.data
);
925 if(pa_database_get(u
->database
, &key
, &value
) != NULL
){
926 if(value
.size
== profile_size
){
927 float *profile
= (float *) value
.data
;
928 a_i
= pa_aupdate_write_begin(u
->a_H
[channel
]);
929 u
->Xs
[channel
][a_i
] = profile
[0];
930 memcpy(u
->Hs
[channel
][a_i
], profile
+ 1, FILTER_SIZE
* sizeof(float));
931 fix_filter(u
->Hs
[channel
][a_i
], u
->fft_size
);
932 pa_aupdate_write_end(u
->a_H
[channel
]);
933 pa_xfree(u
->base_profiles
[channel
]);
934 u
->base_profiles
[channel
] = pa_xstrdup(name
);
936 return "incompatible size";
938 pa_datum_free(&value
);
940 return "profile doesn't exist";
945 static void load_state(struct userdata
*u
){
949 pa_database
*database
;
951 char *state_name
= u
->name
;
952 pa_assert_se(dbname
= pa_state_path(EQ_STATE_DB
, FALSE
));
953 database
= pa_database_open(dbname
, FALSE
);
956 pa_log("No resume state");
960 key
.data
= state_name
;
961 key
.size
= strlen(key
.data
);
963 if(pa_database_get(database
, &key
, &value
) != NULL
){
964 if(value
.size
> FILTER_STATE_SIZE
* sizeof(float) + sizeof(uint16_t)){
965 float *state
= (float *) value
.data
;
968 for(size_t c
= 0; c
< u
->channels
; ++c
){
969 a_i
= pa_aupdate_write_begin(u
->a_H
[c
]);
970 H
= state
+ c
* CHANNEL_PROFILE_SIZE
+ 1;
971 u
->Xs
[c
][a_i
] = state
[c
* CHANNEL_PROFILE_SIZE
];
972 memcpy(u
->Hs
[c
][a_i
], H
, FILTER_SIZE
* sizeof(float));
973 pa_aupdate_write_end(u
->a_H
[c
]);
975 unpack(((char *)value
.data
) + FILTER_STATE_SIZE
* sizeof(float), value
.size
- FILTER_STATE_SIZE
* sizeof(float), &names
, &n_profs
);
976 n_profs
= PA_MIN(n_profs
, u
->channels
);
977 for(size_t c
= 0; c
< n_profs
; ++c
){
978 pa_xfree(u
->base_profiles
[c
]);
979 u
->base_profiles
[c
] = names
[c
];
983 pa_datum_free(&value
);
985 pa_log("resume state exists but is wrong size!");
987 pa_database_close(database
);
990 /* Called from main context */
991 static pa_bool_t
sink_input_may_move_to_cb(pa_sink_input
*i
, pa_sink
*dest
) {
994 pa_sink_input_assert_ref(i
);
995 pa_assert_se(u
= i
->userdata
);
997 return u
->sink
!= dest
;
1000 /* Called from main context */
1001 static void sink_input_moving_cb(pa_sink_input
*i
, pa_sink
*dest
) {
1004 pa_sink_input_assert_ref(i
);
1005 pa_assert_se(u
= i
->userdata
);
1007 pa_sink_set_asyncmsgq(u
->sink
, dest
->asyncmsgq
);
1008 pa_sink_update_flags(u
->sink
, PA_SINK_LATENCY
|PA_SINK_DYNAMIC_LATENCY
, dest
->flags
);
1010 pa_sink_set_asyncmsgq(u
->sink
, NULL
);
1013 int pa__init(pa_module
*m
) {
1020 pa_sink_input_new_data sink_input_data
;
1021 pa_sink_new_data sink_data
;
1028 if (!(ma
= pa_modargs_new(m
->argument
, valid_modargs
))) {
1029 pa_log("Failed to parse module arguments.");
1033 if (!(master
= pa_namereg_get(m
->core
, pa_modargs_get_value(ma
, "master", NULL
), PA_NAMEREG_SINK
))) {
1034 pa_log("Master sink not found, trying default");
1035 master
= pa_namereg_get_default_sink(m
->core
);
1037 pa_log("no default sink found!");
1042 ss
= master
->sample_spec
;
1043 ss
.format
= PA_SAMPLE_FLOAT32
;
1044 map
= master
->channel_map
;
1045 if (pa_modargs_get_sample_spec_and_channel_map(ma
, &ss
, &map
, PA_CHANNEL_MAP_DEFAULT
) < 0) {
1046 pa_log("Invalid sample format specification or channel map");
1049 fs
= pa_frame_size(&ss
);
1051 u
= pa_xnew0(struct userdata
, 1);
1055 u
->set_default
= TRUE
;
1056 pa_modargs_get_value_boolean(ma
, "set_default", &u
->set_default
);
1058 u
->channels
= ss
.channels
;
1059 u
->fft_size
= pow(2, ceil(log(ss
.rate
) / log(2)));//probably unstable near corner cases of powers of 2
1060 pa_log_debug("fft size: %ld", u
->fft_size
);
1061 u
->window_size
= 15999;
1062 if(u
->window_size
% 2 == 0){
1065 u
->R
= (u
->window_size
+ 1) / 2;
1066 u
->overlap_size
= u
->window_size
- u
->R
;
1067 u
->samples_gathered
= 0;
1068 u
->input_buffer_max
= 0;
1069 u
->a_H
= pa_xnew0(pa_aupdate
*, u
->channels
);
1070 u
->Xs
= pa_xnew0(float *, u
->channels
);
1071 u
->Hs
= pa_xnew0(float **, u
->channels
);
1072 for(size_t c
= 0; c
< u
->channels
; ++c
){
1073 u
->Xs
[c
] = pa_xnew0(float, 2);
1074 u
->Hs
[c
] = pa_xnew0(float *, 2);
1075 for(size_t i
= 0; i
< 2; ++i
){
1076 u
->Hs
[c
][i
] = alloc(FILTER_SIZE
, sizeof(float));
1079 u
->W
= alloc(u
->window_size
, sizeof(float));
1080 u
->work_buffer
= alloc(u
->fft_size
, sizeof(float));
1081 memset(u
->work_buffer
, 0, u
->fft_size
*sizeof(float));
1082 u
->input
= pa_xnew0(float *, u
->channels
);
1083 u
->overlap_accum
= pa_xnew0(float *, u
->channels
);
1084 for(size_t c
= 0; c
< u
->channels
; ++c
){
1085 u
->a_H
[c
] = pa_aupdate_new();
1087 u
->overlap_accum
[c
] = alloc(u
->overlap_size
, sizeof(float));
1089 u
->output_window
= alloc((FILTER_SIZE
), sizeof(fftwf_complex
));
1090 u
->forward_plan
= fftwf_plan_dft_r2c_1d(u
->fft_size
, u
->work_buffer
, u
->output_window
, FFTW_ESTIMATE
);
1091 u
->inverse_plan
= fftwf_plan_dft_c2r_1d(u
->fft_size
, u
->output_window
, u
->work_buffer
, FFTW_ESTIMATE
);
1093 hanning_window(u
->W
, u
->window_size
);
1094 u
->first_iteration
= TRUE
;
1096 u
->base_profiles
= pa_xnew0(char *, u
->channels
);
1097 for(size_t c
= 0; c
< u
->channels
; ++c
){
1098 u
->base_profiles
[c
] = pa_xstrdup("default");
1102 pa_sink_new_data_init(&sink_data
);
1103 sink_data
.driver
= __FILE__
;
1104 sink_data
.module
= m
;
1105 if (!(sink_data
.name
= pa_xstrdup(pa_modargs_get_value(ma
, "sink_name", NULL
))))
1106 sink_data
.name
= pa_sprintf_malloc("%s.equalizer", master
->name
);
1107 pa_sink_new_data_set_sample_spec(&sink_data
, &ss
);
1108 pa_sink_new_data_set_channel_map(&sink_data
, &map
);
1109 z
= pa_proplist_gets(master
->proplist
, PA_PROP_DEVICE_DESCRIPTION
);
1110 pa_proplist_setf(sink_data
.proplist
, PA_PROP_DEVICE_DESCRIPTION
, "FFT based equalizer on %s",z
? z
: master
->name
);
1111 pa_proplist_sets(sink_data
.proplist
, PA_PROP_DEVICE_MASTER_DEVICE
, master
->name
);
1112 pa_proplist_sets(sink_data
.proplist
, PA_PROP_DEVICE_CLASS
, "filter");
1114 if (pa_modargs_get_proplist(ma
, "sink_properties", sink_data
.proplist
, PA_UPDATE_REPLACE
) < 0) {
1115 pa_log("Invalid properties");
1116 pa_sink_new_data_done(&sink_data
);
1120 u
->sink
= pa_sink_new(m
->core
, &sink_data
,
1121 PA_SINK_HW_MUTE_CTRL
|PA_SINK_HW_VOLUME_CTRL
|PA_SINK_DECIBEL_VOLUME
|
1122 (master
->flags
& (PA_SINK_LATENCY
|PA_SINK_DYNAMIC_LATENCY
)));
1123 pa_sink_new_data_done(&sink_data
);
1126 pa_log("Failed to create sink.");
1129 u
->name
=pa_xstrdup(u
->sink
->name
);
1130 u
->sink
->parent
.process_msg
= sink_process_msg_cb
;
1131 u
->sink
->set_state
= sink_set_state_cb
;
1132 u
->sink
->update_requested_latency
= sink_update_requested_latency_cb
;
1133 u
->sink
->request_rewind
= sink_request_rewind_cb
;
1134 u
->sink
->set_volume
= sink_set_volume_cb
;
1135 u
->sink
->set_mute
= sink_set_mute_cb
;
1136 u
->sink
->userdata
= u
;
1137 u
->input_q
= pa_memblockq_new(0, MEMBLOCKQ_MAXLENGTH
, 0, fs
, 1, 1, 0, &u
->sink
->silence
);
1139 pa_sink_set_asyncmsgq(u
->sink
, master
->asyncmsgq
);
1140 //pa_sink_set_fixed_latency(u->sink, pa_bytes_to_usec(u->R*fs, &ss));
1142 /* Create sink input */
1143 pa_sink_input_new_data_init(&sink_input_data
);
1144 sink_input_data
.driver
= __FILE__
;
1145 sink_input_data
.module
= m
;
1146 sink_input_data
.sink
= master
;
1147 pa_proplist_sets(sink_input_data
.proplist
, PA_PROP_MEDIA_NAME
, "Equalized Stream");
1148 pa_proplist_sets(sink_input_data
.proplist
, PA_PROP_MEDIA_ROLE
, "filter");
1149 pa_sink_input_new_data_set_sample_spec(&sink_input_data
, &ss
);
1150 pa_sink_input_new_data_set_channel_map(&sink_input_data
, &map
);
1152 pa_sink_input_new(&u
->sink_input
, m
->core
, &sink_input_data
);
1153 pa_sink_input_new_data_done(&sink_input_data
);
1158 u
->sink_input
->pop
= sink_input_pop_cb
;
1159 u
->sink_input
->process_rewind
= sink_input_process_rewind_cb
;
1160 u
->sink_input
->update_max_rewind
= sink_input_update_max_rewind_cb
;
1161 u
->sink_input
->update_max_request
= sink_input_update_max_request_cb
;
1162 u
->sink_input
->update_sink_latency_range
= sink_input_update_sink_latency_range_cb
;
1163 u
->sink_input
->update_sink_fixed_latency
= sink_input_update_sink_fixed_latency_cb
;
1164 u
->sink_input
->kill
= sink_input_kill_cb
;
1165 u
->sink_input
->attach
= sink_input_attach_cb
;
1166 u
->sink_input
->detach
= sink_input_detach_cb
;
1167 u
->sink_input
->state_change
= sink_input_state_change_cb
;
1168 u
->sink_input
->may_move_to
= sink_input_may_move_to_cb
;
1169 u
->sink_input
->moving
= sink_input_moving_cb
;
1170 u
->sink_input
->volume_changed
= sink_input_volume_changed_cb
;
1171 u
->sink_input
->mute_changed
= sink_input_mute_changed_cb
;
1173 u
->sink_input
->userdata
= u
;
1175 pa_sink_put(u
->sink
);
1176 pa_sink_input_put(u
->sink_input
);
1178 pa_modargs_free(ma
);
1183 //default filter to these
1184 for(size_t c
= 0; c
< u
->channels
; ++c
){
1185 a_i
= pa_aupdate_write_begin(u
->a_H
[c
]);
1187 u
->Xs
[c
][a_i
] = 1.0f
;
1188 for(size_t i
= 0; i
< FILTER_SIZE
; ++i
){
1189 H
[i
] = 1.0 / sqrtf(2.0f
);
1191 fix_filter(H
, u
->fft_size
);
1192 pa_aupdate_write_end(u
->a_H
[c
]);
1194 //load old parameters
1201 pa_modargs_free(ma
);
1209 int pa__get_n_used(pa_module
*m
) {
1213 pa_assert_se(u
= m
->userdata
);
1215 return pa_sink_linked_by(u
->sink
);
1218 void pa__done(pa_module
*m
) {
1223 if (!(u
= m
->userdata
))
1230 for(size_t c
= 0; c
< u
->channels
; ++c
){
1231 pa_xfree(u
->base_profiles
[c
]);
1233 pa_xfree(u
->base_profiles
);
1235 /* See comments in sink_input_kill_cb() above regarding
1236 * destruction order! */
1239 pa_sink_input_unlink(u
->sink_input
);
1242 pa_sink_unlink(u
->sink
);
1245 pa_sink_input_unref(u
->sink_input
);
1248 pa_sink_unref(u
->sink
);
1250 pa_memblockq_free(u
->input_q
);
1252 fftwf_destroy_plan(u
->inverse_plan
);
1253 fftwf_destroy_plan(u
->forward_plan
);
1254 pa_xfree(u
->output_window
);
1255 for(size_t c
=0; c
< u
->channels
; ++c
){
1256 pa_aupdate_free(u
->a_H
[c
]);
1257 pa_xfree(u
->overlap_accum
[c
]);
1258 pa_xfree(u
->input
[c
]);
1261 pa_xfree(u
->overlap_accum
);
1263 pa_xfree(u
->work_buffer
);
1265 for(size_t c
= 0; c
< u
->channels
; ++c
){
1267 for(size_t i
= 0; i
< 2; ++i
){
1268 pa_xfree(u
->Hs
[c
][i
]);
1281 * DBus Routines and Callbacks
1283 #define EXTNAME "org.PulseAudio.Ext.Equalizing1"
1284 #define MANAGER_PATH "/org/pulseaudio/equalizing1"
1285 #define MANAGER_IFACE EXTNAME ".Manager"
1286 #define EQUALIZER_IFACE EXTNAME ".Equalizer"
1287 static void manager_get_revision(DBusConnection
*conn
, DBusMessage
*msg
, void *_u
);
1288 static void manager_get_sinks(DBusConnection
*conn
, DBusMessage
*msg
, void *_u
);
1289 static void manager_get_profiles(DBusConnection
*conn
, DBusMessage
*msg
, void *_u
);
1290 static void manager_get_all(DBusConnection
*conn
, DBusMessage
*msg
, void *_u
);
1291 static void manager_handle_remove_profile(DBusConnection
*conn
, DBusMessage
*msg
, void *_u
);
1292 static void equalizer_get_revision(DBusConnection
*conn
, DBusMessage
*msg
, void *_u
);
1293 static void equalizer_get_sample_rate(DBusConnection
*conn
, DBusMessage
*msg
, void *_u
);
1294 static void equalizer_get_filter_rate(DBusConnection
*conn
, DBusMessage
*msg
, void *_u
);
1295 static void equalizer_get_n_coefs(DBusConnection
*conn
, DBusMessage
*msg
, void *_u
);
1296 static void equalizer_get_n_channels(DBusConnection
*conn
, DBusMessage
*msg
, void *_u
);
1297 static void equalizer_get_all(DBusConnection
*conn
, DBusMessage
*msg
, void *_u
);
1298 static void equalizer_handle_seed_filter(DBusConnection
*conn
, DBusMessage
*msg
, void *_u
);
1299 static void equalizer_handle_get_filter_points(DBusConnection
*conn
, DBusMessage
*msg
, void *_u
);
1300 static void equalizer_handle_get_filter(DBusConnection
*conn
, DBusMessage
*msg
, void *_u
);
1301 static void equalizer_handle_set_filter(DBusConnection
*conn
, DBusMessage
*msg
, void *_u
);
1302 static void equalizer_handle_save_profile(DBusConnection
*conn
, DBusMessage
*msg
, void *_u
);
1303 static void equalizer_handle_load_profile(DBusConnection
*conn
, DBusMessage
*msg
, void *_u
);
1304 static void equalizer_handle_save_state(DBusConnection
*conn
, DBusMessage
*msg
, void *_u
);
1305 static void equalizer_handle_get_profile_name(DBusConnection
*conn
, DBusMessage
*msg
, void *_u
);
1306 enum manager_method_index
{
1307 MANAGER_METHOD_REMOVE_PROFILE
,
1311 pa_dbus_arg_info remove_profile_args
[]={
1315 static pa_dbus_method_handler manager_methods
[MANAGER_METHOD_MAX
]={
1316 [MANAGER_METHOD_REMOVE_PROFILE
]{
1317 .method_name
="RemoveProfile",
1318 .arguments
=remove_profile_args
,
1319 .n_arguments
=sizeof(remove_profile_args
)/sizeof(pa_dbus_arg_info
),
1320 .receive_cb
=manager_handle_remove_profile
}
1323 enum manager_handler_index
{
1324 MANAGER_HANDLER_REVISION
,
1325 MANAGER_HANDLER_EQUALIZED_SINKS
,
1326 MANAGER_HANDLER_PROFILES
,
1330 static pa_dbus_property_handler manager_handlers
[MANAGER_HANDLER_MAX
]={
1331 [MANAGER_HANDLER_REVISION
]={.property_name
="InterfaceRevision",.type
="u",.get_cb
=manager_get_revision
,.set_cb
=NULL
},
1332 [MANAGER_HANDLER_EQUALIZED_SINKS
]={.property_name
="EqualizedSinks",.type
="ao",.get_cb
=manager_get_sinks
,.set_cb
=NULL
},
1333 [MANAGER_HANDLER_PROFILES
]={.property_name
="Profiles",.type
="as",.get_cb
=manager_get_profiles
,.set_cb
=NULL
}
1336 pa_dbus_arg_info sink_args
[]={
1340 enum manager_signal_index
{
1341 MANAGER_SIGNAL_SINK_ADDED
,
1342 MANAGER_SIGNAL_SINK_REMOVED
,
1343 MANAGER_SIGNAL_PROFILES_CHANGED
,
1347 static pa_dbus_signal_info manager_signals
[MANAGER_SIGNAL_MAX
]={
1348 [MANAGER_SIGNAL_SINK_ADDED
]={.name
="SinkAdded", .arguments
=sink_args
, .n_arguments
=sizeof(sink_args
)/sizeof(pa_dbus_arg_info
)},
1349 [MANAGER_SIGNAL_SINK_REMOVED
]={.name
="SinkRemoved", .arguments
=sink_args
, .n_arguments
=sizeof(sink_args
)/sizeof(pa_dbus_arg_info
)},
1350 [MANAGER_SIGNAL_PROFILES_CHANGED
]={.name
="ProfilesChanged", .arguments
=NULL
, .n_arguments
=0}
1353 static pa_dbus_interface_info manager_info
={
1354 .name
=MANAGER_IFACE
,
1355 .method_handlers
=manager_methods
,
1356 .n_method_handlers
=MANAGER_METHOD_MAX
,
1357 .property_handlers
=manager_handlers
,
1358 .n_property_handlers
=MANAGER_HANDLER_MAX
,
1359 .get_all_properties_cb
=manager_get_all
,
1360 .signals
=manager_signals
,
1361 .n_signals
=MANAGER_SIGNAL_MAX
1364 enum equalizer_method_index
{
1365 EQUALIZER_METHOD_FILTER_POINTS
,
1366 EQUALIZER_METHOD_SEED_FILTER
,
1367 EQUALIZER_METHOD_SAVE_PROFILE
,
1368 EQUALIZER_METHOD_LOAD_PROFILE
,
1369 EQUALIZER_METHOD_SET_FILTER
,
1370 EQUALIZER_METHOD_GET_FILTER
,
1371 EQUALIZER_METHOD_SAVE_STATE
,
1372 EQUALIZER_METHOD_GET_PROFILE_NAME
,
1373 EQUALIZER_METHOD_MAX
1376 enum equalizer_handler_index
{
1377 EQUALIZER_HANDLER_REVISION
,
1378 EQUALIZER_HANDLER_SAMPLERATE
,
1379 EQUALIZER_HANDLER_FILTERSAMPLERATE
,
1380 EQUALIZER_HANDLER_N_COEFS
,
1381 EQUALIZER_HANDLER_N_CHANNELS
,
1382 EQUALIZER_HANDLER_MAX
1385 pa_dbus_arg_info filter_points_args
[]={
1386 {"channel", "u","in"},
1389 {"preamp", "d","out"}
1391 pa_dbus_arg_info seed_filter_args
[]={
1392 {"channel", "u","in"},
1395 {"preamp", "d","in"}
1398 pa_dbus_arg_info set_filter_args
[]={
1399 {"channel", "u","in"},
1401 {"preamp", "d","in"}
1403 pa_dbus_arg_info get_filter_args
[]={
1404 {"channel", "u","in"},
1406 {"preamp", "d","out"}
1409 pa_dbus_arg_info save_profile_args
[]={
1410 {"channel", "u","in"},
1413 pa_dbus_arg_info load_profile_args
[]={
1414 {"channel", "u","in"},
1417 pa_dbus_arg_info base_profile_name_args
[]={
1418 {"channel", "u","in"},
1422 static pa_dbus_method_handler equalizer_methods
[EQUALIZER_METHOD_MAX
]={
1423 [EQUALIZER_METHOD_SEED_FILTER
]{
1424 .method_name
="SeedFilter",
1425 .arguments
=seed_filter_args
,
1426 .n_arguments
=sizeof(seed_filter_args
)/sizeof(pa_dbus_arg_info
),
1427 .receive_cb
=equalizer_handle_seed_filter
},
1428 [EQUALIZER_METHOD_FILTER_POINTS
]{
1429 .method_name
="FilterAtPoints",
1430 .arguments
=filter_points_args
,
1431 .n_arguments
=sizeof(filter_points_args
)/sizeof(pa_dbus_arg_info
),
1432 .receive_cb
=equalizer_handle_get_filter_points
},
1433 [EQUALIZER_METHOD_SET_FILTER
]{
1434 .method_name
="SetFilter",
1435 .arguments
=set_filter_args
,
1436 .n_arguments
=sizeof(set_filter_args
)/sizeof(pa_dbus_arg_info
),
1437 .receive_cb
=equalizer_handle_set_filter
},
1438 [EQUALIZER_METHOD_GET_FILTER
]{
1439 .method_name
="GetFilter",
1440 .arguments
=get_filter_args
,
1441 .n_arguments
=sizeof(get_filter_args
)/sizeof(pa_dbus_arg_info
),
1442 .receive_cb
=equalizer_handle_get_filter
},
1443 [EQUALIZER_METHOD_SAVE_PROFILE
]{
1444 .method_name
="SaveProfile",
1445 .arguments
=save_profile_args
,
1446 .n_arguments
=sizeof(save_profile_args
)/sizeof(pa_dbus_arg_info
),
1447 .receive_cb
=equalizer_handle_save_profile
},
1448 [EQUALIZER_METHOD_LOAD_PROFILE
]{
1449 .method_name
="LoadProfile",
1450 .arguments
=load_profile_args
,
1451 .n_arguments
=sizeof(load_profile_args
)/sizeof(pa_dbus_arg_info
),
1452 .receive_cb
=equalizer_handle_load_profile
},
1453 [EQUALIZER_METHOD_SAVE_STATE
]{
1454 .method_name
="SaveState",
1457 .receive_cb
=equalizer_handle_save_state
},
1458 [EQUALIZER_METHOD_GET_PROFILE_NAME
]{
1459 .method_name
="BaseProfile",
1460 .arguments
=base_profile_name_args
,
1461 .n_arguments
=sizeof(base_profile_name_args
)/sizeof(pa_dbus_arg_info
),
1462 .receive_cb
=equalizer_handle_get_profile_name
}
1465 static pa_dbus_property_handler equalizer_handlers
[EQUALIZER_HANDLER_MAX
]={
1466 [EQUALIZER_HANDLER_REVISION
]={.property_name
="InterfaceRevision",.type
="u",.get_cb
=equalizer_get_revision
,.set_cb
=NULL
},
1467 [EQUALIZER_HANDLER_SAMPLERATE
]{.property_name
="SampleRate",.type
="u",.get_cb
=equalizer_get_sample_rate
,.set_cb
=NULL
},
1468 [EQUALIZER_HANDLER_FILTERSAMPLERATE
]{.property_name
="FilterSampleRate",.type
="u",.get_cb
=equalizer_get_filter_rate
,.set_cb
=NULL
},
1469 [EQUALIZER_HANDLER_N_COEFS
]{.property_name
="NFilterCoefficients",.type
="u",.get_cb
=equalizer_get_n_coefs
,.set_cb
=NULL
},
1470 [EQUALIZER_HANDLER_N_CHANNELS
]{.property_name
="NChannels",.type
="u",.get_cb
=equalizer_get_n_channels
,.set_cb
=NULL
},
1473 enum equalizer_signal_index
{
1474 EQUALIZER_SIGNAL_FILTER_CHANGED
,
1475 EQUALIZER_SIGNAL_SINK_RECONFIGURED
,
1476 EQUALIZER_SIGNAL_MAX
1479 static pa_dbus_signal_info equalizer_signals
[EQUALIZER_SIGNAL_MAX
]={
1480 [EQUALIZER_SIGNAL_FILTER_CHANGED
]={.name
="FilterChanged", .arguments
=NULL
, .n_arguments
=0},
1481 [EQUALIZER_SIGNAL_SINK_RECONFIGURED
]={.name
="SinkReconfigured", .arguments
=NULL
, .n_arguments
=0},
1484 static pa_dbus_interface_info equalizer_info
={
1485 .name
=EQUALIZER_IFACE
,
1486 .method_handlers
=equalizer_methods
,
1487 .n_method_handlers
=EQUALIZER_METHOD_MAX
,
1488 .property_handlers
=equalizer_handlers
,
1489 .n_property_handlers
=EQUALIZER_HANDLER_MAX
,
1490 .get_all_properties_cb
=equalizer_get_all
,
1491 .signals
=equalizer_signals
,
1492 .n_signals
=EQUALIZER_SIGNAL_MAX
1495 void dbus_init(struct userdata
*u
){
1497 DBusMessage
*signal
= NULL
;
1498 pa_idxset
*sink_list
= NULL
;
1499 u
->dbus_protocol
=pa_dbus_protocol_get(u
->sink
->core
);
1500 u
->dbus_path
=pa_sprintf_malloc("/org/pulseaudio/core1/sink%d", u
->sink
->index
);
1502 pa_dbus_protocol_add_interface(u
->dbus_protocol
, u
->dbus_path
, &equalizer_info
, u
);
1503 sink_list
= pa_shared_get(u
->sink
->core
, SINKLIST
);
1504 u
->database
= pa_shared_get(u
->sink
->core
, EQDB
);
1505 if(sink_list
== NULL
){
1507 sink_list
=pa_idxset_new(&pa_idxset_trivial_hash_func
, &pa_idxset_trivial_compare_func
);
1508 pa_shared_set(u
->sink
->core
, SINKLIST
, sink_list
);
1509 pa_assert_se(dbname
= pa_state_path("equalizer-presets", FALSE
));
1510 pa_assert_se(u
->database
= pa_database_open(dbname
, TRUE
));
1512 pa_shared_set(u
->sink
->core
, EQDB
, u
->database
);
1513 pa_dbus_protocol_add_interface(u
->dbus_protocol
, MANAGER_PATH
, &manager_info
, u
->sink
->core
);
1514 pa_dbus_protocol_register_extension(u
->dbus_protocol
, EXTNAME
);
1516 pa_idxset_put(sink_list
, u
, &dummy
);
1518 pa_assert_se((signal
= dbus_message_new_signal(MANAGER_PATH
, MANAGER_IFACE
, manager_signals
[MANAGER_SIGNAL_SINK_ADDED
].name
)));
1519 dbus_message_append_args(signal
, DBUS_TYPE_OBJECT_PATH
, &u
->dbus_path
, DBUS_TYPE_INVALID
);
1520 pa_dbus_protocol_send_signal(u
->dbus_protocol
, signal
);
1521 dbus_message_unref(signal
);
1524 void dbus_done(struct userdata
*u
){
1525 pa_idxset
*sink_list
;
1528 DBusMessage
*signal
= NULL
;
1529 pa_assert_se((signal
= dbus_message_new_signal(MANAGER_PATH
, MANAGER_IFACE
, manager_signals
[MANAGER_SIGNAL_SINK_REMOVED
].name
)));
1530 dbus_message_append_args(signal
, DBUS_TYPE_OBJECT_PATH
, &u
->dbus_path
, DBUS_TYPE_INVALID
);
1531 pa_dbus_protocol_send_signal(u
->dbus_protocol
, signal
);
1532 dbus_message_unref(signal
);
1534 pa_assert_se(sink_list
=pa_shared_get(u
->sink
->core
,SINKLIST
));
1535 pa_idxset_remove_by_data(sink_list
,u
,&dummy
);
1536 if(pa_idxset_size(sink_list
)==0){
1537 pa_dbus_protocol_unregister_extension(u
->dbus_protocol
, EXTNAME
);
1538 pa_dbus_protocol_remove_interface(u
->dbus_protocol
, MANAGER_PATH
, manager_info
.name
);
1539 pa_shared_remove(u
->sink
->core
, EQDB
);
1540 pa_database_close(u
->database
);
1541 pa_shared_remove(u
->sink
->core
, SINKLIST
);
1542 pa_xfree(sink_list
);
1544 pa_dbus_protocol_remove_interface(u
->dbus_protocol
, u
->dbus_path
, equalizer_info
.name
);
1545 pa_xfree(u
->dbus_path
);
1546 pa_dbus_protocol_unref(u
->dbus_protocol
);
1549 void manager_handle_remove_profile(DBusConnection
*conn
, DBusMessage
*msg
, void *_u
) {
1551 pa_core
*c
= (pa_core
*)_u
;
1552 DBusMessage
*signal
= NULL
;
1553 pa_dbus_protocol
*dbus_protocol
;
1558 dbus_error_init(&error
);
1559 if(!dbus_message_get_args(msg
, &error
,
1560 DBUS_TYPE_STRING
, &name
,
1561 DBUS_TYPE_INVALID
)){
1562 pa_dbus_send_error(conn
, msg
, DBUS_ERROR_INVALID_ARGS
, "%s", error
.message
);
1563 dbus_error_free(&error
);
1566 remove_profile(c
,name
);
1567 pa_dbus_send_empty_reply(conn
, msg
);
1569 pa_assert_se((signal
= dbus_message_new_signal(MANAGER_PATH
, MANAGER_IFACE
, manager_signals
[MANAGER_SIGNAL_PROFILES_CHANGED
].name
)));
1570 dbus_protocol
= pa_dbus_protocol_get(c
);
1571 pa_dbus_protocol_send_signal(dbus_protocol
, signal
);
1572 pa_dbus_protocol_unref(dbus_protocol
);
1573 dbus_message_unref(signal
);
1576 void manager_get_revision(DBusConnection
*conn
, DBusMessage
*msg
, void *_u
){
1578 pa_dbus_send_basic_value_reply(conn
, msg
, DBUS_TYPE_UINT32
, &rev
);
1581 static void get_sinks(pa_core
*u
, char ***names
, unsigned *n_sinks
){
1583 struct userdata
*sink_u
= NULL
;
1585 pa_idxset
*sink_list
;
1590 pa_assert_se(sink_list
= pa_shared_get(u
, SINKLIST
));
1591 *n_sinks
= (unsigned) pa_idxset_size(sink_list
);
1592 *names
= *n_sinks
> 0 ? pa_xnew0(char *,*n_sinks
) : NULL
;
1593 for(uint32_t i
= 0; i
< *n_sinks
; ++i
){
1594 sink_u
= (struct userdata
*) pa_idxset_iterate(sink_list
, &iter
, &dummy
);
1595 (*names
)[i
] = pa_xstrdup(sink_u
->dbus_path
);
1599 void manager_get_sinks(DBusConnection
*conn
, DBusMessage
*msg
, void *_u
){
1601 char **names
= NULL
;
1606 get_sinks((pa_core
*) _u
, &names
, &n
);
1607 pa_dbus_send_basic_array_variant_reply(conn
, msg
, DBUS_TYPE_OBJECT_PATH
, names
, n
);
1608 for(unsigned i
= 0; i
< n
; ++i
){
1614 static void get_profiles(pa_core
*c
, char ***names
, unsigned *n
){
1616 pa_database
*database
;
1617 pa_datum key
, next_key
;
1618 pa_strlist
*head
=NULL
, *iter
;
1620 pa_assert_se(database
= pa_shared_get(c
, EQDB
));
1625 done
= !pa_database_first(database
, &key
, NULL
);
1628 done
= !pa_database_next(database
, &key
, &next_key
, NULL
);
1629 name
=pa_xmalloc(key
.size
+ 1);
1630 memcpy(name
, key
.data
, key
.size
);
1631 name
[key
.size
] = '\0';
1632 pa_datum_free(&key
);
1633 head
= pa_strlist_prepend(head
, name
);
1638 (*names
) = *n
> 0 ? pa_xnew0(char *, *n
) : NULL
;
1640 for(unsigned i
= 0; i
< *n
; ++i
){
1641 (*names
)[*n
- 1 - i
] = pa_xstrdup(pa_strlist_data(iter
));
1642 iter
= pa_strlist_next(iter
);
1644 pa_strlist_free(head
);
1647 void manager_get_profiles(DBusConnection
*conn
, DBusMessage
*msg
, void *_u
){
1654 get_profiles((pa_core
*)_u
, &names
, &n
);
1655 pa_dbus_send_basic_array_variant_reply(conn
, msg
, DBUS_TYPE_STRING
, names
, n
);
1656 for(unsigned i
= 0; i
< n
; ++i
){
1662 void manager_get_all(DBusConnection
*conn
, DBusMessage
*msg
, void *_u
){
1664 char **names
= NULL
;
1666 DBusMessage
*reply
= NULL
;
1667 DBusMessageIter msg_iter
, dict_iter
;
1671 pa_assert_se(c
= _u
);
1673 pa_assert_se((reply
= dbus_message_new_method_return(msg
)));
1674 dbus_message_iter_init_append(reply
, &msg_iter
);
1675 pa_assert_se(dbus_message_iter_open_container(&msg_iter
, DBUS_TYPE_ARRAY
, "{sv}", &dict_iter
));
1678 pa_dbus_append_basic_variant_dict_entry(&dict_iter
, manager_handlers
[MANAGER_HANDLER_REVISION
].property_name
, DBUS_TYPE_UINT32
, &rev
);
1680 get_sinks(c
, &names
, &n
);
1681 pa_dbus_append_basic_array_variant_dict_entry(&dict_iter
,manager_handlers
[MANAGER_HANDLER_EQUALIZED_SINKS
].property_name
, DBUS_TYPE_OBJECT_PATH
, names
, n
);
1682 for(unsigned i
= 0; i
< n
; ++i
){
1687 get_profiles(c
, &names
, &n
);
1688 pa_dbus_append_basic_array_variant_dict_entry(&dict_iter
, manager_handlers
[MANAGER_HANDLER_PROFILES
].property_name
, DBUS_TYPE_STRING
, names
, n
);
1689 for(unsigned i
= 0; i
< n
; ++i
){
1693 pa_assert_se(dbus_message_iter_close_container(&msg_iter
, &dict_iter
));
1694 pa_assert_se(dbus_connection_send(conn
, reply
, NULL
));
1695 dbus_message_unref(reply
);
1698 void equalizer_handle_seed_filter(DBusConnection
*conn
, DBusMessage
*msg
, void *_u
) {
1699 struct userdata
*u
=(struct userdata
*) _u
;
1701 DBusMessage
*signal
= NULL
;
1703 uint32_t *xs
, channel
, r_channel
;
1704 double *_ys
, preamp
;
1705 unsigned x_npoints
, y_npoints
, a_i
;
1707 pa_bool_t points_good
= TRUE
;
1712 dbus_error_init(&error
);
1714 if(!dbus_message_get_args(msg
, &error
,
1715 DBUS_TYPE_UINT32
, &channel
,
1716 DBUS_TYPE_ARRAY
, DBUS_TYPE_UINT32
, &xs
, &x_npoints
,
1717 DBUS_TYPE_ARRAY
, DBUS_TYPE_DOUBLE
, &_ys
, &y_npoints
,
1718 DBUS_TYPE_DOUBLE
, &preamp
,
1719 DBUS_TYPE_INVALID
)){
1720 pa_dbus_send_error(conn
, msg
, DBUS_ERROR_INVALID_ARGS
, "%s", error
.message
);
1721 dbus_error_free(&error
);
1724 if(channel
> u
->channels
){
1725 pa_dbus_send_error(conn
, msg
, DBUS_ERROR_INVALID_ARGS
, "invalid channel: %d", channel
);
1726 dbus_error_free(&error
);
1729 for(size_t i
= 0; i
< x_npoints
; ++i
){
1730 if(xs
[i
] >= FILTER_SIZE
){
1731 points_good
= FALSE
;
1735 if(!is_monotonic(xs
, x_npoints
) || !points_good
){
1736 pa_dbus_send_error(conn
, msg
, DBUS_ERROR_INVALID_ARGS
, "xs must be monotonic and 0<=x<=%ld", u
->fft_size
/ 2);
1737 dbus_error_free(&error
);
1739 }else if(x_npoints
!= y_npoints
|| x_npoints
< 2 || x_npoints
> FILTER_SIZE
){
1740 pa_dbus_send_error(conn
, msg
, DBUS_ERROR_INVALID_ARGS
, "xs and ys must be the same length and 2<=l<=%ld!", FILTER_SIZE
);
1741 dbus_error_free(&error
);
1743 }else if(xs
[0] != 0 || xs
[x_npoints
- 1] != u
->fft_size
/ 2){
1744 pa_dbus_send_error(conn
, msg
, DBUS_ERROR_INVALID_ARGS
, "xs[0] must be 0 and xs[-1]=fft_size/2");
1745 dbus_error_free(&error
);
1749 ys
= pa_xmalloc(x_npoints
* sizeof(float));
1750 for(uint32_t i
= 0; i
< x_npoints
; ++i
){
1751 ys
[i
] = (float) _ys
[i
];
1753 r_channel
= channel
== u
->channels
? 0 : channel
;
1754 a_i
= pa_aupdate_write_begin(u
->a_H
[r_channel
]);
1755 H
= u
->Hs
[r_channel
][a_i
];
1756 u
->Xs
[r_channel
][a_i
] = preamp
;
1757 interpolate(H
, FILTER_SIZE
, xs
, ys
, x_npoints
);
1758 fix_filter(H
, u
->fft_size
);
1759 if(channel
== u
->channels
){
1760 for(size_t c
= 1; c
< u
->channels
; ++c
){
1761 unsigned b_i
= pa_aupdate_write_begin(u
->a_H
[c
]);
1762 float *H_p
= u
->Hs
[c
][b_i
];
1763 u
->Xs
[c
][b_i
] = preamp
;
1764 memcpy(H_p
, H
, FILTER_SIZE
* sizeof(float));
1765 pa_aupdate_write_end(u
->a_H
[c
]);
1768 pa_aupdate_write_end(u
->a_H
[r_channel
]);
1772 pa_dbus_send_empty_reply(conn
, msg
);
1774 pa_assert_se((signal
= dbus_message_new_signal(u
->dbus_path
, EQUALIZER_IFACE
, equalizer_signals
[EQUALIZER_SIGNAL_FILTER_CHANGED
].name
)));
1775 pa_dbus_protocol_send_signal(u
->dbus_protocol
, signal
);
1776 dbus_message_unref(signal
);
1779 void equalizer_handle_get_filter_points(DBusConnection
*conn
, DBusMessage
*msg
, void *_u
) {
1780 struct userdata
*u
= (struct userdata
*) _u
;
1781 uint32_t *xs
, channel
, r_channel
;
1783 unsigned x_npoints
, a_i
;
1785 pa_bool_t points_good
=TRUE
;
1786 DBusMessage
*reply
= NULL
;
1787 DBusMessageIter msg_iter
;
1794 dbus_error_init(&error
);
1795 if(!dbus_message_get_args(msg
, &error
,
1796 DBUS_TYPE_UINT32
, &channel
,
1797 DBUS_TYPE_ARRAY
, DBUS_TYPE_UINT32
, &xs
, &x_npoints
,
1798 DBUS_TYPE_INVALID
)){
1799 pa_dbus_send_error(conn
, msg
, DBUS_ERROR_INVALID_ARGS
, "%s", error
.message
);
1800 dbus_error_free(&error
);
1803 if(channel
> u
->channels
){
1804 pa_dbus_send_error(conn
, msg
, DBUS_ERROR_INVALID_ARGS
, "invalid channel: %d", channel
);
1805 dbus_error_free(&error
);
1809 for(size_t i
= 0; i
< x_npoints
; ++i
){
1810 if(xs
[i
] >= FILTER_SIZE
){
1816 if(x_npoints
> FILTER_SIZE
|| !points_good
){
1817 pa_dbus_send_error(conn
, msg
, DBUS_ERROR_INVALID_ARGS
, "xs indices/length must be <= %ld!", FILTER_SIZE
);
1818 dbus_error_free(&error
);
1822 r_channel
= channel
== u
->channels
? 0 : channel
;
1823 ys
= pa_xmalloc(x_npoints
* sizeof(double));
1824 a_i
= pa_aupdate_read_begin(u
->a_H
[r_channel
]);
1825 H
= u
->Hs
[r_channel
][a_i
];
1826 preamp
= u
->Xs
[r_channel
][a_i
];
1827 for(uint32_t i
= 0; i
< x_npoints
; ++i
){
1828 ys
[i
] = H
[xs
[i
]] * u
->fft_size
;
1830 pa_aupdate_read_end(u
->a_H
[r_channel
]);
1832 pa_assert_se((reply
= dbus_message_new_method_return(msg
)));
1833 dbus_message_iter_init_append(reply
, &msg_iter
);
1835 pa_dbus_append_basic_array(&msg_iter
, DBUS_TYPE_DOUBLE
, ys
, x_npoints
);
1836 pa_dbus_append_basic_variant(&msg_iter
, DBUS_TYPE_DOUBLE
, &preamp
);
1838 pa_assert_se(dbus_connection_send(conn
, reply
, NULL
));
1839 dbus_message_unref(reply
);
1843 static void get_filter(struct userdata
*u
, size_t channel
, double **H_
, double *preamp
){
1846 size_t r_channel
= channel
== u
->channels
? 0 : channel
;
1847 *H_
= pa_xnew0(double, FILTER_SIZE
);
1848 a_i
= pa_aupdate_read_begin(u
->a_H
[r_channel
]);
1849 H
= u
->Hs
[r_channel
][a_i
];
1850 for(size_t i
= 0;i
< FILTER_SIZE
; ++i
){
1851 (*H_
)[i
] = H
[i
] * u
->fft_size
;
1853 *preamp
= u
->Xs
[r_channel
][a_i
];
1855 pa_aupdate_read_end(u
->a_H
[r_channel
]);
1858 void equalizer_handle_get_filter(DBusConnection
*conn
, DBusMessage
*msg
, void *_u
){
1863 DBusMessage
*reply
= NULL
;
1864 DBusMessageIter msg_iter
;
1866 pa_assert_se(u
= (struct userdata
*) _u
);
1870 dbus_error_init(&error
);
1871 if(!dbus_message_get_args(msg
, &error
,
1872 DBUS_TYPE_UINT32
, &channel
,
1873 DBUS_TYPE_INVALID
)){
1874 pa_dbus_send_error(conn
, msg
, DBUS_ERROR_INVALID_ARGS
, "%s", error
.message
);
1875 dbus_error_free(&error
);
1878 if(channel
> u
->channels
){
1879 pa_dbus_send_error(conn
, msg
, DBUS_ERROR_INVALID_ARGS
, "invalid channel: %d", channel
);
1880 dbus_error_free(&error
);
1884 n_coefs
= CHANNEL_PROFILE_SIZE
;
1887 get_filter(u
, channel
, &H_
, &preamp
);
1888 pa_assert_se((reply
= dbus_message_new_method_return(msg
)));
1889 dbus_message_iter_init_append(reply
, &msg_iter
);
1891 pa_dbus_append_basic_array(&msg_iter
, DBUS_TYPE_DOUBLE
, H_
, n_coefs
);
1892 pa_dbus_append_basic_variant(&msg_iter
, DBUS_TYPE_DOUBLE
, &preamp
);
1894 pa_assert_se(dbus_connection_send(conn
, reply
, NULL
));
1895 dbus_message_unref(reply
);
1899 static void set_filter(struct userdata
*u
, size_t channel
, double *H_
, double preamp
){
1901 size_t r_channel
= channel
== u
->channels
? 0 : channel
;
1904 a_i
= pa_aupdate_write_begin(u
->a_H
[r_channel
]);
1905 u
->Xs
[r_channel
][a_i
] = (float) preamp
;
1906 H
= u
->Hs
[r_channel
][a_i
];
1907 for(size_t i
= 0; i
< FILTER_SIZE
; ++i
){
1908 H
[i
] = (float) H_
[i
];
1910 fix_filter(H
, u
->fft_size
);
1911 if(channel
== u
->channels
){
1912 for(size_t c
= 1; c
< u
->channels
; ++c
){
1913 unsigned b_i
= pa_aupdate_write_begin(u
->a_H
[c
]);
1914 u
->Xs
[c
][b_i
] = u
->Xs
[r_channel
][a_i
];
1915 memcpy(u
->Hs
[c
][b_i
], u
->Hs
[r_channel
][a_i
], FILTER_SIZE
* sizeof(float));
1916 pa_aupdate_write_end(u
->a_H
[c
]);
1919 pa_aupdate_write_end(u
->a_H
[r_channel
]);
1922 void equalizer_handle_set_filter(DBusConnection
*conn
, DBusMessage
*msg
, void *_u
){
1927 DBusMessage
*signal
= NULL
;
1929 pa_assert_se(u
= (struct userdata
*) _u
);
1933 dbus_error_init(&error
);
1934 if(!dbus_message_get_args(msg
, &error
,
1935 DBUS_TYPE_UINT32
, &channel
,
1936 DBUS_TYPE_ARRAY
, DBUS_TYPE_DOUBLE
, &H
, &_n_coefs
,
1937 DBUS_TYPE_DOUBLE
, &preamp
,
1938 DBUS_TYPE_INVALID
)){
1939 pa_dbus_send_error(conn
, msg
, DBUS_ERROR_INVALID_ARGS
, "%s", error
.message
);
1940 dbus_error_free(&error
);
1943 if(channel
> u
->channels
){
1944 pa_dbus_send_error(conn
, msg
, DBUS_ERROR_INVALID_ARGS
, "invalid channel: %d", channel
);
1945 dbus_error_free(&error
);
1948 if(_n_coefs
!= FILTER_SIZE
){
1949 pa_dbus_send_error(conn
, msg
, DBUS_ERROR_INVALID_ARGS
, "This filter takes exactly %ld coefficients, you gave %d", FILTER_SIZE
, _n_coefs
);
1952 set_filter(u
, channel
, H
, preamp
);
1954 pa_dbus_send_empty_reply(conn
, msg
);
1956 pa_assert_se((signal
= dbus_message_new_signal(u
->dbus_path
, EQUALIZER_IFACE
, equalizer_signals
[EQUALIZER_SIGNAL_FILTER_CHANGED
].name
)));
1957 pa_dbus_protocol_send_signal(u
->dbus_protocol
, signal
);
1958 dbus_message_unref(signal
);
1961 void equalizer_handle_save_profile(DBusConnection
*conn
, DBusMessage
*msg
, void *_u
) {
1962 struct userdata
*u
= (struct userdata
*) _u
;
1964 uint32_t channel
, r_channel
;
1965 DBusMessage
*signal
= NULL
;
1970 dbus_error_init(&error
);
1972 if(!dbus_message_get_args(msg
, &error
,
1973 DBUS_TYPE_UINT32
, &channel
,
1974 DBUS_TYPE_STRING
, &name
,
1975 DBUS_TYPE_INVALID
)){
1976 pa_dbus_send_error(conn
, msg
, DBUS_ERROR_INVALID_ARGS
, "%s", error
.message
);
1977 dbus_error_free(&error
);
1980 if(channel
> u
->channels
){
1981 pa_dbus_send_error(conn
, msg
, DBUS_ERROR_INVALID_ARGS
, "invalid channel: %d", channel
);
1982 dbus_error_free(&error
);
1985 r_channel
= channel
== u
->channels
? 0 : channel
;
1986 save_profile(u
, r_channel
, name
);
1987 pa_dbus_send_empty_reply(conn
, msg
);
1989 pa_assert_se((signal
= dbus_message_new_signal(MANAGER_PATH
, MANAGER_IFACE
, manager_signals
[MANAGER_SIGNAL_PROFILES_CHANGED
].name
)));
1990 pa_dbus_protocol_send_signal(u
->dbus_protocol
, signal
);
1991 dbus_message_unref(signal
);
1994 void equalizer_handle_load_profile(DBusConnection
*conn
, DBusMessage
*msg
, void *_u
) {
1995 struct userdata
*u
= (struct userdata
*) _u
;
1998 uint32_t channel
, r_channel
;
1999 const char *err_msg
= NULL
;
2000 DBusMessage
*signal
= NULL
;
2005 dbus_error_init(&error
);
2007 if(!dbus_message_get_args(msg
, &error
,
2008 DBUS_TYPE_UINT32
, &channel
,
2009 DBUS_TYPE_STRING
, &name
,
2010 DBUS_TYPE_INVALID
)){
2011 pa_dbus_send_error(conn
, msg
, DBUS_ERROR_INVALID_ARGS
, "%s", error
.message
);
2012 dbus_error_free(&error
);
2015 if(channel
> u
->channels
){
2016 pa_dbus_send_error(conn
, msg
, DBUS_ERROR_INVALID_ARGS
, "invalid channel: %d", channel
);
2017 dbus_error_free(&error
);
2020 r_channel
= channel
== u
->channels
? 0 : channel
;
2022 err_msg
= load_profile(u
, r_channel
, name
);
2023 if(err_msg
!= NULL
){
2024 pa_dbus_send_error(conn
, msg
, DBUS_ERROR_FAILED
, "error loading profile %s: %s", name
, err_msg
);
2025 dbus_error_free(&error
);
2028 if(channel
== u
->channels
){
2029 for(uint32_t c
= 1; c
< u
->channels
; ++c
){
2030 load_profile(u
, c
, name
);
2033 pa_dbus_send_empty_reply(conn
, msg
);
2035 pa_assert_se((signal
= dbus_message_new_signal(u
->dbus_path
, EQUALIZER_IFACE
, equalizer_signals
[EQUALIZER_SIGNAL_FILTER_CHANGED
].name
)));
2036 pa_dbus_protocol_send_signal(u
->dbus_protocol
, signal
);
2037 dbus_message_unref(signal
);
2040 void equalizer_handle_save_state(DBusConnection
*conn
, DBusMessage
*msg
, void *_u
) {
2041 struct userdata
*u
= (struct userdata
*) _u
;
2047 pa_dbus_send_empty_reply(conn
, msg
);
2050 void equalizer_handle_get_profile_name(DBusConnection
*conn
, DBusMessage
*msg
, void *_u
){
2051 struct userdata
*u
= (struct userdata
*) _u
;
2053 uint32_t channel
, r_channel
;
2058 dbus_error_init(&error
);
2060 if(!dbus_message_get_args(msg
, &error
,
2061 DBUS_TYPE_UINT32
, &channel
,
2062 DBUS_TYPE_INVALID
)){
2063 pa_dbus_send_error(conn
, msg
, DBUS_ERROR_INVALID_ARGS
, "%s", error
.message
);
2064 dbus_error_free(&error
);
2067 if(channel
> u
->channels
){
2068 pa_dbus_send_error(conn
, msg
, DBUS_ERROR_INVALID_ARGS
, "invalid channel: %d", channel
);
2069 dbus_error_free(&error
);
2072 r_channel
= channel
== u
->channels
? 0 : channel
;
2073 pa_assert(u
->base_profiles
[r_channel
]);
2074 pa_dbus_send_basic_value_reply(conn
,msg
, DBUS_TYPE_STRING
, &u
->base_profiles
[r_channel
]);
2077 void equalizer_get_revision(DBusConnection
*conn
, DBusMessage
*msg
, void *_u
){
2079 pa_dbus_send_basic_value_reply(conn
, msg
, DBUS_TYPE_UINT32
, &rev
);
2082 void equalizer_get_n_channels(DBusConnection
*conn
, DBusMessage
*msg
, void *_u
){
2085 pa_assert_se(u
= (struct userdata
*) _u
);
2089 channels
= (uint32_t) u
->channels
;
2090 pa_dbus_send_basic_variant_reply(conn
, msg
, DBUS_TYPE_UINT32
, &channels
);
2093 void equalizer_get_n_coefs(DBusConnection
*conn
, DBusMessage
*msg
, void *_u
){
2096 pa_assert_se(u
= (struct userdata
*) _u
);
2100 n_coefs
= (uint32_t) CHANNEL_PROFILE_SIZE
;
2101 pa_dbus_send_basic_variant_reply(conn
, msg
, DBUS_TYPE_UINT32
, &n_coefs
);
2104 void equalizer_get_sample_rate(DBusConnection
*conn
, DBusMessage
*msg
, void *_u
){
2107 pa_assert_se(u
= (struct userdata
*) _u
);
2111 rate
= (uint32_t) u
->sink
->sample_spec
.rate
;
2112 pa_dbus_send_basic_variant_reply(conn
, msg
, DBUS_TYPE_UINT32
, &rate
);
2115 void equalizer_get_filter_rate(DBusConnection
*conn
, DBusMessage
*msg
, void *_u
){
2118 pa_assert_se(u
= (struct userdata
*) _u
);
2122 fft_size
= (uint32_t) u
->fft_size
;
2123 pa_dbus_send_basic_variant_reply(conn
, msg
, DBUS_TYPE_UINT32
, &fft_size
);
2126 void equalizer_get_all(DBusConnection
*conn
, DBusMessage
*msg
, void *_u
){
2128 DBusMessage
*reply
= NULL
;
2129 DBusMessageIter msg_iter
, dict_iter
;
2130 uint32_t rev
, n_coefs
, rate
, fft_size
, channels
;
2131 pa_assert_se(u
= (struct userdata
*) _u
);
2135 n_coefs
= (uint32_t) CHANNEL_PROFILE_SIZE
;
2136 rate
= (uint32_t) u
->sink
->sample_spec
.rate
;
2137 fft_size
= (uint32_t) u
->fft_size
;
2138 channels
= (uint32_t) u
->channels
;
2140 pa_assert_se((reply
= dbus_message_new_method_return(msg
)));
2141 dbus_message_iter_init_append(reply
, &msg_iter
);
2142 pa_assert_se(dbus_message_iter_open_container(&msg_iter
, DBUS_TYPE_ARRAY
, "{sv}", &dict_iter
));
2144 pa_dbus_append_basic_variant_dict_entry(&dict_iter
, equalizer_handlers
[EQUALIZER_HANDLER_REVISION
].property_name
, DBUS_TYPE_UINT32
, &rev
);
2145 pa_dbus_append_basic_variant_dict_entry(&dict_iter
, equalizer_handlers
[EQUALIZER_HANDLER_SAMPLERATE
].property_name
, DBUS_TYPE_UINT32
, &rate
);
2146 pa_dbus_append_basic_variant_dict_entry(&dict_iter
, equalizer_handlers
[EQUALIZER_HANDLER_FILTERSAMPLERATE
].property_name
, DBUS_TYPE_UINT32
, &fft_size
);
2147 pa_dbus_append_basic_variant_dict_entry(&dict_iter
, equalizer_handlers
[EQUALIZER_HANDLER_N_COEFS
].property_name
, DBUS_TYPE_UINT32
, &n_coefs
);
2148 pa_dbus_append_basic_variant_dict_entry(&dict_iter
, equalizer_handlers
[EQUALIZER_HANDLER_N_CHANNELS
].property_name
, DBUS_TYPE_UINT32
, &channels
);
2150 pa_assert_se(dbus_message_iter_close_container(&msg_iter
, &dict_iter
));
2151 pa_assert_se(dbus_connection_send(conn
, reply
, NULL
));
2152 dbus_message_unref(reply
);