学习在 C++ 中处理音频

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/711350/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-27 16:53:16  来源:igfitidea点击:

Learning to work with audio in C++

c++audio

提问by Skilldrick

My degree was in audio engineering, but I'm fairly new to programming. I'd like to learn how to work with audio in a programming environment, partly so I can learn C++ better through interesting projects.

我的学位是音频工程,但我对编程还很陌生。我想学习如何在编程环境中使用音频,部分原因是我可以通过有趣的项目更好地学习 C++。

First off, is C++ the right language for this? Is there any reason I shouldn't be using it? I've heard of Soundfile and some other libraries - what would you recommend?

首先,C++ 是适合这个的语言吗?有什么理由我不应该使用它吗?我听说过 Soundfile 和其他一些库 - 你会推荐什么?

Finally, does anyone know of any good tutorials in this subject? I've learnt the basics of DSP - I just want to program it!

最后,有没有人知道这方面的任何好的教程?我已经学习了 DSP 的基础知识 - 我只想对它进行编程!

EDIT: I use Windows. I'd like to play about with real-time stuff, a bit like Max/MSP but with more control.

编辑:我使用 Windows。我想玩实时的东西,有点像 Max/MSP,但有更多的控制。

采纳答案by MissT

It really depends on what kind of audio work you want to do, If you want to implement audio for a game, C++ is sure the right language. There are many libraries around, OpenAL is great, free and multiplatform. I also used DirectSound and Fmod with great sucess. Check them out, it all depends on your needs.

这真的取决于你想做什么样的音频工作,如果你想为游戏实现音频,C++ 肯定是正确的语言。周围有很多库,OpenAL 很棒,免费且多平台。我还成功地使用了 DirectSound 和 Fmod。检查它们,这一切都取决于您的需求。

回答by Emile Vrijdags

If you do decide to use C++, then The Synthesis Toolkitis worth noting. I use it for a schoolproject and it is very usable, developed at stanford university, crossplatform (win, mac and linux), free and opensource. An extract from the wikipedia pageon it:

如果您决定使用 C++,那么Synthesis Toolkit值得一提。我将它用于学校项目,它非常有用,在斯坦福大学开发,跨平台(win、mac 和 linux),免费和开源。维基百科页面的摘录:

Versions of the STK instrument classes have been integrated into ChucK, Csound, Real-Time Cmix, Max/MSP (as part of PeRColate) and SuperCollider

STK 乐器类的版本已集成到 ChucK、Csound、Real-Time Cmix、Max/MSP(作为 PerColate 的一部分)和 SuperCollider 中

They have a lot of testcode included + on the site are some tutorials to get started with their library. (But you do have to know some C++ of course)

他们有很多测试代码,网站上有一些教程可以开始使用他们的库。(但你当然必须知道一些 C++)

STK has some classed to read/write audio files (and matlab files), realtime audio/midi io, some basic algorithms (usage based on similar functions out of matlab), ...

STK 有一些分类来读/写音频文件(和 matlab 文件),实时音频/midi io,一些基本算法(基于 matlab 的类似函数的用法),......



Another obviousoption and probably THE most used environment to learn/test dsp stuff is Matlab(or free octave, but i havent used it before). I sometimes test stuff out in Matlab before implementing it in C++.

另一个明显的选择,可能是学习/测试 dsp 东西最常用的环境是Matlab(或免费的八度音程,但我以前没有使用过)。在用 C++ 实现之前,我有时会先在 Matlab 中测试一下。



EDIT: In the past year I've been using JUCEwhich is a pretty cool C++ library centered around C++ audio programming, though its more leaning towards something like QT. One of their developers (Timur Doumler) has a nice talk about real time audio with C++ and JUCE on CppCon 2015, definitely worth checking out, even if you don't end up using JUCE: https://youtu.be/boPEO2auJj4

