Make a visual respond to audio¶
Start with an audio clip on the project timeline, measure it, then map its range to the property you want to move. This study uses an original generated bass-and-melody phrase. The preview is silent: its waveform and moving marker show when the input changes. The audio clips remain in the project for analysis.
The audio and signals concept explains why playback and analysis are separate. For a first soundtrack, see add audio.
Choose a measurement¶
Start a new audio_response_small.py with your own assets/music.wav,
containing at least three seconds of audio. The later Python blocks continue
this project:
The render calls below use overwrite=True; rerunning a script replaces its
named output files.
from vestra import Circle, Project
project = Project(
size=(640, 360),
fps=30,
duration=3,
)
project.audio.track("music").add("assets/music.wav", trim_end=3)
pulse = project.root.add(Circle(radius=70, fill="#9edbc7"))
energy = project.audio.signal.rms()
RMS describes overall level and suits a breathing or pulsing scale. Use
peak() for brief amplitude peaks, or band(80, 240) to respond to a bass
range instead of all frequencies. These read the master mix after audio
processing; they are not per-track taps.
Give the visual a safe range¶
Continue the same project:
scale_response = energy.remap(input=(0, 0.18), output=(0.75, 1.30))
bounded_scale = scale_response.clamp(0.75, 1.30)
smoothed_scale = bounded_scale.envelope(0.01, 0.20)
pulse.transform.scale.bind(smoothed_scale, operation="multiply")
The remap maps an RMS of zero to 0.75 and 0.18 to 1.30. Higher levels
would extrapolate beyond 1.30, so the clamp is deliberate. multiply keeps
the layer's authored scale as the baseline. If the authored scale is 2, the
resulting range is 1.50–2.60, before other modifiers.
0.18 fits the generated recording; it is not a universal normalization value.
For another recording, begin with a modest visual range and adjust the input
maximum until typical loud sections use most of it. If every beat reaches the
same maximum, raise the input maximum. If changes are barely visible, lower it.
Keep the clamp while tuning.
Compare attack and release¶
Attack controls movement toward a rising value; release controls movement toward a falling value. These are smoothing time constants in seconds, not a fixed delay or a promise to reach the target exactly at that time. A zero duration follows that direction immediately. “No envelope” still uses the analysis window of the RMS measurement.
For a sharp beat, try a short attack such as 0.01–0.03 s. A longer release
such as 0.15–0.30 s carries motion between beats. Increase attack to ignore
brief transients; increase release to retain a longer tail. Transforms run
in call order: this example bounds the target before smoothing it.
To finish the small project above:
This writes the silent audio-response.mp4 in the current directory and
replaces it on a rerun. Setting
output_audio = False retains the clips and prepared analysis; changing it to
True publishes the same visuals with sound. See the
signals reference for binding operations, frequency
constraints and other transforms.
Complete example¶
Download audio_response.py and run:
The program creates audio-pulses.wav with Python's standard-library wave
module and replaces audio-bounded.mp4, audio-smoothing.mp4 and
audio-spectra.mp4 in the current directory. The three four-second videos are
silent; the generated WAV is their analysis input. The spectrum study is
explained in draw an audio spectrum.
The curves come from actual prepared Vestra signal values sampled at 30 fps; the moving dots are bound directly to those signals. The input waveform comes from the WAV samples. This makes the plots useful when playback is paused as well as while it runs. See preview reproduction for contributor checks and regeneration.