From 1d7dea17d56be2a4a79725dd3ab7c906e2793c2b Mon Sep 17 00:00:00 2001
From: Jean-Marc Valin <jmvalin@jmvalin.ca>
Date: Sun, 4 Jun 2017 17:45:06 -0400
Subject: [PATCH] Initialize speech/music prob based on application

---
 src/analysis.c     | 15 +++++++++++----
 src/analysis.h     |  1 +
 src/opus_encoder.c |  4 ++++
 3 files changed, 16 insertions(+), 4 deletions(-)

diff --git a/src/analysis.c b/src/analysis.c
index 8f13d93b9..75289102a 100644
--- a/src/analysis.c
+++ b/src/analysis.c
@@ -360,8 +360,12 @@ static void tonality_analysis(TonalityAnalysisState *tonal, const CELTMode *celt
        offset = 3*offset/2;
     }
 
-    if (tonal->count<4)
-       tonal->music_prob = .5;
+    if (tonal->count<4) {
+       if (tonal->application == OPUS_APPLICATION_VOIP)
+          tonal->music_prob = .1;
+       else
+          tonal->music_prob = .625;
+    }
     kfft = celt_mode->mdct.kfft[0];
     if (tonal->count==0)
        tonal->mem_fill = 240;
@@ -820,8 +824,11 @@ static void tonality_analysis(TonalityAnalysisState *tonal, const CELTMode *celt
        music0  = (float)pow(frame_probs[0], beta);
        if (tonal->count==1)
        {
-          tonal->pspeech[0]=.5;
-          tonal->pmusic [0]=.5;
+          if (tonal->application == OPUS_APPLICATION_VOIP)
+             tonal->pmusic[0] = .1;
+          else
+             tonal->pmusic[0] = .625;
+          tonal->pspeech[0] = 1-tonal->pmusic[0];
        }
        /* Updated probability of having only speech (s0) or only music (m0),
           before considering the new observation. */
diff --git a/src/analysis.h b/src/analysis.h
index 826773e57..b94a0c91f 100644
--- a/src/analysis.h
+++ b/src/analysis.h
@@ -45,6 +45,7 @@
 
 typedef struct {
    int arch;
+   int application;
    opus_int32 Fs;
 #define TONALITY_ANALYSIS_RESET_START angle
    float angle[240];
diff --git a/src/opus_encoder.c b/src/opus_encoder.c
index 0bd65a028..474e6c248 100644
--- a/src/opus_encoder.c
+++ b/src/opus_encoder.c
@@ -264,6 +264,7 @@ int opus_encoder_init(OpusEncoder* st, opus_int32 Fs, int channels, int applicat
 
 #ifndef DISABLE_FLOAT_API
     tonality_analysis_init(&st->analysis, st->Fs);
+    st->analysis.application = st->application;
 #endif
 
     return OPUS_OK;
@@ -2263,6 +2264,9 @@ int opus_encoder_ctl(OpusEncoder *st, int request, ...)
                break;
             }
             st->application = value;
+#ifndef DISABLE_FLOAT_API
+            st->analysis.application = value;
+#endif
         }
         break;
         case OPUS_GET_APPLICATION_REQUEST:
-- 
GitLab