Skip to main content

Overview

The AudioClip class extends the base Clip class to provide audio-specific functionality. It manages audio clip data, handles waveform visualization, and provides methods for clip manipulation such as splitting and resizing.

Constructor

AudioClip(AudioTrack audioTrack, AudioClipData clip, long startingTick = 0)

Creates a new audio clip on the specified track.
audioTrack
AudioTrack
required
The audio track this clip belongs to
clip
AudioClipData
required
The audio clip data containing the audio file reader and wave channel
startingTick
long
default:"0"
The starting position in ticks on the timeline
var clipData = new AudioClipData("drums.wav");
var audioClip = new AudioClip(audioTrack, clipData, startingTick: 1920);

Properties

Clip
AudioClipData
The underlying audio clip data containing the audio file reader and wave format information.
var duration = audioClip.Clip.AudioFileReader.TotalTime;
Console.WriteLine($"Duration: {duration.TotalSeconds} seconds");
WaveFormImage
IntPtr
Pointer to the waveform image texture for rendering.
if (audioClip.WaveFormImage != IntPtr.Zero)
{
    // Render waveform image
}
Name
string
The name of the clip (derived from the audio file name without extension). Inherited from base Clip class.
Track
Track
The track this clip belongs to. Inherited from base Clip class.
Enabled
bool
Whether the clip is enabled (active). When disabled, the clip is muted. Inherited from base Clip class.
audioClip.Enabled = false; // Mute the clip
Color
Vector4
The color of the clip as RGBA values. Inherited from base Clip class.
audioClip.Color = new Vector4(0.8f, 0.4f, 0.1f, 1.0f);
ClipWidth
float
The width of the clip in pixels. Inherited from base Clip class.
StartTick
long
The starting position of the clip on the timeline in ticks. Inherited from base Clip class.
long position = audioClip.StartTick;
EndTick
long
The ending position of the clip on the timeline in ticks. Inherited from base Clip class.
DurationTicks
long
The duration of the clip in ticks. Inherited from base Clip class.
StartMusicalTime
MusicalTime
The starting position in musical time (Bars:Beats:Ticks). Inherited from base Clip class.
EndMusicalTime
MusicalTime
The ending position in musical time (Bars:Beats:Ticks). Inherited from base Clip class.
DurationMusicalTime
MusicalTime
The duration in musical time (Bars:Beats:Ticks). Inherited from base Clip class.
StartMarker
long
The cutted portion in ticks from the start of the clip (trim start). Inherited from base Clip class.
EndMarker
long
The cutted portion in ticks from the end of the clip (trim end). Inherited from base Clip class.
ClipIsHovered
bool
Whether the clip is currently being hovered by the mouse. Inherited from base Clip class.
MenuBarIsHovered
bool
Whether the clip’s menu bar is currently being hovered. Inherited from base Clip class.
HasPlayed
bool
Whether the clip has been played during the current playback session. Inherited from base Clip class.
DeleteRequested
bool
Flag indicating the clip should be deleted. Inherited from base Clip class.
DuplicateRequested
bool
Flag indicating the clip should be duplicated. Inherited from base Clip class.
WantsToMove
bool
Flag used to move the clip between tracks. Inherited from base Clip class.
Id
string
Unique identifier (GUID) for the clip. Inherited from base Clip class.

Methods

ResizeWaveformData()

Resizes the waveform data to match the current clip width. This should be called when the clip is resized or the zoom level changes.
// After zoom level changes
audioClip.ResizeWaveformData();

Split(float time)

Splits the audio clip at the specified time position. This method is currently not implemented (commented out in the source).
time
float
The time position in seconds where the clip should be split
// Split clip at 2.5 seconds
// audioClip.Split(2.5f); // Currently not implemented

SetStartTick(long tick)

Sets the starting position of the clip on the timeline.
tick
long
The starting position in ticks
audioClip.SetStartTick(3840); // Move to tick 3840

GetStartTimeInMusicalTime()

