Gain

How to chain gain with consecutive adjustments for dynamic control

This example demonstrates how to chain multiple gain stages, applying adjustments to the level of an audio signal. Each Gain node modifies the amplitude of the flow before passing it to the next, allowing the construction of dynamic control systems or volume automation.

// Configure the graph for rendering
let context = Assembler(rendering: .live).createAudioGraph()

// Configure an audio source (Renderer) from a local audio file
let url = URL(fileURLWithPath: "/path/to/source.mp3")
let source = MediaRenderer(contentsOf: url)

// Configure gain stages to apply to the source
let gainStage1 = Gain(defaultGain: 1.0)
let gainStage2 = Gain(defaultGain: 0.5)

// Configure node connections
try context.connect(source: source, to: gainStage1)
try context.connect(source: gainStage1, to: gainStage2)
try context.connect(source: gainStage2, to: context.destination)
source.play(after: 0)

The following video shows the sample code in action and its real-time processing.

circle-info

For a complete description of each class, method, and property referenced in this section, refer to the official API documentationarrow-up-right, where you will find detailed references, available components, parameters, and additional usage code examples.

Last updated