Search Issue Tracker

Not Reproducible

Votes

3

Found in

5.0.0f3

Issue ID

675086

Regression

No

AudioClip.GetData wrapping bug

Audio

-

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();
}
}

Comments (1)

  1. 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

Add comment

Log in to post comment

All about bugs

View bugs we have successfully reproduced, and vote for the bugs you want to see fixed most urgently.