Class NoteBuffer
A fixed-capacity, native-memory buffer of notes stored as a structure of arrays (pitch, offset, duration, velocity). Offsets and durations are in whole-note units. Must be disposed to release the unmanaged allocation.
public sealed class NoteBuffer : IDisposable
- Inheritance
-
NoteBuffer
- Implements
- Inherited Members
Constructors
NoteBuffer(int)
Allocates a buffer that can hold up to capacity notes.
public NoteBuffer(int capacity)
Parameters
capacityintMaximum number of notes; must be positive.
Exceptions
- ArgumentOutOfRangeException
capacityis not positive.- OutOfMemoryException
The required allocation exceeds the process address space.
Properties
Capacity
Maximum number of notes the buffer can hold.
public int Capacity { get; }
Property Value
Count
Number of notes currently stored.
public int Count { get; }
Property Value
PitchReadOnlySpan
Alias for PitchesReadOnly.
public ReadOnlySpan<int> PitchReadOnlySpan { get; }
Property Value
PitchSpan
Alias for Pitches.
public Span<int> PitchSpan { get; }
Property Value
Pitches
Writable span over the stored MIDI pitches (length Count).
public Span<int> Pitches { get; }
Property Value
Exceptions
- ObjectDisposedException
The buffer has been disposed.
PitchesReadOnly
Read-only span over the stored MIDI pitches (length Count).
public ReadOnlySpan<int> PitchesReadOnly { get; }
Property Value
Exceptions
- ObjectDisposedException
The buffer has been disposed.
Velocities
Writable span over the stored velocities (length Count).
public Span<float> Velocities { get; }
Property Value
Exceptions
- ObjectDisposedException
The buffer has been disposed.
VelocitiesReadOnly
Read-only span over the stored velocities (length Count).
public ReadOnlySpan<float> VelocitiesReadOnly { get; }
Property Value
Exceptions
- ObjectDisposedException
The buffer has been disposed.
VelocityReadOnlySpan
Alias for VelocitiesReadOnly.
public ReadOnlySpan<float> VelocityReadOnlySpan { get; }
Property Value
VelocitySpan
Alias for Velocities.
public Span<float> VelocitySpan { get; }
Property Value
Methods
Add(in NoteEvent)
Appends a note event to the buffer.
public void Add(in NoteEvent note)
Parameters
noteNoteEvent
Exceptions
- ObjectDisposedException
The buffer has been disposed.
- InvalidOperationException
The buffer is at capacity.
AddNote(int, Rational, Rational, float)
Appends a note to the buffer.
public void AddNote(int pitch, Rational offset, Rational duration, float velocity = 0.8)
Parameters
pitchintMIDI pitch number.
offsetRationalStart offset in whole-note units.
durationRationalDuration in whole-note units.
velocityfloatVelocity (default 0.8).
Exceptions
- ObjectDisposedException
The buffer has been disposed.
- InvalidOperationException
The buffer is at capacity.
AddRange(ReadOnlySpan<NoteEvent>)
Appends a batch of note events to the buffer.
public void AddRange(ReadOnlySpan<NoteEvent> notes)
Parameters
notesReadOnlySpan<NoteEvent>
Exceptions
- ObjectDisposedException
The buffer has been disposed.
- InvalidOperationException
The notes would exceed the buffer's capacity.
Clear()
Fast reset for reuse (does not zero memory)
public void Clear()
Dispose()
Releases the buffer's unmanaged memory.
public void Dispose()
~NoteBuffer()
Releases the unmanaged pitch buffer if Dispose() was not called.
protected ~NoteBuffer()
Get(int)
Returns the full note event (pitch, offset, duration, velocity) at index.
public NoteEvent Get(int index)
Parameters
indexint
Returns
Exceptions
- ObjectDisposedException
The buffer has been disposed.
- ArgumentOutOfRangeException
indexis outside [0, Count).
GetChords()
Legacy chord analysis that allocates a List<T>. Groups consecutive notes that share the same start offset into one chord per offset. Requires the buffer to be sorted by offset — call Sort() first (appending notes only in nondecreasing offset order also counts as sorted).
public List<(Rational Time, ushort Mask)> GetChords()
Returns
Exceptions
- ObjectDisposedException
The buffer has been disposed.
- InvalidOperationException
The buffer is not sorted by offset.
GetChords(Span<(Rational Time, ushort Mask)>)
Zero-allocation chord analysis. Groups consecutive notes that share the same start offset into one chord per offset. Requires the buffer to be sorted by offset — call Sort() first (appending notes only in nondecreasing offset order also counts as sorted).
If output is shorter than the number of distinct offsets, the result
is silently truncated to output.Length chords (span-API convention); the return
value is the number of chords actually written. Size output to
Count to guarantee no truncation.
public int GetChords(Span<(Rational Time, ushort Mask)> output)
Parameters
Returns
- int
The number of chords written to
output.
Exceptions
- ObjectDisposedException
The buffer has been disposed.
- InvalidOperationException
The buffer is not sorted by offset.
GetDuration(int)
Returns the duration (whole-note units) of the note at index.
public Rational GetDuration(int index)
Parameters
indexint
Returns
Exceptions
- ObjectDisposedException
The buffer has been disposed.
- ArgumentOutOfRangeException
indexis outside [0, Count).
GetOffset(int)
Returns the start offset (whole-note units) of the note at index.
public Rational GetOffset(int index)
Parameters
indexint
Returns
Exceptions
- ObjectDisposedException
The buffer has been disposed.
- ArgumentOutOfRangeException
indexis outside [0, Count).
GetVelocity(int)
Returns the velocity of the note at index.
public float GetVelocity(int index)
Parameters
indexint
Returns
Exceptions
- ObjectDisposedException
The buffer has been disposed.
- ArgumentOutOfRangeException
indexis outside [0, Count).
PitchAt(int)
Returns the MIDI pitch at index.
public int PitchAt(int index)
Parameters
indexint
Returns
Exceptions
- ObjectDisposedException
The buffer has been disposed.
- ArgumentOutOfRangeException
indexis outside [0, Count).
SetPitch(int, int)
Sets the MIDI pitch at index to value.
public void SetPitch(int index, int value)
Parameters
Exceptions
- ObjectDisposedException
The buffer has been disposed.
- ArgumentOutOfRangeException
indexis outside [0, Count).
Sort()
Sorts the notes in place by ascending start offset (stable on ties). Sorting (or appending notes only in nondecreasing offset order) makes the buffer eligible for GetChords() and its span overload.
public void Sort()
Exceptions
- ObjectDisposedException
The buffer has been disposed.