Audio
This guide will walk you through the steps of splitting an audio file into multiple clips, offsetting the start time of each clip, and trimming them.
Manual Approach
Setup
First, let's fetch an MP3 file of a piano recording and create an audio clip from it:
Hint: The MP3 file contains 16 seconds of audio.
Splitting the AudioClip
Splitting a clip creates a new copy of the clip that starts 1 millisecond after the specified split time (in frames). The original clip is shortened to end at the split time.
In this example, 240
represents the midpoint of the audio clip.
Manipulating the Clips
We can then manipulate each clip individually:
This code trims the first clip from frame 15 to frame 80 and offsets it by -15 frames, moving it to the start of the composition.
For the second clip:
- It is trimmed from frame 420 (14 seconds) to the end of the clip (16 seconds).
- It is offset by
-420
frames to align it with the start of the composition. Since the first clip (clip0
) already occupies the start position, we adjust the offset by adding the stop frame of the first clip. - The volume is reduced to 50% for demonstration purposes.
Automated Approach
To achieve the same result more efficiently, we can use a stacked track:
In this approach:
- A new track is created and converted to a stacked track using
.stacked()
. - An audio clip is appended to the track using the loaded audio source.
Manipulating the Clips
Using the track reference, we can manipulate the clips without needing to manage individual clip references:
With the stacked track approach, the offsets are automatically handled, making it easier to manage clip positions within the composition.
Captions
The AudioClip
(and VideoClip
) also makes it easy to add captions to your composition. Let's see this in action:
First we're adding a transcript to the AudioClip
we created above. Then we're calling the asynchronous method addCaptions
on the clip.
This only works if your clip has been added to the composition (example).