Search Unity

Audio Help in Microphone input

Discussion in 'Audio & Video' started by tonymedd01, Jun 3, 2019.

  1. tonymedd01

    tonymedd01

    Joined:
    Feb 7, 2018
    Posts:
    19
    Hello , i need help to modifie a script its about recording from microphone input and save it in temporary clip then put the audio clip in the audio source in the same gameobject , and the script that i have can Start a recording with the Space bar, and toggle playback of loops using the numbers keys, the first loop being number 1 , i want just 1 clip recorded then added to the audio source , then record again and replace the last clip recorded in the audiosource , some one help me please i need this script for school project (talkin character game like talking Tom ) thank you. (i took this script from here https://forum.unity.com/threads/rec...PBzv8hSeaCEsVnyjJ7G_PNKELcvrLKDC8b2PTkk0ziTMs)




    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. }