编辑:在过去的一年里,我一直在使用JUCE,它是一个非常酷的 C++ 库,以 C++ 音频编程为中心,尽管它更倾向于 QT 之类的东西。他们的一位开发人员 (Timur Doumler) 在 CppCon 2015 上很好地谈论了使用 C++ 和 JUCE 的实时音频,绝对值得一看,即使您最终没有使用 JUCE:https://youtu.be/boPEO2auJj4

回答by jalf

Is C++ the right language? That depends entirely on the context. If you need to plug your code into another application, framework or whatever else, you probably have to use whatever language that framework is made for.

C++ 是正确的语言吗?这完全取决于上下文。如果您需要将您的代码插入到另一个应用程序、框架或其他任何东西中,您可能必须使用该框架所针对的任何语言。

If you're making your own free-standing app? Probably not. There are several reasons why I'd suggest you go with another language:

如果您正在制作自己的独立应用程序?可能不是。我建议您使用另一种语言有几个原因:

  • C++ isn't well suited for beginners. If your goal is to learn programming through these projects, go with a language that is more amenable to "learning by doing". C++ is full of pitfalls and things that "seem to work", until one day they don't. Add to this a horribly complex syntax and some pretty complex idioms and techniques you have to master to work around quirks of the language that simply are non-issues in pretty much any other language, and you have a good source of bugs and frustrations, and not much more.
  • C++ has no concept of "audio". C++ doesn't come with any kind of support for recording, processing or playing audio, loading or saving audio files or anything else. Most modern languages have some form of audio support built into the standard language (at least the ability to load and play mp3 files and other such basics). In C++, all of this will have to be done through third-party libraries and OS-specific code.
  • C++ 不太适合初学者。如果您的目标是通过这些项目学习编程,请使用更适合“边做边学”的语言。C++ 充满了陷阱和“似乎有效”的东西,直到有一天它们不起作用。再加上一个极其复杂的语法和一些非常复杂的习语和技术,你必须掌握一些非常复杂的习语和技术才能解决该语言的怪癖,而这些怪癖在几乎任何其他语言中都没有问题,并且你有一个很好的错误和挫折来源,并且不多了。
  • C++ 没有“音频”的概念。C++ 不支持录制、处理或播放音频、加载或保存音频文件或其他任何内容。大多数现代语言都在标准语言中内置了某种形式的音频支持(至少能够加载和播放 mp3 文件和其他此类基础知识)。在 C++ 中,所有这些都必须通过第三方库和特定于操作系统的代码来完成。

If you have a real choice in language, I'd say go for something like Python or C#. They're easier to learn, come with a lot more functionality out of the box, and make it a lot harder to shoot yourself in the foot. I wouldn't recommend C++ to someone who isn't already an experienced programmer.

如果您在语言方面有真正的选择,我会说选择 Python 或 C# 之类的语言。它们更容易学习,具有更多开箱即用的功能,并且使您更难在脚下射击。我不会向还不是经验丰富的程序员的人推荐 C++。

回答by Chris Lutz

Check out Audacity. It is a cross-platform (Linux, OS X, and Windows) open-source audio editor written in C++. I don't know what level your GUI skills are at, but the code that deals with audio work should be fairly easy to distinguish from the wxWidgets (GUI) code.

检查无畏。它是一个用 C++ 编写的跨平台(Linux、OS X 和 Windows)开源音频编辑器。我不知道您的 GUI 技能处于什么水平,但是处理音频工作的代码应该很容易与 wxWidgets (GUI) 代码区分开来。

回答by Assembler

Dont listen to me! I might be wrong!

不要听我的!我可能错了!

I do not program C++, but agree with Jalf in it being difficult for beginners, there are lots of things that have to be done manually that other languages will do for you automatically, such as memory allocation, garbage collection etc. they are also right in saying that C++ does not know audio, and that other languages have audio capabilities built in from scratch. However, generally, most of these are fairly basic capabilties: opening and playing files, sending the playhead to different parts of the file, adjust panning and volume, maybe extracting eq data.

