我想在持续的噪音声音时激活我的传感器(Android Studio)

huangapple go评论81阅读模式
英文:

I want to activate my sensor when a continuous noise sounds ( Android Studio )

问题

我正在学习编程事实上我并不是很了解我正在编写一个应用程序其中包括一个音频传感器当房间里听到声音时它会被激活我目前有以下代码

活动 检测声音

```java
public class DetectNoise {

    // 用于录制声音的文件
    static final private double EMA_FILTER = 0.6;

    private MediaRecorder mRecorder = null;
    private double mEMA = 0.0;

    public void start() {
        if (mRecorder == null) {
            mRecorder = new MediaRecorder();
            mRecorder.setAudioSource(MediaRecorder.AudioSource.MIC);
            mRecorder.setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP);
            mRecorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB);
            mRecorder.setOutputFile("/dev/null");

            try {
                mRecorder.prepare();
                mRecorder.start();
                mEMA = 0.0;
            } catch (IllegalStateException e) {
                Log.e("Error", e.toString());
                e.printStackTrace();
            } catch (IOException e) {
                Log.e("Error", e.toString());
                e.printStackTrace();
            }
        }
    }

    public double getAmplitude() {
        if (mRecorder != null)
            return 20 * Math.log10(mRecorder.getMaxAmplitude() / 2000);
        else
            return 0;
    }

    public double getAmplitudeEMA() {
        double amp = getAmplitude();
        mEMA = EMA_FILTER * amp + (1.0 - EMA_FILTER) * mEMA;
        return mEMA;
    }
}

活动 一

private int mThreshold;
private static final int POLL_INTERVAL = 300;
mSensor = new DetectNoise();
private Handler mHandler = new Handler();

private Runnable mPollTask = new Runnable() {
    public void run() {
        double amp = mSensor.getAmplitude();
        updateDisplay("监测声音中...", amp);

        if (amp > mThreshold) {
            callForHelp(amp);
        }
        mHandler.postDelayed(mPollTask, POLL_INTERVAL);
    }
};

问题在于任何声音都会触发它。我希望只有在声音持续约5到7秒时才会激活。有人能帮助我吗?
非常感谢您的一切。


<details>
<summary>英文:</summary>
I am learning to program and the truth is that I don&#39;t have much idea. I am programming an application that consists of an audio sensor and when a noise is heard in the house it is activated. I currently have this code.
Activity DetectNoise

public class DetectNoise {

// This file is used to record voice
static final private double EMA_FILTER = 0.6;
private MediaRecorder mRecorder = null;
private double mEMA = 0.0;
public void start() {
if (mRecorder == null) {
mRecorder = new MediaRecorder();
mRecorder.setAudioSource(MediaRecorder.AudioSource.MIC);
mRecorder.setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP);
mRecorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB);
mRecorder.setOutputFile(&quot;/dev/null&quot;);
try {
mRecorder.prepare();
mRecorder.start();
mEMA = 0.0;
} catch (IllegalStateException e) {
Log.e(&quot;Error&quot;,e.toString());
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
Log.e(&quot;Error&quot;,e.toString());
e.printStackTrace();
}
}
}

public double getAmplitude() {

    if (mRecorder != null)
//return  (mRecorder.getMaxAmplitude()/1100.0);
return   20 * Math.log10(mRecorder.getMaxAmplitude() / 2000);
else
return 0;
}

public double getAmplitudeEMA() {
double amp = getAmplitude();
mEMA = EMA_FILTER * amp + (1.0 - EMA_FILTER) * mEMA;
return mEMA;
}


Activity One

private int mThreshold;
private static final int POLL_INTERVAL = 300;
mSensor = new DetectNoise();
private Handler mHandler = new Handler();

private Runnable mPollTask = new Runnable() {
public void run() {

            double amp = mSensor.getAmplitude();
//Log.i(&quot;Noise&quot;, &quot;runnable mPollTask&quot;);
updateDisplay(&quot;Monitoring Voice...&quot;, amp);
if ((amp &gt; mThreshold)) {
callForHelp(amp);
//Log.i(&quot;Noise&quot;, &quot;==== onCreate ===&quot;);
}// Runnable(mPollTask) will again execute after POLL_INTERVAL
mHandler.postDelayed(mPollTask, POLL_INTERVAL); }

The problem is that with any sound it is activated. I want it to activate only when the sound lasts about 5 or 7 seconds continued.
Can anybody help me?
Thank you so much for everything
</details>
# 答案1
**得分**: 0
开始初始化时间戳为空,您可以从第一个振幅到记录的时间戳获取,如果在检测过程中出现声音消失,则可以将时间戳重置为空。如果声音持续存在,则将保留第一个记录的时间戳。您只需要确定时间戳和时间戳记录被获取后进行减法,以判断声音持续时间。
<details>
<summary>英文:</summary>
start init time stamp is null, you can get from the first amplitude to record time stamp,  if they are sound in the detection process disappear,you can reset time stamp to null, If the sound persists then the first recorded time stamp will be kept.You only need to determine the time stamp and time stamp records are obtained subtraction judge the sound duration
</details>
# 答案2
**得分**: 0
```html
<!-- begin snippet: js hide: false console: true babel: false -->
<!-- language: lang-html -->
/**
* 对不起,我的英语不好
*/
import java.util.Random;
public class Test implements Runnable {
//您可以尝试启动
public static void main(String[] args) {
new Thread(new Test()).start();
}
private static Long lastTime = null;
@Override
public void run() {
//这是一个循环监视录音音量
while (true) {
char temp = getAmplitude();
System.out.print(temp);//测试输出
try {
//第一次检测到声音
if (lastTime == null && temp!='_')
lastTime = System.currentTimeMillis();
//检测到停止声音
if (temp=='_')
lastTime=null;
//持续有声音
//判断时间间隔
if(lastTime != null && System.currentTimeMillis()-lastTime>=2*1000){
//说话超过两秒
System.out.println("--|>说话超过两秒");
lastTime=null;
}
//取样率为100毫秒
Thread.sleep(100);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
//--------------------------------------------------------------
// 模拟录音音量
private static char getAmplitude() {
// 如果有声音则输出'A',没有声音则输出'_'
// 您可以通过声音水平判断是否有声音
// 这是一个示例
if (new Random().nextInt(100) <= 7)
return '_';//无声音
return 'A';//有声音
}
//--------------------------------------------------------------
}
<!-- end snippet -->
英文:

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-html -->

/**
*  Sorry for my poor english
*/
import java.util.Random;
public class Test implements Runnable {
//You can try start
public static void main(String[] args) {
new Thread(new Test()).start();
}
private static Long lastTime = null;
@Override
public void run() {
//      This is a cycle monitor the recording volume
while (true) {
char temp = getAmplitude();
System.out.print(temp);//test output
try {
//              Sound is detected for the first time
if (lastTime == null &amp;&amp; temp!=&#39;_&#39;)
lastTime = System.currentTimeMillis();
//              Stopped sound detected
if (temp==&#39;_&#39;)
lastTime=null;
//              Sound keeps coming
//              Judgment time interval
if(lastTime != null &amp;&amp; System.currentTimeMillis()-lastTime&gt;=2*1000){
//                  Speak more than two seconds
System.out.println(&quot;---|&gt;Speak more than two seconds&quot;);
lastTime=null;
}
//              A sampling rate of 100 ms
Thread.sleep(100);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
//--------------------------------------------------------------
// Analog recording volume
private static char getAmplitude() {
// If there is sound output &#39;A&#39;  , No sound output &#39;_&#39;
// You can determine whether there is sound by the sound level
// This is an example
if (new Random().nextInt(100) &lt;=7)
return &#39;_&#39;;//无声音
return &#39;A&#39;;//有声音
}
//--------------------------------------------------------------
}

<!-- end snippet -->

huangapple
  • 本文由 发表于 2020年3月16日 14:18:26
  • 转载请务必保留本文链接:https://go.coder-hub.com/60701179.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定