Home | History | Annotate | Download | only in tts
      1 /*
      2  * Copyright (C) 2013 The Android Open Source Project
      3  *
      4  * Licensed under the Apache License, Version 2.0 (the "License"); you may not
      5  * use this file except in compliance with the License. You may obtain a copy of
      6  * the License at
      7  *
      8  * http://www.apache.org/licenses/LICENSE-2.0
      9  *
     10  * Unless required by applicable law or agreed to in writing, software
     11  * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
     12  * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
     13  * License for the specific language governing permissions and limitations under
     14  * the License.
     15  */
     16 package android.speech.tts;
     17 
     18 import android.text.TextUtils;
     19 
     20 /**
     21  * Writes data about a given speech synthesis request to the event logs. The data that is logged
     22  * includes the calling app, length of the utterance, speech rate / pitch, the latency, and overall
     23  * time taken.
     24  */
     25 class EventLogger extends AbstractEventLogger {
     26     private final SynthesisRequest mRequest;
     27 
     28     EventLogger(SynthesisRequest request, int callerUid, int callerPid, String serviceApp) {
     29         super(callerUid, callerPid, serviceApp);
     30         mRequest = request;
     31     }
     32 
     33     @Override
     34     protected void logFailure(int statusCode) {
     35         // We don't report stopped syntheses because their overall
     36         // total time spent will be inaccurate (will not correlate with
     37         // the length of the utterance).
     38         if (statusCode != TextToSpeech.STOPPED) {
     39             EventLogTags.writeTtsSpeakFailure(mServiceApp, mCallerUid, mCallerPid,
     40                     getUtteranceLength(), getLocaleString(),
     41                     mRequest.getSpeechRate(), mRequest.getPitch());
     42         }
     43     }
     44 
     45     @Override
     46     protected void logSuccess(long audioLatency, long engineLatency, long engineTotal) {
     47         EventLogTags.writeTtsSpeakSuccess(mServiceApp, mCallerUid, mCallerPid,
     48                 getUtteranceLength(), getLocaleString(),
     49                 mRequest.getSpeechRate(), mRequest.getPitch(),
     50                 engineLatency, engineTotal, audioLatency);
     51     }
     52 
     53     /**
     54      * @return the length of the utterance for the given synthesis, 0
     55      *          if the utterance was {@code null}.
     56      */
     57     private int getUtteranceLength() {
     58         final String utterance = mRequest.getText();
     59         return utterance == null ? 0 : utterance.length();
     60     }
     61 
     62     /**
     63      * Returns a formatted locale string from the synthesis params of the
     64      * form lang-country-variant.
     65      */
     66     private String getLocaleString() {
     67         StringBuilder sb = new StringBuilder(mRequest.getLanguage());
     68         if (!TextUtils.isEmpty(mRequest.getCountry())) {
     69             sb.append('-');
     70             sb.append(mRequest.getCountry());
     71 
     72             if (!TextUtils.isEmpty(mRequest.getVariant())) {
     73                 sb.append('-');
     74                 sb.append(mRequest.getVariant());
     75             }
     76         }
     77 
     78         return sb.toString();
     79     }
     80 }
     81