在 Linux 和/或 Python 中生成吉他和弦的最简单方法

发布于 2024-11-19 19:57:35 字数 503 浏览 9 评论 0原文

我想要实现的是从我的 python 应用程序中弹奏吉他和弦。如果需要,我知道(或可以计算)和弦中的频率。

我在想,即使我做了以正确频率产生多个正弦波的低级工作,它听起来也不会正确,因为包络也需要正确,否则它听起来不会像吉他,而更像是嗡嗡声。

令人兴奋的是,linux sox 命令 play 可以产生一个非常令人信服的单独音符:

play -n Synth 0 pluck E3

所以我真正要问的是,

a)是否可以将 play 命令硬塞到做一个完整的和弦(理想情况下,开始时间略有不同,以模拟拨片弦的行程)——我无法做到这一点,但也许有一些 bash Fairydust 会分叉一个过程或类似的东西,所以听起来不错。如果可能的话,我会满足于从我的代码中调用 bash 命令(我不喜欢重新发明轮子)。

b)(更好)Python中有没有办法实现这个(吉他和弦声音)?我见过一些可访问的 python midi 库,但坦率地说,据我所知,midi 不太适合我想要的声音。

What i'm trying to achieve is playing a guitar chord from my python application. I know (or can calculate) the frequencies in the chord if needed.

I'm thinking that even if I do the low level leg work of producing multiple sine waves at the right frequencies it wont sound right due to the envelope needing to be correct also, else it wont sound like a guitar but more of a hum.

Tantilisingly, the linux sox command play can produce a pretty convincing individual note with:

play -n synth 0 pluck E3

So really what i'm asking is,

a) is it possible to shoehorn the play command to do a whole chord (ideally with slightly differing start times to simulate the plectrum string stroke) -- i've not been able to do this but maybe theres some bash fairydust that'll fork a process or such so it sounds right. If this is possible i'd settle for just calling out to a bash command from my code (I dont like reinventing the wheel).

b) (even better) is there a way in python of achieving this (a guitar chord sound) ? I've seen a few accessable python midi librarys but frankly midi isn't a good fit for the sound I want, as far as i can tell.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

花落人断肠 2024-11-26 19:57:35

手册给出了这个例子:

play -n synth pl G2 pl B2 pl D3 pl G3 pl D4 pl G4 \
               delay 0 .05 .1 .15 .2 .25 remix - fade 0 4 .1 norm -1

这创建了 6 个同步合成器实例(作为单独的音频通道),通过稍微增加时间来延迟其中 5 个通道,然后将它们混合为单个通道。

结果是一个非常令人信服的吉他和弦;当然,您可以轻松更改音符或延迟。您还可以尝试“吉他”的延音和音调,或添加过载效果 - 有关详细信息,请参阅手册。

The manual gives this example:

play -n synth pl G2 pl B2 pl D3 pl G3 pl D4 pl G4 \
               delay 0 .05 .1 .15 .2 .25 remix - fade 0 4 .1 norm -1

This creates 6 simultaneous instances of synth (as separate audio channels), delays 5 of the channels by slightly increasing times, then mixes them down to a single channel.

The result is a pretty convincing guitar chord; you can of course change the notes or the delays very easily. You can also play around with the sustain and tone of the 'guitar', or add an overdrive effect—see the manual for details.

往日情怀 2024-11-26 19:57:35

a) 黑客方法是生成一个后台子进程来运行每个 play 命令。由于后台子进程不会让 shell 等待其完成,因此您可以同时运行多个 play。像这样的东西会起作用:

for p in "C3" "E3" "G3"; do ( play -n synth 3 pluck $p & ); done

我看到 ninjagecko 发布的内容与我正在写的内容基本相同。

b) 认识 MIDI 数据的关键点是它更像是产生声音的高级配方,而不是声音本身。换句话说,每个 MIDI 音符都表示为音高、动态电平、开始和停止时间以及各种其他元数据。实际的声音是由合成器产生的,不同的合成器以不同的质量水平完成工作。如果您不喜欢从 MIDI 文件中获得的声音,这不是 MIDI 的问题,而是合成器的问题,因此您只需要找到一个更好的即可。 (实际上,这通常需要花费 $$$;大多数免费或便宜的合成器都非常糟糕。)

另一种选择是实际挖掘引擎盖下的内容,可以这么说,并实现一种算法来创建您自己的吉他声音。为此,您需要研究数字信号处理,特别是诸如Karplus-Strong 算法(创建合成拨弦的多种方法之一 声音)。这是一个令人着迷的主题,但如果您对声音合成的唯一接触是在播放和创建 MIDI 文件的级别,那么您需要学习一些东西。此外,Python 可能不是最佳语言选择,因为执行速度非常关键。

如果您对 DSP 感到好奇,您可能需要下载并使用 ChucK

a) The hackish way is to spawn a background subprocess to run each play command. Since a background subprocess doesn't make the shell wait for it to finish, you can have multiple plays running at once. Something like this would work:

for p in "C3" "E3" "G3"; do ( play -n synth 3 pluck $p & ); done

I see that ninjagecko posted basically the same thing as I'm writing this.

b) The key point to realize about MIDI data is that it's more like a high-level recipe for producing a sound, not the sound itself. In other words, each MIDI note is expressed as a pitch, a dynamic level, start and stop times, and assorted other metadata. The actual sound is produced by a synthesizer, and different synthesizers do the job with different levels of quality. If you don't like the sound you're getting from your MIDI files, it's not a problem with MIDI, it's a problem with your synthesizer, so you just need to find a better one. (In practice, that usually takes $$$; most free or cheap synthesizers are pretty bad.)

An alternative would be to actually dig under the hood, so to speak, and implement an algorithm to create your own guitar sound. For that you'd want to look into digital signal processing, in particular something like the Karplus-Strong algorithm (one of many ways to create a synthetic plucked string sound). It's a fascinating subject, but if your only exposure to sound synthesis is at the level of play and creating MIDI files, you'd have a bit of learning to do. Additionally, Python probably isn't the best choice of language, since execution speed is pretty critical.

If you're curious about DSP, you might want to download and play with ChucK.

慵挽 2024-11-26 19:57:35

a)是否可以硬塞播放命令来执行整个和弦...?

如果您的声音架构支持它,您可以运行多个同时输出音频的命令。如果您使用 ALSA,则需要在 ~/.asoundrc 中使用 dmix 或其他变体。使用 subprocess.Popen 生成许多子进程。如果这是假设的 bash 脚本,您可以这样做:

command1 &
command2 &
...

b)(甚至更好)Python 中是否有办法实现此目的(吉他和弦声音)?

编译为 MIDI 并通过软件合成器输出就像 FluidSynth 一样。

a) is it possible to shoehorn the play command to do a whole chord... ?

If your sound architecture supports it, you can run multiple commands that output audio at the same time. If you're using ALSA, you need dmix or other variants in your ~/.asoundrc. Use subprocess.Popen to spawn many child processes. If this were hypothetically a bash script, you could do:

command1 &
command2 &
...

b) (even better) is there a way in python of achieving this (a guitar chord sound)?

Compile to MIDI and output via a software synthesizer like FluidSynth.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文