]> code.delx.au - pulseaudio/blobdiff - src/modules/echo-cancel/module-echo-cancel.c
devices: Set certain sink/source flags automatically.
[pulseaudio] / src / modules / echo-cancel / module-echo-cancel.c
index 90577d4cbcab092c0dcb15dff043f8212a6579a4..62b436b66fa1870fe325f80bf0762d91d94ca384 100644 (file)
@@ -31,7 +31,6 @@
 #endif
 
 #include <stdio.h>
-#include <math.h>
 
 #include "echo-cancel.h"
 
 
 #include <pulsecore/atomic.h>
 #include <pulsecore/macro.h>
-#include <pulsecore/core-error.h>
 #include <pulsecore/namereg.h>
 #include <pulsecore/sink.h>
 #include <pulsecore/module.h>
 #include <pulsecore/core-rtclock.h>
 #include <pulsecore/core-util.h>
-#include <pulsecore/core-error.h>
 #include <pulsecore/modargs.h>
 #include <pulsecore/log.h>
-#include <pulsecore/thread.h>
-#include <pulsecore/thread-mq.h>
 #include <pulsecore/rtpoll.h>
 #include <pulsecore/sample-util.h>
 #include <pulsecore/ltdl-helper.h>
@@ -79,7 +74,9 @@ PA_MODULE_USAGE(
           "aec_args=<parameters for the AEC engine> "
           "agc=<perform automagic gain control?> "
           "denoise=<apply denoising?> "
-          "echo_suppress=<perform echo suppression? (only with the speex canceller)> "
+          "echo_suppress=<perform residual echo suppression? (only with the speex canceller)> "
+          "echo_suppress_attenuation=<dB value of residual echo attenuation> "
+          "echo_suppress_attenuation_active=<dB value of residual echo attenuation when near end is active> "
           "save_aec=<save AEC data in /tmp> "
           "autoloaded=<set if this module is being loaded automatically> "
         ));
@@ -108,10 +105,13 @@ static const pa_echo_canceller ec_table[] = {
     },
 };
 
+#define DEFAULT_RATE 32000
+#define DEFAULT_CHANNELS 1
 #define DEFAULT_ADJUST_TIME_USEC (1*PA_USEC_PER_SEC)
-#define DEFAULT_AGC_ENABLED FALSE
-#define DEFAULT_DENOISE_ENABLED FALSE
-#define DEFAULT_ECHO_SUPPRESS_ENABLED FALSE
+#define DEFAULT_AGC_ENABLED TRUE
+#define DEFAULT_DENOISE_ENABLED TRUE
+#define DEFAULT_ECHO_SUPPRESS_ENABLED TRUE
+#define DEFAULT_ECHO_SUPPRESS_ATTENUATION 0
 #define DEFAULT_SAVE_AEC 0
 #define DEFAULT_AUTOLOADED FALSE
 
@@ -221,6 +221,8 @@ static const char* const valid_modargs[] = {
     "agc",
     "denoise",
     "echo_suppress",
+    "echo_suppress_attenuation",
+    "echo_suppress_attenuation_active",
     "save_aec",
     "autoloaded",
     NULL
@@ -521,8 +523,7 @@ static void source_set_volume_cb(pa_source *s) {
         !PA_SOURCE_OUTPUT_IS_LINKED(pa_source_output_get_state(u->source_output)))
         return;
 
-    /* FIXME, no volume control in source_output, set volume at the master */
-    pa_source_set_volume(u->source_output->source, &s->volume, TRUE);
+    pa_source_output_set_volume(u->source_output, &s->real_volume, s->save_volume, TRUE);
 }
 
 /* Called from main context */
