- Home /
Getting Real-Time Continuous Audio from the Mic
Hi there. So, as the question suggests, I need real time continuous audio from the Mic in Unity. What I mean by continuous is that I don't want to specify any limit such as the one dictated by Microphone.Start(deviceName, true, 5, 44100) Here 5 secs specifies the time for which the Mic input will be recorded.
But, what if I am trying to implement a Push to talk functionality, where the user will hold on to 'S' as long as he/she wants to speak? We dont know the value of that 'S'.
After trying for quite a bit, and looking around on the internet, I got to doing this:
using UnityEngine;
using System.Collections;
public class EchoTest : MonoBehaviour
{
public AudioSource tempSource;
string deviceName = "";
// Use this for initialization
void Start()
{
}
bool m_bIsSpeak = false;
public bool IsSpeak
{
get
{
return m_bIsSpeak;
}
set
{
m_bIsSpeak = value;
}
}
// Update is called once per frame
void Update()
{
if (Input.GetKeyDown(KeyCode.S))
{
Debug.Log("PTT Enabled");
IsSpeak = true;
}
if (Input.GetKeyUp(KeyCode.S))
{
Debug.Log("PTT Disabled");
StopTheSound();
}
if (IsSpeak)
{
if (!(tempSource.audio.isPlaying))
{
PlayTheSound();
}
}
}
public void StopTheSound()
{
Debug.Log("Stopping the sound..");
IsSpeak = false;
Microphone.End(deviceName);
}
public void PlayTheSound()
{
tempSource.clip = Microphone.Start(deviceName, true, 5, 44100);
while (!(Microphone.GetPosition(deviceName) > 0))
{ }
tempSource.playOnAwake = false;
tempSource.loop = false;
tempSource.Play();
}
}
The problem here is that there is an obvious jerk every 5 seconds, since the mic records for 5 secs then there is a check if it is playing and if not, start to record for 5 secs again! This method is obviously flawed, as if the user begins to speak at the 4th sec he will get cut off from the 5th sec onwards until the recording begins again! :/ What to do guys? Help!
Answer by kumi360 · Apr 26, 2016 at 06:52 PM
Have you tried getting rid of the GetPosition method? That might reduce some latency.
Your answer
Follow this Question
Related Questions
recording sound from different locations? 0 Answers
Sending Microphone Input over Network 1 Answer
How to record stereo sound in Unity 0 Answers
Mic input to Scale data in live time? 3 Answers