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.CalibrateVolumeActivity;
     22 import com.android.cts.verifier.audioquality.Native;
     23 import com.android.cts.verifier.audioquality.Utils;
     24 
     25 import android.content.Context;
     26 
     27 /**
     28  * Experiment to test the clipping behaviour of the microphone.
     29  *
     30  * The stimulus is sinusoidal, and calculated to cause clipping for
     31  * part of the waveform. The experiment looks for strange clipping behaviour
     32  * by checking if the signal has any discontinuities (which might indicate
     33  * wraparound, for example).
     34  */
     35 public class OverflowExperiment extends LoopbackExperiment {
     36     private static final float FREQ = 250.0f;
     37     private static final float AMPL = 32768.0f * 1.1f * CalibrateVolumeActivity.OUTPUT_AMPL
     38             / CalibrateVolumeActivity.TARGET_AMPL;
     39     private static final float DURATION = 3.0f; // Duration of tone in seconds
     40     private static final float MIN_DURATION = DURATION * 0.9f;
     41     private static final float RAMP = 0.01f;
     42 
     43     public OverflowExperiment() {
     44         super(true);
     45     }
     46 
     47     @Override
     48     protected String lookupName(Context context) {
     49         return context.getString(R.string.aq_overflow_exp);
     50     }
     51 
     52     @Override
     53     protected byte[] getStim(Context context) {
     54         short[] sinusoid = mNative.generateSinusoid(FREQ, DURATION,
     55                 AudioQualityVerifierActivity.SAMPLE_RATE, AMPL, RAMP);
     56         return Utils.shortToByteArray(sinusoid);
     57     }
     58 
     59     @Override
     60     protected void compare(byte[] stim, byte[] record) {
     61         short[] pcm = Utils.byteToShortArray(record);
     62         float[] ret = mNative.overflowCheck(pcm, AudioQualityVerifierActivity.SAMPLE_RATE);
     63         int numDeltas = Math.round(ret[0]);
     64         float error = ret[Native.OVERFLOW_ERROR];
     65         float duration = ret[Native.OVERFLOW_DURATION];
     66         float minPeak = ret[Native.OVERFLOW_MIN];
     67         float maxPeak = ret[Native.OVERFLOW_MAX];
     68 
     69         if (error < 0.0f) {
     70             setScore(getString(R.string.aq_fail));
     71             setReport(getString(R.string.aq_overflow_report_error));
     72         } else if (duration < MIN_DURATION) {
     73             setScore(getString(R.string.aq_fail));
     74             setReport(String.format(getString(R.string.aq_overflow_report_short),
     75                     DURATION, duration));
     76         } else if (numDeltas > 0) {
     77             setScore(getString(R.string.aq_fail));
     78             setReport(String.format(getString(R.string.aq_overflow_report_fail),
     79                     numDeltas, duration, minPeak, maxPeak));
     80         } else {
     81             setScore(getString(R.string.aq_pass));
     82             setReport(String.format(getString(R.string.aq_overflow_report_pass),
     83                     numDeltas, duration, minPeak, maxPeak));
     84         }
     85    }
     86 }
     87