[PATCH 1/4] Give the 4 streams clearer names

Neil Jerram neil at ossau.homelinux.net
Wed Nov 28 22:48:49 CET 2012


---
 gsm-voice-routing.c |  100 +++++++++++++++++++++++++--------------------------
 1 file changed, 50 insertions(+), 50 deletions(-)

diff --git a/gsm-voice-routing.c b/gsm-voice-routing.c
index 55b7a5d..f607dbc 100644
--- a/gsm-voice-routing.c
+++ b/gsm-voice-routing.c
@@ -30,12 +30,12 @@ arecord -Dhw:1,0 -fS16_LE | aplay
 However the program is written in C for better control over the process. E.g.
 we want to wait with routing until sound is available from UMTS.
 
-We have 4 streams called r0, p1, r1, p0
+We have 4 streams called r_mic, p_mod, r_mod, p_ear
 
-r0 - record from hw:0,0 (default) internal sound card
-r1 - record from hw:1,0 umts sound card
-p0 - play on hw:0,0 (default) internal sound card
-p1 - play on hw:1,0 umts sound card
+r_mic - record from hw:0,0 (default) internal sound card (i.e. the microphone)
+r_mod - record from hw:1,0 modem sound card
+p_ear - play on hw:0,0 (default) internal sound card (i.e. the earpiece)
+p_mod - play on hw:1,0 modem sound card
 
 All streams have rate 8000 (rate of umts sound card), 1 channel and 16bit per
 sample (SND_PCM_FORMAT_S16_LE).
