- Home /
Audio, GetData not returning Data.
Hi there, So Me and my colleague have been trying to work on some audio code throughout the day trying various work around(s) but to no avail.
We are using the audio.clip = Microphone.Start("", true, 1, 44100); and playing a audio clip through a phone plugged into the Microphone port.
We then check with GetData against the duplicate clips we have in our Unity Project. The problem is GetData never returns anything but 0's and some Epsilon Values from time to time.
Due to this we can't do a comparison of the audio.
does anyone know a solution to this? Thanks
using UnityEngine;
using System.Collections;
public class ToneTest : MonoBehaviour {
float[] samples;
public AudioClip Sine;
public AudioClip Square;
public AudioClip Saw;
float[] SineSamples;
float[] SquareSamples;
float[] SawSamples;
// Use this for initialization
void Start()
{
audio.clip = Microphone.Start("", true, 1, 44100);
samples = new float[100];
SineSamples = new float[100];
SquareSamples = new float[100];
SawSamples = new float[100];
}
// Update is called once per frame
void Update()
{
if (audio.clip != null)
{
audio.clip.GetData(samples, 0);
Sine.GetData(SineSamples, 0);
Square.GetData(SquareSamples, 0);
Saw.GetData(SawSamples, 0);
I used this piece of code to check if GetData was working for me and it was
public AudioClip $$anonymous$$usic;
void Start()
{
audio.clip = $$anonymous$$usic;
float[] samples = new float[1000];
audio.clip.GetData(samples, 0);
int i = 0;
while (i < samples.Length)
{
print(samples[i]);
++i;
}
}
So to me it seems like GetData isn't the problem here but it's how you're getting your audio from your microphone.
Unless I'm misunderstanding something
The AudioClip is declared when you call $$anonymous$$icrophone.Start, but you need to stop recording before the AudioClip's data is set. That is, you will need to use $$anonymous$$icrophone.End(""); before you can play back or access the data of the AudioClip.
Hi, Thanks for the help. I added a timer to count an amount into the tone and then to end the $$anonymous$$icrophone after around 5 seconds. Unfortunately the SineSample is still empty.
Thanks Rob
Can you post the code where you call and the method you use for the timer?
Thanks
void Update()
{
if (audio.clip != null)
{
if (!timerEnd)
{
timer += Time.deltaTime;
//print(timer);
if (timer > 5f)
{
$$anonymous$$icrophone.End("");
timerEnd = true;
}
}
else
{
// Rest of the code for the checking is here.
}
We also added this as a test. it still just returned 0's
using UnityEngine;
using System.Collections;
public class TestGetData : $$anonymous$$onoBehaviour {
float[] samples;
// Use this for initialization
void Start ()
{
samples = new float[100];
}
// Update is called once per frame
void Update ()
{
audio.clip.GetData(samples, 0);
}
Your answer