我不会编程 C++,但同意 Jalf 的观点,它对初学者来说很困难,有很多事情必须手动完成,其他语言会自动为您完成,例如内存分配、垃圾收集等。他们也是对的说 C++ 不知道音频,而其他语言从头开始内置音频功能。然而,一般来说,其中大部分都是相当基本的功能:打开和播放文件,将播放头发送到文件的不同部分,调整声像和音量,可能提取 eq 数据。

But...Im guessing that you want to get a little deeper and actually shape the sound, whether through synthesis or effects, and that you want to make these things instead of just add them. I would encourage you to look at VST and VSTi, but this is c++ area (JUCE library, awesome awesome awesome), and hard hard work, but if you want to make your own product, then it might be the way. otherwise, as others have suggested CSound, Max, Cycling, and maybe Processing might be of interest. software like Reaktor allow you to patch together modular componenets with a lot less (if any) coding.

但是......我猜你想要更深入一点并实际塑造声音,无论是通过合成还是效果,并且你想要制作这些东西而不是仅仅添加它们。我鼓励你看看 VST 和 VSTi,但这是 C++ 领域(JUCE 库,真棒真棒),努力工作,但如果你想制作自己的产品,那么它可能是方法。否则,正如其他人所建议的那样,CSound、Max、Cycling 和 Processing 可能会引起人们的兴趣。像 Reaktor 这样的软件允许你用更少的(如果有的话)编码将模块化组件拼凑在一起。

go and have a look at some of the forums on KVRAudio, there are a few heroes there who developed old classic freeware like Hammerhead, and then other indie software like Fruity Loops, Rebirth, Tuareg, Audiomulch etc.

去看看KVRAudio上的一些论坛,那里有一些英雄开发了像Hammerhead这样的老经典免费软件,然后是其他独立软件,如Fruity Loops、Rebirth、Tuareg、Audiomulch等。

I think learning about VST and C++ is the best, but it will be a long road with pain and anguish, and unless you wish to create a stand alone piece of software or a plugin for another piece of software (apart from Csound etc), it will be dissapointing.

我认为学习 VST 和 C++ 是最好的,但这将是一条充满痛苦和痛苦的漫长道路,除非您希望创建一个独立的软件或另一个软件的插件(除了 Csound 等),这将是令人失望的。

I think.

我认为。

回答by sness

C++ can be a great language to do audio processing in, I use the Marsyasframework, a cross-platform package that lets you do complicated tasks in audio signal processing, music information retrieval and machine learning really easily. It's also really fast, and you can do lots of complicated tasks in real time, unlike many other packages. In tests we've run, Marsyas can be hundreds of times faster than Matlab.

C++ 是一种很好的音频处理语言,我使用Marsyas框架,这是一个跨平台的包,可以让你在音频信号处理、音乐信息检索和机器学习方面轻松完成复杂的任务。它也非常快,与许多其他软件包不同,您可以实时完成许多复杂的任务。在我们运行的测试中,Marsyas 可以比 Matlab 快数百倍。

To get you started, here's some code. Let's say you wanted to play an audio file, for this, you'd create a network in Marsyas that looks like:

为了让您开始,这里有一些代码。假设您想播放一个音频文件,为此,您将在 Marsyas 中创建一个如下所示的网络:

  • Series
    • SoundFileSource
    • Gain
    • AudioSink
  • 系列
    • 声音文件源
    • 获得
    • 音频接收器

Marsyas uses an implict patching model, which is kind of like the explicit patching that you see in Max/MSP, but you don't have to draw wires between objects, you just put them in containers, and the wires are created automatically. The Series object above holds a SoundFileSource, which you feed an audio file, then passes it to a Gain, which allows you to change the volume of the audio, and then feeds it into an AudioSink, which plays the audio file on your speakers.

