Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
375 views
in Technique[技术] by (71.8m points)

Remove Noise from audio recorder in java android android

I'm building an android app to record audio, and it records successfully by Mediarecord class. but I want to remove noise from this audio, I tried to use NoiseSuppressor class with AudioRecord class but file extension ".pcm" is not supported to open on phone. this is code with AudioRecord and NoiseSuppressor classes

private static final int RECORDER_SAMPLERATE = 16000;
    private static final int RECORDER_CHANNELS = AudioFormat.CHANNEL_IN_MONO;
    private static final int RECORDER_AUDIO_ENCODING = AudioFormat.ENCODING_PCM_16BIT;
    private AudioRecord recorder = null;
    private Thread recordingThread = null;
    private boolean isRecording = false;

    int BufferElements2Rec = 1024; // want to play 2048 (2K) since 2 bytes we use only 1024
    int BytesPerElement = 2; // 2 bytes in 16bit format

    private void record() {

        recorder = new AudioRecord(MediaRecorder.AudioSource.MIC,
                RECORDER_SAMPLERATE, RECORDER_CHANNELS,
                RECORDER_AUDIO_ENCODING, BufferElements2Rec * BytesPerElement);
        int id = 0;
        id = recorder.getAudioSessionId();
        recorder.startRecording();
        isRecording = true;
        NoiseSuppressor ns = NoiseSuppressor.create(id);
        recordingThread = new Thread(new Runnable() {
            public void run() {
                writeAudioDataToFile();
                if (ns!=null) {
                    ns.release();
                }
            }
        }, "AudioRecorder Thread");
        recordingThread.start();
    }

    //convert short to byte
    private byte[] short2byte(short[] sData) {
        int shortArrsize = sData.length;
        byte[] bytes = new byte[shortArrsize * 2];
        for (int i = 0; i < shortArrsize; i++) {
            bytes[i * 2] = (byte) (sData[i] & 0x00FF);
            bytes[(i * 2) + 1] = (byte) (sData[i] >> 8);
            sData[i] = 0;
        }
        return bytes;

    }

    private void writeAudioDataToFile() {
        // Write the output audio in byte

        String filepath = Environment.getExternalStorageDirectory().getPath();
        File file = new File(filepath,"E-voice Recorder");
        if(!file.exists()){
            file.mkdirs();
        }

        String filePath = file.getAbsolutePath() + "/Untitled-" + System.currentTimeMillis() + ".pcm";

        short sData[] = new short[BufferElements2Rec];

        FileOutputStream os = null;
        try {
            os = new FileOutputStream(filePath);
        } catch (Exception e) {
            e.printStackTrace();
        }

        while (isRecording) {
            // gets the voice output from microphone to byte format

            recorder.read(sData, 0, BufferElements2Rec);
            //System.out.println("Short wirting to file" + sData.toString());
            try {
                // // writes the data to file from buffer
                // // stores the voice buffer
                byte bData[] = short2byte(sData);
                os.write(bData, 0, BufferElements2Rec * BytesPerElement);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        try {
            os.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

how can I remove noise from audio and output format is supported to open on phone ?

question from:https://stackoverflow.com/questions/65907733/remove-noise-from-audio-recorder-in-java-android-android

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)
Waitting for answers

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...