Search Unity

How to Create a Microphone endless loop? - Unity

Discussion in 'Scripting' started by DomandiereFolle, Aug 15, 2017.

  1. DomandiereFolle

    DomandiereFolle

    Joined:
    Aug 15, 2017
    Posts:
    1
    hi everyone!
    there is way to create, save and loop a x seconds of Microphone input?
    i have find a way to record and save a file, but when i try to play it, it have a millisecond of silence, and i need a clear loop for my app

    Anyone can help me? :)
     
  2. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    38,744
    For one, it has to be saved as WAV, as you won't be able to smoothly loop an MP3 for various reasons of the format's "chunked" nature.

    The other thing to do is choose some number of samples (say 1/10th of a second worth) and fade out the end, fade in the beginning, and slightly-overlay the end to the beginning and the beginning to the end (in the sound editor). This will also help smooth out any issues.

    Also, when you cut the audio, make sure you cut it right as the wave sample passes through the zero mark, otherwise there will be a click.
     
  3. DomandiereFolle1

    DomandiereFolle1

    Joined:
    Aug 16, 2017
    Posts:
    2
    Thanks for reply,

    I already save it as a wav file

    i not understand well the part of cut and wave sample, can you explain me in more detail?

    this is my code:

    Code (CSharp):
    1. public void RilasciaBottone()
    2.     {
    3.         StartCoroutine (DelayStato (0));
    4.         Microphone.End(_device);
    5.  
    6.         int converti = _clipRecord.samples;
    7.         int converti2 = _clipRecord.samples - (_clipRecord.samples/2);
    8.  
    9.  
    10.         int overlapSamples = (int)(0.2f * _clipRecord.frequency);
    11.  
    12.         SalvaClip = AudioClip.Create ("cut1", converti, _clipRecord.channels, _clipRecord.frequency, false, false);
    13.  
    14.         float[] smp1 = new float[(converti) * _clipRecord.channels];
    15.  
    16.         float[] smp2 = new float[(converti2 +overlapSamples) * _clipRecord.channels];
    17.  
    18.  
    19.         _clipRecord.GetData(smp1, 0);
    20.         SalvaClip.SetData(smp1,0);
    21.  
    22.  
    23.  
    24.  
    25.         var filepath = Path.Combine(Application.dataPath, "porcamadonna.wav");
    26.         SavWav.Save (filepath, SalvaClip);
    27.  
    28.         CASSA.GetComponent<ExampleClass> ().sourceClip = SalvaClip;
    29.         CASSA.SetActive (true);
    30.  
    31.         DiversiLoop [contaclip] = SalvaClip;
    32.  
    33.         contaclip++;
    34.  
    35.         GameObject.Find ("PORCAMADONNA").GetComponent<AudioSource> ().clip = DiversiLoop[contaclip-1];
    36.  
    37.         GameObject.Find ("PORCAMADONNA").GetComponent<AudioSource> ().Play ();
    38.  
    39.  
    40.         DeviContare = false;
    41.  
    42.     }
     
  4. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    38,744
    Using an audio tool, like Audacity or SoundForge or whatever, you would ensure the endpoints match, or optionally blend them over a short period of time.
     
  5. DomandiereFolle1

    DomandiereFolle1

    Joined:
    Aug 16, 2017
    Posts:
    2
    thanks for reply

    i can not use tool because i must create the microphone loop on runtime.

    i have tried to blend it but i have always the milliseconds problem...