Prepare to play stereo and sample rate issue

Hi everyone,
I’m having some issue here trying to figure out what i do wrong.
My plugin is playing Binary Wav Files simultaly

in the constructor

newSource = getReader(BinaryData::Test1_wav, BinaryData::Test1_wavSize);
newSource2 = getReader(BinaryData::Test2_wav, BinaryData::Test2_wavSize);
void FXAudioProcessor::processBlock(juce::AudioBuffer<float>& buffer, juce::MidiBuffer& midiMessages)
{

	juce::AudioSourceChannelInfo info(&mixBuffer, 0, buffer.getNumSamples());
	
	sourceToBuffer(newSource, info, mixBuffer, buffer, ((1.0 * p)  /2 * d));
	sourceToBuffer(newSource2, info, mixBuffer, buffer, ((1.0 * i) / 2 * d));`
}
void FXAudioProcessor::sourceToBuffer(juce::AudioFormatReaderSource* source, juce::AudioSourceChannelInfo info, juce::AudioSampleBuffer mixBuffer, juce::AudioSampleBuffer buffer, float calcul)
{

	source->getNextAudioBlock(info);
	int mini = (mixBuffer.getNumChannels() < buffer.getNumChannels()) ? mixBuffer.getNumChannels() : buffer.getNumChannels();
	for (int c = 0; c < mini; ++c)
		buffer.addFrom(0, c, mixBuffer.getReadPointer(c), buffer.getNumSamples(), calcul);

}
void FXAudioProcessor::prepareToPlay(double sampleRate, int samplesPerBlock)
{
	mixBuffer.setSize(1, samplesPerBlock);

}

Now it plays fine (left speaker) but when it gets to the point to switch in stereo

void FXAudioProcessor::prepareToPlay(double sampleRate, int samplesPerBlock)
{
	mixBuffer.setSize(2, samplesPerBlock);

}

somehow doesn’t work… on top of that by launching the plugin with the daw set up at 44100hz wich is the sample rate of the files too, everything plays fine but when it get to the point to set the project to 48000hz, files aren’t been resampled then obviously been played faster.

Been trying to find thoose past days step by step what i do wrong, would anybody get a clue??
Thanks a lot!

oh and forgot to share the reader function

juce::AudioFormatReaderSource* FXAudioProcessor::getReader(const char* Test7_wav, int Test7_wavSize)
{
	
	juce::AudioFormatReader* reader = formatManager.createReaderFor(std::make_unique<juce::MemoryInputStream>(Test7_wav, Test7_wavSize, false));
	juce::AudioFormatReaderSource* source;
	
	if (reader != nullptr)
	
	{
		source = new juce::AudioFormatReaderSource(reader, true);
		source->setLooping(true);
	}

	return source;

}

Have you checked that the files you’re loading are definitely stereo? If the files are mono, you may need some additional processing to copy the first channel to the other channels.

You’ll need to resample the files so that they play back at the correct rate. The ResamplingAudioSource should do what you want.

1 Like

Thanks a lot replying :wink:
Have you checked that the files you’re loading are definitely stereo? If the files are mono, you may need some additional processing to copy the first channel to the other channels.
i did yeah i guess there would be something else i do wrong

void FXAudioProcessor::prepareToPlay(double sampleRate, int samplesPerBlock)
{
	mixBuffer.setSize(1, samplesPerBlock);//*trying with 2 and jassert goes up

}

Thanks for the ResamplingAudioSource tip sounds fair

buffer.addFrom(c, 0, mixBuffer.getReadPointer(c), buffer.getNumSamples(), calcul);
buffer.addFrom(0, c, mixBuffer.getReadPointer(c), buffer.getNumSamples(), calcul);

just got thoose two argument mixed up , my bad