- Home /
AudioClip.Create()
The Forums didn't let me post this question, they got automatically classified as spam. Forum: http://forum.unity3d.com/threads/audioclip-setdata-crackling.325833/
Hey Forum(Answers) I'm on a journey to efficiently create an Audio Source out of multiple other sources. The source should copy what other sources are playing right now. I want to use this to simulate simple sound propagation.
So far I've tried OnAudioFilterRead(), AudioClip.Create() streaming with the PCMReaderCallback and now AudioClip.Create() without streaming enabled.
OnAudioFilterRead() worked quite well, but got output as a 2D sound, without any panning or volume falloff, so that's not what I'm looking for.
AudioClip.Create() with streaming = true seemed nice at first, but I didn't get it to work. The Callbacks seem very unreliable and are oddly timed. Surely there is a way, but I failed using it. Any tips?
AudioClip.Create() with streaming = false works... almost. I call AudioSource.GetData() and .SetData() every few frames and save the result in a buffer that is long enough for the interval. The problem is, that I have small crackles in the sound whenever I call those two functions. Any ideas how to avoid those?
What's the proper way to tackle this?
stream = false version:
public AudioSource OriginalSource;
private float[] data;
private AudioSource src;
private AudioClip clp;
private int refreshInterval
private float Timer;
void Update()
{
Timer += Time.deltaTime;
//Timer does not need to be accurate, because data[] is longer than
//the refreshInterval
if (Timer > refreshInterval)
{
GetSampleData();
Timer = 0;
}
}
void GetSampleData()
{
OriginalSource.clip.GetData(data, OriginalSource.timeSamples);
clp.SetData(data, 0);
src.Play();
}
EDIT: Got the streamed version to work, too... but again with minor crackles.
void PCMRead(float[] tempData)
{
int count = 0;
while (count < tempData.Length)
{
tempData[count] = data[count+position];
count++;
}
position += count;
}
void Update()
{
GetSampleData();
position = 0;
}
void GetSampleData()
{ //adding 15000 to compensate the offset, magical number for now.
OriginalSource.clip.GetData(data, OriginalSource.timeSamples + 15000);
}
The problem with the strea$$anonymous$$g version seems to be, that the function (PC$$anonymous$$Read) simply takes to long to process.
Answer by Sprite101 · Nov 16, 2015 at 10:11 PM
So I've been looking into this myself, and I've found no real good solution. AudioClip.Create asks you if you want the clip to be streaming, but if you read the clip's loadType, it reads as DecompressOnLoad instead of Streaming. My assumption is all the gaps in the audio on playback are due to the AudioSource trying to decompress the data...
Unfortunately, loadType is read only, and you can't access the PCMReader callback without using Create() I have yet to find a solution.
Your answer
![](https://koobas.hobune.stream/wayback/20220613195603im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
Distribute terrain in zones 3 Answers
Audio Trigger 3 Answers
Every few seconds, play an audio clip 3 Answers
Illuminating a 3D object's edges OnMouseOver (script in c#)? 1 Answer