bulliedMonster
bulliedMonster

Reputation: 256

Get Basic audio spectrum data in unity

I want to visualize if an audio clip has sound or not. The microphone and the audiosource is working correctly but I am stuck with its visualizing part. I have hard time understanding the official document and I want a solution.

I tried the following code:

    void Update () {

    AnalyzeSound();

    text1.text = "sound!\n"+ " rmsValue : " + rmsValue ;
}


void AnalyzeSound()
{
    audio.GetOutputData(samples, 0);

    //GetComponent rms

    int i = 0;
    float sum = 0;

    for (; i < SAMPLE_SIZE; i++)
    {
        sum = samples[i] * samples[i];
    }

    rmsValue = Mathf.Sqrt(sum / SAMPLE_SIZE);

    //get the dbValue
    dbValue = 20 * Mathf.Log10(rmsValue / 0.1f);
   }

Can I take rmsValue as the input of sound on microphone? or should I take the dbValue? what should be the threshold value? in a few words, When can I say the microphone has sound?

Upvotes: 0

Views: 615

Answers (1)

jaket
jaket

Reputation: 9341

There is no hard and fast definition that would separate noise from silence in all cases. It really depends on how loud the background noise is. Compare for example, silence recorded in an anechoic chamber vs silence recorded next to an HVAC system. The easiest thing to try is to experiment with different dB threshold values below which you consider the signal as noise and above which it is considered signal. Then adjust the threshold value up or down to suit your needs. Depending on the nature of the signal (e.g. music vs. speech) you could look into other techniques such as Voice Activity Detection (https://en.wikipedia.org/wiki/Voice_activity_detection) or a convolutional neural network to segment speech and music

Upvotes: 1

Related Questions