Class PatternBuilder
Provides a fluent API to build an instance of the
Pattern.
Inheritance
PatternBuilder
Assembly: Melanchall.DryWetMidi.dll
Syntax
public sealed class PatternBuilder
Examples
Following example shows how to create first four bars of Beethoven's 'Moonlight Sonata':
// Define a chord for bass part which is just an octave
var bassChord = new[] { Interval.Twelve };
// Build the composition
var pattern = new PatternBuilder()
// The length of all main theme's notes within four first bars is
// triplet eight so set it which will free us from necessity to specify
// the length of each note explicitly
.SetNoteLength(MusicalTimeSpan.Eighth.Triplet())
// Anchor current time (start of the pattern) to jump to it
// when we'll start to program bass part
.Anchor()
// We will add notes relative to G#3.
// Instead of Octave.Get(3).GSharp it is possible to use Note.Get(NoteName.GSharp, 3)
.SetRootNote(Octave.Get(3).GSharp)
// Add first three notes and repeat them seven times which will
// give us two bars of the main theme
// G#3
.Note(Interval.Zero) // +0 (G#3)
.Note(Interval.Five) // +5 (C#4)
.Note(Interval.Eight) // +8 (E4)
.Repeat(3, 7) // repeat three previous notes seven times
// Add notes of the next two bars
// G#3
.Note(Interval.One) // +1 (A3)
.Note(Interval.Five) // +5 (C#4)
.Note(Interval.Eight) // +8 (E4)
.Repeat(3, 1) // repeat three previous notes
.Note(Interval.One) // +1 (A3)
.Note(Interval.Six) // +6 (D4)
.Note(Interval.Ten) // +10 (F#4)
.Repeat(3, 1) // repeat three previous notes
// reaching the end of third bar
.Note(Interval.Zero) // +0 (G#3)
.Note(Interval.Four) // +4 (C4)
.Note(Interval.Ten) // +10 (F#4)
.Note(Interval.Zero) // +0 (G#3)
.Note(Interval.Five) // +5 (C#4)
.Note(Interval.Eight) // +8 (E4)
.Note(Interval.Zero) // +0 (G#3)
.Note(Interval.Five) // +5 (C#4)
.Note(Interval.Seven) // +7 (D#4)
.Note(-Interval.Two) // -2 (F#3)
.Note(Interval.Four) // +4 (C4)
.Note(Interval.Seven) // +7 (D#4)
// Now we will program bass part. To start adding notes from the
// beginning of the pattern we need to move to the anchor we set
// above
.MoveToFirstAnchor()
// First two chords have whole length
.SetNoteLength(MusicalTimeSpan.Whole)
// insert a chord relative to
.Chord(bassChord, Octave.Get(2).CSharp) // C#2 (C#2, C#3)
.Chord(bassChord, Octave.Get(1).B) // B1 (B1, B2)
// Remaining four chords has half length
.SetNoteLength(MusicalTimeSpan.Half)
.Chord(bassChord, Octave.Get(1).A) // A1 (A1, A2)
.Chord(bassChord, Octave.Get(1).FSharp) // F#1 (F#1, F#2)
.Chord(bassChord, Octave.Get(1).GSharp) // G#1 (G#1, G#2)
.Repeat() // repeat the previous chord
// Build a pattern that can be then saved to a MIDI file
.Build();
Constructors
PatternBuilder()
Declaration
PatternBuilder(Pattern)
Initializes a new instance of the
PatternBuilder with the specified pattern.
Declaration
public PatternBuilder(Pattern pattern)
Parameters
Type |
Name |
Description |
Pattern |
pattern |
Pattern to initialize builder with. |
Exceptions
Fields
DefaultNoteLength
Default length that will be applied to all further notes and chords if it's not
specified explicitly. The length can be altered with
SetNoteLength(ITimeSpan).
Declaration
public static readonly ITimeSpan DefaultNoteLength
Field Value
DefaultOctave
Default octave further notes and chords will be relative to if it's not
specified explicitly. Octave can be altered with
SetOctave(Octave).
Declaration
public static readonly Octave DefaultOctave
Field Value
DefaultRootNote
Default root note further notes will be based on if it's not specified explicitly.
Root note can be altered with
SetRootNote(Note).
Declaration
public static readonly Note DefaultRootNote
Field Value
DefaultStep
Default step size that will be applied to all further move operations if it's not
specified explicitly. Step size can be altered with
SetStep(ITimeSpan).
Declaration
public static readonly ITimeSpan DefaultStep
Field Value
DefaultVelocity
Default velocity that will be applied to all further notes and chords if it's not
specified explicitly. Velocity can be altered with
SetVelocity(SevenBitNumber).
Declaration
public static readonly SevenBitNumber DefaultVelocity
Field Value
Properties
NoteLength
Gets the length that will be applied to all further notes and chords if it's not
specified explicitly. The length can be altered with
SetNoteLength(ITimeSpan).
Declaration
public ITimeSpan NoteLength { get; }
Property Value
Octave
Gets the octave further notes and chords will be relative to if it's not
specified explicitly. Octave can be altered with
SetOctave(Octave).
Declaration
public Octave Octave { get; }
Property Value
RootNote
Gets the root note further notes will be based on if it's not specified explicitly.
Root note can be altered with
SetRootNote(Note).
Declaration
public Note RootNote { get; }
Property Value
Step
Gets the step size that will be applied to all further move operations if it's not
specified explicitly. Step size can be altered with
SetStep(ITimeSpan).
Declaration
public ITimeSpan Step { get; }
Property Value
Velocity
Gets the velocity that will be applied to all further notes and chords if it's not
specified explicitly. Velocity can be altered with
SetVelocity(SevenBitNumber).
Declaration
public SevenBitNumber Velocity { get; }
Property Value
Methods
Anchor()
Places an anchor at the current time.
Declaration
public PatternBuilder Anchor()
Returns
Anchor(object)
Places the specified anchor at the current time.
Declaration
public PatternBuilder Anchor(object anchor)
Parameters
Type |
Name |
Description |
object |
anchor |
Anchor to place. |
Returns
Exceptions
Build()
Build an instance of the
Pattern holding all actions
defined via builder.
Declaration
Returns
Chord(Chord, Octave, ITimeSpan, SevenBitNumber?)
Adds a chord using the specified octave, length and velocity.
Declaration
public PatternBuilder Chord(Chord chord, Octave octave = null, ITimeSpan length = null, SevenBitNumber? velocity = null)
Parameters
Returns
Exceptions
Chord(IEnumerable<Interval>, Note, ITimeSpan, SevenBitNumber?)
Adds a chord by the specified intervals relative to the root note using specified
length and velocity.
Declaration
public PatternBuilder Chord(IEnumerable<Interval> intervals, Note rootNote = null, ITimeSpan length = null, SevenBitNumber? velocity = null)
Parameters
Returns
Exceptions
Chord(IEnumerable<Interval>, NoteName, ITimeSpan, SevenBitNumber?)
Adds a chord by the specified intervals, root note's name, length and velocity.
Declaration
public PatternBuilder Chord(IEnumerable<Interval> intervals, NoteName rootNoteName, ITimeSpan length = null, SevenBitNumber? velocity = null)
Parameters
Returns
Exceptions
Chord(IEnumerable<NoteName>, ITimeSpan, SevenBitNumber?)
Adds a chord by the specified notes names using specified velocity and length, and default octave.
Declaration
public PatternBuilder Chord(IEnumerable<NoteName> noteNames, ITimeSpan length = null, SevenBitNumber? velocity = null)
Parameters
Returns
Exceptions
Chord(IEnumerable<Note>, ITimeSpan, SevenBitNumber?)
Adds a chord by the specified notes using specified velocity and length.
Declaration
public PatternBuilder Chord(IEnumerable<Note> notes, ITimeSpan length = null, SevenBitNumber? velocity = null)
Parameters
Returns
Exceptions
Chord(string, Octave, ITimeSpan, SevenBitNumber?)
Adds a chord using the specified octave, length and velocity.
Declaration
public PatternBuilder Chord(string chord, Octave octave = null, ITimeSpan length = null, SevenBitNumber? velocity = null)
Parameters
Returns
Exceptions
ControlChange(SevenBitNumber, SevenBitNumber)
Inserts
ControlChangeEvent to specify a change of a controller that will be used by following notes.
Declaration
public PatternBuilder ControlChange(SevenBitNumber controlNumber, SevenBitNumber controlValue)
Parameters
Returns
Lyrics(string)
Adds lyrics.
Declaration
public PatternBuilder Lyrics(string text)
Parameters
Type |
Name |
Description |
string |
text |
Text of lyrics. |
Returns
Exceptions
Marker(string)
Adds a marker.
Declaration
public PatternBuilder Marker(string marker)
Parameters
Type |
Name |
Description |
string |
marker |
The text of marker. |
Returns
Exceptions
MoveToFirstAnchor()
Move to the first anchor.
Declaration
public PatternBuilder MoveToFirstAnchor()
Returns
Exceptions
MoveToFirstAnchor(object)
Moves to the first specified anchor.
Declaration
public PatternBuilder MoveToFirstAnchor(object anchor)
Parameters
Type |
Name |
Description |
object |
anchor |
Anchor to move to. |
Returns
Exceptions
MoveToLastAnchor()
Moves to the last anchor.
Declaration
public PatternBuilder MoveToLastAnchor()
Returns
Exceptions
MoveToLastAnchor(object)
Moves to the last specified anchor.
Declaration
public PatternBuilder MoveToLastAnchor(object anchor)
Parameters
Type |
Name |
Description |
object |
anchor |
Anchor to move to. |
Returns
Exceptions
MoveToNthAnchor(int)
Moves to the nth anchor.
Declaration
public PatternBuilder MoveToNthAnchor(int index)
Parameters
Type |
Name |
Description |
int |
index |
Index of an anchor to move to. |
Returns
Exceptions
MoveToNthAnchor(object, int)
Moves to the nth specified anchor.
Declaration
public PatternBuilder MoveToNthAnchor(object anchor, int index)
Parameters
Type |
Name |
Description |
object |
anchor |
Anchor to move to. |
int |
index |
Index of an anchor to move to. |
Returns
Exceptions
MoveToPreviousTime()
Moves the current time to the previous one.
Declaration
public PatternBuilder MoveToPreviousTime()
Returns
MoveToStart()
Moves the current time to the start (zero time) of a pattern.
Declaration
public PatternBuilder MoveToStart()
Returns
MoveToTime(ITimeSpan)
Moves the current time to the specified one.
Declaration
public PatternBuilder MoveToTime(ITimeSpan time)
Parameters
Type |
Name |
Description |
ITimeSpan |
time |
Time to move to. |
Returns
Exceptions
Note(Interval, ITimeSpan, SevenBitNumber?)
Adds a note by the specified interval relative to the current root note using
specified length and velocity.
Declaration
public PatternBuilder Note(Interval interval, ITimeSpan length = null, SevenBitNumber? velocity = null)
Parameters
Returns
Exceptions
Note(Note, ITimeSpan, SevenBitNumber?)
Adds a note using specified velocity and length.
Declaration
public PatternBuilder Note(Note note, ITimeSpan length = null, SevenBitNumber? velocity = null)
Parameters
Returns
Exceptions
Note(NoteName, ITimeSpan, SevenBitNumber?)
Adds a note by the specified note name using specified velocity and length, and default octave.
Declaration
public PatternBuilder Note(NoteName noteName, ITimeSpan length = null, SevenBitNumber? velocity = null)
Parameters
Returns
Exceptions
Note(string, ITimeSpan, SevenBitNumber?)
Adds a note using specified velocity and length.
Declaration
public PatternBuilder Note(string note, ITimeSpan length = null, SevenBitNumber? velocity = null)
Parameters
Returns
Exceptions
Pattern(Pattern)
Adds a pattern.
Declaration
public PatternBuilder Pattern(Pattern pattern)
Parameters
Type |
Name |
Description |
Pattern |
pattern |
Pattern to add. |
Returns
Exceptions
PianoRoll(string, PianoRollSettings)
Inserts notes data by the specified piano roll string. More info in the
Pattern: Piano roll article.
Declaration
public PatternBuilder PianoRoll(string pianoRoll, PianoRollSettings settings = null)
Parameters
Type |
Name |
Description |
string |
pianoRoll |
String that represents notes data as a piano roll. |
PianoRollSettings |
settings |
Settings according to which a piano roll should be handled. |
Returns
Exceptions
Type |
Condition |
ArgumentException |
pianoRoll is null , a zero-length string,
contains only white space, or contains one or more invalid characters as defined by
InvalidPathChars. |
InvalidOperationException |
One of the following errors occurred:
- Failed to parse a note.
- Single-cell note can't be placed inside a multi-cell one.
- Note can't be started while a previous one is not ended.
- Note is not started.
|
PitchBend(ushort)
Inserts
PitchBendEvent to specify a pitch bend that will be used by following notes.
Declaration
public PatternBuilder PitchBend(ushort pitchValue)
Parameters
Type |
Name |
Description |
ushort |
pitchValue |
Pitch value. |
Returns
Exceptions
ProgramChange(SevenBitNumber)
Declaration
public PatternBuilder ProgramChange(SevenBitNumber programNumber)
Parameters
Type |
Name |
Description |
SevenBitNumber |
programNumber |
The number of a MIDI program. |
Returns
ProgramChange(GeneralMidi2Program)
Declaration
public PatternBuilder ProgramChange(GeneralMidi2Program program)
Parameters
Returns
Exceptions
ProgramChange(GeneralMidiProgram)
Declaration
public PatternBuilder ProgramChange(GeneralMidiProgram program)
Parameters
Returns
Exceptions
Repeat(RepeatSettings)
Repeats the previous action one time.
Declaration
public PatternBuilder Repeat(RepeatSettings settings = null)
Parameters
Type |
Name |
Description |
RepeatSettings |
settings |
Settings according to which actions should be repeated. |
Returns
Exceptions
Repeat(int, RepeatSettings)
Repeats the previous action the specified number of times.
Declaration
public PatternBuilder Repeat(int repeatsNumber, RepeatSettings settings = null)
Parameters
Type |
Name |
Description |
int |
repeatsNumber |
Count of repetitions. |
RepeatSettings |
settings |
Settings according to which actions should be repeated. |
Returns
Exceptions
Repeat(int, int, RepeatSettings)
Repeats the specified number of previous actions.
Declaration
public PatternBuilder Repeat(int actionsCount, int repeatsNumber, RepeatSettings settings = null)
Parameters
Type |
Name |
Description |
int |
actionsCount |
Number of previous actions to repeat. |
int |
repeatsNumber |
Count of repetitions. |
RepeatSettings |
settings |
Settings according to which actions should be repeated. |
Returns
Exceptions
Type |
Condition |
ArgumentOutOfRangeException |
One of the following errors occurred:
-
actionsCount is negative. -
actionsCount is greater than count of existing actions. -
repeatsNumber is negative.
|
ReplayPattern(Pattern)
Replays all actions contained in the specified pattern.
Declaration
public PatternBuilder ReplayPattern(Pattern pattern)
Parameters
Type |
Name |
Description |
Pattern |
pattern |
Pattern to replay actions of. |
Returns
Exceptions
SetNoteLength(ITimeSpan)
Sets default note length that will be used by next actions of the builder.
Declaration
public PatternBuilder SetNoteLength(ITimeSpan length)
Parameters
Type |
Name |
Description |
ITimeSpan |
length |
New default note length. |
Returns
Exceptions
SetOctave(Octave)
Sets default note octave that will be used by next actions of the builder.
Declaration
public PatternBuilder SetOctave(Octave octave)
Parameters
Type |
Name |
Description |
Octave |
octave |
New default octave. |
Returns
Exceptions
SetRootNote(Note)
Sets a root note that will be used by next actions of the builder using
Interval objects.
Declaration
public PatternBuilder SetRootNote(Note rootNote)
Parameters
Type |
Name |
Description |
Note |
rootNote |
The root note. |
Returns
Exceptions
SetStep(ITimeSpan)
Sets default step for step back and step forward actions of the builder.
Declaration
public PatternBuilder SetStep(ITimeSpan step)
Parameters
Type |
Name |
Description |
ITimeSpan |
step |
New default step. |
Returns
Exceptions
SetVelocity(SevenBitNumber)
Sets default velocity that will be used by next actions of the builder.
Declaration
public PatternBuilder SetVelocity(SevenBitNumber velocity)
Parameters
Type |
Name |
Description |
SevenBitNumber |
velocity |
New default velocity of a note. |
Returns
StepBack()
Moves the current time by the default step back.
Declaration
public PatternBuilder StepBack()
Returns
StepBack(ITimeSpan)
Moves the current time by the specified step back.
Declaration
public PatternBuilder StepBack(ITimeSpan step)
Parameters
Type |
Name |
Description |
ITimeSpan |
step |
Step to move by. |
Returns
Exceptions
StepForward()
Moves the current time by the default step forward.
Declaration
public PatternBuilder StepForward()
Returns
StepForward(ITimeSpan)
Moves the current time by the specified step forward.
Declaration
public PatternBuilder StepForward(ITimeSpan step)
Parameters
Type |
Name |
Description |
ITimeSpan |
step |
Step to move by. |
Returns
Exceptions