summaryrefslogtreecommitdiff
path: root/LoopbackApp/app/src/main/java/org/drrickorang/loopback/RecorderRunnable.java
blob: 8c3c7a12ed11b42f8e08bc236149572f05d995cf (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
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
/*
 * Copyright (C) 2015 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 org.drrickorang.loopback;

import android.content.Context;
import android.media.AudioFormat;
import android.media.AudioManager;
import android.media.AudioRecord;
import android.os.Build;
import android.util.Log;

/**
 * This thread records incoming sound samples (uses AudioRecord).
 */

public class RecorderRunnable implements Runnable {
    private static final String TAG = "RecorderRunnable";

    private AudioRecord         mRecorder;
    private boolean             mIsRunning;
    private boolean             mIsRecording = false;
    private static final Object sRecordingLock = new Object();

    private final LoopbackAudioThread mAudioThread;
    // This is the pipe that connects the player and the recorder in latency test.
    private final PipeShort           mLatencyTestPipeShort;
    // This is the pipe that is used in buffer test to send data to GlitchDetectionThread
    private PipeShort                 mBufferTestPipeShort;

    private boolean   mIsRequestStop = false;
    private final int mTestType;    // latency test or buffer test
    private final int mSelectedRecordSource;
    private final int mSamplingRate;

    private int       mChannelConfig = AudioFormat.CHANNEL_IN_MONO;
    private int       mAudioFormat = AudioFormat.ENCODING_PCM_16BIT;
    private int       mMinRecorderBuffSizeInBytes = 0;
    private int       mMinRecorderBuffSizeInSamples = 0;

    private short[] mAudioShortArray;   // this array stores values from mAudioTone in read()
    private short[] mBufferTestShortArray;
    private short[] mAudioTone;

    // for glitch detection (buffer test)
    private BufferPeriod          mRecorderBufferPeriodInRecorder;
    private final int             mBufferTestWavePlotDurationInSeconds;
    private final int             mChannelIndex;
    private final double          mFrequency1;
    private final double          mFrequency2; // not actually used
    private int[]                 mAllGlitches; // value = 1 means there's a glitch in that interval
    private boolean               mGlitchingIntervalTooLong;
    private int                   mFFTSamplingSize; // the amount of samples used per FFT.
    private int                   mFFTOverlapSamples; // overlap half the samples
    private long                  mStartTimeMs;
    private int                   mBufferTestDurationInSeconds;
    private long                  mBufferTestDurationMs;
    private final CaptureHolder   mCaptureHolder;
    private final Context         mContext;
    private AudioManager          mAudioManager;
    private GlitchDetectionThread mGlitchDetectionThread;

    // for adjusting sound level in buffer test
    private double[] mSoundLevelSamples;
    private int      mSoundLevelSamplesIndex = 0;
    private boolean  mIsAdjustingSoundLevel = true; // is true if still adjusting sound level
    private double   mSoundBotLimit = 0.6;    // we want to keep the sound level high
    private double   mSoundTopLimit = 0.8;    // but we also don't want to be close to saturation
    private int      mAdjustSoundLevelCount = 0;
    private int      mMaxVolume;   // max possible volume of the device

    private double[]  mSamples; // samples shown on WavePlotView
    private int       mSamplesIndex;

    RecorderRunnable(PipeShort latencyPipe, int samplingRate, int channelConfig, int audioFormat,
                     int recorderBufferInBytes, int micSource, LoopbackAudioThread audioThread,
                     BufferPeriod recorderBufferPeriod, int testType, double frequency1,
                     double frequency2, int bufferTestWavePlotDurationInSeconds,
                     Context context, int channelIndex, CaptureHolder captureHolder) {
        mLatencyTestPipeShort = latencyPipe;
        mSamplingRate = samplingRate;
        mChannelConfig = channelConfig;
        mAudioFormat = audioFormat;
        mMinRecorderBuffSizeInBytes = recorderBufferInBytes;
        mSelectedRecordSource = micSource;
        mAudioThread = audioThread;
        mRecorderBufferPeriodInRecorder = recorderBufferPeriod;
        mTestType = testType;
        mFrequency1 = frequency1;
        mFrequency2 = frequency2;
        mBufferTestWavePlotDurationInSeconds = bufferTestWavePlotDurationInSeconds;
        mContext = context;
        mChannelIndex = channelIndex;
        mCaptureHolder = captureHolder;
    }


    /** Initialize the recording device for latency test. */
    public boolean initRecord() {
        log("Init Record");
        if (mMinRecorderBuffSizeInBytes <= 0) {
            mMinRecorderBuffSizeInBytes = AudioRecord.getMinBufferSize(mSamplingRate,
                                          mChannelConfig, mAudioFormat);
            log("RecorderRunnable: computing min buff size = " + mMinRecorderBuffSizeInBytes
                + " bytes");
        } else {
            log("RecorderRunnable: using min buff size = " + mMinRecorderBuffSizeInBytes +
                " bytes");
        }

        if (mMinRecorderBuffSizeInBytes <= 0) {
            return false;
        }

        mMinRecorderBuffSizeInSamples = mMinRecorderBuffSizeInBytes / Constant.BYTES_PER_FRAME;
        mAudioShortArray = new short[mMinRecorderBuffSizeInSamples];

        try {
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
                mRecorder = new AudioRecord.Builder()
                        .setAudioFormat((mChannelIndex < 0 ?
                                new AudioFormat.Builder()
                                        .setChannelMask(AudioFormat.CHANNEL_IN_MONO) :
                                new AudioFormat
                                        .Builder().setChannelIndexMask(1 << mChannelIndex))
                                .setSampleRate(mSamplingRate)
                                .setEncoding(mAudioFormat)
                                .build())
                        .setAudioSource(mSelectedRecordSource)
                        .setBufferSizeInBytes(2 * mMinRecorderBuffSizeInBytes)
                        .build();
            } else {
                mRecorder = new AudioRecord(mSelectedRecordSource, mSamplingRate,
                        mChannelConfig, mAudioFormat, 2 * mMinRecorderBuffSizeInBytes);
            }
        } catch (IllegalArgumentException | UnsupportedOperationException e) {
            e.printStackTrace();
            return false;
        } finally {
            if (mRecorder == null){
                return false;
            } else if (mRecorder.getState() != AudioRecord.STATE_INITIALIZED) {
                mRecorder.release();
                mRecorder = null;
                return false;
            }
        }

        //generate sinc wave for use in loopback test
        ToneGeneration sincTone = new RampedSineTone(mSamplingRate, Constant.LOOPBACK_FREQUENCY);
        mAudioTone = new short[Constant.LOOPBACK_SAMPLE_FRAMES];
        sincTone.generateTone(mAudioTone, Constant.LOOPBACK_SAMPLE_FRAMES);

        return true;
    }


    /** Initialize the recording device for buffer test. */
    boolean initBufferRecord() {
        log("Init Record");
        if (mMinRecorderBuffSizeInBytes <= 0) {

            mMinRecorderBuffSizeInBytes = AudioRecord.getMinBufferSize(mSamplingRate,
                                          mChannelConfig, mAudioFormat);
            log("RecorderRunnable: computing min buff size = " + mMinRecorderBuffSizeInBytes
                + " bytes");
        } else {
            log("RecorderRunnable: using min buff size = " + mMinRecorderBuffSizeInBytes +
                " bytes");
        }

        if (mMinRecorderBuffSizeInBytes <= 0) {
            return false;
        }

        mMinRecorderBuffSizeInSamples = mMinRecorderBuffSizeInBytes / Constant.BYTES_PER_FRAME;
        mBufferTestShortArray = new short[mMinRecorderBuffSizeInSamples];

        final int cycles = 100;
        int soundLevelSamples =  (mSamplingRate / (int) mFrequency1) * cycles;
        mSoundLevelSamples = new double[soundLevelSamples];
        mAudioManager = (AudioManager) mContext.getSystemService(mContext.AUDIO_SERVICE);
        mMaxVolume = mAudioManager.getStreamMaxVolume(AudioManager.STREAM_MUSIC);

        try {
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
                mRecorder = new AudioRecord.Builder()
                        .setAudioFormat((mChannelIndex < 0 ?
                                new AudioFormat.Builder()
                                        .setChannelMask(AudioFormat.CHANNEL_IN_MONO) :
                                new AudioFormat
                                        .Builder().setChannelIndexMask(1 << mChannelIndex))
                                .setSampleRate(mSamplingRate)
                                .setEncoding(mAudioFormat)
                                .build())
                        .setAudioSource(mSelectedRecordSource)
                        .setBufferSizeInBytes(2 * mMinRecorderBuffSizeInBytes)
                        .build();
            } else {
                mRecorder = new AudioRecord(mSelectedRecordSource, mSamplingRate,
                        mChannelConfig, mAudioFormat, 2 * mMinRecorderBuffSizeInBytes);
            }
        } catch (IllegalArgumentException | UnsupportedOperationException e) {
            e.printStackTrace();
            return false;
        } finally {
            if (mRecorder == null){
                return false;
            } else if (mRecorder.getState() != AudioRecord.STATE_INITIALIZED) {
                mRecorder.release();
                mRecorder = null;
                return false;
            }
        }

        final int targetFFTMs = 20; // we want each FFT to cover 20ms of samples
        mFFTSamplingSize = targetFFTMs * mSamplingRate / Constant.MILLIS_PER_SECOND;
        // round to the nearest power of 2
        mFFTSamplingSize = (int) Math.pow(2, Math.round(Math.log(mFFTSamplingSize) / Math.log(2)));

        if (mFFTSamplingSize < 2) {
            mFFTSamplingSize = 2; // mFFTSamplingSize should be at least 2
        }
        mFFTOverlapSamples = mFFTSamplingSize / 2; // mFFTOverlapSamples is half of mFFTSamplingSize

        return true;
    }


    boolean startRecording() {
        synchronized (sRecordingLock) {
            mIsRecording = true;
        }

        final int samplesDurationInSecond = 2;
        int nNewSize = mSamplingRate * samplesDurationInSecond; // 2 seconds!
        mSamples = new double[nNewSize];

        boolean status = initRecord();
        if (status) {
            log("Ready to go.");
            startRecordingForReal();
        } else {
            log("Recorder initialization error.");
            synchronized (sRecordingLock) {
                mIsRecording = false;
            }
        }

        return status;
    }


    boolean startBufferRecording() {
        synchronized (sRecordingLock) {
            mIsRecording = true;
        }

        boolean status = initBufferRecord();
        if (status) {
            log("Ready to go.");
            startBufferRecordingForReal();
        } else {
            log("Recorder initialization error.");
            synchronized (sRecordingLock) {
                mIsRecording = false;
            }
        }

        return status;
    }


    void startRecordingForReal() {
        mLatencyTestPipeShort.flush();
        mRecorder.startRecording();
    }


    void startBufferRecordingForReal() {
        mBufferTestPipeShort = new PipeShort(Constant.MAX_SHORTS);
        mGlitchDetectionThread = new GlitchDetectionThread(mFrequency1, mFrequency2, mSamplingRate,
                mFFTSamplingSize, mFFTOverlapSamples, mBufferTestDurationInSeconds,
                mBufferTestWavePlotDurationInSeconds, mBufferTestPipeShort, mCaptureHolder);
        mGlitchDetectionThread.start();
        mRecorder.startRecording();
    }


    void stopRecording() {
        log("stop recording A");
        synchronized (sRecordingLock) {
            log("stop recording B");
            mIsRecording = false;
        }
        stopRecordingForReal();
    }


    void stopRecordingForReal() {
        log("stop recording for real");
        if (mRecorder != null) {
            mRecorder.stop();
        }

        if (mRecorder != null) {
            mRecorder.release();
            mRecorder = null;
        }
    }


    public void run() {
        // keeps the total time elapsed since the start of the test. Only used in buffer test.
        long elapsedTimeMs;
        mIsRunning = true;
        while (mIsRunning) {
            boolean isRecording;

            synchronized (sRecordingLock) {
                isRecording = mIsRecording;
            }

            if (isRecording && mRecorder != null) {
                int nSamplesRead;
                switch (mTestType) {
                case Constant.LOOPBACK_PLUG_AUDIO_THREAD_TEST_TYPE_LATENCY:
                    nSamplesRead = mRecorder.read(mAudioShortArray, 0,
                                   mMinRecorderBuffSizeInSamples);

                    if (nSamplesRead > 0) {
                        mRecorderBufferPeriodInRecorder.collectBufferPeriod();
                        { // inject the tone that will be looped-back
                            int currentIndex = mSamplesIndex - 100; //offset
                            for (int i = 0; i < nSamplesRead; i++) {
                                if (currentIndex >= 0 && currentIndex < mAudioTone.length) {
                                    mAudioShortArray[i] = mAudioTone[currentIndex];
                                }
                                currentIndex++;
                            }
                        }

                        mLatencyTestPipeShort.write(mAudioShortArray, 0, nSamplesRead);
                        if (isStillRoomToRecord()) { //record to vector
                            for (int i = 0; i < nSamplesRead; i++) {
                                double value = mAudioShortArray[i];
                                value = value / Short.MAX_VALUE;
                                if (mSamplesIndex < mSamples.length) {
                                    mSamples[mSamplesIndex++] = value;
                                }

                            }
                        } else {
                            mIsRunning = false;
                        }
                    }
                    break;
                case Constant.LOOPBACK_PLUG_AUDIO_THREAD_TEST_TYPE_BUFFER_PERIOD:
                    if (mIsRequestStop) {
                        endBufferTest();
                    } else {
                        // before we start the test, first adjust sound level
                        if (mIsAdjustingSoundLevel) {
                            nSamplesRead = mRecorder.read(mBufferTestShortArray, 0,
                                    mMinRecorderBuffSizeInSamples);
                            if (nSamplesRead > 0) {
                                for (int i = 0; i < nSamplesRead; i++) {
                                    double value = mBufferTestShortArray[i];
                                    if (mSoundLevelSamplesIndex < mSoundLevelSamples.length) {
                                        mSoundLevelSamples[mSoundLevelSamplesIndex++] = value;
                                    } else {
                                        // adjust the sound level to appropriate level
                                        mIsAdjustingSoundLevel = AdjustSoundLevel();
                                        mAdjustSoundLevelCount++;
                                        mSoundLevelSamplesIndex = 0;
                                        if (!mIsAdjustingSoundLevel) {
                                            // end of sound level adjustment, notify AudioTrack
                                            mAudioThread.setIsAdjustingSoundLevel(false);
                                            mStartTimeMs = System.currentTimeMillis();
                                            break;
                                        }
                                    }
                                }
                            }
                        } else {
                            // the end of test is controlled here. Once we've run for the specified
                            // test duration, end the test
                            elapsedTimeMs = System.currentTimeMillis() - mStartTimeMs;
                            if (elapsedTimeMs >= mBufferTestDurationMs) {
                                endBufferTest();
                            } else {
                                nSamplesRead = mRecorder.read(mBufferTestShortArray, 0,
                                        mMinRecorderBuffSizeInSamples);
                                if (nSamplesRead > 0) {
                                    mRecorderBufferPeriodInRecorder.collectBufferPeriod();
                                    mBufferTestPipeShort.write(mBufferTestShortArray, 0,
                                            nSamplesRead);
                                }
                            }
                        }
                    }
                    break;
                }
            }
        } //synchronized
        stopRecording(); //close this
    }


    /** Someone is requesting to stop the test, will stop the test even if the test is not done. */
    public void requestStop() {
        switch (mTestType) {
        case Constant.LOOPBACK_PLUG_AUDIO_THREAD_TEST_TYPE_BUFFER_PERIOD:
            mIsRequestStop = true;
            break;
        case Constant.LOOPBACK_PLUG_AUDIO_THREAD_TEST_TYPE_LATENCY:
            mIsRunning = false;
            break;
        }
    }


    /** Collect data then clean things up.*/
    private void endBufferTest() {
        mIsRunning = false;
        mAllGlitches = mGlitchDetectionThread.getGlitches();
        mGlitchingIntervalTooLong = mGlitchDetectionThread.getGlitchingIntervalTooLong();
        mSamples = mGlitchDetectionThread.getWaveData();
        endDetecting();
    }


    /** Clean everything up. */
    public void endDetecting() {
        mBufferTestPipeShort.flush();
        mBufferTestPipeShort = null;
        mGlitchDetectionThread.requestStop();
        GlitchDetectionThread tempThread = mGlitchDetectionThread;
        mGlitchDetectionThread = null;
        try {
            tempThread.join(Constant.JOIN_WAIT_TIME_MS);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }


    /**
     * Adjust the sound level such that the buffer test can run with small noise disturbance.
     * Return a boolean value to indicate whether or not the sound level has adjusted to an
     * appropriate level.
     */
    private boolean AdjustSoundLevel() {
        // if after adjusting 20 times, we still cannot get into the volume we want, increase the
        // limit range, so it's easier to get into the volume we want.
        if (mAdjustSoundLevelCount != 0 && mAdjustSoundLevelCount % 20 == 0) {
            mSoundTopLimit += 0.1;
            mSoundBotLimit -= 0.1;
        }

        double topThreshold = Short.MAX_VALUE * mSoundTopLimit;
        double botThreshold = Short.MAX_VALUE * mSoundBotLimit;
        double currentMax = mSoundLevelSamples[0];
        int currentVolume = mAudioManager.getStreamVolume(AudioManager.STREAM_MUSIC);

        // since it's a sine wave, we are only checking max positive value
        for (int i = 1; i < mSoundLevelSamples.length; i++) {
            if (mSoundLevelSamples[i] > topThreshold) { // once a sample exceed, return
                // adjust sound level down
                currentVolume--;
                mAudioManager.setStreamVolume(AudioManager.STREAM_MUSIC, currentVolume, 0);
                return true;
            }

            if (mSoundLevelSamples[i] > currentMax) {
                currentMax = mSoundLevelSamples[i];
            }
        }

        if (currentMax < botThreshold) {
            // adjust sound level up
            if (currentVolume < mMaxVolume) {
                currentVolume++;
                mAudioManager.setStreamVolume(AudioManager.STREAM_MUSIC,
                        currentVolume, 0);
                return true;
            } else {
                return false;
            }
        }

        return false;
    }


    /** Check if there's any room left in mSamples. */
    public boolean isStillRoomToRecord() {
        boolean result = false;
        if (mSamples != null) {
            if (mSamplesIndex < mSamples.length) {
                result = true;
            }
        }

        return result;
    }


    public void setBufferTestDurationInSeconds(int bufferTestDurationInSeconds) {
        mBufferTestDurationInSeconds = bufferTestDurationInSeconds;
        mBufferTestDurationMs = Constant.MILLIS_PER_SECOND * mBufferTestDurationInSeconds;
    }


    public int[] getAllGlitches() {
        return mAllGlitches;
    }


    public boolean getGlitchingIntervalTooLong() {
        return mGlitchingIntervalTooLong;
    }


    public double[] getWaveData() {
        return mSamples;
    }


    public int getFFTSamplingSize() {
        return mFFTSamplingSize;
    }


    public int getFFTOverlapSamples() {
        return mFFTOverlapSamples;
    }


    private static void log(String msg) {
        Log.v(TAG, msg);
    }

}