Search Issue Tracker
Not Reproducible
Votes
3
Found in
5.0.0f3
Issue ID
675086
Regression
No
AudioClip.GetData wrapping bug
To reproduce:
1. Open attached project
2. Open test scene
3. Press play
4. Hear the last targetLength samples of the clip
5. Stop scene
6. Disable Stereo game object and enable Mono
7. Press play
8. Wrapped samples seem to be wrongly offset, resulting in the first samples of the clip being heard at the end of the new grabbed data
Resolved (Not Reproducible):
The script is computing the buffer offset incorrectly. The GetData API works in sample frames (left+right) while the C# array works on single samples and therefore needs to be multiplied by the channel count. Here's a fixed version:
using UnityEngine;
using System.Collections;
[ RequireComponent( typeof( AudioSource ) ) ]
public class WrapMonoBug : MonoBehaviour
{
// Only mono clips seem to trigger the bug
public AudioClip clip;
// Use this for initialization
void Start ()
{
int bufLength = 16384;
int targetLength = 15000;
float[] buf1, buf2;
buf1 = new float[ bufLength * clip.channels ];
buf2 = new float[ targetLength * clip.channels ];
int offset = clip.samples - targetLength; //grab the last targetLength samples of the clip.
clip.GetData( buf1, offset ); // Probably a wrap error here in GetData: after wrapping, it seems offset in the target buffer is incorrectly updated
System.Array.Copy( buf1, buf2, targetLength * clip.channels ); //copy to new buffer( to set data in a new clip )
AudioClip newClip = AudioClip.Create( "test", targetLength, clip.channels, clip.frequency, false); //create new clip of targetLength and set data
newClip.SetData( buf2, 0 );
//play result: we hear the attack of the original clip instead of only the targetLength last samples
AudioSource source = this.GetComponent< AudioSource >();
source.clip = newClip;
source.loop = true;
source.Play();
}
}
Add comment
All about bugs
View bugs we have successfully reproduced, and vote for the bugs you want to see fixed most urgently.
Latest issues
- “[Worker0] Could not generate preview image“ error when opening macOS native plugin in the Inspector with Architecture to build for set to ARM 64-bit
- [iOS] Application.absoluteURL is empty on Awake/Start when opening via deep link with Splash Screen disabled
- Crash on MemoryManager::Deallocate when rapidly calling Addressables.LoadAssetAsync
- Crash on physx::shdfnd::atomicIncrement when adjusting values on a character controller component after entering Play mode in Prefab edit mode
- [Rendering Debugger] [NewInputSystem] Debug Overlays in Play mode throws InvalidOperationException when using New Input System
webbp
Oct 15, 2015 18:30
Same issue here. I used Microphone.Start with loop=true, channels=1, and rate=16000. AudioClip.GetData that overlaps the wraparound results in audio chunks rearranged like: A B C -> A C B or A C ?
e.g., http://babybrain.org/should-rise-continuously.wav