I'm writing an audio app in Swift - and one task consists in frequency modulation of a given signal (microphone input or sound file) - thus I'm iterating through all frames of a buffer like
let leftSourceData = readBuffer.floatChannelData?[0]
let rightSourceData = readBuffer.floatChannelData?[1]
let leftTargetData = renderBuffer.floatChannelData?[0]
let rightTargetData = renderBuffer.floatChannelData?[1]
for i in 0..<Int(readBuffer.frameLength) {
// TODO: perform frequency modulation on source data, writing the results to target data
leftTargetData[i] = ???
rightTargetData[i] = ???
}
The equation for Frequency Modulation is clear - you'll find in the Internet s.th. like that:
f(t) = A sin(2*pi*fc*t + I * sin(2*pi*fm*t))
with fc = carrier frequency, fm = modulation frequency
and I = modulation index.
The above-mentioned equation only covers a sinusoidal input signal - anyhow, I want to modulate an arbitrary signal - thus s.th. different than a pure sine wave.
For amplitude modulation, I've used the following algorithm (within the loop) which works well for Swift and iOS:
let val: Double = sin(Double(2 * modulationFrequency) * Double(index) * Double.pi / Double(renderBuffer.format.sampleRate))
leftTargetData?[i] = Float(val) * (leftSourceData?[i] ?? 0)
rightTargetData?[i] = Float(val) * (rightSourceData?[i] ?? 0)
How can I do the same for FM?