Search Unity

Unity detect if user is speaking. (Microphone Input - Speech Detection)

Discussion in 'Scripting' started by FK22, Jun 15, 2019.

  1. FK22

    FK22

    Joined:
    Jul 7, 2017
    Posts:
    40
    Hi I am trying to make a simple program by just printing to the console that user is speaking, if user talks. So I need to write a program that captures microphone input and prints : -You are speaking.( if the user is speaking ) or prints: -You are not speaking if the user is not speaking.

    The code below works only if you yell lol. But I want to make it more sensitive and detect speach in normal voice.


    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class MicInput : MonoBehaviour
    6. {
    7.  
    8.     int sample_size = 128;
    9.  
    10.     string micro_;
    11.  
    12.     AudioClip audio_;
    13.  
    14.     public float level_;
    15.  
    16.  
    17.     void Start()
    18.     {
    19.         audio_ = GetComponent<AudioClip>();
    20.  
    21.         if (micro_ == null)
    22.         {
    23.  
    24.             micro_ = Microphone.devices[0];
    25.  
    26.         }
    27.  
    28.         audio_= Microphone.Start(micro_, true, 1, 441000);
    29.  
    30.     }
    31.  
    32.  
    33.     void Update()
    34.     {
    35.  
    36.         float[] spectrum = new float[sample_size];
    37.  
    38.         level_ = 0;
    39.  
    40.         int mic_pos = Microphone.GetPosition(null) - (sample_size + 1);
    41.  
    42.         if (mic_pos < 0)
    43.         {
    44.  
    45.             return;
    46.  
    47.         }
    48.  
    49.         audio_.GetData(spectrum,mic_pos);
    50.  
    51.         for (int i = 0; i < spectrum.Length; i++)
    52.         {
    53.  
    54.             float peak = spectrum[i] * spectrum[i];
    55.  
    56.             if (level_ < peak)
    57.             {
    58.                 level_ = peak;
    59.  
    60.                 Debug.Log("You are speaking");
    61.  
    62.             }
    63.  
    64.             else
    65.             {
    66.  
    67.                 Debug.Log("You are not speaking");
    68.             }
    69.  
    70.  
    71.         }
    72.  
    73.     }
    74. }
    75.  
     
    Last edited: Jun 15, 2019
  2. mgear

    mgear

    Joined:
    Aug 3, 2010
    Posts:
    9,428
    it seems that you are taking highest peak as the new level at
    level_ = peak;

    so once that happens, you need to create louder noise everytime to trigger that code.
    maybe just set level at start only to some suitable value.
     
    FK22 likes this.
  3. FK22

    FK22

    Joined:
    Jul 7, 2017
    Posts:
    40
    Hi, many thanks for your response. I have tried it in many ways and it does somehow work but is not perfect. Do you know any other way to do this? As far as I see from the internet there are not a lot of tutorials about this. There are about keyword recognition, but that is not what I need. I just want to detect if someone spoke in the microphone.