Dissonance icon indicating copy to clipboard operation
Dissonance copied to clipboard

cannot retrieving audio data when using AEC function

Open jackyetz opened this issue 9 months ago • 3 comments

Following the "https://placeholder-software.co.uk/dissonance/docs/Tutorials/Acoustic-Echo-Cancellation.html", I am trying to deal with the Echo issue using the class AECHelper (see https://github.com/jackyetz/pub/blob/main/AECHelper.cs). The following first paragraph of code in my main thread calls AECHelper, and "aechelper.Update()" in the Update() of the main thread. When retrieving the audio data using the following second paragraph of code, I got error prompt (see the bottom paragraph) which happened with "recordClip.GetData".

BTW, if instead using "audioLocal.clip = Microphone.Start(device, true, loopLength, sampleRate)" to retrieve audio data, no error prompt come out.

aechelper = gameObject.AddComponent<AECHelper.AECHelper>();
aechelper.gameobjname = "xiaoyang";  // unity object with an audiosource
aechelper.playlocalspeech = true;       
aechelper.Start();	// start 
while (aechelper.audioclip == null || aechelper.audioclip.length <= 0)
{
    // AudioClip没有内容
    Thread.Sleep(100);
}
audioLocal = aechelper.audiosrc;
int length = recordClip.channels * recordClip.samples;
int isplit = length / recordClip.frequency * 10; //split audio data into ten parts and retrieve one by one。
int micPosition = 0;
float[] micDataTemp;
int i;
while (true)
{
    for (i = 1; i <= isplit - 1; i++) 
    {
        yield return new WaitUntil(() => { micPosition = Microphone.GetPosition(device); return micPosition > length * i / isplit && micPosition < length * (i + 1) / isplit; });
        micDataTemp = new float[length / isplit];
        recordClip.GetData(micDataTemp, (i - 1) * length / isplit);
    }
    yield return new WaitUntil(() => { micPosition = Microphone.GetPosition(device); return micPosition < length / isplit; });//get the last part of the audio data
    micDataTemp = new float[length / isplit];
    recordClip.GetData(micDataTemp, (isplit - 1) * length / isplit);
}

Can't get data from streamed samples for audio clip "MySinusoid". If you created the audio clip with AudioClip.Create, set the 'stream' argument to false, so you can access the data with 'AudioClip.GetData'. For a disk-based audio clip, change the load type to 'DecompressOnLoad', so you can access the data with 'AudioClip.GetData'. UnityEngine.AudioClip:GetData (single[],int) xfHelper.xfAPIHelper/<MSCAwaken>d__21:MoveNext () (at Assets/Scripts/Helper/xfHelper.cs:90) UnityEngine.SetupCoroutine:InvokeMoveNext (System.Collections.IEnumerator,intptr)

jackyetz avatar May 11 '24 11:05 jackyetz