Home | History | Annotate | Download | only in experiments
      1 /*
      2  * Copyright (C) 2010 The Android Open Source Project
      3  *
      4  * Licensed under the Apache License, Version 2.0 (the "License");
      5  * you may not use this file except in compliance with the License.
      6  * You may obtain a copy of 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,
     12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     13  * See the License for the specific language governing permissions and
     14  * limitations under the License.
     15  */
     16 
     17 package com.android.cts.verifier.audioquality.experiments;
     18 
     19 import com.android.cts.verifier.R;
     20 import com.android.cts.verifier.audioquality.AudioQualityVerifierActivity;
     21 import com.android.cts.verifier.audioquality.Utils;
     22 
     23 import android.content.Context;
     24 
     25 /**
     26  * Experiment to test the linearity of the microphone gain response.
     27  *
     28  * This plays a sequence of identical stimuli at increasing volumes and then
     29  * analyzes the set of recordings.
     30  */
     31 public class GainLinearityExperiment extends SequenceExperiment {
     32     private static final int LEVELS = 4;
     33     private static final float DB_STEP_SIZE = 10.0f;
     34     private static final float TOLERANCE = 2.0f; // Maximum allowed deviation from linearity in dB
     35     private static final int STIM_NUM = 31;
     36 
     37     private short[] mReference = null;
     38 
     39     public GainLinearityExperiment() {
     40         super(true);
     41     }
     42 
     43     @Override
     44     protected String lookupName(Context context) {
     45         return context.getString(R.string.aq_linearity_exp);
     46     }
     47 
     48     @Override
     49     protected int getTrials() {
     50         return LEVELS;
     51     }
     52 
     53     @Override
     54     protected byte[] getStim(Context context, int trial) {
     55         float db = (trial - (LEVELS - 1)) * DB_STEP_SIZE;
     56         if (mReference == null) {
     57             mReference = Utils.byteToShortArray(Utils.getStim(context, STIM_NUM));
     58         }
     59         short[] samples = Utils.scale(mReference, db);
     60         return Utils.shortToByteArray(samples);
     61     }
     62 
     63     @Override
     64     protected void compare(byte[][] stim, byte[][] record) {
     65         short[][] pcms = new short[LEVELS][];
     66         for (int i = 0; i < LEVELS; i++) {
     67             pcms[i] = Utils.byteToShortArray(record[i]);
     68         }
     69         // We specify the middle stimulus (LEVELS / 2) as the "reference":
     70         float deviation = mNative.linearityTest(pcms, AudioQualityVerifierActivity.SAMPLE_RATE,
     71                 DB_STEP_SIZE, LEVELS / 2);
     72         if (deviation < 0.0f) {
     73             setScore(getString(R.string.aq_fail));
     74             setReport(String.format(getString(R.string.aq_linearity_report_error), deviation));
     75         } else if (deviation > TOLERANCE) {
     76             setScore(getString(R.string.aq_fail));
     77             setReport(String.format(getString(R.string.aq_linearity_report_normal),
     78                     deviation, TOLERANCE));
     79         } else {
     80             setScore(getString(R.string.aq_pass));
     81             setReport(String.format(getString(R.string.aq_linearity_report_normal),
     82                     deviation, TOLERANCE));
     83         }
     84     }
     85 }
     86