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
- NullReferenceException when setting 'isTextObjectScaleStatic' to false on a disabled TextMeshPro GameObject
- Shader Stripping Custom Options disappear when exiting Play mode without reloading Domain
- Decals do not get projected when 'Rendering Layer Mask' on a GameObject is 23rd Layer or above due to encoding/decoding issues
- Deriving from SearchContextAttribute doesn't always work
- Scripting API documentation is missing for macOS editor extensions
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