@@ -541,6 +542,7 @@ static void sink_set_volume_cb(pa_sink *s) {
 
 static void source_get_volume_cb(pa_source *s) {
     struct userdata *u;
+    pa_cvolume v;
 
     pa_source_assert_ref(s);
     pa_assert_se(u = s->userdata);
@@ -549,18 +551,16 @@ static void source_get_volume_cb(pa_source *s) {
         !PA_SOURCE_OUTPUT_IS_LINKED(pa_source_output_get_state(u->source_output)))
         return;
 
-    /* FIXME, no volume control in source_output, get the info from the master */
-    pa_source_get_volume(u->source_output->source, TRUE);
+    pa_source_output_get_volume(u->source_output, &v, TRUE);
 
-    if (pa_cvolume_equal(&s->volume,&u->source_output->source->volume))
+    if (pa_cvolume_equal(&s->real_volume, &v))
         /* no change */
         return;
 
-    s->volume = u->source_output->source->volume;
+    s->real_volume = v;
     pa_source_set_soft_volume(s, NULL);
 }
 
-
 /* Called from main context */
 static void source_set_mute_cb(pa_source *s) {
     struct userdata *u;
@@ -572,8 +572,7 @@ static void source_set_mute_cb(pa_source *s) {
         !PA_SOURCE_OUTPUT_IS_LINKED(pa_source_output_get_state(u->source_output)))
         return;
 
-    /* FIXME, no volume control in source_output, set mute at the master */
-    pa_source_set_mute(u->source_output->source, TRUE, TRUE);
+    pa_source_output_set_mute(u->source_output, s->muted, s->save_muted);
 }
 
 /* Called from main context */
@@ -601,8 +600,7 @@ static void source_get_mute_cb(pa_source *s) {
         !PA_SOURCE_OUTPUT_IS_LINKED(pa_source_output_get_state(u->source_output)))
         return;
 
-    /* FIXME, no volume control in source_output, get the info from the master */
-    pa_source_get_mute(u->source_output->source, TRUE);
+    pa_source_output_get_mute(u->source_output);
 }
 
 /* must be called from the input thread context */
