首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >音频因VST插件失真

音频因VST插件失真
EN

Stack Overflow用户
提问于 2015-11-08 22:59:52
回答 1查看 383关注 0票数 0

我不得不插入一个预先存在的软件,管理ASIO音频流,一个简单的VST主机。尽管缺少一些文档,我还是设法做到了,但是一旦我加载了插件,我就得到了一个严重失真的音频信号。

我使用的VST工作正常(与其他VST主机),所以它可能是我编写的代码中的某种bug,但是当我禁用插件中的“进程”时(我的流通过插件,它根本不会被处理),它会像我发送的那样返回,没有任何噪音或失真。

我稍微关心的一件事是ASIO驱动程序填充__int32缓冲区时使用的数据类型,而插件需要一些浮动缓冲区。

这真的很令人沮丧,因为我无数次地检查了我的代码,它似乎是好的。

以下是我正在使用的类的代码;请注意,一些数字是临时硬编码的,以帮助调试。

代码语言:javascript
复制
VSTPlugIn::VSTPlugIn(const char* fullDirectoryName, const char* ID)
    : plugin(NULL)
    , blocksize(128)        // TODO
    , sampleRate(44100.0F)  // TODO
    , hostID(ID)
{
    this->LoadPlugin(fullDirectoryName);
    this->ConfigurePluginCallbacks();
    this->StartPlugin();

    out = new float*[2];

    for (int i = 0; i < 2; ++i)
    {
        out[i] = new float[128];
        memset(out[i], 0, 128);
    }
}

void VSTPlugIn::LoadPlugin(const char* path)
{
    HMODULE modulePtr = LoadLibrary(path);

    if(modulePtr == NULL)
    {
        printf("Failed trying to load VST from '%s', error %d\n", path, GetLastError());
        plugin = NULL;
    }

    // vst 2.4 export name
    vstPluginFuncPtr mainEntryPoint = (vstPluginFuncPtr)GetProcAddress(modulePtr, "VSTPluginMain");

    // if "VSTPluginMain" was not found, search for "main" (backwards compatibility mode)
    if(!mainEntryPoint)
    {
        mainEntryPoint = (vstPluginFuncPtr)GetProcAddress(modulePtr, "main");
    }

    // Instantiate the plugin
    plugin = mainEntryPoint(hostCallback);
}

void VSTPlugIn::ConfigurePluginCallbacks()
{
    // Check plugin's magic number
    // If incorrect, then the file either was not loaded properly, is not a
    // real VST plugin, or is otherwise corrupt.
    if(plugin->magic != kEffectMagic) 
    {
        printf("Plugin's magic number is bad. Plugin will be discarded\n");
        plugin = NULL;
    }

    // Create dispatcher handle
    this->dispatcher = (dispatcherFuncPtr)(plugin->dispatcher);

    // Set up plugin callback functions
    plugin->getParameter     = (getParameterFuncPtr)plugin->getParameter;
    plugin->processReplacing = (processFuncPtr)plugin->processReplacing;
    plugin->setParameter     = (setParameterFuncPtr)plugin->setParameter;
}

void VSTPlugIn::StartPlugin()
{
    // Set some default properties
    dispatcher(plugin, effOpen, 0, 0, NULL, 0);
    dispatcher(plugin, effSetSampleRate, 0, 0, NULL, sampleRate);
    dispatcher(plugin, effSetBlockSize, 0, blocksize, NULL, 0.0f);
    this->ResumePlugin();
}

void VSTPlugIn::ResumePlugin()
{
    dispatcher(plugin, effMainsChanged, 0, 1, NULL, 0.0f);
}

void VSTPlugIn::SuspendPlugin()
{
    dispatcher(plugin, effMainsChanged, 0, 0, NULL, 0.0f);
}

void VSTPlugIn::ProcessAudio(float** inputs, float** outputs, long numFrames)
{
    plugin->processReplacing(plugin, inputs, out, 128);
    memcpy(outputs, out, sizeof(float) * 128);
}

编辑:这是我用来连接我的软件和VST主机的代码

代码语言:javascript
复制
// Copying the outer buffer in the inner container
for(unsigned i = 0; i < bufferLenght; i++)
{
    float f;
    f = ((float) buff[i]) / (float) std::numeric_limits<int>::max()

    if( f > 1 ) f = 1;
    if( f < -1 ) f = -1; 

    samples[0][i] = f;
}

// DO JOB
for(auto it = inserts.begin(); it != inserts.end(); ++it)
{
    (*it)->ProcessAudio(samples, samples, bufferLenght);
}

// Copying the result back into the buffer
for(unsigned i = 0; i < bufferLenght; i++)
{
    float f = samples[0][i];
    int intval;
    f = f * std::numeric_limits<int>::max();
    if( f > std::numeric_limits<int>::max() ) f = std::numeric_limits<int>::max();
    if( f < std::numeric_limits<int>::min() ) f = std::numeric_limits<int>::min();
    intval = (int) f;

    buff[i] = intval;
}

其中"buff“定义为"__int32* buff”

EN

回答 1

Stack Overflow用户

发布于 2016-03-31 22:41:15

我猜当您调用f = std::numeric_limits<int>::max() (以及下面这行中的相关min()用例)时,这可能会导致溢出。你试过f = std::numeric_limits<int>::max() - 1吗?

上面使用f = ((float) buff[i]) / (float) std::numeric_limits<int>::max()的代码片段也是如此……我还会在那里减去一个,以避免稍后可能的溢出。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/33595333

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档