Wondering about locking model in ResamplingAudioSource

25 posts / 0 new
Last post
TheVinn
Offline
Last seen: 2 months 2 weeks ago
Joined: 29 Aug 2009 - 11:31
Wondering about locking model in ResamplingAudioSource

I'm having a look at ResamplingAudioSource and this is what i see:

void ResamplingAudioSource::setResamplingRatio (const double samplesInPerOutputSample)
{
    //...
    const ScopedLock sl (ratioLock);
    ratio = jmax (0.0, samplesInPerOutputSample);
}

void ResamplingAudioSource::getNextAudioBlock (const AudioSourceChannelInfo& info)
{
    const ScopedLock sl (ratioLock);

Why does getNextAudioBlock hold the lock for the entire function call? Is it because it accesses the 'ratio' class member more than once? Assignments of floats are atomic (at least, on Intel, I think).

Regardless, why not just do this

void ResamplingAudioSource::getNextAudioBlock (const AudioSourceChannelInfo& info)
{
    {
        const ScopedLock sl (ratioLock);
        float workingRatio = ratio;
    }

    // use workingRatio instead of ratio now

Am I missing something important?

jules
Offline
Last seen: 18 min 4 sec ago
Joined: 29 Apr 2013 - 18:37
Re: Wondering about locking model in ResamplingAudioSource

Thanks, I'll take a look..

TheVinn
Offline
Last seen: 2 months 2 weeks ago
Joined: 29 Aug 2009 - 11:31
Re: Wondering about locking model in ResamplingAudioSource

jules wrote:
Thanks, I'll take a look..

I'm not complaining...nor am I using this code. I was just studying it to see how it works - I was already a fairly knowledgable programmer but I have learned a lot from looking at your designs.

So I'm not saying its a "bug" I'm just wondering if I am missing out on something unknown.

jules
Offline
Last seen: 18 min 4 sec ago
Joined: 29 Apr 2013 - 18:37
Re: Wondering about locking model in ResamplingAudioSource

I think the only thing you're missing is how unlikely it is that this would cause anybody a problem.

You are right, and I will tidy it up because it could be done better. But for it to make any noticeable impact on performance, you'd have to be absolutely hammering that setResamplingRatio() call. And since the class wasn't designed to have its speed continuously varied, that would probably create quite an ugly noise, so isn't something that people are likely to ever do!

steffen
Offline
Last seen: 8 months 1 week ago
Joined: 4 May 2010 - 11:52
Re: Wondering about locking model in ResamplingAudioSource

Quote:
think the only thing you're missing is how unlikely it is that this would cause anybody a problem.

I've been wondering about this as well. In another recent post you mentioned that CriticalSections are a sure way to cause mayhem in ProTools.

I know locks in the audio-thread should be avoided at all costs, but there are two or three locations in our plugin where I still use them, as it's impossible (or at least ridicously complex) to solve it with atomics or other lock-free patterns.

So is a CriticalSection in the PT audio-thread still a critical issue if you know
- it will only be held for a short time
- the chance it is already locked by another thread is very low
- not having a lock is likely to result in a crash or an ugly race-condition

Quote:
And since the class wasn't designed to have its speed continuously varied, that would probably create quite an ugly noise, so isn't something that people are likely to ever do!

You underestimate what people will do if they can. We are actually using the resampling in the Elastik plugin and allow to control the ratio in real-time. It's a fun effect, and while there might be small glitches as the class wasn't designed for that, they are not audible.
jules
Offline
Last seen: 18 min 4 sec ago
Joined: 29 Apr 2013 - 18:37
Re: Wondering about locking model in ResamplingAudioSource

Quote:
We are actually using the resampling in the Elastik plugin and allow to control the ratio in real-time. It's a fun effect, and while there might be small glitches as the class wasn't designed for that, they are not audible.

Well, I bet they're audible in mine!

The locking could indeed be a problem in PT, so using this class in a plugin could be a bad idea. Annoyingly, although it'd be very easy to change the ratio to an Atomic and avoid locks, I think that'd probably stop it compiling on some platforms that don't have support for 64-bit atomics.

TheVinn
Offline
Last seen: 2 months 2 weeks ago
Joined: 29 Aug 2009 - 11:31
Re: Wondering about locking model in ResamplingAudioSource

jules wrote:
Quote:
it'd be very easy to change the ratio to an Atomic and avoid locks

The lock is fine, since with your recent change it is held for O(1) instead of O(N).

TheVinn
Offline
Last seen: 2 months 2 weeks ago
Joined: 29 Aug 2009 - 11:31
Re: Wondering about locking model in ResamplingAudioSource

jules wrote:
since the class wasn't designed to have its speed continuously varied, that would probably create quite an ugly noise, so isn't something that people are likely to ever do!

Let me tell you that you couldn't be more wrong. Not only does changing the speed continuously work GREAT (like, a new speed every 16 samples), its fast as hell, the sound quality is perfect, and it has almost no latency! This is just what the doctor ordered for simulation of vinyl scratching effects.

I've gone through 4 other sets of resampling code (secret rabbit, libresample, others) for doing continuously variable time-stretching of audio material and the juce ResamplingAudioSource is the best by a clear margin!

jules
Offline
Last seen: 18 min 4 sec ago
Joined: 29 Apr 2013 - 18:37
Re: Wondering about locking model in ResamplingAudioSource

Quote:
I've gone through 4 other sets of resampling code (secret rabbit, libresample, others) for doing continuously variable time-stretching of audio material and the juce ResamplingAudioSource is the best by a clear margin!

Excellent! It's probably good this that situation because it's not trying to do anything fancy - the others probably use all kinds of clever tricks that sound great at a continuous speed, but don't handle changes in pitch very smoothly.

robiwan
Offline
Last seen: 4 days 6 hours ago
Joined: 18 Dec 2006 - 20:38
Re: Wondering about locking model in ResamplingAudioSource

Regarding the lock, how bout piping the ratio into the PT via AbstractFifo ? Then you won't need a lock at all.

Welcome to oblivion, will you be staying?

jules
Offline
Last seen: 18 min 4 sec ago
Joined: 29 Apr 2013 - 18:37
Re: Wondering about locking model in ResamplingAudioSource

It could also be done using a ReferenceCountedObjectPtr to pass it.. But probably the easiest lock-free method would just be to convert it to an integer and pass it as an Atomic

Pages