如何在Android

时间:2018-09-24 02:27:28

标签: android avaudiorecorder android-mediarecorder android-audiorecord noise-reduction

我正在尝试使用android AudioRecorder录制音频,并将其放大(通过提供搜索栏或其他东西来满足用户的兴趣),并使其与LoudnessEnhancer一起使用,但我能听到频率为44100的许多背景噪音。我尝试使用noisesuppressor,但这没什么用。是否可以减少android中的背景噪音?我知道有很多应用程序可以消除背景噪音。请帮忙,这是我的代码:

private int freq = 44100;
    private AudioRecord audioRecord = null;
    private Thread Rthread = null;

    int PERMISSION_ALL = 1;
    String[] PERMISSIONS = {Manifest.permission.RECORD_AUDIO};

    private AudioTrack audioTrack = null;
    byte[] buffer = new byte[freq];
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        if(!hasPermissions(this, PERMISSIONS)){
            ActivityCompat.requestPermissions(this, PERMISSIONS, PERMISSION_ALL);
        }

        android.os.Process.setThreadPriority(android.os.Process.THREAD_PRIORITY_URGENT_AUDIO);
        final int bufferSize = AudioRecord.getMinBufferSize(freq,
                AudioFormat.CHANNEL_CONFIGURATION_MONO,
                AudioFormat.ENCODING_PCM_16BIT);


        audioRecord = new AudioRecord(MediaRecorder.AudioSource.MIC, freq,
                AudioFormat.CHANNEL_CONFIGURATION_MONO,
                MediaRecorder.AudioEncoder.AMR_NB, bufferSize);

        audioTrack = new AudioTrack(AudioManager.ROUTE_HEADSET, freq,
                AudioFormat.CHANNEL_CONFIGURATION_MONO,
                MediaRecorder.AudioEncoder.AMR_NB, bufferSize,
                AudioTrack.MODE_STREAM);

        LoudnessEnhancer enhancer = new LoudnessEnhancer(audioTrack.getAudioSessionId());
        NoiseSuppressor.create(audioTrack.getAudioSessionId());
        AcousticEchoCanceler.create(audioTrack.getAudioSessionId());

        enhancer.setTargetGain(10000);
        enhancer.setEnabled(true);


        audioTrack.setPlaybackRate(freq);
        buffer = new byte[bufferSize];
        audioRecord.startRecording();
        Log.i("info", "Audio Recording started");
        audioTrack.play();
        Log.i("info", "Audio Playing started");
        Rthread = new Thread(new Runnable() {
            public void run() {
                while (true) {
                    try {
                        audioRecord.read(buffer, 0, bufferSize);
                        audioTrack.write(buffer, 0, buffer.length);

                    } catch (Throwable t) {
                        Log.e("Error", "Read write failed");
                        t.printStackTrace();
                    }
                }
            }
        });
        Rthread.start();
    }


    }

0 个答案:

没有答案