Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by beefyt123 · Sep 17, 2016 at 07:19 AM · audiosourceaudioclipmicrophone

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;
     }
Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

1 Reply

· Add your reply
  • Sort: 
avatar image
0

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;
         }
 
 }
 
Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image beefyt123 · Sep 19, 2016 at 09:43 AM 0
Share

Hey thanks for getting back to me. I have the line audioSource.Play() because without it I get the same problem.

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

55 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

How to record audio in chunks and send through websocket? 0 Answers

Why I am getting Negative Decibels (around -70dB) where Normal Speech around 60dB 2 Answers

Multiple Microphone Inputs 0 Answers

Microphone Question 0 Answers

Microphone capture different channels 0 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges