【问题标题】:What do I need to know to start programming a synth in c++/ other language? [closed]我需要知道什么才能开始用 c++/其他语言编写合成器? [关闭]
【发布时间】:2017-06-04 22:41:11
【问题描述】:

我还是一个初学者,所以这对我来说将是一个非常高级的项目,但我想现在就开始它并让它成为一个持续的事情(我不打算在 2 周内完成一个成品,甚至也没有构建我将使用的东西,只是一种学习体验)。

我想为ableton live 9 构建一个用作vst3 插件的合成器。我在想第一步就是制作一些输出任意波形并响应midi 输入的东西(频率会随着时间的变化而变化)注意)和一个基本的 GUI 模板。然后我会尝试引入不同的波形,然后是更多的振荡器和失谐,然后是声音,然后是滤波器,然后是多个声音等。

我将如何开始这样做?我知道基本的 C++,但我不知道如何创建波形并将其作为声音输出。 c++ 是否适合用于此类应用程序?

【问题讨论】:

标签: c++ audio synthesizer


【解决方案1】:

虽然您的问题相当广泛,但我可以提供一些关于从哪里开始的见解。

首先,您需要一份来自 Steinberg 的 VST3 SDK 的副本

根据我的经验,开始使用 VST SDK 有相当长的学习曲线。因此,我建议找到一个在其之上提供包装器的库。例如JUCE

使用包装器将帮助您克服许多需要完成的样板编程以使 VST 工作。

至于波形生成、滤波和其他与 DSP 相关的概念......这里的主题太多了,我什至无法开始描述它。

查看musicdspThe STK 了解一些基本概念。

买一本关于这个主题的书。经典是The Audio Programming Book

此外,您还需要确保您对音频理论有很强的掌握。看看Introduction to Computer Music: Volume One

当然,Google 是你的朋友。

编辑:

更完整地回答您的问题。是的,C++(或 C)将是此类应用程序的首选语言(尽管不是唯一可能的选择)

在深入研究 VST 开发之前,我会考虑使用音频 API,这将使您能够在没有 VST 开发的麻烦的情况下锻炼自己的技能。

至于音频库,有两个很受欢迎的选项:

  • PortAudio(用 C 编写)

  • RtAudio(用 C++ 编写,但不是 c++11 或更高版本)

    或者您可以查看libzaudio,这是我目前正在开发的音频 API。 (目前它依赖于 PortAudio,但提供了更现代的 C++11 风格的音频处理方式)

假设您已安装 PortAudio 和 libzaudio,以下将生成一秒钟的 sin 440hz 波:

#include <iostream>
#include <cmath>
#include <libzaudio/zaudio.hpp>

int main(int argc, char** argv)
{
    try
    {
        //bring the needed zaudio components into scope
        using zaudio::no_error;
        using zaudio::sample;
        using zaudio::sample_format;
        using zaudio::stream_params;
        using zaudio::time_point;
        using zaudio::make_stream_context;
        using zaudio::make_stream_params;
        using zaudio::make_audio_stream;
        using zaudio::start_stream;
        using zaudio::stop_stream;
        using zaudio::thread_sleep;

        //create an alias for a 32 bit float sample
        using sample_type = sample<sample_format::f32>;

        //create a stream context with the default api (portaudio currently)
        auto&& context = make_stream_context<sample_type>();

        //create a stream params object
        auto&& params = make_stream_params<sample_type>(44100,512,0,2);

        //setup to generate a sine wave
        constexpr sample_type _2pi = M_PI * 2.0;
        float hz = 440.0;
        sample_type phs = 0;
        sample_type stp = hz / params.sample_rate() * _2pi;

        //create a zaudio::stream_callback compliant lambda that generates a sine wave
        auto&& callback = [&](const sample_type* input,
                              sample_type* output,
                              time_point stream_time,
                              stream_params<sample_type>& params) noexcept
        {
            for(std::size_t i = 0; i < params.frame_count(); ++i)
            {
                for(std::size_t j = 0; j < params.output_frame_width(); ++j)
                {
                    *(output++) = std::sin(phs);
                }
                phs += stp;
                if(phs > _2pi)
                {
                    phs -= _2pi;
                }
            }
            return no_error;
        };

        //create an audio stream using the params, context, and callback created above. Uses the default error callback
        auto&& stream = make_audio_stream<sample_type>(params,context,callback);

        //start the stream
        start_stream(stream);
        //run for 1 second
        thread_sleep(std::chrono::seconds(1));
        //stop the stream
        stop_stream(stream);
    }
    catch (std::exception& e)
    {
        std::cout<<e.what()<<std::endl;
    }
    return 0;
}

(Pulled from one of my example files)

如果您想在其他地方与我联系,我很乐意为您提供更详细的说明。 (Stack Overflow 上不允许就该主题进行长时间讨论)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-07-23
    • 2015-04-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-08-03
    相关资源
    最近更新 更多