Marsyas 使用隐式修补模型,这有点像您在 Max/MSP 中看到的显式修补,但您不必在对象之间绘制连线,只需将它们放入容器中,连线会自动创建。上面的 Series 对象包含一个 SoundFileSource,您可以将它提供给一个音频文件,然后将其传递给一个 Gain,它允许您更改音频的音量,然后将其提供给一个 AudioSink,后者在您的扬声器上播放音频文件。

The C++ to create this network is dead simple:

创建这个网络的 C++ 非常简单:

     MarSystem* playbacknet = mng.create("Series", "playbacknet");
     playbacknet->addMarSystem(mng.create("SoundFileSource", "src"));
     playbacknet->addMarSystem(mng.create("Gain", "gt"));
     playbacknet->addMarSystem(mng.create("AudioSink", "dest"));

     // Set the SoundFileName
     playbacknet->updctrl("SoundFileSource/src/mrs_string/filename",inAudioFileName);

     // Turn on the audio output
     playbacknet->updctrl("AudioSink/dest/mrs_bool/initAudio", true);

     while (playbacknet->getctrl("SoundFileSource/src/mrs_bool/notEmpty")->isTrue())    {
        playbacknet->tick();
     }

The first four lines create the MarSystem that processes your audio. The Series object, which contains everything else, is created first, and then the SoundFileSource, Gain and AudioSink are added to it.

前四行创建了处理音频的 MarSystem。首先创建包含其他所有内容的 Series 对象,然后向其中添加 SoundFileSource、Gain 和 AudioSink。

The next line sets the filename of the SoundFileSource, you would give it the name of the .wav, .au, .aiff or .mp3 file that you wanted to process here.

下一行设置 SoundFileSource 的文件名,您可以在此处为其指定要处理的 .wav、.au、.aiff 或 .mp3 文件的名称。

You then turn on the AudioSource, which tells your sound card to start playing sound by updating the "initAudio" control of the AudioSource. The while loop then loops as long as the SoundFileSource has audio data.

然后打开 AudioSource,它告诉您的声卡通过更新 AudioSource 的“initAudio”控件开始播放声音。只要 SoundFileSource 有音频数据,while 循环就会循环。

You can see that this simple example is really easy to do, but the real power of Marsyas is when you want to do more complicated tasks. For example, to do an FFT, you would just do:

你可以看到这个简单的例子真的很容易做,但 Marsyas 的真正力量是当你想要做更复杂的任务时。例如,要进行 FFT,您只需执行以下操作:

     MarSystem* net = mng.create("Series", "net");
     net->addMarSystem(mng.create("SoundFileSource", "src"));
     net->addMarSystem(mng.create("Spectrum", "spectrum"));
     net->addMarSystem(mng.create("PowerSpectrum", "powerspectrum"));

     while (playbacknet->getctrl("SoundFileSource/src/mrs_bool/notEmpty")->isTrue())    {
         cout << net->getctrl("mrs_realvec/processedData")->to<mrs_realvec>() << endl;
     }

To estimate the fundamental frequency of an audio source you would do something like:

要估计音频源的基频,您可以执行以下操作:

  MarSystem* net = mng.create("Series", "series");
  net->addMarSystem(mng.create("AudioSource", "src"));
  net->addMarSystem(mng.create("AubioYin", "yin"));

    while (playbacknet->getctrl("SoundFileSource/src/mrs_bool/notEmpty")->isTrue())    {
         cout << net->getctrl("mrs_realvec/processedData")->to<mrs_realvec>() << endl;
     }

This last one will read in audio in realtime from your soundcard and will run the powerful YIN pitch detection algorithm on it. Sweet, eh?

最后一个将从您的声卡实时读取音频,并将在其上运行强大的 YIN 音高检测算法。甜,嗯?

For lots more code, check out the Marsyas website, especially check out the documentation.

如需更多代码,请查看Marsyas 网站,尤其是查看文档

