Search Issue Tracker

By Design

Votes

0

Found in

2017.1.1f1

Issue ID

976455

Regression

No

[Windows] Sounds played with PlayScheduled don't have perfect stitching if second sound begins with non-zero amplitude

Audio

-

If User plays the shifted sound twice in a row in play mode, using PlayScheduled, the second time and eventual further times will produce the click as if the clip was being started from silence and not stitched to its own end.

To reproduce:

1. Download attached project "PlayScheduled imperfect stitching.zip" and open in Unity
2. Open "Scene" scene
3. Place the "sineA" sound on the Clip field of the "VoicePlayer" script in the Main Camera
4. Enter Play mode
Note: Observe that after roughly 1.5 seconds, the sound will play twice in a row with a seamless and unnoticeable transition. This works perfectly.
5. Exit Play mode
6. Place the "sineA shifted" sound on the Clip field of the "VoicePlayer" script in the Main Camera
7. Enter Play mode
Note: Observe that after roughly 1.5 seconds, the sound will start with a small click (this is normal!). After 2 seconds, where a gapless transition should happen, you will hear another small click when the sound begins again, even though the same clip used in a loop in the Inspector will play flawlessly

Notes:
- This issue appears only on Windows
- There are two examples of sine waves that are perfect loops: one is normal and the other is shifted so that the first samples of the clip were moved to the end.
- The shifted clip does not begin on a zeroed sample, it begins somewhere in the middle of the wave, having non-zero amplitude, the clip produces a small click at the start when reproduced (this is normal!).
- Both sounds are perfect loops
- Use headphones or get close to the speakers in a silent place to hear that click
- User cannot solve this with a loop since he uses two different sounds in the real-world case

Reproduced on Unity 5.6.4p4, 2017.1.2p3, 2017.2.1f1, 2017.3.0f2 and 2018.1.0a7

  1. Resolution Note:

    I was able to find the right algorithm to fix the issue on the script side. If you calculate the audio clip duration using this algorithm, it would play without a click.

    float freq = clip.frequency;
    int targetFreq = AudioSettings.GetConfiguration().sampleRate;

    float fSpeed = freq / targetFreq * 4294967296.0f;
    long speed = (long)fSpeed;
    long samples = (long)clip.samples<<32;
    long fracleft = samples % speed;
    samples /= speed;
    if (fracleft != 0) {
    samples++;
    }
    clipDuration = samples / (double) targetFreq;

    After some discussion in the team, we concluded that this is not a bug as such, and getting the desired result is simply hard without replicating the exact sample-perfect calculation as FMOD. We'll be looking into adding a new API for getting the sample-perfect stitching in the future.

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.