Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

Record Microphone to File

Discussion in 'Audio & Video' started by cehkay, Jul 3, 2016.

  1. cehkay

    cehkay

    Joined:
    Sep 12, 2015
    Posts:
    4
    Hi, i am trying to record from microphone to a file.

    Specifically i want the user to press a button, which than starts the recording until another button is pressed which ends the recording process.

    Unforunately i was unable to find a solution for this simple task.
    The Microphone.Start() uses a predetermined time.

    I found a Asset called G-Audio which can do just this. (Example 5, free Asset) But it is so complicated and not well deocumented that i can not extract this functionality.

    Is there anyone who can help with this problem? I am Googling for 2 Days, it seems as if noone could solve this problem or noone posted a solution to this.
     
    ina likes this.
  2. rorywalsh

    rorywalsh

    Joined:
    Apr 10, 2015
    Posts:
    114
    Here's the answer I posted to the short lived new Unity forum! As I mentioned there, it stills needs some kind of fading so that the loops don't click each time they repeat themselves. Start a recording with the Space bar, and toggle playback of loops using the numbers keys, the first loop being number 1.

    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections.Generic;
    3.  
    4. public class CubeController : MonoBehaviour {
    5.  
    6.     bool isRecording = true;
    7.     private AudioSource audioSource;
    8.  
    9.     //temporary audio vector we write to every second while recording is enabled..
    10.     List<float> tempRecording = new List<float>();
    11.  
    12.     //list of recorded clips...
    13.     List<float[]> recordedClips = new List<float[]>();
    14.  
    15.     void Start()
    16.     {
    17.         audioSource = GetComponent<AudioSource>();
    18.         //set up recording to last a max of 1 seconds and loop over and over
    19.         audioSource.clip = Microphone.Start(null, true, 1, 44100);
    20.         audioSource.Play();
    21.         //resize our temporary vector every second
    22.         Invoke("ResizeRecording", 1);
    23.     }
    24.  
    25.     void ResizeRecording()
    26.     {
    27.         if (isRecording)
    28.         {
    29.             //add the next second of recorded audio to temp vector
    30.             int length = 44100;
    31.             float[] clipData = new float[length];
    32.             audioSource.clip.GetData(clipData, 0);
    33.             tempRecording.AddRange(clipData);
    34.             Invoke("ResizeRecording", 1);
    35.         }
    36.     }
    37.  
    38.     void Update()
    39.     {
    40.         //space key triggers recording to start...
    41.         if (Input.GetKeyDown("space"))
    42.         {
    43.             isRecording = !isRecording;
    44.             Debug.Log(isRecording == true ? "Is Recording" : "Off");
    45.  
    46.             if (isRecording == false)
    47.             {
    48.                 //stop recording, get length, create a new array of samples
    49.                 int length = Microphone.GetPosition(null);
    50.              
    51.                 Microphone.End(null);
    52.                 float[] clipData = new float[length];
    53.                 audioSource.clip.GetData(clipData, 0);
    54.  
    55.                 //create a larger vector that will have enough space to hold our temporary
    56.                 //recording, and the last section of the current recording
    57.                 float[] fullClip = new float[clipData.Length + tempRecording.Count];
    58.                 for (int i = 0; i < fullClip.Length; i++)
    59.                 {
    60.                     //write data all recorded data to fullCLip vector
    61.                     if (i < tempRecording.Count)
    62.                         fullClip[i] = tempRecording[i];
    63.                     else
    64.                         fullClip[i] = clipData[i - tempRecording.Count];
    65.                 }
    66.                
    67.                 recordedClips.Add(fullClip);
    68.                 audioSource.clip = AudioClip.Create("recorded samples", fullClip.Length, 1, 44100, false);
    69.                 audioSource.clip.SetData(fullClip, 0);
    70.                 audioSource.loop = true;    
    71.  
    72.             }
    73.             else
    74.             {
    75.                 //stop audio playback and start new recording...
    76.                 audioSource.Stop();
    77.                 tempRecording.Clear();
    78.                 Microphone.End(null);
    79.                 audioSource.clip = Microphone.Start(null, true, 1, 44100);
    80.                 Invoke("ResizeRecording", 1);
    81.             }
    82.  
    83.         }
    84.  
    85.         //use number keys to switch between recorded clips, start from 1!!
    86.         for (int i = 0; i < 10; i++)
    87.         {
    88.             if (Input.GetKeyDown("" + i))
    89.             {
    90.                 SwitchClips(i-1);
    91.             }
    92.         }
    93.  
    94.     }
    95.  
    96.     //chooose which clip to play based on number key..
    97.     void SwitchClips(int index)
    98.     {
    99.         if (index < recordedClips.Count)
    100.         {
    101.             audioSource.Stop();
    102.             int length = recordedClips[index].Length;
    103.             audioSource.clip = AudioClip.Create("recorded samples", length, 1, 44100, false);
    104.             audioSource.clip.SetData(recordedClips[index], 0);
    105.             audioSource.loop = true;
    106.             audioSource.Play();
    107.         }
    108.     }
    109.  
    110.  
    111. }
     
    deus0, DeadCastles, Hermonirr and 5 others like this.
  3. Shaba1

    Shaba1

    Joined:
    Jan 21, 2010
    Posts:
    63
    ok this is off topic but what would be the way to convert this to something that could be send across a network. So say you wanted to have live chat in a multiplayer game.
     
  4. rorywalsh

    rorywalsh

    Joined:
    Apr 10, 2015
    Posts:
    114
    Good question. Maybe this might help?
     
  5. spaceedge

    spaceedge

    Joined:
    Aug 19, 2017
    Posts:
    1
    where does this save the recording?
     
  6. rahulpatil6220375

    rahulpatil6220375

    Joined:
    Dec 10, 2018
    Posts:
    19


    not work
     
  7. malviyanpawan

    malviyanpawan

    Joined:
    Oct 24, 2013
    Posts:
    7
    Not working at all.
     
  8. jbsenthilkumar209

    jbsenthilkumar209

    Joined:
    Dec 12, 2020
    Posts:
    1
    @rorywalsh: its working bro.Thanks for the code
     
    Flavosky likes this.
  9. Flavosky

    Flavosky

    Joined:
    Apr 6, 2020
    Posts:
    4
    Hi @jbsenthilkumar209 , I put the code on my GameObject with an AudioSource. The space bar is working and I have an audio clip but... The audio clip is empty (cf. screenshots).
    My microphone is working on Windows (I am using the built-in Realtek audio device of the motherboard).
    How did you succeed to use this script ? Thanks a lot in advance !

    (@rorywalsh)

    Recorded_samples_01.JPG
    Recorded_samples_02.JPG
     
  10. Flavosky

    Flavosky

    Joined:
    Apr 6, 2020
    Posts:
    4
    Oups, sory, EDIT --> This is working but the sound is very irregular and interspersed with silences , Any ideas ?!
     
  11. unity_771D202EDB79B06CB710

    unity_771D202EDB79B06CB710

    Joined:
    Oct 16, 2022
    Posts:
    2
    Damn ..... no human in this earth can record sound from microphone in unity>>?? man ......