AudioKit:如何执行AKPlayer的频率调制 [英] AudioKit: how to perform frequency modulation for AKPlayer

查看:61
本文介绍了AudioKit:如何执行AKPlayer的频率调制的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试对来自AKPlayer的信号执行频率调制,该信号反过来会播放mp3文件.我尝试使用AKOperationEffect,但无法按预期工作:

I'm trying to perform frequency modulation on a signal coming from AKPlayer, which in return plays a mp3 file. I've tried to work with AKOperationEffect, but it doesn't work as expected:

let modulatedPlayer = AKOperationEffect(player) { player, _ in

                let oscillator = AKOperation.fmOscillator(baseFrequency: modulationFrequency,
                                                          carrierMultiplier: player.toMono(),
                               modulatingMultiplier: 100,
                               modulationIndex: 0,
                               amplitude: 1)
              return oscillator
            }

有人知道如何调制mp3吗?不幸的是,AudioKit API的文档不够完善....有很多示例,但是它们都处理诸如正弦波,方波等合成声音.

Has anybody an idea how to get the mp3 modulated? Unfortunately, the AudioKit API is not so well documented .... there are a tons of examples, but they all deal with synthetic sounds such as sine, square waves etc.

推荐答案

我花了一些时间来创建一个实用的实际示例来帮助您@Ulrich,如果您有游乐场可用,则可以拖放游戏,或者只是使用它作为信任我的参考,它可以修改您的代码,这是不言而喻的,但是您可以阅读更多有关为什么我的版本在TLDR代码之后可以正常工作的信息;

I took the time to create a working practical example to help you @Ulrich, you can drop and play if you have the playground environment available, or just use it as a reference trusting me it works to amend your code, it's self-explanatory but you can read more about why my version work after the code TLDR;

在< 音频>

在< 音频>

在编写本文时,以下版本已在最新的XCode和Swift(XCode 11.4,Swift 5.2和AudioKit 4.9.5)中进行了测试,并且没有问题:

The following was tested and ran without problems in the latest XCode and Swift at the time of writing (XCode 11.4, Swift 5.2 and AudioKit 4.9.5):

import AudioKitPlaygrounds
import AudioKit

let audiofile = try! AKAudioFile(readFileName: "demo.mp3")
let player = AKPlayer(audioFile: audiofile)
let generator = AKOperationEffect(player) { player, _ in
    let oscillator = AKOperation.fmOscillator(baseFrequency: 400,
    carrierMultiplier: player.toMono(),
    modulatingMultiplier: 100,
    modulationIndex: 0,
    amplitude: 1)
    return oscillator
}

AudioKit.output = generator
try! AudioKit.start()

player.play()
generator.start()

在下载"页面中找到准备使用的游乐场( https://audiokit.io/downloads/)

Find the playground ready to use in the Download page ( https://audiokit.io/downloads/ )

如您所见,除了在初始化新的 AKAudioFile 并将其传递给 AKPlayer 实例时声明mp3文件的路径外,还需要三个步骤以一定顺序发生:

As you can see, apart from declaring a path to the mp3 file when initializing a new AKAudioFile and passing to an AKPlayer instance, there are three steps that you need to occur in a certain order:

1) Assign an `AKNode` to the AudioKit output
2) Start the AudioKit engine
3) Start the `player` to generate output
4) Start the generator to moderate your sound

了解为什么这样做的最好方法是忘记一点代码并想象在现实世界中打补丁;最后,尝试想象音频流.

The best way to understand why this is to forget about code for a bit and imagine patching things in the real world; and finally, try to imagine the audio flow.

希望这对您和未来的读者有帮助!

Hope this helps you and future readers!

这篇关于AudioKit:如何执行AKPlayer的频率调制的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