@@ -102,7 +102,7 @@ int terminating = 0;
 
 struct route_stream
 {
-    const char *id;             // in: one of r0, r1, p0, p1
+    const char *id;             // in: one of r_mic, r_mod, p_ear, p_mod
     char *pcm_name;             // in: "default" or "hw:1,0"
     snd_pcm_stream_t stream;    // in: SND_PCM_STREAM_PLAYBACK / SND_PCM_STREAM_CAPTURE
     snd_pcm_uframes_t start_threshold;  // in: start treshold or 0 to keep default
@@ -439,41 +439,41 @@ static void vol_down(char *buf, snd_pcm_uframes_t period_size)
    We use integer for computing volumes, this should be fine if the buffer size
    is not too big (65535 is max).
 */
-static void reduce_echo(char *p0, char *p1, snd_pcm_uframes_t period_size)
+static void reduce_echo(char *p_ear, char *p_mod, snd_pcm_uframes_t period_size)
 {
     int i;
-    int sum_p0 = 0;
-    int sum_p1 = 0;
+    int sum_p_ear = 0;
+    int sum_p_mod = 0;
     s16 *v0;
     s16 *v1;
 
-    v0 = (s16 *) (p0);
-    v1 = (s16 *) (p1);
+    v0 = (s16 *) (p_ear);
+    v1 = (s16 *) (p_mod);
     for (i = 0; i < period_size; i++) {
-        sum_p0 += abs(*v0);
-        sum_p1 += abs(*v1);
+        sum_p_ear += abs(*v0);
+        sum_p_mod += abs(*v1);
         v0++;
         v1++;
     }
 
-    int diff = sum_p0 - sum_p1;
+    int diff = sum_p_ear - sum_p_mod;
 
     /* 10000 seems to be good limit value. Silence is ~2000 and speech is
        ~80000. It would be nice to somehow compute this value on the fly, but
        i have no idea how to do it now */
     if (diff > 10000) {
-        /* fprintf(stderr, "listening p0=%*d    p1=%*d    diff=%*d\n", -6, sum_p0,
-           -6, sum_p1, -6, diff); */
-        vol_up(p0, period_size);
-        vol_down(p1, period_size);
+        /* fprintf(stderr, "listening p_ear=%*d    p_mod=%*d    diff=%*d\n", -6, sum_p_ear,
+           -6, sum_p_mod, -6, diff); */
+        vol_up(p_ear, period_size);
+        vol_down(p_mod, period_size);
         set_aux_leds(0, 1);
         return;
 
     } else if (diff < -10000) {
-        /* fprintf(stderr, "talking   p0=%*d    p1=%*d    diff=%*d\n", -6, sum_p0,
-           -6, sum_p1, -6, diff); */
-        vol_up(p1, period_size);
-        vol_down(p0, period_size);
+        /* fprintf(stderr, "talking   p_ear=%*d    p_mod=%*d    diff=%*d\n", -6, sum_p_ear,
+           -6, sum_p_mod, -6, diff); */
+        vol_up(p_mod, period_size);
+        vol_down(p_ear, period_size);
         set_aux_leds(1, 0);
     } else {
         set_aux_leds(0, 0);
@@ -482,8 +482,8 @@ static void reduce_echo(char *p0, char *p1, snd_pcm_uframes_t period_size)
 }
 #endif
 
-struct route_stream p0 = {
-    .id = "p0",
+struct route_stream p_ear = {
+    .id = "p_ear",
     .pcm_name = "default",
     .stream = SND_PCM_STREAM_PLAYBACK,
     .start_threshold = 1024,
@@ -494,8 +494,8 @@ struct route_stream p0 = {
     .period_buffer = 0
 };
 
-struct route_stream r0 = {
-    .id = "r0",
+struct route_stream r_mic = {
+    .id = "r_mic",
     .pcm_name = "default",
     .stream = SND_PCM_STREAM_CAPTURE,
     .start_threshold = 0,
@@ -506,8 +506,8 @@ struct route_stream r0 = {
     .period_buffer = 0
 };
 
-struct route_stream p1 = {
-    .id = "p1",
+struct route_stream p_mod = {
+    .id = "p_mod",
     .pcm_name = "hw:1,0",
     .stream = SND_PCM_STREAM_PLAYBACK,
     .start_threshold = 1024,
@@ -518,8 +518,8 @@ struct route_stream p1 = {
     .period_buffer = 0
 };
 
-struct route_stream r1 = {
-    .id = "r1",
+struct route_stream r_mod = {
+    .id = "r_mod",
     .pcm_name = "hw:1,0",
     .stream = SND_PCM_STREAM_CAPTURE,
     .start_threshold = 0,
@@ -532,10 +532,10 @@ struct route_stream r1 = {
 
 static void cleanup()
 {
-    close_route_stream(&p0);
-    close_route_stream(&p1);
-    close_route_stream(&r0);
-    close_route_stream(&r1);
+    close_route_stream(&p_ear);
+    close_route_stream(&p_mod);
+    close_route_stream(&r_mic);
+    close_route_stream(&r_mod);
     
     set_aux_leds(0, 0);
     fclose(logfile);
@@ -590,23 +590,23 @@ int main()
     echo_state = speex_echo_state_init(256, 8192);
 #endif
 
-    /* Open streams - umts first */
-    open_route_stream_repeated(&p1);
-    open_route_stream_repeated(&r1);
-    open_route_stream_repeated(&p0);
-    open_route_stream_repeated(&r0);
+    /* Open streams - modem first */
+    open_route_stream_repeated(&p_mod);
+    open_route_stream_repeated(&r_mod);
+    open_route_stream_repeated(&p_ear);
+    open_route_stream_repeated(&r_mic);
 
     /* Route sound */
     while (!terminating) {
 
         /* Recording  - first from internal card (so that we always clean the
            recording buffer), then UMTS, which can fail */
-        if (route_stream_read(&r0)) {
+        if (route_stream_read(&r_mic)) {
             blink_aux();
             continue;
         }
 
-        rc = route_stream_read(&r1);
+        rc = route_stream_read(&r_mod);
         if (rc == ERR_READ && started) {
             fprintf(logfile,
                     "read error after some succesful routing (hangup)\n");
@@ -624,21 +624,21 @@ int main()
         }
 
 #ifdef USE_SPEEX_AEC
-        speex_echo_cancellation(echo_state, (spx_int16_t *) r0.period_buffer,
-                                (spx_int16_t *) p0.period_buffer,
-                                (spx_int16_t *) p1.period_buffer);
+        speex_echo_cancellation(echo_state, (spx_int16_t *) r_mic.period_buffer,
+                                (spx_int16_t *) p_ear.period_buffer,
+                                (spx_int16_t *) p_mod.period_buffer);
 
-        memmove(p0.period_buffer, r1.period_buffer, r1.period_buffer_size);
+        memmove(p_ear.period_buffer, r_mod.period_buffer, r_mod.period_buffer_size);
 #endif
 
 #ifdef USE_WALKIE_TALKIE_AEC
-        memmove(p0.period_buffer, r1.period_buffer, r1.period_buffer_size);
-        memmove(p1.period_buffer, r0.period_buffer, r0.period_buffer_size);
-        reduce_echo(p0.period_buffer, p1.period_buffer, p0.period_size);
+        memmove(p_ear.period_buffer, r_mod.period_buffer, r_mod.period_buffer_size);
+        memmove(p_mod.period_buffer, r_mic.period_buffer, r_mic.period_buffer_size);
+        reduce_echo(p_ear.period_buffer, p_mod.period_buffer, p_ear.period_size);
 #endif
 
-        route_stream_write(&p0);
-        route_stream_write(&p1);
+        route_stream_write(&p_ear);
+        route_stream_write(&p_mod);
     }
 
 #ifdef USE_SPEEX_AEC
-- 
1.7.10.4


--=-=-=
Content-Type: text/x-diff
Content-Disposition: attachment;
 filename=0002-Avoid-instability-in-A3-voice-routing-code.patch



More information about the community mailing list