Sampler with different start points in sample

Hello everyone,

I am relatively new to juce and just programmed my first sampler. Now I want to add the functionality to start the loaded sample at a different point. E.g. there is a 5 second sample and it starts playing from second 2 when I play it. I see how you can edit the next .renderNextBlock, but I am not quite sure how to change the playing position for the whole sample.

Kind regards

I thought about creating a custom audio buffer that I can change and give this changed buffer to the processblock. But this results in either no sound, when I use my custom buffer or just the pprocessblock using its own and ignoring mine.

Basically, you just need to start the sample reading index from another position than 0. How you would do that, completely depends on how you have implemented your code, so I can’t give any further instructions on that. If you are using the Juce SamplerSound and SamplerVoice, you won’t really be able to do it, though. Those are quite limited classes and you can’t do much by inheriting from them, either.

Mhm maybe there is the problem. I am using a synthesiser with SamplerSound and SamplerVoice…

What would you suggest to use instead?

I tried to skip samples with the audioFormatReader but it seems to not change anything:

if (filechooser.browseForFileToOpen())
		auto choosenFile = filechooser.getResult();
		audioFormatReader = audioFormatManager.createReaderFor(choosenFile);

		auto sampleLengthOfLoadedSample = static_cast<int>(audioFormatReader->lengthInSamples);

		loadedSample.setSize(1, sampleLengthOfLoadedSample);
		audioFormatReader->read(&loadedSample, numberOfSkippedSamples, (sampleLengthOfLoadedSample - numberOfSkippedSamples), numberOfSkippedSamples, true, false);
		auto buffer = loadedSample.getReadPointer(0);

	juce::BigInteger samplerSoundRange;
	samplerSoundRange.setRange(0, 128, true);
	//Adds a new sound to our Sampler
	synthesiser.addSound(new juce::SamplerSound("Sample", *audioFormatReader, samplerSoundRange, 60, 0.1, 0.1, 5.0));

Okay. I am still working on it. Thinking about a workaroung, where I use the AudioFormatWriter with the .write() method to create an edited audio stream and read this stream with an AudioFormatReader. But I right now my application stops when I am loading the file. I am still new to this all help is appreciated! Thank you in advance :slight_smile:

This is the part of code that causes the stopping:

audioFormatWriter = wavAudioFormat.createWriterFor(memoryOutputStream, 


and this is the stopping point and it says “this” was nullptr:

const int bufferSize = 16384;
    AudioBuffer<float> tempBuffer ((int) numChannels, bufferSize);