@@ -1363,7 +1361,9 @@ int pa__init(pa_module*m) {
     pa_assert(sink_master);
 
     source_ss = source_master->sample_spec;
-    source_map = source_master->channel_map;
+    source_ss.rate = DEFAULT_RATE;
+    source_ss.channels = DEFAULT_CHANNELS;
+    pa_channel_map_init_auto(&source_map, source_ss.channels, PA_CHANNEL_MAP_DEFAULT);
     if (pa_modargs_get_sample_spec_and_channel_map(ma, &source_ss, &source_map, PA_CHANNEL_MAP_DEFAULT) < 0) {
         pa_log("Invalid sample format specification or channel map");
         goto fail;
@@ -1429,6 +1429,26 @@ int pa__init(pa_module*m) {
         goto fail;
     }
 
+    u->ec->echo_suppress_attenuation = DEFAULT_ECHO_SUPPRESS_ATTENUATION;
+    if (pa_modargs_get_value_s32(ma, "echo_suppress_attenuation", &u->ec->echo_suppress_attenuation) < 0) {
+        pa_log("Failed to parse echo_suppress_attenuation value");
+        goto fail;
+    }
+    if (u->ec->echo_suppress_attenuation > 0) {
+        pa_log("echo_suppress_attenuation should be a negative dB value");
+        goto fail;
+    }
+
+    u->ec->echo_suppress_attenuation_active = DEFAULT_ECHO_SUPPRESS_ATTENUATION;
+    if (pa_modargs_get_value_s32(ma, "echo_suppress_attenuation_active", &u->ec->echo_suppress_attenuation_active) < 0) {
+        pa_log("Failed to parse echo_supress_attenuation_active value");
+        goto fail;
+    }
+    if (u->ec->echo_suppress_attenuation_active > 0) {
+        pa_log("echo_suppress_attenuation_active should be a negative dB value");
+        goto fail;
+    }
+
     u->save_aec = DEFAULT_SAVE_AEC;
     if (pa_modargs_get_value_u32(ma, "save_aec", &u->save_aec) < 0) {
         pa_log("Failed to parse save_aec value");
@@ -1451,18 +1471,28 @@ int pa__init(pa_module*m) {
     }
 
     if (u->ec->agc || u->ec->denoise || u->ec->echo_suppress) {
+        spx_int32_t tmp;
+
         if (source_ss.channels != 1) {
             pa_log("AGC, denoising and echo suppression only work with channels=1");
             goto fail;
         }
 
-        u->ec->pp_state = speex_preprocess_state_init(u->blocksize, source_ss.rate);
-
-        speex_preprocess_ctl(u->ec->pp_state, SPEEX_PREPROCESS_SET_AGC, &u->ec->agc);
-        speex_preprocess_ctl(u->ec->pp_state, SPEEX_PREPROCESS_SET_DENOISE, &u->ec->denoise);
-        speex_preprocess_ctl(u->ec->pp_state, SPEEX_PREPROCESS_SET_ECHO_SUPPRESS, &u->ec->echo_suppress);
-        if (u->ec->echo_suppress)
+        u->ec->pp_state = speex_preprocess_state_init(u->blocksize / pa_frame_size(&source_ss), source_ss.rate);
+
+        tmp = u->ec->agc;
+        speex_preprocess_ctl(u->ec->pp_state, SPEEX_PREPROCESS_SET_AGC, &tmp);
+        tmp = u->ec->denoise;
+        speex_preprocess_ctl(u->ec->pp_state, SPEEX_PREPROCESS_SET_DENOISE, &tmp);
+        if (u->ec->echo_suppress) {
+            if (u->ec->echo_suppress_attenuation)
+                speex_preprocess_ctl(u->ec->pp_state, SPEEX_PREPROCESS_SET_ECHO_SUPPRESS, &u->ec->echo_suppress_attenuation);
+            if (u->ec->echo_suppress_attenuation_active) {
+                speex_preprocess_ctl(u->ec->pp_state, SPEEX_PREPROCESS_SET_ECHO_SUPPRESS_ACTIVE,
+                                     &u->ec->echo_suppress_attenuation_active);
+            }
             speex_preprocess_ctl(u->ec->pp_state, SPEEX_PREPROCESS_SET_ECHO_STATE, u->ec->params.priv.speex.state);
+        }
     }
 
     /* Create source */
@@ -1493,7 +1523,7 @@ int pa__init(pa_module*m) {
     }
 
     u->source = pa_source_new(m->core, &source_data,
-                          PA_SOURCE_HW_MUTE_CTRL|PA_SOURCE_HW_VOLUME_CTRL|PA_SOURCE_DECIBEL_VOLUME|
+                          PA_SOURCE_DECIBEL_VOLUME|
                           (source_master->flags & (PA_SOURCE_LATENCY|PA_SOURCE_DYNAMIC_LATENCY)));
     pa_source_new_data_done(&source_data);
 
@@ -1505,10 +1535,10 @@ int pa__init(pa_module*m) {
     u->source->parent.process_msg = source_process_msg_cb;
     u->source->set_state = source_set_state_cb;
     u->source->update_requested_latency = source_update_requested_latency_cb;
-    u->source->set_volume = source_set_volume_cb;
-    u->source->set_mute = source_set_mute_cb;
-    u->source->get_volume = source_get_volume_cb;
-    u->source->get_mute = source_get_mute_cb;
+    pa_source_set_get_volume_callback(u->source, source_get_volume_cb);
+    pa_source_set_set_volume_callback(u->source, source_set_volume_cb);
+    pa_source_set_get_mute_callback(u->source, source_get_mute_cb);
+    pa_source_set_set_mute_callback(u->source, source_set_mute_cb);
     u->source->userdata = u;
 
     pa_source_set_asyncmsgq(u->source, source_master->asyncmsgq);
@@ -1541,7 +1571,7 @@ int pa__init(pa_module*m) {
     }
 
     u->sink = pa_sink_new(m->core, &sink_data,
-                          PA_SINK_HW_MUTE_CTRL|PA_SINK_HW_VOLUME_CTRL|PA_SINK_DECIBEL_VOLUME|
+                          PA_SINK_DECIBEL_VOLUME|
                           (sink_master->flags & (PA_SINK_LATENCY|PA_SINK_DYNAMIC_LATENCY)));
     pa_sink_new_data_done(&sink_data);
 
@@ -1554,8 +1584,8 @@ int pa__init(pa_module*m) {
     u->sink->set_state = sink_set_state_cb;
     u->sink->update_requested_latency = sink_update_requested_latency_cb;
     u->sink->request_rewind = sink_request_rewind_cb;
-    u->sink->set_volume = sink_set_volume_cb;
-    u->sink->set_mute = sink_set_mute_cb;
+    pa_sink_set_set_volume_callback(u->sink, sink_set_volume_cb);
+    pa_sink_set_set_mute_callback(u->sink, sink_set_mute_cb);
     u->sink->userdata = u;
 
     pa_sink_set_asyncmsgq(u->sink, sink_master->asyncmsgq);
@@ -1564,7 +1594,7 @@ int pa__init(pa_module*m) {
     pa_source_output_new_data_init(&source_output_data);
     source_output_data.driver = __FILE__;
     source_output_data.module = m;
-    source_output_data.source = source_master;
+    pa_source_output_new_data_set_source(&source_output_data, source_master, FALSE);
     source_output_data.destination_source = u->source;
     /* FIXME
        source_output_data.flags = PA_SOURCE_OUTPUT_DONT_INHIBIT_AUTO_SUSPEND; */