Struct PatternEvent
- Namespace
- SunSharp
- Assembly
- SunSharp.dll
Represents a SunVox pattern event that encodes music commands such as notes, effects, and controller changes in a compact 64-bit format.
[Serializable]
public struct PatternEvent
Remarks
For better readability, consider using the property accessors and factory methods provided.
Constructors
PatternEvent(byte, byte, ushort, byte, byte, ushort)
Initializes a pattern event with individual fields. Uses raw data format.
public PatternEvent(byte nn, byte vv, ushort mm, byte cc, byte ee, ushort xxyy)
Parameters
nnbyteNote number or special command. Consider using Note instead. vvbyteRaw velocity value (actual velocity + 1). A value of 0 indicates default velocity should be used. Consider using Velocity property instead for automatic offset handling. mmushortRaw module number (actual module ID + 1). A value of 0 indicates no module is targeted. Consider using ModuleId property instead for automatic offset handling. ccbyteRaw controller number (high byte of CCEE, actual controller + 1). Consider using ControllerId property instead for automatic offset handling. eebyteRaw effect code (low byte of CCEE). Consider using Effect property instead for type-safe effect handling. xxyyushortCombined 16-bit parameter value (low byte = XX, high byte = YY).
Remarks
Consider using factory methods for better readability.
PatternEvent(ulong)
Initializes a new instance of the PatternEvent struct from raw 64-bit data.
public PatternEvent(ulong value)
Parameters
valueulongThe raw 64-bit value containing all event fields.
Fields
PatternEventHeaderString
Column header string for pattern display: "NNVVMMMMCCEEXXYY".
public const string PatternEventHeaderString = "NNVVMMMMCCEEXXYY"
Field Value
- string
Remarks
Primarily used for formatting pattern output in tracker-style displays.
Properties
CC
Raw controller number (high byte of CCEE, actual controller + 1). Consider using ControllerId property instead for automatic offset handling.
public byte CC { readonly get; set; }
Property Value
- byte
CCEE
Combined controller and effect code as a 16-bit value (high byte = CC, low byte = EE). Consider using ControllerId and Effect properties instead for better readability.
public ushort CCEE { readonly get; set; }
Property Value
- ushort
ControllerId
Controller identifier (0-based). Returns null when no controller is specified. Module controllers: 0-126 (raw data: 1-127). MIDI controllers: 127+ (raw data: 0x80+). Automatically handles the +1 offset used in raw data.
public byte? ControllerId { readonly get; set; }
Property Value
- byte?
Data
Raw 64-bit data representing the entire event.
public ulong Data { readonly get; set; }
Property Value
- ulong
EE
Raw effect code (low byte of CCEE). Consider using Effect property instead for type-safe effect handling.
public byte EE { readonly get; set; }
Property Value
- byte
Effect
Pattern event effect code.
public Effect Effect { readonly get; set; }
Property Value
Empty
Gets an empty pattern event with all fields set to zero (represents a blank pattern cell).
public static PatternEvent Empty { get; }
Property Value
HasController
Gets a value indicating whether this event contains a controller change.
public readonly bool HasController { get; }
Property Value
- bool
HasEffect
Gets a value indicating whether this event contains an effect command.
public readonly bool HasEffect { get; }
Property Value
- bool
HasModule
Gets a value indicating whether this event targets a specific module.
public readonly bool HasModule { get; }
Property Value
- bool
HasNote
Gets a value indicating whether this event contains a note command (musical note or special command).
public readonly bool HasNote { get; }
Property Value
- bool
HasValue
Gets a value indicating whether this event contains a non-zero parameter value.
public readonly bool HasValue { get; }
Property Value
- bool
HasVelocity
Gets a value indicating whether this event contains an explicit velocity value (not using default).
public readonly bool HasVelocity { get; }
Property Value
- bool
IsEmpty
Whether this is an empty event (all fields zero).
public readonly bool IsEmpty { get; }
Property Value
- bool
MM
Raw module number (actual module ID + 1). A value of 0 indicates no module is targeted. Consider using ModuleId property instead for automatic offset handling.
public ushort MM { readonly get; set; }
Property Value
- ushort
ModuleId
Module identifier (0-based). Returns null when no module is specified. Automatically handles the +1 offset used in raw data.
public int? ModuleId { readonly get; set; }
Property Value
- int?
NN
Note number or special command. Consider using Note instead.
public byte NN { readonly get; set; }
Property Value
- byte
Note
Note value. Allows for explicit note commands.
public Note Note { readonly get; set; }
Property Value
VV
Raw velocity value (actual velocity + 1). A value of 0 indicates default velocity should be used. Consider using Velocity property instead for automatic offset handling.
public byte VV { readonly get; set; }
Property Value
- byte
Value
Parameter value for controller or effect (16-bit unsigned integer).
Range: 0-32768 (0x8000) for controllers, 0-65535 (0xFFFF) for effects.
For pitch commands: 0x0000 = highest pitch, 0x7800 = C0, 0x100 = one semitone.
public ushort Value { readonly get; set; }
Property Value
- ushort
Velocity
Note velocity. Returns null when default velocity is used. Automatically handles the +1 offset used in raw data.
public byte? Velocity { readonly get; set; }
Property Value
- byte?
XX
Low byte of parameter value (lower 8 bits of XXYY).
public byte XX { readonly get; set; }
Property Value
- byte
XXYY
Combined 16-bit parameter value (low byte = XX, high byte = YY).
public ushort XXYY { readonly get; set; }
Property Value
- ushort
YY
High byte of parameter value (upper 8 bits of XXYY).
public byte YY { readonly get; set; }
Property Value
- byte
Methods
ControllerEvent(int, byte, ushort)
Creates a controller event that changes a module parameter.
public static PatternEvent ControllerEvent(int moduleId, byte controller, ushort value)
Parameters
moduleIdintModule identifier (0-based). Returns null when no module is specified. Automatically handles the +1 offset used in raw data. controllerbyteController identifier (0-based). Returns null when no controller is specified. Module controllers: 0-126 (raw data: 1-127). MIDI controllers: 127+ (raw data: 0x80+). Automatically handles the +1 offset used in raw data. valueushortParameter value for controller or effect (16-bit unsigned integer). Range: 0-32768 (0x8000) for controllers, 0-65535 (0xFFFF) for effects.
For pitch commands: 0x0000 = highest pitch, 0x7800 = C0, 0x100 = one semitone.
Returns
Remarks
For better type safety and readability, consider using typed module handles and extension methods.
EffectEvent(int?, Effect, ushort)
Creates an effect event that applies a pattern effect.
public static PatternEvent EffectEvent(int? moduleId, Effect effect, ushort value)
Parameters
moduleIdint?Module identifier (0-based). Returns null when no module is specified. Automatically handles the +1 offset used in raw data. effectEffectPattern event effect code. valueushortParameter value for controller or effect (16-bit unsigned integer). Range: 0-32768 (0x8000) for controllers, 0-65535 (0xFFFF) for effects.
For pitch commands: 0x0000 = highest pitch, 0x7800 = C0, 0x100 = one semitone.
Returns
- PatternEvent
A new PatternEvent configured as an effect event.
Equals(PatternEvent)
Indicates whether the current object is equal to another object of the same type.
public readonly bool Equals(PatternEvent other)
Parameters
otherPatternEventAn object to compare with this object.
Returns
Equals(object?)
Indicates whether this instance and a specified object are equal.
public override readonly bool Equals(object? obj)
Parameters
objobjectThe object to compare with the current instance.
Returns
- bool
true if
objand this instance are the same type and represent the same value; otherwise, false.
GetHashCode()
Returns the hash code for this instance.
public override readonly int GetHashCode()
Returns
- int
A 32-bit signed integer that is the hash code for this instance.
Merge(PatternEvent, PatternEvent)
Merges two events, with non-zero values from second overwriting
corresponding values in first.
public static PatternEvent Merge(PatternEvent first, PatternEvent second)
Parameters
firstPatternEventThe base event providing default values.
secondPatternEventThe event whose non-zero fields override the base event.
Returns
- PatternEvent
A merged PatternEvent combining both inputs.
Remarks
This performs a field-by-field merge where any non-zero field in second
replaces the corresponding field in first. Useful for layering
pattern data or applying partial updates.
NewEvent(Note, byte?, int?, byte?, Effect, ushort)
Creates a new generic pattern event with optional parameters for flexible event construction.
public static PatternEvent NewEvent(Note note = default, byte? velocity = null, int? moduleId = null, byte? controller = null, Effect effect = Effect.None, ushort value = 0)
Parameters
noteNoteNote value. Allows for explicit note commands. velocitybyte?Note velocity. Returns null when default velocity is used. Automatically handles the +1 offset used in raw data. moduleIdint?Module identifier (0-based). Returns null when no module is specified. Automatically handles the +1 offset used in raw data. controllerbyte?Controller identifier (0-based). Returns null when no controller is specified. Module controllers: 0-126 (raw data: 1-127). MIDI controllers: 127+ (raw data: 0x80+). Automatically handles the +1 offset used in raw data. effectEffectPattern event effect code. valueushortParameter value for controller or effect (16-bit unsigned integer). Range: 0-32768 (0x8000) for controllers, 0-65535 (0xFFFF) for effects.
For pitch commands: 0x0000 = highest pitch, 0x7800 = C0, 0x100 = one semitone.
Returns
- PatternEvent
A new PatternEvent with the specified fields.
Remarks
This method provides maximum flexibility for creating events. For common scenarios, consider using specific factory methods like NoteEvent(Note, int?, byte?), ControllerEvent(int, byte, ushort), etc.
NoteEvent(Note, int?, byte?)
Creates a note event that triggers a note on the specified module.
public static PatternEvent NoteEvent(Note note, int? moduleId, byte? velocity = null)
Parameters
noteNoteNote value. Allows for explicit note commands. moduleIdint?Module identifier (0-based). Returns null when no module is specified. Automatically handles the +1 offset used in raw data. velocitybyte?Note velocity. Returns null when default velocity is used. Automatically handles the +1 offset used in raw data.
Returns
SetFrequencyEvent(int, double, byte?)
Creates a Set Pitch event by converting a frequency in Hz to the appropriate pitch value.
public static PatternEvent SetFrequencyEvent(int moduleId, double frequency, byte? velocity = null)
Parameters
moduleIdintModule number (0-based).
frequencydoubleFrequency in Hz (e.g., 440.0 for A4).
velocitybyte?Velocity value (0-128). Use null for default velocity.
Returns
- PatternEvent
A new PatternEvent with the calculated pitch value.
Examples
// Set pitch to A4 (440 Hz)
var a440 = PatternEvent.SetFrequencyEvent(0, 440.0, 100);
// Set pitch to middle C (261.63 Hz)
var c4 = PatternEvent.SetFrequencyEvent(0, 261.63);
SetPitchEvent(int, ushort, byte?)
Creates a Set Pitch event with exact pitch value for microtonal or pitch-bend effects.
public static PatternEvent SetPitchEvent(int moduleId, ushort pitch, byte? velocity = null)
Parameters
moduleIdintModule number (0-based).
pitchushortPitch value where 0x0000 = highest pitch, 0x7800 = C0 (lowest), 0x100 = one semitone.
velocitybyte?Velocity value (0-128). Use null for default velocity.
Returns
- PatternEvent
A new PatternEvent configured as a set pitch event.
Examples
// Set pitch to middle C (C4 = 0x3C00)
var middleC = PatternEvent.SetPitchEvent(0, 0x3C00);
// Set pitch to A4 (440Hz reference = 0x4500)
var a440 = PatternEvent.SetPitchEvent(0, 0x4500, 100);
ToString()
Returns hexadecimal string representation that roughly follows the SunVox format. Empty fields shown as spaces.
public override readonly string ToString()
Returns
- string
Remarks
Some examples of the output format:
- "C4__00001________" - Note C4 on module 0, no velocity, no effect/controller/value.
- "__64__0001_02_1000" - Velocity 100 on module 0, controller 2 set to 4096.
Keep in mind:
- Module number (MMMM) is as displayed in SunVox, incremented by one compared to library-side module IDs
- CC and EE are independent, unlike in SunVox
- CC is as displayed in SunVox, incremented by one compared to library-side module IDs
Operators
operator ==(PatternEvent, PatternEvent)
public static bool operator ==(PatternEvent a, PatternEvent b)
Parameters
Returns
- bool
implicit operator ulong(PatternEvent)
public static implicit operator ulong(PatternEvent @event)
Parameters
eventPatternEvent
Returns
- ulong
implicit operator PatternEvent(ulong)
public static implicit operator PatternEvent(ulong value)
Parameters
valueulong
Returns
operator !=(PatternEvent, PatternEvent)
public static bool operator !=(PatternEvent a, PatternEvent b)
Parameters
Returns
- bool