Click here to Skip to main content
13,898,253 members
Click here to Skip to main content
Add your own
alternative version

Tagged as

Stats

13.1K views
18 bookmarked
Posted 24 Aug 2017
Licenced CPOL

DryWetMIDI: High-Level Processing of MIDI Files

, 1 Feb 2019
Rate this:
Please Sign up or sign in to vote.
Overview of how to use the DryWetMIDI library for high-level managing of a MIDI file data

Introduction

DryWetMIDI is a .NET library to work with Standard MIDI Files (SMF) – read, write, create and modify them, and also to work with MIDI devices (which is described in DryWetMIDI: Working with MIDI Devices article). In this article we'll discuss processing MIDI files.

Although there are a lot of .NET libraries which provide parsing of MIDI files, there are some features that make the DryWetMIDI special:

  • Ability to read files with some corruptions like missed End of Track event
  • Ability to finely adjust process of reading and writing which allows, for example, to specify Encoding of text stored in text-based meta events like Lyric
  • Set of high-level classes that allow to manage content of a MIDI file in a more understandable way like Note or MusicalTimeSpan

The last point is the most important part of the library since many users ask about managing notes or conversion of MIDI time and length to more human understandable representation like seconds. To solve these problems, they are forced to write the same code again and again. DryWetMIDI frees them from the necessity to reinvent the wheel providing built-in tools to perform described tasks.

This article gives a quick overview of high-level data managing capabilities provided by the DryWetMIDI. It is not an API reference. The library also has a low-level layer of interaction with MIDI file content, but it is not a subject of the article.

There are examples at the end of the article that show how you can solve real tasks using features provided by the DryWetMIDI. So you can move to them right now if you want to get an overall impression of the library.

[^] top

Background

It is recommended to be familiar with SMF format but not necessary if you are going to work only with high-level API of the DryWetMIDI.

[^] top

Contents

  1. Absolute Time
  2. Notes
  3. Chords
  4. Tempo Map
  5. Time and Length Representations
  6. Pattern
  7. Tools
  8. Examples
  9. Links
  10. History

[^] top

Absolute Time

All events inside a MIDI file have a delta-time attached to them. Delta-time is an offset from the previous event. Units of this offset defined by the time division of the file. According to SMF specification, there are two possible time divisions:

  • ticks per quarter note defines amount of time the quarter note lasts (this time division used by 99.999% of all MIDI files so we can assume that all files have it)
  • SMPTE time division defines times as numbers of subdivisions of SMPTE frame along with the specified frame rate

In practice, it is often more convenient to operate by absolute time rather than relative one. DryWetMIDI provides TimedEventsManager class designed to manage events by their absolute times:

using Melanchall.DryWetMidi.Smf;
using Melanchall.DryWetMidi.Smf.Interaction;

// Read a MIDI file
var midiFile = MidiFile.Read("Cool song.mid");

// Manage timed events of the first track chunk of the file
using (TimedEventsManager timedEventsManager = midiFile.Chunks
                                                       .OfType<TrackChunk>()
                                                       .First()
                                                       .ManageTimedEvents())
{
    // Get timed events ordered by time
    TimedEventsCollection events = timedEventsManager.Events;
    
    // Set absolute time of the first Lyric event
    TimedEvent firstLyricEvent = events.Where(e => e.Event is LyricEvent).FirstOrDefault();
    if (firstLyricEvent != null)
        firstLyricEvent.Time = 2000;

    // Add new Pitch Bend event with absolute time = 3000
    events.Add(new TimedEvent(new PitchBendEvent(8000), 3000));
}

After exiting from the using section, all events contained in the managing track chunk will be replaced with ones contained in the events collection updating all delta-times. Also, you can call SaveChanges method of the TimedEventsManager to save all changes. This method is especially useful if you are working with TimedEventsManager across multiple methods:

private TimedEventsManager _timedEventsManager;

private void BeginManageEvents(TrackChunk trackChunk)
{
    _timedEventsManager = trackChunk.ManageTimedEvents();
}

