igorski / MWEngine

Audio engine and DSP library for Android, written in C++ providing low latency performance within a musical context, while providing a Java/Kotlin API. Supports both OpenSL and AAudio.
MIT License
264 stars 45 forks source link

Is it possible to get hertz from the mic while recording? #175

Open YogarajRamesh opened 2 years ago

YogarajRamesh commented 2 years ago

Hi @igorski , I am trying to reduce a noise but it sill existing. So as a work around I am trying to mute the recording When the volume of the mic is low(below certain Hz). And unmute the recording when mic volume is above certain hertz.

In order to do that , Is it possible to get hertz from the mic while input recording and set the volume of input channel according to the hertz.

Thank you in advance.

igorski commented 2 years ago

Hi @YogarajRamesh

you mean dB ? So ideally when the sound goes below a certain threshold (e.g. audio level) you want to completely silence the signal.

If so, sounds like a gate effect, which is actually under development at the moment, it might work for your use case.

YogarajRamesh commented 2 years ago

Hi @igorski Yes, that's what I meant. Thanks for your reply,

igorski commented 1 year ago

Alright, the latest version of the repository now includes Compressor and Gate processors.

For your use case, I would recommend to use the gate. It has the following methods:

gate.setThreshold( float valueInDb );
gate.setAttack( float value );
gate.setRelease( float value );

The threshold defines the level in dB at which the gate should operate. Experiment with values in the 0 - 100 range. When the signal drops below the provided threshold, the gate becomes active (e.g. sound will be muted).

With the attack and release setters you can set a value (in milliseconds) that determines how fast the gate opens and closes (so you make the effect more smoother instead of a hard open- and close).

I suggest you place the Gate as the first processor in the processing chain of the audio channel (e.g. the one that records the mic input). That means all effects coming afterwards (for instance reverb) will operate as normal (and in the case of reverb add a more natural tail when the input closes).

Also, beware that the createOutput signature of MWEngine has been updated with a new version (as input channel amount is now configurable at runtime). The new way of creating output is:

MWEngineInstance.createOutput( int sampleRate, int bufferSize, int outputChannels, int inputChannels, Drivers.types driver );

So you likely need to provide an argument for inputChannels (setting this to 1 ensures you can access the device microphone).

YogarajRamesh commented 1 year ago

Hi @igorski, Thanks for reply, I try to add gate processor to input channel processing chain. With dB value from 0.0f-100.0f no matter what ever the value set the sound stay muted always. Seems gate is always stay active.

Below is my code snippet


    _gate=  new Gate(); 

    _gate.setThreshold(100.0f);

    _gate.setAttack(100 );

    _gate.setRelease(100);
    _engine.getInputChannel().getProcessingChain().reset();

    _engine.getInputChannel().getProcessingChain().addProcessor(_gate);

    _engine.startInputRecording(
     Environment.getExternalStorageDirectory().getAbsolutePath() + "/Download/test/outputt2.wav",false);

This is the log:

V/MWENGINE: AAudio_IO::AAudioStream_read() returns 0 read frames instead of 96
V/MWENGINE: AAudio_IO::Setting buffer size to 192
V/MWENGINE: AAudio_IO::AAudioStream_read() returns 48 read frames instead of 96
V/MWENGINE: AAudio_IO::Setting buffer size to 288
V/MWENGINE: AAudio_IO::AAudioStream_read() returns 0 read frames instead of 96
V/MWENGINE: AAudio_IO::Setting buffer size to 384
V/MWENGINE: AAudio_IO::AAudioStream_read() returns 0 read frames instead of 96
V/MWENGINE: AAudio_IO::Setting buffer size to 480
V/MWENGINE: AAudio_IO::AAudioStream_read() returns 0 read frames instead of 96
V/MWENGINE: AAudio_IO::Setting buffer size to 576
V/MWENGINE: AAudio_IO::AAudioStream_read() returns 48 read frames instead of 96
V/MWENGINE: AAudio_IO::AAudioStream_read() returns 0 read frames instead of 96
V/MWENGINE: AAudio_IO::AAudioStream_read() returns 48 read frames instead of 96
V/MWENGINE: AAudio_IO::AAudioStream_read() returns 48 read frames instead of 96
I/chatty: uid=10479(nl.igorski.mwengine.example) Thread-2 identical 1 line
V/MWENGINE: AAudio_IO::AAudioStream_read() returns 48 read frames instead of 96
V/MWENGINE: AAudio_IO::AAudioStream_read() returns 48 read frames instead of 96
V/MWENGINE: AAudio_IO::AAudioStream_read() returns 48 read frames instead of 96
V/MWENGINE: AAudio_IO::AAudioStream_read() returns 48 read frames instead of 96
I/chatty: uid=10479(nl.igorski.mwengine.example) Thread-2 identical 1 line
V/MWENGINE: AAudio_IO::AAudioStream_read() returns 48 read frames instead of 96
D/MWENGINE: Recorded snippet 0 saved to storage
V/MWENGINE: WaveReader::About to parse data for WAV file '/storage/emulated/0/Download/test/rec_snippet_0.WAV'
V/MWENGINE: File size    : 1440036
V/MWENGINE: Audio format   : 1
V/MWENGINE: Channel amount  : 1
V/MWENGINE: Sample rate   : 48000
V/MWENGINE: Bytes per second : 96000
V/MWENGINE: Block align   : 2
V/MWENGINE: Bits per sample : 16
V/MWENGINE: WaveReader::About to parse data for WAV file '/storage/emulated/0/Download/test/rec_snippet_1.WAV'
V/MWENGINE: File size    : 291300
V/MWENGINE: Audio format   : 1
V/MWENGINE: Channel amount  : 1
V/MWENGINE: Sample rate   : 48000
V/MWENGINE: Bytes per second : 96000
V/MWENGINE: Block align   : 2
V/MWENGINE: Bits per sample : 16
D/MWENGINE: Recording has completed

Thank you,