Welcome to mirror list, hosted at ThFree Co, Russian Federation.

ActivityInputMode.java « inputmode « audio « jumble « morlunk « com « java « main « src - gitlab.com/quite/humla.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: bd96b30d901f0179ac6f7ebfe2db6717285b9b48 (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
/*
 * Copyright (C) 2016 Andrew Comminos <andrew@comminos.com>
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

package com.morlunk.jumble.audio.inputmode;

/**
 * An input mode that sends audio if the amplitude exceeds a certain threshold.
 * Created by andrew on 13/02/16.
 */
public class ActivityInputMode implements IInputMode {
    // Continue speech for 250ms to prevent dropping.
    private static final int SPEECH_DELAY = (int) (0.25 * Math.pow(10, 9));

    private final float mVADThreshold;
    private long mVADLastDetected;

    public ActivityInputMode(float detectionThreshold) {
        mVADThreshold = detectionThreshold;
    }

    @Override
    public boolean onInputReceived(short[] pcm, int length) {
        // Use a logarithmic energy-based scale for VAD.
        float sum = 1.0f;
        for (int i = 0; i < length; i++) {
            sum += pcm[i] * pcm[i];
        }
        float micLevel = (float) Math.sqrt(sum / (float)length);
        float peakSignal = (float) (20.0f * Math.log10(micLevel / 32768.0f)) / 96.0f;
        boolean talking = (peakSignal + 1) >= mVADThreshold;

        talking |= (System.nanoTime() - mVADLastDetected) < SPEECH_DELAY;

        // Record the last time where VAD was detected in order to prevent speech dropping.
        if(talking) {
            mVADLastDetected = System.nanoTime();
        }

        return talking;
    }
}