Placeholder-Software / Dissonance

Unity Voice Chat Asset
69 stars 5 forks source link

cannot retrieving audio data when using AEC function #290

Open jackyetz opened 1 month ago

jackyetz commented 1 month ago

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/d__21:MoveNext () (at Assets/Scripts/Helper/xfHelper.cs:90) UnityEngine.SetupCoroutine:InvokeMoveNext (System.Collections.IEnumerator,intptr)

martindevans commented 1 month ago

Sorry, but I'm not clear on what the issue is.

jackyetz commented 1 month ago

Sorry, but I'm not clear on what the issue is.

i wanna get the acoustic echo cancelled microphone audio data, but failed. Can u help me. See above for the detailed error prompt

martindevans commented 1 month ago

To get mic audio with AEC from Dissonance:

  1. Set up Dissonance AEC. (docs).
  2. Use BaseMicrophoneSubscriber or IMicrophoneSubscriber to access the recorded audio stream. (docs).