Class LrcDocumentBuilder
- Namespace
- ModernLrc
- Assembly
- ModernLrc.dll
Mutable builder for LrcDocument. Maintains insertion order; Build() sorts and freezes. Single-threaded contract — concurrent use from multiple threads is undefined (mirrors StringBuilder).
public sealed class LrcDocumentBuilder
- Inheritance
-
LrcDocumentBuilder
- Inherited Members
Examples
using ModernLrc;
using ModernLrc.Model;
var doc = new LrcDocumentBuilder()
.WithTitle("Song")
.WithArtist("Artist")
.WithOffset(TimeSpan.FromMilliseconds(-150))
.AddLine("00:01.00", "intro")
.AddLine("00:05.00", "she sings", LrcVoice.Female)
.AddEnhancedLine(
LrcTimestamp.FromMilliseconds(10_000),
[(LrcTimestamp.FromMilliseconds(10_000), "word "),
(LrcTimestamp.FromMilliseconds(10_500), "by word")])
.Build();
Remarks
Build() is idempotent: it does not mutate the builder, so the same builder instance can keep adding lines and produce updated documents.
Constructors
LrcDocumentBuilder()
Empty builder.
public LrcDocumentBuilder()
LrcDocumentBuilder(LrcDocument)
Seed from an existing document (shallow copy — records are immutable so sharing is safe).
public LrcDocumentBuilder(LrcDocument source)
Parameters
sourceLrcDocument
Properties
LineCount
Number of lines currently held (in insertion order).
public int LineCount { get; }
Property Value
Methods
AddEnhancedLine(LrcTimestamp, IEnumerable<(LrcTimestamp Time, string Text)>, LrcVoice)
Add an enhanced (word-timed) line from a tuple sequence.
public LrcDocumentBuilder AddEnhancedLine(LrcTimestamp lineTimestamp, IEnumerable<(LrcTimestamp Time, string Text)> words, LrcVoice voice = LrcVoice.Default)
Parameters
lineTimestampLrcTimestampwordsIEnumerable<(LrcTimestamp Time, string Text)>voiceLrcVoice
Returns
AddEnhancedLine(LrcTimestamp, ReadOnlySpan<LrcWord>, LrcVoice)
Add an enhanced (word-timed) line at a single timestamp.
public LrcDocumentBuilder AddEnhancedLine(LrcTimestamp lineTimestamp, ReadOnlySpan<LrcWord> words, LrcVoice voice = LrcVoice.Default)
Parameters
lineTimestampLrcTimestampwordsReadOnlySpan<LrcWord>voiceLrcVoice
Returns
AddEnhancedLineGroup(ReadOnlySpan<LrcTimestamp>, ReadOnlySpan<LrcWord>, LrcVoice)
Add the same enhanced line content at multiple top-level timestamps. Each timestamp
produces its own LrcEnhancedLine sharing the same words.
public LrcDocumentBuilder AddEnhancedLineGroup(ReadOnlySpan<LrcTimestamp> lineTimestamps, ReadOnlySpan<LrcWord> words, LrcVoice voice = LrcVoice.Default)
Parameters
lineTimestampsReadOnlySpan<LrcTimestamp>wordsReadOnlySpan<LrcWord>voiceLrcVoice
Returns
AddLine(LrcLine)
Append a fully-constructed LrcLine (reference held; records are immutable).
public LrcDocumentBuilder AddLine(LrcLine line)
Parameters
lineLrcLine
Returns
AddLine(LrcTimestamp, string, LrcVoice)
Add a plain line at timestamp.
public LrcDocumentBuilder AddLine(LrcTimestamp timestamp, string text, LrcVoice voice = LrcVoice.Default)
Parameters
timestampLrcTimestamptextstringvoiceLrcVoice
Returns
AddLine(string, string, LrcVoice)
Add a plain line whose timestamp is parsed from timestamp.
public LrcDocumentBuilder AddLine(string timestamp, string text, LrcVoice voice = LrcVoice.Default)
Parameters
Returns
AddLine(TimeSpan, string, LrcVoice)
Add a plain line at timestamp (TimeSpan overload).
public LrcDocumentBuilder AddLine(TimeSpan timestamp, string text, LrcVoice voice = LrcVoice.Default)
Parameters
Returns
AddLineGroup(ReadOnlySpan<LrcTimestamp>, string, LrcVoice)
Add the same plain text at multiple timestamps. Each timestamp produces its own
LrcPlainLine sharing the same text reference. The writer's
CollapseIdenticalLines setting controls whether they re-emit
as a [t1][t2]text group.
public LrcDocumentBuilder AddLineGroup(ReadOnlySpan<LrcTimestamp> timestamps, string text, LrcVoice voice = LrcVoice.Default)
Parameters
timestampsReadOnlySpan<LrcTimestamp>textstringvoiceLrcVoice
Returns
AddLines(IEnumerable<LrcLine>)
Append every line from a sequence.
public LrcDocumentBuilder AddLines(IEnumerable<LrcLine> lines)
Parameters
linesIEnumerable<LrcLine>
Returns
Build()
Materialize an immutable LrcDocument. Sorts by timestamp (stable: ties resolved by insertion order). Idempotent — does not mutate the builder.
public LrcDocument Build()
Returns
- LrcDocument
A fresh immutable document. Calling Build() again after further fluent calls produces an updated document; the builder stays usable.
Remarks
The sort takes a fast O(N) path when lines were added in ascending timestamp order (the common case); otherwise an indexed stable sort is used.
Clear()
Drop both lines and metadata.
public LrcDocumentBuilder Clear()
Returns
ClearLines()
Drop all lines (metadata kept).
public LrcDocumentBuilder ClearLines()
Returns
ClearMetadata()
Drop all metadata (lines kept).
public LrcDocumentBuilder ClearMetadata()
Returns
GetLineAt(int)
Get a line in insertion order. Use Build() to retrieve sorted output.
public LrcLine GetLineAt(int index)
Parameters
indexint
Returns
RemoveLineAt(int)
Remove the line at index (insertion order).
public LrcDocumentBuilder RemoveLineAt(int index)
Parameters
indexint
Returns
RemoveLinesWhere(Func<LrcLine, bool>)
Remove every line for which predicate returns true.
public LrcDocumentBuilder RemoveLinesWhere(Func<LrcLine, bool> predicate)
Parameters
Returns
RemoveRawTag(string)
Remove every raw tag whose key matches (ordinal).
public LrcDocumentBuilder RemoveRawTag(string key)
Parameters
keystring
Returns
ReplaceLine(int, LrcLine)
Replace the line at index (insertion order).
public LrcDocumentBuilder ReplaceLine(int index, LrcLine replacement)
Parameters
Returns
ShiftAll(TimeSpan)
Add delta to every line and word timestamp.
Validates upfront — if any resulting timestamp would be negative, throws
ArgumentOutOfRangeException and the builder is unchanged.
To shift past zero, edit Offset instead.
public LrcDocumentBuilder ShiftAll(TimeSpan delta)
Parameters
deltaTimeSpanPositive or negative offset.
Returns
- LrcDocumentBuilder
The builder, for chaining.
Exceptions
- ArgumentOutOfRangeException
A resulting timestamp would be negative, or the addition overflowed.
WithAlbum(string?)
Set or clear the album (al).
public LrcDocumentBuilder WithAlbum(string? value)
Parameters
valuestring
Returns
WithArtist(string?)
Set or clear the artist (ar).
public LrcDocumentBuilder WithArtist(string? value)
Parameters
valuestring
Returns
WithAuthor(string?)
Set or clear the author (au).
public LrcDocumentBuilder WithAuthor(string? value)
Parameters
valuestring
Returns
WithCreatedBy(string?)
Set or clear the created-by attribution (by).
public LrcDocumentBuilder WithCreatedBy(string? value)
Parameters
valuestring
Returns
WithLength(TimeSpan?)
Set or clear the track length (length:mm:ss).
public LrcDocumentBuilder WithLength(TimeSpan? value)
Parameters
valueTimeSpan?
Returns
WithLyricist(string?)
Set or clear the lyricist (lr).
public LrcDocumentBuilder WithLyricist(string? value)
Parameters
valuestring
Returns
WithOffset(TimeSpan)
Set the document offset (offset:±N ms).
public LrcDocumentBuilder WithOffset(TimeSpan value)
Parameters
valueTimeSpan
Returns
WithRawTag(string, string)
Append a raw tag entry (preserves insertion order).
key must be non-empty (whitespace-only is rejected — it would round-trip
to [ :value] which the parser cannot read back).
public LrcDocumentBuilder WithRawTag(string key, string value)
Parameters
Returns
WithTitle(string?)
Set or clear the title (ti).
public LrcDocumentBuilder WithTitle(string? value)
Parameters
valuestring
Returns
WithTool(string?)
Set or clear the tool (re / tool).
public LrcDocumentBuilder WithTool(string? value)
Parameters
valuestring
Returns
WithVersion(string?)
Set or clear the version (ve).
public LrcDocumentBuilder WithVersion(string? value)
Parameters
valuestring