- Home /
Movement speed based on audio input
Hey all,
I am trying to use a mic input to control the movement speed of a character which I have managed to get working however my problem is that when I set the AudioSource to mute (because I don't want the player to hear their own voice played back to them) then I no longer get a return value.
I was wondering if anyone knew a way to set the AudioSource to mute but still return some values in regards to the input. I have tried to set the volume to 0 and have it unmuted but that didn't work either.
Here is the code I am using:
     private AudioSource audioSource;
 
     public float loudness;
     public float sensitivity;
 
     [Range(0, 100)]
     public float sourceVolume = 100;
 
     private string selectedDevice;
 
     void Awake()
     {
         audioSource = GetComponent<AudioSource>();
         audioSource.mute = false;
         audioSource.loop = true;
         selectedDevice = Microphone.devices[0].ToString();
     }
 
     public void StartMicrophone()
     {
         audioSource.clip = Microphone.Start(selectedDevice, true, 10, 44100);
         while (!(Microphone.GetPosition(selectedDevice) > 0)) { } 
         audioSource.Play();
     }
 
 
     void Update()
     {
         audioSource.volume = (sourceVolume / 100);
         loudness = GetAveragedVolume() * sensitivity * (sourceVolume / 10);
         if (!Microphone.IsRecording(selectedDevice))
         {
             StartMicrophone();
         }
             
     }
 
 
     float GetAveragedVolume()
     {
         float[] data = new float[256];
         float a = 0;
         GetComponent<AudioSource>().GetOutputData(data, 0);
         foreach (float s in data)
         {
             a += Mathf.Abs(s);
         }
         return a / 256;
     }
Answer by Atran · Sep 18, 2016 at 02:09 PM
I implemented a game for the birthparty of my son which included steering a spaceship with miro input (higher volume => spaceship flies higher). I didn't had the issue you mentioned (hearing what you record), but i am also wondering why you use the method AudioSource.Play() in line 23 ...
Here's my the class from my game for the mic input:
 using UnityEngine;
 using System.Collections;
 
 public class MicrophoneVolumeMeter : MonoBehaviour {
         public float timeForVolumeCalcInSeconds = 0.3F;
         private AudioClip audioclip;
         private AudioSource audioSource;
         private float timeLastVolumeWasTaken = 0;
         private float lastVolume = 0;
 
         // Use this for initialization
         IEnumerator Start () {
                 yield return Application.RequestUserAuthorization (UserAuthorization.WebCam | UserAuthorization.Microphone);
 
                 if (Application.HasUserAuthorization (UserAuthorization.Microphone)) {
                         foreach (string device in Microphone.devices) {
                                 Debug.Log ("Microphone name: " + device);
                         }
                         if (Microphone.devices.Length == 0) {
                                 Debug.LogError ("No Microphones available!");
                         } else {
                                 audioSource = gameObject.AddComponent ("AudioSource") as AudioSource;
                                 if (audioSource == null) {
                                         Debug.LogError ("audioSource is null!!!!");
                                 }
                         }
                 } else {
                         Debug.LogError ("User gave no access to his microphones!");
                 }
         }
     
         // Update is called once per time unit (normally 0.2 seconds)
         void FixedUpdate () {
                 if (audioSource != null) {
                         if (!Microphone.IsRecording (null)) {
                                 audioSource.clip = Microphone.Start (null, false, 1, 44100);
                         } else {
                                 if (Time.fixedTime - timeLastVolumeWasTaken > timeForVolumeCalcInSeconds) {
                                         timeLastVolumeWasTaken = Time.fixedTime;
                                         float[] samples = new float[audioSource.clip.samples * audioSource.clip.channels];
                                         audioSource.clip.GetData (samples, 0);
                                         lastVolume = Mathf.Round (calculateAverageVolume (samples) * 10000F) / 100F;
                                         Debug.Log ("Time: " + Time.fixedTime + " / average volume * 100F = " + lastVolume);
                                         Microphone.End (null);
                                 }
                         }
                 }
         }
 
         /// <summary>
         /// Calculates the average volume of the given samples
         /// </summary>
         /// <returns>The average volume.</returns>
         /// <param name="samples">Samples.</param>
         private float calculateAverageVolume (float[] samples) {
                 float sum = 0;
                 for (int i=0; i < samples.Length; i++) {
                         sum += samples [i] * samples [i];    // sum squared samples
                 }
                 return Mathf.Sqrt (sum / samples.Length); // rms = square root of average }
         }
 
         /// <summary>
         /// Gets the last calculated volume taken from the microphone (ranges roughly between 0 and 1)
         /// </summary>
         /// <returns>The last volume.</returns>
         public float getLastVolume () {
                 return lastVolume;
         }
 
 }
 
Hey thanks for getting back to me. I have the line audioSource.Play() because without it I get the same problem.
Your answer
 
 
              koobas.hobune.stream
koobas.hobune.stream 
                       
                
                       
			     
			 
                