2

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
            }

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 Vormbrock
  • 369
  • 2
  • 13

1 Answers1

3

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;

Before <audio>

After <audio>

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()

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

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!

punkbit
  • 7,347
  • 10
  • 55
  • 89
  • Thank you for your response! But connecting the elements into the right order was not my problem. Ok I didn't post it into my code as I was focused on the frequency modulation itself. My concrete issue: I try to perform frequency modulation on my input signal coming from the mp3. When I modulate it with a sine of - let's say - 10.000 Hz, I expect that my sound is modulated in a way that you can no longer hear low frequencies. Or in other words: when I perform a FFTPlot on such signal, I expect that there is a peak at 10.000 Hz with a certain bandwidth, coming from the mp3 signal. – Ulrich Vormbrock May 15 '20 at 06:27
  • @ulrich I spent time to answer the question you posted. You should have explained better if the answer you're looking is extended to some obscure issue that is not described. Do you understand? Open a new thread about it, as above you got the answer for the problem you posted. – punkbit May 15 '20 at 11:12
  • "doesn't work as expected" , "Has anybody an idea how to get the mp3 modulated?", Yes for both, my answer states. Also you complain about the documentation and you haven't documented your problem correctly. – punkbit May 15 '20 at 11:17
  • 1
    Thanks, lessons learned! Yes you're right, I should have better explained my statement "it doesn't work as expected" - ok good idea to open a new thread! Anyhow, everybody who wants to learn about how the modulate a mp3 get's a good tutorial (written by you) in this thread! – Ulrich Vormbrock May 15 '20 at 16:50
  • @UlrichVormbrock happy to help! Now, if the answer works for as state it is a good-citizen practice here on SO to vote it up and mark accepted to make it relevant for future users. Thanks! – punkbit May 15 '20 at 20:26
  • just did vote it up! As already mentioned, your answer is good and nearly a tutorial about how to wire up the different elements within the audio chain. Also for me your answer is useful as I'm sure now that my initial approach - working with AKOperationEffect - was correct. Thank you again! – Ulrich Vormbrock May 16 '20 at 06:16