private void ShiftEvents(long shift)
{
    foreach (TimedEvent timedEvent in _timedEventsManager.Events)
    {
        timedEvent.Time += shift;
    }
}

private void EndManageEvents()
{
    _timedEventsManager.SaveChanges(); // or you can call Dispose
}

All other managers described below have the same saving logic.

Also, there are some useful extension methods contained in the TimedEventsManagingUtilities. For example, you can easily remove all System Exclusive events with time of 400 from a file:

midiFile.RemoveTimedEvents(e => e.Event is SysExEvent && e.Time == 400);

Or you can divide times of all events by 2 to shrink a MIDI file:

midiFile.ProcessTimedEvents(e => e.Time /= 2);

Other managers also have utility methods similar to those for timed events. It is worth taking a look at classes where these extensions methods are placed. Also, all managers provided by the DryWetMIDI can be obtained via constructor rather than via utility methods for low-level entities.

[^] top

Notes

To present notes, a MIDI file uses pairs of Note On and Note Off events. But often people want to work with notes without messing with low-level MIDI events. DryWetMIDI provides NotesManager class for this purpose:

using (NotesManager notesManager = midiFile.GetTrackChunks() // shortcut method for
                                                             // Chunks.OfType<TrackChunk>()
                                           .First()
                                           .ManageNotes())
{
    // Get notes ordered by time
    NotesCollection notes = notesManager.Notes;

    // Get all C# notes
    IEnumerable<Note> cSharpNotes = notes.Where(n => n.NoteName == NoteName.CSharp);

    // Reduce length of all C# notes by 100
    foreach (var note in cSharpNotes)
    {
        note.Length -= 100;
    }
    
    // Add new note: C# of octave with number of 2
    // Note: DryWetMIDI uses scientific pitch notation which means middle C is C4
    notes.Add(new Note(NoteName.CSharp, 2)
    {
        Channel = (FourBitNumber)2,
        Velocity = (SevenBitNumber)95
    });
}

As with timed events, there are useful utilities for notes managing which are contained in the NotesManagingUtilities class. One of the most useful ones is GetNotes method that allows to get all notes contained in a track chunk or entire MIDI file:

IEnumerable<Note> notes = midiFile.GetNotes();

Note that if you'll do any changes on returned notes, they will not be applied. All manipulations with notes must be done via NotesManager or you can use ProcessNotes method from the NotesManagingUtilities. For example, to transpose all F notes up by one octave, you can use this code:

f.ProcessNotes(n => n.NoteNumber += (SevenBitNumber)12,
               n => n.NoteName == NoteName.F);

Or you can iterate through collection of TimedEvent and get collection of ITimedObject where an element either Note ot TimedEvent. Note will be returned for every pair of TimedEvent that represent Note On/Note Off events. MakeNotes method does this processing.

[^] top

Chords

Chord is just a group of notes. To work with chords, you need to use ChordsManager class:

using (ChordsManager chordsManager = midiFile.GetTrackChunks()
                                             .First()
                                             .ManageChords(100)) // 100 is a notes tolerance
                                                                 // that defines maximum distance
                                                                 // of notes from the start of the
                                                                 // first note of a chord. Notes
                                                                 // within this tolerance will be
                                                                 // considered as a chord
{
    // Get chords ordered by time
    ChordsCollection chords = chordsManager.Chords;

    // Get all chords that have C# note
    IEnumerable<Chord> cSharpChords = chords.Where(c => c.Notes
                                                         .Any(n => n.NoteName == NoteName.CSharp));
}

