{"id":2703,"date":"2023-01-15T18:20:47","date_gmt":"2023-01-15T18:20:47","guid":{"rendered":"https:\/\/inspiredtoeducate.net\/inspiredtoeducate\/?p=2703"},"modified":"2023-03-04T19:50:35","modified_gmt":"2023-03-04T19:50:35","slug":"make-music-with-code-using-dotnet-core","status":"publish","type":"post","link":"http:\/\/inspiredtoeducate.net\/inspiredtoeducate\/make-music-with-code-using-dotnet-core\/","title":{"rendered":"Make music with code using DotNet Core"},"content":{"rendered":"\n<!-- Facebook Like Button v1.9.6 BEGIN [http:\/\/blog.bottomlessinc.com] -->\n<iframe src=\"http:\/\/www.facebook.com\/plugins\/like.php?href=http%3A%2F%2Finspiredtoeducate.net%2Finspiredtoeducate%2Fmake-music-with-code-using-dotnet-core%2F&amp;layout=standard&amp;show_faces=false&amp;width=450&amp;action=like&amp;colorscheme=light\" scrolling=\"no\" frameborder=\"0\" allowTransparency=\"true\" style=\"border:none; overflow:hidden; width:450px; height: 30px; align: left; margin: 2px 0px 2px 0px\"><\/iframe>\n<!-- Facebook Like Button END -->\n<p>Curious about making music with code? As a software engineer and music guy, I have enjoyed seeing the connections between music and computers.   The first computer programmer, Ada Lovelace predicted that computers will move beyond doing boring math problems into the world of creative arts.   If a problem can be converted to a system of symbols, she reasoned that computers could help.  She used music as her example.<\/p>\n<p>In previous experiments, I have explored the ideas of code and music using TypeScript, NodeJs, and Angular.  You can find this work here.<\/p>\n<ul>\n<li><a class=\"wp-editor-md-post-content-link\" href=\"https:\/\/github.com\/michaelprosario\/ng-music-maker\">NG-Music-Maker<\/a><\/li>\n<\/ul>\n<p>After looking around GitHub, I found a really cool music library for C# devs. I&#8217;m hoping to use it to create tools to make quick backup tracks for practicing improv. It&#8217;s just fun to explore electronic music, theory, and computational music.  Make sure to check out the blog post by Maxim. ( the author of DryWetMidi )  It&#8217;s a pretty comprehensive guide to his library.<\/p>\n<ul>\n<li><a class=\"wp-editor-md-post-content-link\" href=\"https:\/\/github.com\/melanchall\/drywetmidi\">DryWetMidi<\/a><\/li>\n<li><a class=\"wp-editor-md-post-content-link\" href=\"https:\/\/www.codeproject.com\/Articles\/1200014\/DryWetMIDI-High-level-processing-of-MIDI-files\">DryWetMIDI High level-processing of-MIDI files<\/a><\/li>\n<\/ul>\n<h2>What is MIDI?<\/h2>\n<p>MIDI stands for musical instrument digital interface.  (MIDI)  Under a file format like WAV or MP3, the computer stores the raw wave form data about sound.  The MIDI file format and protocols operate an conceptual layer of music data.  You can think of a MIDI file having many tracks.   You can assign different instruments ( sounds to tracks ).   In each track, the musician can record songs as many events.   MIDI music events might include turning a note on, turning a note off, engaging the sustain pedal, and changing tempo.  MIDI music software like Garage band, Cakewalk and Bandlab can send the MIDI event data to a software synth which interprets the events into sound.  In general, the MIDI event paradigm can be extended to support other things like lighting, lyrics, and other stuff.<\/p>\n<h2>DryWetMidi Features<\/h2>\n<ul>\n<li>Writing MIDI files: For my experiments, I have used DryWetMIDI to explore projects for making drum machines and arpeggio makers.   I&#8217;m really curious about using computers to generate the skeleton of songs.  Can computers generate a template for a POP song, a fiddle tune, or a ballad?  We&#8217;re about to find out!  DryWetMIDI provides a lower level API for raw MIDI event data.  The higher level &#8220;Pattern&#8221; and &#8220;PatternBuilder&#8221; APIs enable coders to think of expressing a single thread of musical ideas.  Let&#8217;s say you&#8217;re trying to describe a piece for a string quartet.  The &#8220;PatternBuilder&#8221; API enables you to use a fluent syntax to describe the notes played by the cello player.  While playing with this API, I have to say that I loved the ability to combine musical patterns.  The framework can stack or combine musical patterns into a single pattern. Let&#8217;s say you have three violin parts in 3 patterns.  The library enables you to blend those patterns into a single idea with one line of code.  Maxim showed great care in designing these APIs.<\/li>\n<li>Music theory tools: The framework provides good concepts for working for notes, intervals, chords and other fundamental concepts of music.<\/li>\n<li>Reading MIDI files: The early examples show that DryWetMIDI can read MIDI files well. I&#8217;ve seen some utility functions that enable you to dump MIDI files to CSVs to support debugging.  The documentation hints at a chord extraction API that looks really cool.  Looking forward to testing this.<\/li>\n<li>Device interaction:   DryWetMIDI enables makers to send MIDI events and receive them. This capability might become helpful if you&#8217;re making a music tutor app.  You can use the music device interaction API to watch note events.   The system can provide feedback to the player if they&#8217;re playing the right notes at the appropriate time.<\/li>\n<\/ul>\n<h2>Visions for MusicMaker.NET for .NET Core<\/h2>\n<p>In the following code example, I&#8217;ve built an API to describe drum patterns using strings.<br \/>\nThe strings represent sound at a resolution of 16th notes. Using the &#8220;MakeDrumTrack&#8221; service,<br \/>\nwe can quickly express patterns of percussion.<\/p>\n<pre><code class=\"language-csharp \">IMidiServices midiServices = new MidiServices();\nvar service = new MakeDrumTrackService(midiServices);\nvar command = new MakeDrumTrackCommand\n{\n    BeatsPerMinute = 50,\n    FileName = fileName,\n    Tracks = new List&lt;DrumTrackRow&gt;\n    {\n        new()\n        {\n            Pattern = \"x-x-|x-x-|x-x-|x-x-|x-x-|x-x-|x-x-|x-x-|\",\n            InstrumentNumber = DrumConstants.HiHat\n        },\n        new()\n        {\n            Pattern = \"x---|----|x---|----|x---|----|x---|----|\",\n            InstrumentNumber = DrumConstants.AcousticBassDrum\n        },\n        new()\n        {\n            Pattern = \"----|x---|----|x--x|----|x---|----|x--x|\",\n            InstrumentNumber = DrumConstants.AcousticSnare\n        },\n        new()\n        {\n            Pattern = \"-x-x|x-x-|-x-x|x--x|-xx-|xx--|-xx-|x--x|\",\n            InstrumentNumber = DrumConstants.HiBongo\n        }\n    },\n    UserId = \"system\"\n};\n\n\/\/ act\nvar response = service.MakeDrumTrack(command);\n<\/code><\/pre>\n<p>Using the ArpeggioPlayer service, we&#8217;ll be able to render out a small fragement of music given a list of chords and an arpeggio spec.<\/p>\n<pre><code class=\"language-csharp \">var tempo = 180;\nvar instrument = (byte)Instruments.AcousticGrandPiano;\nvar channel = 1;\n\nvar track = new ChordPlayerTrack(instrument, channel, tempo);\nvar command = ArpeggioPatternCommandFactory.MakeArpeggioPatternCommand1();\nvar player = new ArpeggioPlayer(track, );\nvar chordChanges = GetChords1();  \/\/ Am | G | F | E\n\nplayer.PlayFromChordChanges(chordChanges);\n\n\/\/ Write MIDI file with DryWetMIDI\nvar midiFile = new MidiFile();\nmidiFile.Chunks.Add(track.MakeTrackChunk());\nmidiFile.Write(\"arp1.mid\", true);\n<\/code><\/pre>\n<p>In the following method, the maker can describe the arpeggio patterns using ASCII art strings. The arpeggio patterns operate at resolution of sixteenth notes.  This works fine for most POP or eletronic music. In future work, we can build web apps or mobile UX to enable the user to design the arpeggio patterns or drum patterns.<\/p>\n<pre><code class=\"language-csharp \">public static MakeArpeggioPatternCommand MakeArpeggioPatternCommand1()\n{\n    var command = new MakeArpeggioPatternCommand\n    {\n        Pattern = new ArpeggioPattern\n        {\n            Rows = new List&lt;ArpeggioPatternRow&gt;\n            {\n                new() { Type = ArpeggioPatternRowType.Fifth, Octave = 2, Pattern = \"----|----|----|---s|\" },\n                new() { Type = ArpeggioPatternRowType.Third, Octave = 2, Pattern = \"----|--s-|s---|s---|\" },\n                new() { Type = ArpeggioPatternRowType.Root, Octave = 2, Pattern =  \"---s|-s-s|---s|-s--|\" },\n                new() { Type = ArpeggioPatternRowType.Fifth, Octave = 1, Pattern = \"--s-|s---|--s-|--s-|\" },\n                new() { Type = ArpeggioPatternRowType.Third, Octave = 1, Pattern = \"-s--|----|-s--|----|\" },\n                new() { Type = ArpeggioPatternRowType.Root, Octave = 1, Pattern =  \"s---|----|s---|----|\" }\n            },\n            InstrumentNumber = Instruments.Banjo\n        },\n        UserId = \"mrosario\",\n        BeatsPerMinute = 120,\n        Channel = 0\n    };\n    return command;\n}\n<\/code><\/pre>\n<p>The previous code sample writes out a music fragment like the following.<br \/>\n<img src=\"http:\/\/inspiredtoeducate.net\/inspiredtoeducate\/wp-content\/uploads\/2023\/01\/arp1.png\" alt=\"\" \/><\/p>\n<p>If you&#8217;re interested in following my work here, check out the following repo.<\/p>\n<ul>\n<li><a class=\"wp-editor-md-post-content-link\" href=\"https:\/\/github.com\/michaelprosario\/music-maker\">MusicMaker for DotNet Core<\/a><\/li>\n<\/ul>\n\n<!-- Facebook Like Button v1.9.6 BEGIN [http:\/\/blog.bottomlessinc.com] -->\n<iframe src=\"http:\/\/www.facebook.com\/plugins\/like.php?href=http%3A%2F%2Finspiredtoeducate.net%2Finspiredtoeducate%2Fmake-music-with-code-using-dotnet-core%2F&amp;layout=standard&amp;show_faces=false&amp;width=450&amp;action=like&amp;colorscheme=light\" scrolling=\"no\" frameborder=\"0\" allowTransparency=\"true\" style=\"border:none; overflow:hidden; width:450px; height: 30px; align: left; margin: 2px 0px 2px 0px\"><\/iframe>\n<!-- Facebook Like Button END -->\n","protected":false},"excerpt":{"rendered":"<p>Curious about making music with code? As a software engineer and music guy, I have enjoyed seeing the connections between music and computers. The first computer programmer, Ada Lovelace predicted that computers will move beyond doing boring math problems into the world of creative arts. If a problem can be [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":2521,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":[],"categories":[19,16,23],"tags":[],"_links":{"self":[{"href":"http:\/\/inspiredtoeducate.net\/inspiredtoeducate\/wp-json\/wp\/v2\/posts\/2703"}],"collection":[{"href":"http:\/\/inspiredtoeducate.net\/inspiredtoeducate\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/inspiredtoeducate.net\/inspiredtoeducate\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/inspiredtoeducate.net\/inspiredtoeducate\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/inspiredtoeducate.net\/inspiredtoeducate\/wp-json\/wp\/v2\/comments?post=2703"}],"version-history":[{"count":3,"href":"http:\/\/inspiredtoeducate.net\/inspiredtoeducate\/wp-json\/wp\/v2\/posts\/2703\/revisions"}],"predecessor-version":[{"id":2715,"href":"http:\/\/inspiredtoeducate.net\/inspiredtoeducate\/wp-json\/wp\/v2\/posts\/2703\/revisions\/2715"}],"wp:featuredmedia":[{"embeddable":true,"href":"http:\/\/inspiredtoeducate.net\/inspiredtoeducate\/wp-json\/wp\/v2\/media\/2521"}],"wp:attachment":[{"href":"http:\/\/inspiredtoeducate.net\/inspiredtoeducate\/wp-json\/wp\/v2\/media?parent=2703"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/inspiredtoeducate.net\/inspiredtoeducate\/wp-json\/wp\/v2\/categories?post=2703"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/inspiredtoeducate.net\/inspiredtoeducate\/wp-json\/wp\/v2\/tags?post=2703"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}