I’m trying to synchronize an ending/transition sound to a streaming loop using setDelay but it’s not working.
The first and second times the sounds stitch correctly but then I get a gap every other loop and I can’t seem to find the error.
[edit] actually, the sound stitches correctly only in the first loop, then it’s kinda random.

Something like this:
|-end-| |-end-| |-end-|

This is my scheduling code:
//This function is called when the main stream is played and when the Ending sound stops so it’ll be rescheduled at the next loop/end.
//The main stream is always bigger than the end.
private void ScheduleEnd()
//Lock the DSP so we don’t mess the precision.

        uint hi = 0, low = 0;
        ERRCHECK(FModSystem.getDSPClock(ref hi, ref low));

        uint mainLow = 0u;
        _mainChannel.getDelay(DELAYTYPE.DSPCLOCK_START, ref hi, ref mainLow);

        low = low < mainLow ? mainLow : low;

//The main streaming loop might be scheduled too, so we use it’s start time if it’s higher than the DSP clock.
//This should be the real start time. If I only used the DSP clock time this would be wrong when the stream itself was scheduled.

        uint length = 0;
        ERRCHECK(_mainSound.getLength(ref length, TIMEUNIT.PCM));

//Main stream length.

        uint position = 0;
        ERRCHECK(_mainChannel.getPosition(ref position, TIMEUNIT.PCM));

//Main stream current position.

        uint nextEnd = (uint)((length - position) * (_outputrate / _frequency));

//Calculate the time remaining from the main stream current position till the end. It might be at any point in the stream.
//I get the same problem if I take the "outputrate/frequency" part. The result would be 1, so no gaps should be introduced from fraction loss here.

        DELAYTYPE_UTILITY.FMOD_64BIT_ADD(ref hi, ref low, 0, nextEnd);

//Add the real main stream start time with the time remaining till the end.

        ERRCHECK(_endChannel.setDelay(DELAYTYPE.DSPCLOCK_START, hi, low));


Any ideas?

Thanks for reading.

  • You must to post comments

Hi Peter,

I’ve found another solution that works but I still don’t now what was wrong.
Here what I was going to answer before:

I’m trying to schedule a sound to the end/loop point of another sound that is looping.
With that code sometimes my scheduling misses the end by a few MS.

low = low < mainLow ? mainLow : low;
Is used in case the main loop itself was scheduled.

(length – position)
Is there because the looping sound might be at any point when I schedule the ending sound.

(TimeNow or LoopStartTime) + (Loop Time Remaining) = Loop End.

The outputrate/frequency comes from the wiki: [url:1h0b3hvl]http://www.fmod.org/wiki/index.php5?title=Sample-accurate_sequencing_with_setDelay[/url:1h0b3hvl]
I think thats used to fix the scheduling when playing sounds at a frequency that differs from the fmod current output.
void SoundManager::scheduleChannel(int i)
int prev = (i + m_numsounds – 1) % m_numsounds;

Uint64P start_time;

bool playing = false;

    // schedule at the end of the previous channel
    unsigned int length_pcm;
    float frequency;

                                        &amp;start_time.mHi, &amp;start_time.mLo));
    ERRCHECK(m_sounds[prev]-&gt;getDefaults(&amp;frequency, 0, 0, 0));
    ERRCHECK(m_sounds[prev]-&gt;getLength(&amp;length_pcm, FMOD_TIMEUNIT_PCM));

    length_pcm = unsigned int((length_pcm * m_outputrate / frequency) + 0.5f);
    start_time += length_pcm;
    // the previous channel isn't playing; schedule ASAP
    ERRCHECK(m_system-&gt;getDSPClock(&amp;start_time.mHi, &amp;start_time.mLo));
    start_time += m_min_delay;

                                 start_time.mHi, start_time.mLo));


  • You must to post comments

Hi Jose,

I dont’ completely understand your intention but if you want to stich the sounds back-to-back, it would probably be better to call getDelay DSPCLOCK_END. I’m not sure why you are doing that arithmatic with outputrate and frequency, but you shouldn’t need to do any of that.

  • You must to post comments
Showing 2 results
Your Answer

Please first to submit.