Buzz/crackle with sample player

Hey there. I’ve just been getting started with both C++ and JUCE. This about my third plugin. The first was a basic sample opener that took MIDI, the second was basically the standalone version of this. This is a sample player that loads four random samples and then crossfades through them. Sort of like a primitive version of Lunacy Audio’s CUBE before I knew it existed. The standalone version works fine, but works differently (no processBlock). For whatever reason I’m getting some serious crunchy/lo-fi results in the plugin attempt. I thought it might be sample rate, but I’ve added resampling after files are loaded. The way midi notes affect pitch is primitive right now, but also not the cause since the buzz/crunch was happening before that was implemented.

edit: code removed. fixed the issues pointed out in comments, but the problem persisted. ended up refactoring using JUCE’s AudioTransportSource and AudioFormatReaderSource to handle audio playback

You’re doing at least three things you shouldn’t in the audio thread:

  • allocating a new AudioBuffer
  • using a lock
  • using DBG

The DBG and the lock probably aren’t the issue here since you’re not printing that much and there may not be a lot of contention for the lock, but the memory allocation seems problematic.

1 Like

I would change how playRates is used:

int readPosition = static_cast<int>(playHeads[fileIndex]) % sampleLengths[fileIndex];
playHeads[fileIndex] += playRates[fileIndex]);
if (playHeads[fileIndex] >= static_cast<float>(sampleLengths[fileIndex])) {

juce::AudioBuffer<float> should not be allocated in processBlock.

1 Like

Experienced buzz/crackle issues in JUCE plugin sample player, despite resampling. Refactored with AudioTransportSource, AudioFormatReaderSource for playback.