回答by Paul R

C might be a better choice than C++ for audio, particularly if you want to do any DSP.

对于音频,C 可能是比 C++ 更好的选择,特别是如果您想做任何 DSP。

  • Most practical audio and DSP reference books use C for code examples
  • Most audio/DSP libraries are written in C (or at least have a C API)
  • C++ greatly increases the learning curve compared to C but doesn't add a lot of benefit for audio/DSP work (OOP is not particularly relevant for low-level/real-time code)
  • 大多数实用的音频和 DSP 参考书都使用 C 作为代码示例
  • 大多数音频/DSP 库都是用 C 编写的(或者至少有一个 C API)
  • 与 C 相比,C++ 大大增加了学习曲线,但并没有为音频/DSP 工作增加很多好处(OOP 与低级/实时代码不是特别相关)

There's nothing "wrong" with learning C++ of course and it won't stop you understanding and using C code (since C++ is mostly a superset of C) - it's just that you'll be up and running a lot quicker with C, and you can always move on to C++ later if you decide to.

当然,学习 C++ 没有什么“错误”,它不会阻止你理解和使用 C 代码(因为 C++ 主要是 C 的超集)——只是你会用 C 更快地启动和运行,并且如果您决定,您可以随时转向 C++。

回答by basteln

If you already have some DSP knowledge, these tutorialsshow how to turn DSP code into a finished program, including GUI controls. It starts with a simple distortion and then moves on to synthesis etc.
Basically you write code that can be built either as a standalone .exe or as a VST plugin for hosts like Cubase or Ableton Live.

如果您已经具备一些 DSP 知识,这些教程将展示如何将 DSP 代码转换为完整的程序,包括 GUI 控件。它从简单的失真开始,然后进行合成等。
基本上,您编写的代码可以构建为独立的 .exe 或作为 Cubase 或 Ableton Live 等主机的 VST 插件。

回答by ThePhantom05

To answer your second question, you should check out this book: Designing Audio Effect Plug-Ins in C++: With Digital Audio Signal Processing Theory by Will Pirkle.

要回答你的第二个问题,你应该看看这本书:Will Pirkle 的《Designing Audio Effect Plug-Ins in C++:With Digital Audio Signal Processing Theory》。

The book goes through some basic DSP and walks you through how to make audio plugins. You'll code in C++ but it won't be hard because the book suggest you download a framework, RACKAFX, which handles all GUI events. All you'll have to do is focus on writing the formulas and storing the data in the right channels.

这本书介绍了一些基本的 DSP,并带您了解如何制作音频插件。您将使用 C++ 进行编码,但这并不难,因为本书建议您下载一个框架 RACKAFX,它可以处理所有 GUI 事件。您所要做的就是专注于编写公式并将数据存储在正确的渠道中。

Good luck!

祝你好运!

回答by Iain Duncan

Lots of good suggestions in here. In addition I'd suggest looking at the Hyman Audio Toolkit, the JUCE framework, and learn how to port the C code from synthesis libraries like Csound. The "Audio Programming Book" is good too for that.

这里有很多好的建议。此外,我建议查看 Hyman Audio Toolkit、JUCE 框架,并学习如何从 Csound 等合成库中移植 C 代码。“音频编程书”对此也有好处。

Re C vs C++, I think you'll find that in audio work, the C++ part is often really just chrome for code management. The DSP bits are all low level procedural math on signals, so it's likely to be C calls and it really doesn't matter for that side of things whether you use C or C++. A lot of C++ audio projects are essentially just C in the audio guts.

Re C vs C++,我想你会发现在音频工作中,C++ 部分通常实际上只是用于代码管理的 chrome。DSP 位都是信号的低级程序数学,因此它很可能是 C 调用,无论您使用 C 还是 C++,这方面的事情都无关紧要。许多 C++ 音频项目本质上只是音频内容中的 C。

hth!

嗯!