You can see that ManageChords (and ChordsManager's constructor) can take notes tolerance that will be taken into an account to build chords. For example, two notes where first one has time of 10 and second one has time of 100 will represent a chord if the tolerance will be greater than or equal to 90. For smaller values of the tolerance, the second note will be out of it and will not fall into the chord. The default tolerance is 0 which means notes must start at the same time to make up a chord.

As with managing of notes, there are utility methods for chords manipulations. No prizes for guessing the name of the class that holds these methods. It is ChordsManagingUtilities.

[^] top

Tempo Map

Tempo map is a list of all changes of the tempo and time signature in a MIDI file. With TempoMapManager, you can set new values of these parameters at the specified time and obtain current tempo map:

using (TempoMapManager tempoMapManager = midiFile.ManageTempoMap())
{
    // Get current tempo map
    TempoMap tempoMap = tempoMapManager.TempoMap;

    // Get time signature at 2000
    TimeSignature timeSignature = tempoMap.TimeSignatureLine.AtTime(2000);

    // Set new tempo (230,000 microseconds per quarter note) at the time of
    // 20 seconds from the start of the file. See "Time representations"
    // section below to learn about time classes
    tempoMapManager.SetTempo(new MetricTimeSpan(0, 0, 20),
                             new Tempo(230000));
}

TempoMap also holds an instance of TimeDivision in order to use it for time and length conversions. To get tempo map of a MIDI file, just call GetTempoMap extension method from the TempoMapManagingUtilities:

TempoMap tempoMap = midiFile.GetTempoMap();

Also, you can easily replace the tempo map of a MIDI file with another one using ReplaceTempoMap method. For example, to change tempo of a file to the 50 BPM and time signature to 5/8, you can write this code:

midiFile.ReplaceTempoMap(TempoMap.Create(Tempo.FromBeatsPerMinute(50),
                                         new TimeSignature(5, 8)));

Tempo map is a very important object since it allows to perform time and length conversions as you'll see in the next sections.

[^] top

Time and Length Representations

As you could notice, all times and lengths in code samples above are presented as some long values in units defined by the time division of a file. In practice, it is much more convenient to operate by "human understandable" representations like seconds or bars/beats. In fact, there is no difference between time and length since time within a MIDI file is just a length that always starts at zero. So we will use the time span term to describe both time and length. DryWetMIDI provides the following classes to represent time span:

  • MetricTimeSpan for time span in terms of microseconds
  • BarBeatTimeSpan for time span in terms of number of bars, beats and ticks
  • MusicalTimeSpan for time span in terms of a fraction of the whole note length
  • MidiTimeSpan exists for unification purposes and simply holds long value in units defined by the time division of a file

All time span classes implement ITimeSpan interface. To convert time span between different representations, you should use TimeConverter or LengthConverter classes:

// Tempo map is needed in order to perform time span conversions

TempoMap tempoMap = midiFile.GetTempoMap();

// =================================================================================================
// Time conversion
// -------------------------------------------------------------------------------------------------
// You can use LengthConverter as well but with the TimeConverter you don't need to specify time
// where time span starts since it is always zero.
// =================================================================================================

// Some time in MIDI ticks (we assume time division of a MIDI file is "ticks per quarter note")
long ticks = 123;

// Convert ticks to metric time
MetricTimeSpan metricTime = TimeConverter.ConvertTo<MetricTimeSpan>(ticks, tempoMap);

// Convert ticks to musical time
MusicalTimeSpan musicalTimeFromTicks = TimeConverter.ConvertTo<MusicalTimeSpan>(ticks, tempoMap);

// Convert metric time to musical time
MusicalTimeSpan musicalTimeFromMetric = TimeConverter.ConvertTo<MusicalTimeSpan>(metricTime, tempoMap);

// Convert metric time to bar/beat time
BarBeatTimeSpan barBeatTimeFromMetric = TimeConverter.ConvertTo<BarBeatTimeSpan>(metricTime, tempoMap);

// Convert musical time back to ticks
long ticksFromMusical = TimeConverter.ConvertFrom(musicalTimeFromTicks, tempoMap);

// =================================================================================================
// Length conversion
// -------------------------------------------------------------------------------------------------
// Length conversion is the same as time conversion but you need to specify the time where
// a time span starts.
// =================================================================================================

// Convert ticks to metric length
MetricTimeSpan metricLength = LengthConverter.ConvertTo<MetricTimeSpan>(ticks, time, tempoMap);

// Convert metric length to musical length using metric time
MusicalTimeSpan musicalLengthFromMetric = LengthConverter.ConvertTo<MusicalTimeSpan>(metricLength,
                                                                                     metricTime,
                                                                                     tempoMap);

// Convert musical length back to ticks
long ticksFromMetricLength = LengthConverter.ConvertFrom(metricLength, time, tempoMap);

You could notice that LengthConverter's methods take a time. In general case, MIDI file has changes of the tempo and time signature. Thus, the same long value can represent different amount of seconds, for example, depending on the time of an object with length of this value. The methods above can take time either as long or as ITimeSpan.

There are some useful methods in the TimedObjectUtilities class. This class contains extension methods for types that implement the ITimedObject interface – TimedEvent, Note and Chord. For example, you can get time of a timed event in hours, minutes, seconds with TimeAs method:

var metricTime = timedEvent.TimeAs<MetricTimeSpan>(tempoMap);

Or you can find all notes of a MIDI file that start at time of 10 bars and 4 beats:

TempoMap tempoMap = midiFile.GetTempoMap();
IEnumerable<Note> notes = midiFile.GetNotes()
                                  .AtTime(new BarBeatTimeSpan(10, 4), tempoMap);

Also, there is the LengthedObjectUtilities class. This class contains extension methods for types that implement the ILengthedObject interface – Note and Chord. For example, you can get length of a note as a fraction of the whole note with LengthAs method:

var musicalLength = note.LengthAs<MusicalTimeSpan>(tempoMap);

Or you can get all notes of a MIDI file that end exactly at 30 seconds from the start of the file:

var tempoMap = midiFile.GetTempoMap();
var notesAt30sec = midiFile.GetNotes()
                           .EndAtTime(new MetricTimeSpan(0, 0, 30), tempoMap);

Some examples of how you can create an instance of specific time span class:

// 100,000 microseconds
var metricTimeSpan1 = new MetricTimeSpan(100000 /* microseconds */);

// 0 hours, 1 minute and 55 seconds
var metricTimeSpan2 = new MetricTimeSpan(0, 1, 55);

// Zero time
var metricTimeSpan3 = new MetricTimeSpan();

// 2 bars and 7 beats
var barBeatTimeSpan = new BarBeatTimeSpan(2, 7);

// Triplet eight
var musicalTimeSpan1 = MusicalTimeSpan.Eighth.Triplet();

// Five 5/17
var musicalTimeSpan2 = 5 * new MusicalTimeSpan(5, 17);

If you want, for example, to know length of a MIDI file in minutes and seconds, you can use this code:

var tempoMap = midiFile.GetTempoMap();
var midiFileDuration = midiFile.GetTimedEvents()
                               .LastOrDefault(e => e.Event is NoteOffEvent)
                              ?.TimeAs<MetricTimeSpan>(tempoMap)
                              ?? new MetricTimeSpan();

ITimeSpan interface has several methods to perform arithmetic operations on time spans. For example, to add metric length to metric time, you can write:

var timeSpan1 = new MetricTimeSpan(0, 2, 20);
var timeSpan2 = new MetricTimeSpan(0, 0, 10);
ITimeSpan result = timeSpan1.Add(timeSpan2, TimeSpanMode.TimeLength);

You need to specify mode of the operation. In the example above, TimeLength is used which means that first time span represents a time and the second one represents a length. This information is needed for conversion engine when operands are of different types. There are also TimeTime and LengthLength modes.

You can also subtract one time span from another one:

var timeSpan1 = new MetricTimeSpan(0, 10, 0);
var timeSpan2 = new MusicalTimeSpan(3, 8);
ITimeSpan result = timeSpan1.Subtract(timeSpan2, TimeSpanMode.TimeTime);

If operands of the same type, result time span will be of this type too. But if you sum or subtract time spans of different types, the type of a result time span will be MathTimeSpan which holds operands along with operation (addition or subtraction) and mode.

To stretch or shrink a time span, use Multiply or Divide methods:

ITimeSpan stretchedTimeSpan = new MetricTimeSpan(0, 0, 10).Multiply(2.5);
ITimeSpan shrinkedTimeSpan = new BarBeatTimeSpan(0, 2).Divide(2);

Both TimeAs and LengthAs methods have non-generic versions where the desired type of result should be passed as an argument of the TimeSpanType type. Also generic methods in TimeConverter and LengthConverter classes have non-generic versions that take TimeSpanType too.

[^] top

Pattern

For purpose of simple MIDI file creation that allows you to focus on the music, there is the PatternBuilder class. This class provides a fluent interface to build a musical composition that can be exported to a MIDI file. A quick example of what you can do with the builder:

var patternBuilder = new PatternBuilder()
     
    // Insert a pause of 5 seconds
    .StepForward(new MetricTimeSpan(0, 0, 5))

    // Insert an eighth C# note of the 4th octave
    .Note(OctaveDefinition.Get(4).CSharp, MusicalTimeSpan.Eighth)

    // Set default note length to triplet eighth and default octave to 5
    .SetNoteLength(MusicalTimeSpan.Eighth.Triplet())
    .SetOctave(5)

    // Now we can add triplet eighth notes of the 5th octave in a simple way
    .Note(NoteName.A)
    .Note(NoteName.B)
    .Note(NoteName.GSharp);

Build method will return an instance of the Pattern class containing all actions performed with the builder. Pattern then can be exported to a MIDI file:

Pattern pattern = patternBuilder.Build();

// Export the pattern to a MIDI file using default tempo map (4/4, 120 BPM)
MidiFile midiFile = pattern.ToFile(TempoMap.Default);

It is only a small part of the PatternBuilder features. It has much more ones including specifying note velocity, inserting of chords, setting time anchors, moving to specific time and repeating previous actions. So the Pattern is a sort of music programming that is bound to MIDI. See example at the end of the article that shows how to build the first four bars of the Beethoven's "Moonlight Sonata".

[^] top

Tools

There are several classes in the DryWetMIDI which can help to solve complex tasks such as notes quantizing. Let's take a short overview of tools aimed to do these tasks.

Notes and Chords Splitter

To split notes or chords, there are NotesSplitter and ChordsSplitter classes respectively which have methods to splitting objects in different ways:

IEnumerable<TObject> SplitByStep(IEnumerable<TObject> objects,
                                 ITimeSpan step,
                                 TempoMap tempoMap)
IEnumerable<TObject> SplitByPartsNumber(IEnumerable<TObject> objects,
                                        int partsNumber,
                                        TimeSpanType lengthType,
                                        TempoMap tempoMap)
IEnumerable<TObject> SplitByGrid(IEnumerable<TObject> objects,
                                 IGrid grid,
                                 TempoMap tempoMap)
IEnumerable<TObject> SplitAtDistance(IEnumerable<TObject> objects,
                                     ITimeSpan distance,
                                     LengthedObjectTarget from,
                                     TempoMap tempoMap)
IEnumerable<TObject> SplitAtDistance(IEnumerable<TObject> objects,
                                     double ratio,
                                     TimeSpanType lengthType,
                                     LengthedObjectTarget from,
                                     TempoMap tempoMap)

Also, there are useful methods in NotesSplitterUtilities and ChordsSplitterUtilities classes which allow to split objects inside TrackChunk or MidiFile without the necessity to work with collection of notes or chords directly.

Visit Notes and chords splitter page of the library Wiki to learn more.

Quantizer

To quantize timed events, notes or chords there are TimedEventsQuantizer, NotesQuantizer and ChordsQuantizer classes respectively. All these classes have Quantize method that do the job. Notes and chords can be quantized either by start time or by end time.

Also, there are TimedEventsQuantizerUtilities, NotesQuantizerUtilities and ChordsQuantizerUtilities classes that contain useful methods to quantize objects inside TrackChunk and MidiFile without the necessity to work with collection of timed events, notes or chords directly.

Visit Quantizer page of the library Wiki to learn more.

Randomizer

To randomize time of timed events, notes and chords, there are TimedEventsRandomizer, NotesRandomizer and ChordsRandomizer classes. All these classes have Randomize method that do the job. Similar to quantizing, notes and chords can be randomized either by start time or by end time.

Visit Randomizer page of the library Wiki to learn more.

Notes Merger

To merge nearby notes into one DryWetMIDI provides NotesMerger class which contains the method:

IEnumerable<Note> Merge(IEnumerable<Note> notes,
                        TempoMap tempoMap,
                        NotesMergingSettings settings = null)

Example of usage:

var notes = midiFile.GetNotes().Where(n => n.NoteName == NoteName.CSharp);
var tempoMap = midiFile.GetTempoMap();

var notesMerger = new NotesMerger();
notesMerger.Merge(notes,
                  tempoMap,
                  new NotesMergingSettings
                  {
                      Tolerance = new MetricTimeSpan(0, 0, 1)
                  });

This code can be rewritten with using of method from NotesMergerUtilities:

midiFile.MergeNotes(new NotesMergingSettings
                    {
                        Tolerance = new MetricTimeSpan(0, 0, 1)
                    },
                    n => n.NoteName == NoteName.CSharp);

Visit Notes merger page of the library Wiki to learn more.

MIDI File Splitter

You can split MIDI file in different ways using extension methods from the MidiFileSplitter class. Available methods of splitting:

IEnumerable<MidiFile> SplitByChannel(this MidiFile midiFile)
IEnumerable<MidiFile> SplitByNotes(this MidiFile midiFile)
IEnumerable<MidiFile> SplitByGrid(this MidiFile midiFile,
                                  IGrid grid,
                                  SplittingMidiFileByGridSettings settings = null)

Visit MIDI file splitter page of the library Wiki to learn more.

CSV Converter

DryWetMIDI provides a way to convert MIDI objects to CSV representation and read them back. CSV allows you to edit MIDI data, for example, via Microsoft Excel or to write it to database. CsvConverter class performs such conversions. Let's see what methods it provides:

void ConvertMidiFileToCsv(MidiFile midiFile,
                          string filePath,
                          bool overwriteFile = false,
                          MidiFileCsvConversionSettings settings = null)
MidiFile ConvertCsvToMidiFile(string filePath,
                              MidiFileCsvConversionSettings settings = null)
void ConvertNotesToCsv(IEnumerable<Note> notes,
                       string filePath,
                       TempoMap tempoMap,
                       bool overwriteFile = false,
                       NoteCsvConversionSettings settings = null)
IEnumerable<Note> ConvertCsvToNotes(string filePath,
                                    TempoMap tempoMap,
                                    NoteCsvConversionSettings settings = null)

All these methods also has overloads to read from/write to a stream instead of file defined by filePath parameter.

Visit CSV converter page of the library Wiki to learn more.

[^] top

Examples

Let's see how you can use the DryWetMIDI for some real tasks.

Notes merging

Sometimes, in real MIDI files, notes can overlap each other. The following method merges overlapping notes of the same channel and pitch:

public static void MergeNotes(MidiFile midiFile)
{
    foreach (var trackChunk in midiFile.GetTrackChunks())
    {
        MergeNotes(trackChunk);
    }
}

private static void MergeNotes(TrackChunk trackChunk)
{
    using (var notesManager = trackChunk.ManageNotes())
    {
        var notes = notesManager.Notes;

        // Create dictionary for storing currently merging notes of each channel (key)
        // and each pitch (key of dictionary used as value for channel)
        var currentNotes = new Dictionary<FourBitNumber, Dictionary<SevenBitNumber, Note>>();

        foreach (var note in notes.ToList())
        {
            var channel = note.Channel;

            // Get currently merging notes of the channel
            if (!currentNotes.TryGetValue(channel, out var currentNotesByNoteNumber))
                currentNotes.Add(channel, currentNotesByNoteNumber =
                                              new Dictionary<SevenBitNumber, Note>());

            // Get the currently merging note
            if (!currentNotesByNoteNumber.TryGetValue(note.NoteNumber, out var currentNote))
            {
                currentNotesByNoteNumber.Add(note.NoteNumber, currentNote = note);
                continue;
            }

            var currentEndTime = currentNote.Time + currentNote.Length;

            // If time of the note is less than end of currently merging one,
            // we should update length of currently merging note and delete the
            // note from the notes collection
            if (note.Time <= currentEndTime)
            {
                var endTime = Math.Max(note.Time + note.Length, currentEndTime);
                currentNote.Length = endTime - currentNote.Time;

                notes.Remove(note);
            }

            // If the note doesn't overlap currently merging one, the note become
            // a currently merging note
            else
                currentNotesByNoteNumber[note.NoteNumber] = note;
        }
    }
}

If we take input file like that:

and perform merging with the method above, we'll get the following result:

MergeNotes(midiFile);

[^] top

Building of "Moonlight Sonata"

This example shows how you can use Pattern to build a musical composition. First, four bars of the Beethoven's "Moonlight Sonata" will help us with this.

using Melanchall.DryWetMidi.MusicTheory;

public static Pattern BuildMoonlightSonata()
{
    // Define a chord for bass part which is just an octave
    var bassChord = new[] { Interval.Twelve };

    // Build the composition
    return 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(Intervaln.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();
}

[^] top

Links

[^] top

History

  • 2nd February, 2019
    • Article updated to reflect changes introduced in the DryWetMIDI 4.0.0: new methods and classes
  • 11th June, 2018
    • Article updated to reflect changes introduced in the DryWetMIDI 3.0.0: new methods and classes
  • 7th November, 2017
    • Article updated to reflect changes introduced in the DryWetMIDI 2.0.0: time and length classes were generalized with ITimeSpan
  • 24th August, 2017
    • Article submitted

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Share

About the Author

Maxim Dobroselsky
Software Developer
Russian Federation Russian Federation
My primary skills are C#, WPF and ArcObjects/ArcGIS Pro SDK. Currently I'm working on autotests in Kaspersky Lab.

Also I'm writing music which led me to starting the DryWetMIDI project on the GitHub. DryWetMIDI is an open source .NET library written in C# for managing MIDI files. The library is currently actively developing.

Also I actively help people on Code Review Stack Exchange to improve their C# code and have some answers on WPF related questions on Stack Overflow.

You may also be interested in...

Pro

Comments and Discussions

 
QuestionProblem with creating your midi file example Pin
Member 1130758114-Feb-19 19:43
memberMember 1130758114-Feb-19 19:43 
AnswerRe: Problem with creating your midi file example Pin
Member 1130758114-Feb-19 20:41
memberMember 1130758114-Feb-19 20:41 
QuestionDo You Have a full example of writing a midi file? Pin
Member 1354659220-Dec-17 2:20
memberMember 1354659220-Dec-17 2:20 
AnswerRe: Do You Have a full example of writing a midi file? Pin
Maxim Dobroselsky24-Dec-17 2:17
memberMaxim Dobroselsky24-Dec-17 2:17 
QuestionI Got following errors how to resolve it Pin
htmlmaster18-Sep-17 22:51
professionalhtmlmaster18-Sep-17 22:51 
AnswerRe: I Got following errors how to resolve it Pin
Maxim Dobroselsky21-Sep-17 0:27
memberMaxim Dobroselsky21-Sep-17 0:27 
GeneralRe: I Got following errors how to resolve it Pin
htmlmaster22-Sep-17 4:01
professionalhtmlmaster22-Sep-17 4:01 
GeneralRe: I Got following errors how to resolve it Pin
Maxim Dobroselsky22-Sep-17 4:34
memberMaxim Dobroselsky22-Sep-17 4:34 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Cookies | Terms of Use | Mobile
Web03 | 2.8.190306.1 | Last Updated 2 Feb 2019
Article Copyright 2017 by Maxim Dobroselsky
Everything else Copyright © CodeProject, 1999-2019
Layout: fixed | fluid