I’m getting this error when calling AudioClip.GetData. It seems to be caused when I pass an index that tries to access samples outside the 1st channel. Is this a bug and if it is, is there a workaround?
Edit to include code. This uses MathNet so won’t compile in Unity out of the box. I should also note that GetData seems to sometimes start returning false and I have to Stop/Play a bunch of times before it will start returning true again. I’m not sure what’s up with that either.
using UnityEngine;
using System.Collections;
public class FFTTest : MonoBehaviour {
bool filtered;
AudioClip clip;
AudioSource audioSrc;
int maxSamples;
int sampleSize = 128;
int sampleIdx = 0;
MathNet.Numerics.Transformations.RealFourierTransformation rft;
float playTimeStart;
void Start()
{
filtered = false;
clip = Resources.Load("Audio/<<SOUND FILE>>") as AudioClip;
audioSrc = Camera.main.gameObject.GetComponent<AudioSource>();
audioSrc.clip = clip;
rft = new MathNet.Numerics.Transformations.RealFourierTransformation(MathNet.Numerics.Transformations.TransformationConvention.Default);
maxSamples = clip.channels * clip.samples;
}
// Use this for initialization
void Filter () {
float [] filteredSamples = new float[maxSamples];
bool res = false;
float[] samples = new float[sampleSize];
while(sampleIdx + sampleSize < maxSamples)
{
double [] real;
double [] img;
res = clip.GetData(samples, sampleIdx); //ERROR HERE WHEN sampleIdx >= maxSamples/2
if(res)
{
filtered = true;
}
double [] dsamples = System.Array.ConvertAll(samples, x => (double)x);
rft.TransformForward(dsamples, out real, out img);
double f0 = 1.5;
double DCGain = 100;
double order = 2;
double binWidth = clip.frequency / (double)(sampleSize); // Hz
//do filter
for(int i = 0; i < real.Length; i++)
{
double binFreq = binWidth * i;
double gain = DCGain / ( System.Math.Sqrt( ( 1 + System.Math.Pow( binFreq / f0, 2.0 * order ) ) ) );
if(i > (real.Length>>3))
{
real _*= gain;_
-
}*
-
if(i > (real.Length>>3))*
-
{*
img *= gain;
* }*
* }*
* double [] dsamples2;*
* rft.TransformBackward(real, img, out dsamples2);*
* float [] samples2 = System.Array.ConvertAll(dsamples2, x => (float)x);*
* int idx = 0;*
* for(int i = sampleIdx; i < sampleIdx + sampleSize; i++)*
* {*
_ filteredSamples = samples2[idx++];
* }
sampleIdx += sampleSize;
}*_
* res = audioSrc.clip.SetData(filteredSamples, 0);*
* audioSrc.Play();*
* playTimeStart = Time.time;*
* }*
* // Update is called once per frame*
* void Update () {*
* if(!filtered)*
* {*
* Filter();*
* }else*
* {*
* }*
* }*
}