aboutsummaryrefslogtreecommitdiff
path: root/apps/OboeTester/app/src/main/java/com/mobileer/oboetester/GlitchActivity.java
blob: 8df0d98b5e957db8cf8fd206e53466e06537b4ce (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
/*
 * Copyright 2018 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.mobileer.oboetester;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

import java.io.IOException;

/**
 * Activity to measure the number of glitches.
 */
public class GlitchActivity extends AnalyzerActivity {
    private TextView mAnalyzerTextView;
    private Button mStartButton;
    private Button mStopButton;
    private Button mShareButton;

    // These must match the values in LatencyAnalyzer.h
    final static int STATE_IDLE = 0;
    final static int STATE_IMMUNE = 1;
    final static int STATE_WAITING_FOR_SIGNAL = 2;
    final static int STATE_WAITING_FOR_LOCK = 3;
    final static int STATE_LOCKED = 4;
    final static int STATE_GLITCHING = 5;
    String mLastGlitchReport;
    private int mInputChannel;
    private int mOutputChannel;

    native int getStateFrameCount(int state);
    native int getGlitchCount();
    native double getSignalToNoiseDB();
    native double getPeakAmplitude();
    native double getSineAmplitude();

    private GlitchSniffer mGlitchSniffer;
    private NativeSniffer mNativeSniffer = createNativeSniffer();

    synchronized NativeSniffer createNativeSniffer() {
        if (mGlitchSniffer == null) {
            mGlitchSniffer = new GlitchSniffer(this);
        }
        return mGlitchSniffer;
    }

    // Note that these strings must match the enum result_code in LatencyAnalyzer.h
    String stateToString(int resultCode) {
        switch (resultCode) {
            case STATE_IDLE:
                return "IDLE";
            case STATE_IMMUNE:
                return "IMMUNE";
            case STATE_WAITING_FOR_SIGNAL:
                return "WAITING_FOR_SIGNAL";
            case STATE_WAITING_FOR_LOCK:
                return "WAITING_FOR_LOCK";
            case STATE_LOCKED:
                return "LOCKED";
            case STATE_GLITCHING:
                return "GLITCHING";
            default:
                return "UNKNOWN";
        }
    }

    // Periodically query for glitches from the native detector.
    protected class GlitchSniffer extends NativeSniffer {

        private long mTimeAtStart;
        private long mTimeOfLastGlitch;
        private double mSecondsWithoutGlitches;
        private double mMaxSecondsWithoutGlitches;
        private int mLastGlitchCount;
        private int mLastUnlockedFrames;
        private int mLastLockedFrames;
        private int mLastGlitchFrames;

        private int mStartResetCount;
        private int mLastResetCount;
        private int mPreviousState;

        private double mSignalToNoiseDB;
        private double mPeakAmplitude;
        private double mSineAmplitude;

        public GlitchSniffer(Activity activity) {
            super(activity);
        }

        @Override
        public void startSniffer() {
            long now = System.currentTimeMillis();
            mTimeAtStart = now;
            mTimeOfLastGlitch = now;
            mLastUnlockedFrames = 0;
            mLastLockedFrames = 0;
            mLastGlitchFrames = 0;
            mSecondsWithoutGlitches = 0.0;
            mMaxSecondsWithoutGlitches = 0.0;
            mLastGlitchCount = 0;
            mStartResetCount = mLastResetCount;
            super.startSniffer();
        }

        public void run() {
            int state = getAnalyzerState();
            mSignalToNoiseDB = getSignalToNoiseDB();
            mPeakAmplitude = getPeakAmplitude();
            mSineAmplitude = getSineAmplitude();
            int glitchCount = getGlitchCount();
            if (state != mPreviousState) {
                if ((state == STATE_WAITING_FOR_SIGNAL || state == STATE_WAITING_FOR_LOCK)
                        && glitchCount == 0) { // did not previously lock
                    GlitchActivity.this.giveAdvice("Try raising volume!");
                } else {
                    GlitchActivity.this.giveAdvice(null);
                }
            }
            mPreviousState = state;

            long now = System.currentTimeMillis();
            int resetCount = getResetCount();
            mLastUnlockedFrames = getStateFrameCount(STATE_WAITING_FOR_LOCK);
            int lockedFrames = getStateFrameCount(STATE_LOCKED);
            int glitchFrames = getStateFrameCount(STATE_GLITCHING);

            if (glitchFrames > mLastGlitchFrames || glitchCount > mLastGlitchCount) {
                mTimeOfLastGlitch = now;
                mSecondsWithoutGlitches = 0.0;
                if (glitchCount > mLastGlitchCount) {
                    onGlitchDetected();
                }
            } else if (lockedFrames > mLastLockedFrames) {
                mSecondsWithoutGlitches = (now - mTimeOfLastGlitch) / 1000.0;
            }

            if (resetCount > mLastResetCount) {
                mLastResetCount = resetCount;
            }

            if (mSecondsWithoutGlitches > mMaxSecondsWithoutGlitches) {
                mMaxSecondsWithoutGlitches = mSecondsWithoutGlitches;
            }

            mLastGlitchCount = glitchCount;
            mLastGlitchFrames = glitchFrames;
            mLastLockedFrames = lockedFrames;
            mLastResetCount = resetCount;

            reschedule();
        }

        private String getCurrentStatusReport() {
            long now = System.currentTimeMillis();
            double totalSeconds = (now - mTimeAtStart) / 1000.0;

            StringBuffer message = new StringBuffer();
            message.append("state = " + stateToString(mPreviousState) + "\n");
            message.append(String.format("unlocked.frames = %d\n", mLastUnlockedFrames));
            message.append(String.format("locked.frames = %d\n", mLastLockedFrames));
            message.append(String.format("glitch.frames = %d\n", mLastGlitchFrames));
            message.append(String.format("reset.count = %d\n", mLastResetCount - mStartResetCount));
            message.append(String.format("peak.amplitude = %8.6f\n", mPeakAmplitude));
            message.append(String.format("sine.amplitude = %8.6f\n", mSineAmplitude));
            if (mLastLockedFrames > 0) {
                message.append(String.format("signal.noise.ratio.db = %5.1f\n", mSignalToNoiseDB));
            }
            message.append(String.format("time.total = %4.2f seconds\n", totalSeconds));
            if (mLastLockedFrames > 0) {
                message.append(String.format("time.no.glitches = %4.2f\n", mSecondsWithoutGlitches));
                message.append(String.format("max.time.no.glitches = %4.2f\n",
                        mMaxSecondsWithoutGlitches));
                message.append(String.format("glitch.count = %d\n", mLastGlitchCount));
            }
            return message.toString();
        }

        @Override
        public String getShortReport() {
            String resultText = "#glitches = " + getLastGlitchCount()
                    + ", #resets = " + getLastResetCount()
                    + ", max no glitch = " + getMaxSecondsWithNoGlitch() + " secs\n";
            resultText += String.format("SNR = %5.1f db", mSignalToNoiseDB);
            resultText += ", #locked = " + mLastLockedFrames;
            return resultText;
        }

        @Override
        public void updateStatusText() {
            mLastGlitchReport = getCurrentStatusReport();
            setAnalyzerText(mLastGlitchReport);
        }

        public double getMaxSecondsWithNoGlitch() {
            return mMaxSecondsWithoutGlitches;
        }

        public int getLastGlitchCount() {
            return mLastGlitchCount;
        }
        public int getLastResetCount() {
            return mLastResetCount;
        }
    }

    public void giveAdvice(String s) {
    }

    // Called on UI thread
    protected void onGlitchDetected() {
    }

    protected void setAnalyzerText(String s) {
        mAnalyzerTextView.setText(s);
    }

    /**
     * Set tolerance to deviations from expected value.
     * The normalized value will be converted in the native code.
     * @param tolerance normalized between 0.0 and 1.0
     */
    public native void setTolerance(float tolerance);

    public void setInputChannel(int channel) {
        mInputChannel = channel;
        setInputChannelNative(channel);
    }

    public void setOutputChannel(int channel) {
        mOutputChannel = channel;
        setOutputChannelNative(channel);
    }

    public int getInputChannel() {
        return mInputChannel;
    }

    public int getOutputChannel() {
        return mOutputChannel;
    }

    public native void setInputChannelNative(int channel);

    public native void setOutputChannelNative(int channel);

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        mStartButton = (Button) findViewById(R.id.button_start);
        mStopButton = (Button) findViewById(R.id.button_stop);
        mStopButton.setEnabled(false);
        mShareButton = (Button) findViewById(R.id.button_share);
        mShareButton.setEnabled(false);
        mAnalyzerTextView = (TextView) findViewById(R.id.text_status);
        updateEnabledWidgets();
        hideSettingsViews();
        // TODO hide sample rate menu
        StreamContext streamContext = getFirstInputStreamContext();
        if (streamContext != null) {
            if (streamContext.configurationView != null) {
                streamContext.configurationView.hideSampleRateMenu();
            }
        }
    }

    @Override
    int getActivityType() {
        return ACTIVITY_GLITCHES;
    }

    @Override
    protected void onStart() {
        super.onStart();
        setInputChannel(0);
        setOutputChannel(0);
    }

    @Override
    protected void onStop() {
        if (!isBackgroundEnabled()) {
            stopAudioTest();
        }
        super.onStop();
    }

    @Override
    protected void onDestroy() {
        if (isBackgroundEnabled()) {
            stopAudioTest();
        }
        super.onDestroy();
    }

    // Called on UI thread
    public void onStartAudioTest(View view) throws IOException {
        openAudio();
        startAudioTest();
        mStartButton.setEnabled(false);
        mStopButton.setEnabled(true);
        mShareButton.setEnabled(false);
        keepScreenOn(true);
    }

    public void startAudioTest() throws IOException {
        startAudio();
        mNativeSniffer.startSniffer();
        onTestBegan();
    }

    public void onCancel(View view) {
        stopAudioTest();
        onTestFinished();
    }

    // Called on UI thread
    public void onStopAudioTest(View view) {
        stopAudioTest();
        onTestFinished();
        mStartButton.setEnabled(true);
        mStopButton.setEnabled(false);
        mShareButton.setEnabled(false);
        keepScreenOn(false);
    }

    // Must be called on UI thread.
    public void onTestBegan() {
    }

    // Must be called on UI thread.
    public void onTestFinished() {
        mStartButton.setEnabled(true);
        mStopButton.setEnabled(false);
        mShareButton.setEnabled(true);
    }

    public void stopAudioTest() {
        mNativeSniffer.stopSniffer();
        stopAudio();
        closeAudio();
    }

    public void stopTest() {
        stopAudio();
    }

    @Override
    boolean isOutput() {
        return false;
    }

    public double getMaxSecondsWithNoGlitch() {
        return mGlitchSniffer.getMaxSecondsWithNoGlitch();
    }

    public String getShortReport() {
        return mNativeSniffer.getShortReport();
    }

    @Override
    String getWaveTag() {
        return "glitches";
    }
}