1 /*
2  * Copyright (C) 2015 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.audio.wavelib;
18 
19 import android.util.Log;
20 
21 public class VectorAverage {
22     private static final String LOGTAG = "VectorAverage";
23     private static final int mVersion = 0;
24     private double[] mData;
25     private int mValueCount = 0;
26 
setData(double[] data, boolean replace)27     public void setData(double[] data, boolean replace) {
28         int size = data.length;
29         if (mData == null || mData.length != size) {
30             mData = new double[size];
31             mValueCount = 0;
32         }
33         if (replace || mValueCount == 0) {
34             System.arraycopy(data, 0, mData, 0, size);
35             mValueCount = 1;
36         } else {
37             for (int i = 0; i < size; i++) {
38                 mData[i] += data[i];
39             }
40             mValueCount++;
41         }
42     }
43 
getData(double[] data, boolean raw)44     public int getData(double[] data, boolean raw) {
45         int nCount = 0;
46         if (mData != null && mData.length <= data.length) {
47             nCount = mData.length;
48             if (mValueCount == 0) {
49                 for (int i = 0; i < nCount; i++) {
50                     data[i] = 0;
51                 }
52             } else if (!raw && mValueCount > 1) {
53                 for (int i = 0; i < nCount; i++) {
54                     data[i] = mData[i] / mValueCount;
55                 }
56             } else {
57                 for (int i = 0; i < nCount; i++) {
58                     data[i] = mData[i];
59                 }
60             }
61         }
62         return nCount;
63     }
64 
getCount()65     public int getCount() {
66         return mValueCount;
67     }
68 
getSize()69     public int getSize() {
70         if (mData != null) {
71             return mData.length;
72         }
73         return 0;
74     }
75 
reset()76     public void reset() {
77         mValueCount = 0;
78     }
79 
80     private final String SERIALIZED_VERSION = "VECTOR_AVERAGE_VERSION";
81     private final String SERIALIZED_COUNT = "COUNT";
82 
toString()83     public String toString() {
84         StringBuffer sb = new StringBuffer();
85 
86         //version
87         sb.append(SERIALIZED_VERSION +"="+ mVersion +"\n");
88 
89         double[] data = new double[getSize()];
90         getData(data,false);
91 
92         //element count
93         int nCount = data.length;
94         sb.append(SERIALIZED_COUNT + "=" + nCount +"\n");
95 
96         for (int i = 0; i < nCount; i++) {
97             sb.append(String.format("%f\n",data[i]));
98         }
99 
100         return sb.toString();
101     }
102 
initFromString(String string)103     public boolean initFromString(String string) {
104         boolean success = false;
105 
106         String[] lines = string.split(System.getProperty("line.separator"));
107 
108         int lineCount = lines.length;
109         if (lineCount > 3) {
110             int nVersion = -1;
111             int nCount = -1;
112             int nIndex = 0;
113 
114             //search for version:
115             while (nIndex < lineCount) {
116                 String[] separated = lines[nIndex].split("=");
117                 nIndex++;
118                 if (separated.length > 1 && separated[0].equalsIgnoreCase(SERIALIZED_VERSION)) {
119                     nVersion = Integer.parseInt(separated[1]);
120                     break;
121                 }
122             }
123 
124             if (nVersion >= 0) {
125                 //get count
126 
127                 while (nIndex < lineCount) {
128                     String[] separated = lines[nIndex].split("=");
129                     nIndex++;
130                     if (separated.length > 1 && separated[0].equalsIgnoreCase(SERIALIZED_COUNT)) {
131                         nCount = Integer.parseInt(separated[1]);
132                         break;
133                     }
134                 }
135 
136                 if (nCount > 0 && nCount <= lineCount-2 && nCount < 20000) { //foolproof
137                     //now add nCount to the vector.
138                     double[] data = new double[nCount];
139                     int dataIndex=0;
140 
141                     while (nIndex < lineCount) {
142                         double value = Double.parseDouble(lines[nIndex]);
143                         data[dataIndex++] = value;
144                         nIndex++;
145                     }
146                     setData(data, true);
147                     success = true;
148                 }
149             }
150         }
151 
152         return success;
153     }
154 
log(String msg)155     private static void log(String msg) {
156         Log.v(LOGTAG, msg);
157     }
158 }
159