hi,sorry to disturb.I’m a new user of JUCE.
I need to render audio file through vst2 and vst3 plugins.vst3 just works fine,but the result of vst2 sounds very weird,I’ve tried several vst2 plugins,all weird.
Is there any wrong with my codes?
thanks a lot.
// scan plugin
juce::AudioFormatManager formatManager;
formatManager.registerBasicFormats();
juce::AudioPluginFormatManager pluginFormatManager;
pluginFormatManager.addDefaultFormats();
juce::PluginDescription foundPluginDescription;
juce::KnownPluginList pluginList;
juce::OwnedArray<juce::PluginDescription> typesFound;
juce::AudioPluginFormat* vst2Format = pluginFormatManager.getFormat(0);
juce::AudioPluginFormat* vst3Format = pluginFormatManager.getFormat(1);
juce::String vst2PluginPath = "D:/Program Files/VSTPlugins/SoundToys/Crystallizer.dll";
juce::String vst3PluginPath = "C:/Program Files/Common Files/VST3/kiloHearts/kHs Bitcrush.vst3";
pluginList.scanAndAddFile(vst2PluginPath, false, typesFound, *vst2Format);
juce::Logger::writeToLog("Found " + juce::String(typesFound.size()) + " plugins in " + vst2PluginPath);
foundPluginDescription = *typesFound[0];
// read audio file
juce::String inputFilePath("F:/Projects/python/VSTRender/resources/mono.wav");
juce::File inputFile(inputFilePath);
std::unique_ptr<juce::AudioFormatReader> reader(formatManager.createReaderFor(inputFile));
if (reader.get() == nullptr)
{
juce::Logger::writeToLog("Failed to open input file: " + inputFilePath);
return;
}
// create buffer
juce::AudioBuffer<float> buffer(reader->numChannels, static_cast<int>(reader->lengthInSamples));
reader->read(&buffer, 0, static_cast<int>(reader->lengthInSamples), 0, true, true);
// load a vst2 or vst3 plugin
std::unique_ptr<juce::AudioPluginInstance> pluginInstance =
vst2Format->createInstanceFromDescription(foundPluginDescription, reader->sampleRate,
512);
/*std::unique_ptr<juce::AudioPluginInstance> pluginInstance =
vst3Format->createInstanceFromDescription(foundPluginDescription, reader->sampleRate,
512);*/
juce::Logger::writeToLog("Plugin instance created " + juce::String(pluginInstance->getName()));
// set plugin channel layout
if (pluginInstance == nullptr)
{
juce::Logger::writeToLog("Failed to load plugin: " + juce::String(pluginInstance->getName()));
}
if (reader->numChannels == 1)
{
pluginInstance->setChannelLayoutOfBus(false, 0, juce::AudioChannelSet::mono());
}
else if (reader->numChannels == 2)
{
pluginInstance->setChannelLayoutOfBus(false, 0, juce::AudioChannelSet::stereo());
}
else
{
juce::Logger::writeToLog("Unsupported number of channels: " + juce::String(reader->numChannels));
return;
}
pluginInstance->setNonRealtime(true);
pluginInstance->prepareToPlay(reader->sampleRate, 512);
// process audio
pluginInstance->processBlock(buffer, juce::MidiBuffer());
// write output file
juce::String outputFilePath("F:/Projects/python/VSTRender/resources/output.wav");
juce::File outputFile(outputFilePath);
if (outputFile.exists())
{
outputFile.deleteFile();
}
outputFile.create();
std::unique_ptr<juce::FileOutputStream> outputStream(outputFile.createOutputStream());
if (outputStream.get() == nullptr)
{
juce::Logger::writeToLog("Failed to open output file: " + outputFilePath);
return;
}
std::unique_ptr<juce::AudioFormatWriter> writer(formatManager.findFormatForFileExtension(inputFile.getFileExtension())
->createWriterFor(outputStream.get(), reader->sampleRate, buffer.getNumChannels(), reader->bitsPerSample, {}, 0));
outputStream.release();
writer->writeFromAudioSampleBuffer(buffer, 0, buffer.getNumSamples());
pluginInstance->releaseResources();
juce::Logger::writeToLog("Finished processing audio file: " + inputFilePath);