Gets the start time of the clip in musical time format (Bars:Beats:Ticks).
MusicalTime startTime = audioClip.GetStartTimeInMusicalTime();
Console.WriteLine($"{startTime.Bars}:{startTime.Beats}:{startTime.Ticks}");

GetEndTimeInMusicalTime()

Gets the end time of the clip in musical time format (Bars:Beats:Ticks).
MusicalTime endTime = audioClip.GetEndTimeInMusicalTime();
Console.WriteLine($"End: {endTime.Bars}:{endTime.Beats}:{endTime.Ticks}");

GetDurationInMusicalTime()

Gets the duration of the clip in musical time format (Bars:Beats:Ticks).
MusicalTime duration = audioClip.GetDurationInMusicalTime();
Console.WriteLine($"Length: {duration.Bars} bars, {duration.Beats} beats");

GetDurationInSeconds()

Gets the duration of the clip in seconds.
double durationSeconds = audioClip.GetDurationInSeconds();
Console.WriteLine($"Duration: {durationSeconds:F2} seconds");

Play(AudioFileReader audioFile, float offset, float endOffset)

Plays the audio clip with the specified offsets.
audioFile
AudioFileReader
The audio file reader to play
offset
float
The starting offset in the audio file
endOffset
float
The ending offset in the audio file
audioClip.Play(clipData.AudioFileReader, 0.0f, 0.0f);

Render()

Renders the clip in the arrangement view with waveform visualization. Inherited from base Clip class.
audioClip.Render();

Protected Methods

GetClipDuration()

Calculates the duration of the clip in ticks based on the audio file’s total time.
protected override long GetClipDuration()
{
    return TimeLineV2.SecondsToTicks(_clip.AudioFileReader.TotalTime.TotalSeconds);
}

GetClipWidth()

Calculates the width of the clip in pixels based on duration and current zoom level.
protected override float GetClipWidth()
{
    return TimeLineV2.SecondsToTicks(_clip.AudioFileReader.TotalTime.TotalSeconds) 
           * TimeLineV2.PixelsPerTick;
}

RenderClipContent(float menuBarHeight, float clipHeight)

Renders the waveform content of the clip within the safe rendering area.
menuBarHeight
float
The height of the menu bar in pixels
clipHeight
float
The height of the clip content area in pixels

RenderClipContent(Vector2 pos, float width, float height)

Renders the waveform content at a specific position with specific dimensions.
pos
Vector2
The position to render at
width
float
The width to render
height
float
The height to render

OnClipDoubleClickLeft()

Called when the clip is double-clicked. Currently has no implementation but can be overridden for custom behavior.

Usage Example

using Lumix.Clips.AudioClips;
using Lumix.Tracks.AudioTracks;
using System.Numerics;

// Create an audio track
var drumTrack = new AudioTrack("Drums");

// Load audio data
var audioData = new AudioClipData("drums.wav");

// Create an audio clip starting at bar 1
var drumClip = new AudioClip(drumTrack, audioData, startingTick: 0);

// Set clip color
drumClip.Color = new Vector4(0.95f, 0.58f, 0.13f, 1.0f);

// Add to track
drumTrack.Clips.Add(drumClip);

// Get clip information
var startTime = drumClip.GetStartTimeInMusicalTime();
var duration = drumClip.GetDurationInSeconds();
Console.WriteLine($"Clip starts at {startTime.Bars}:{startTime.Beats}:{startTime.Ticks}");
Console.WriteLine($"Duration: {duration:F2} seconds");

// Move the clip to a different position
drumClip.SetStartTick(7680); // Move to bar 5

// Update waveform after zoom change
drumClip.ResizeWaveformData();

// Disable (mute) the clip
drumClip.Enabled = false;

// Check if clip is being hovered
if (drumClip.ClipIsHovered)
{
    // Show tooltip or highlight
}

// Access the underlying audio data
var sampleRate = drumClip.Clip.AudioFileReader.WaveFormat.SampleRate;
var channels = drumClip.Clip.AudioFileReader.WaveFormat.Channels;
Console.WriteLine($"Sample Rate: {sampleRate} Hz, Channels: {channels}");

See Also

Build docs developers (and LLMs) love