{"id":5350,"date":"2023-02-15T11:24:44","date_gmt":"2023-02-15T10:24:44","guid":{"rendered":"https:\/\/www.pschatzmann.ch\/home\/?p=5350"},"modified":"2023-02-15T11:58:08","modified_gmt":"2023-02-15T10:58:08","slug":"a-simple-midi-file-parser-not-only-for-arduino","status":"publish","type":"post","link":"https:\/\/www.pschatzmann.ch\/home\/2023\/02\/15\/a-simple-midi-file-parser-not-only-for-arduino\/","title":{"rendered":"A simple Midi File Parser (not only) for Arduino"},"content":{"rendered":"<p>I was looking for a simple <strong>midi file parser<\/strong> library, but I did not find anything that made me happy, so I decided to <a href=\"https:\/\/github.com\/pschatzmann\/arduino-midi-fileparser\">provide one<\/a> myself. Here are my design goals:<\/p>\n<ul>\n<li><strong>minimal RAM<\/strong> requirements for midi with 1 track<\/li>\n<li><strong>incremental<\/strong> writing and parsing of data<\/li>\n<li><strong>No external dependencies<\/strong>: compile and run outside of Arduino with cmake<\/li>\n<li>Using the <strong>Arduino Print<\/strong> API for writing data <\/li>\n<li>For processors with a lot of RAM: provide some <strong>multitrack support<\/strong><\/li>\n<\/ul>\n<p>I did not want to start from scratch, so I based my work on the <a href=\"https:\/\/github.com\/abique\/midi-parser\">Midi Parser from Alexandre Bique<\/a>.<\/p>\n<h2>Arduino Example Sketch<\/h2>\n<p>Here is an example sketch that shows how to parse a midi file. To reduce the number of lines of code I was using my AudioTools library, but you can use this library w\/o it.<\/p>\n<pre><code>#include \"AudioTools.h\" \/\/ https:\/\/github.com\/pschatzmann\/arduino-audio-tools\n#include \"MidiFileParserMultiTrack.h\"\n#include \"examples\/example-midi.h\"\n\nusing namespace midi;\n\nconst int write_size = 256;\nMidiFileParser parser;  \/\/ Midi parser\nMemoryStream midi_data(example_mid, example_mid_len); \/\/ Midi Data\nStreamCopy copy(parser, midi_data, write_size); \/\/ Copy data to parser\nbool debug = false;\n\nvoid setup() {\n  Serial.begin(115200);\n  parser.begin(debug, 256 * 5);\n}\n\nvoid loop() {\n  \/\/ Try to keep the parser filled\n  if (parser.availableForWrite() &gt; write_size) {\n    copy.copy();\n  }\n\n  \/\/ Parse midi\n  auto state = parser.parse(); \/\/ parseTimed() or parse();\n\n  \/\/ Process Result\n  switch (state.status)\n  case MIDI_PARSER_TRACK_MIDI: {\n    \/\/ process midi\n    char msg[100];\n    sprintf(msg, \"process  time-cumulated ms: %ld status: %d [%s]   channel: %d  param1: %d   param2: %d\",\n            (long)state.timeInMs(), state.midi.status,\n            parser.midi_status_name(state.midi.status), state.midi.channel,\n            state.midi.param1, state.midi.param2);\n    Serial.println(msg);\n    break;\n  case MIDI_PARSER_ERROR:\n    Serial.println(\"Error\\n\");\n  case MIDI_PARSER_EOB:\n    stop();\n    break;\n  default:\n    break;\n  }\n}\n<\/code><\/pre>\n<p>The parse() method is returning immediately while the parseTimed() returns the result only after taking the timing into consideration.<\/p>\n<p>This (potentially updated) <a href=\"https:\/\/github.com\/pschatzmann\/arduino-midi-fileparser\/blob\/main\/examples\/midi-parse-using-audiotools\/midi-parse-using-audiotools.ino\">example can be found on Github<\/a>. There you can also find <a href=\"https:\/\/github.com\/pschatzmann\/arduino-midi-fileparser\/blob\/main\/examples\">more examples<\/a>!<\/p>\n<p>And last but not least: here is a <a href=\"https:\/\/pschatzmann.github.io\/arduino-midi-fileparser\/docs\/html\/group__midifile.html\">link to the class documentation<\/a>.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>I was looking for a simple midi file parser library, but I did not find anything that made me happy, so I decided to provide one myself. Here are my design goals: minimal RAM requirements for midi with 1 track incremental writing and parsing of data No external dependencies: compile [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":2000,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_crdt_document":"","_import_markdown_pro_load_document_selector":0,"_import_markdown_pro_submit_text_textarea":"","_exactmetrics_skip_tracking":false,"_exactmetrics_sitenote_active":false,"_exactmetrics_sitenote_note":"","_exactmetrics_sitenote_category":0,"footnotes":""},"categories":[20,22],"tags":[],"class_list":["post-5350","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-arduino","category-machine-sound"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v27.5 - https:\/\/yoast.com\/product\/yoast-seo-wordpress\/ -->\n<title>A simple Midi File Parser (not only) for Arduino - Phil Schatzmann<\/title>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/www.pschatzmann.ch\/home\/2023\/02\/15\/a-simple-midi-file-parser-not-only-for-arduino\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"A simple Midi File Parser (not only) for Arduino - Phil Schatzmann\" \/>\n<meta property=\"og:description\" content=\"I was looking for a simple midi file parser library, but I did not find anything that made me happy, so I decided to provide one myself. Here are my design goals: minimal RAM requirements for midi with 1 track incremental writing and parsing of data No external dependencies: compile [&hellip;]\" \/>\n<meta property=\"og:url\" content=\"https:\/\/www.pschatzmann.ch\/home\/2023\/02\/15\/a-simple-midi-file-parser-not-only-for-arduino\/\" \/>\n<meta property=\"og:site_name\" content=\"Phil Schatzmann\" \/>\n<meta property=\"article:published_time\" content=\"2023-02-15T10:24:44+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2023-02-15T10:58:08+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/www.pschatzmann.ch\/wp-content\/uploads\/2020\/09\/midi.png\" \/>\n\t<meta property=\"og:image:width\" content=\"1280\" \/>\n\t<meta property=\"og:image:height\" content=\"582\" \/>\n\t<meta property=\"og:image:type\" content=\"image\/png\" \/>\n<meta name=\"author\" content=\"pschatzmann\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"pschatzmann\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"2 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\\\/\\\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\\\/\\\/www.pschatzmann.ch\\\/home\\\/2023\\\/02\\\/15\\\/a-simple-midi-file-parser-not-only-for-arduino\\\/#article\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/www.pschatzmann.ch\\\/home\\\/2023\\\/02\\\/15\\\/a-simple-midi-file-parser-not-only-for-arduino\\\/\"},\"author\":{\"name\":\"pschatzmann\",\"@id\":\"https:\\\/\\\/www.pschatzmann.ch\\\/home\\\/#\\\/schema\\\/person\\\/73a53638a4e34e8373405fd737dac9b1\"},\"headline\":\"A simple Midi File Parser (not only) for Arduino\",\"datePublished\":\"2023-02-15T10:24:44+00:00\",\"dateModified\":\"2023-02-15T10:58:08+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\\\/\\\/www.pschatzmann.ch\\\/home\\\/2023\\\/02\\\/15\\\/a-simple-midi-file-parser-not-only-for-arduino\\\/\"},\"wordCount\":191,\"commentCount\":0,\"publisher\":{\"@id\":\"https:\\\/\\\/www.pschatzmann.ch\\\/home\\\/#\\\/schema\\\/person\\\/73a53638a4e34e8373405fd737dac9b1\"},\"image\":{\"@id\":\"https:\\\/\\\/www.pschatzmann.ch\\\/home\\\/2023\\\/02\\\/15\\\/a-simple-midi-file-parser-not-only-for-arduino\\\/#primaryimage\"},\"thumbnailUrl\":\"https:\\\/\\\/www.pschatzmann.ch\\\/wp-content\\\/uploads\\\/2020\\\/09\\\/midi.png\",\"articleSection\":[\"Arduino\",\"Machine Sound\"],\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"CommentAction\",\"name\":\"Comment\",\"target\":[\"https:\\\/\\\/www.pschatzmann.ch\\\/home\\\/2023\\\/02\\\/15\\\/a-simple-midi-file-parser-not-only-for-arduino\\\/#respond\"]}]},{\"@type\":\"WebPage\",\"@id\":\"https:\\\/\\\/www.pschatzmann.ch\\\/home\\\/2023\\\/02\\\/15\\\/a-simple-midi-file-parser-not-only-for-arduino\\\/\",\"url\":\"https:\\\/\\\/www.pschatzmann.ch\\\/home\\\/2023\\\/02\\\/15\\\/a-simple-midi-file-parser-not-only-for-arduino\\\/\",\"name\":\"A simple Midi File Parser (not only) for Arduino - Phil Schatzmann\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/www.pschatzmann.ch\\\/home\\\/#website\"},\"primaryImageOfPage\":{\"@id\":\"https:\\\/\\\/www.pschatzmann.ch\\\/home\\\/2023\\\/02\\\/15\\\/a-simple-midi-file-parser-not-only-for-arduino\\\/#primaryimage\"},\"image\":{\"@id\":\"https:\\\/\\\/www.pschatzmann.ch\\\/home\\\/2023\\\/02\\\/15\\\/a-simple-midi-file-parser-not-only-for-arduino\\\/#primaryimage\"},\"thumbnailUrl\":\"https:\\\/\\\/www.pschatzmann.ch\\\/wp-content\\\/uploads\\\/2020\\\/09\\\/midi.png\",\"datePublished\":\"2023-02-15T10:24:44+00:00\",\"dateModified\":\"2023-02-15T10:58:08+00:00\",\"breadcrumb\":{\"@id\":\"https:\\\/\\\/www.pschatzmann.ch\\\/home\\\/2023\\\/02\\\/15\\\/a-simple-midi-file-parser-not-only-for-arduino\\\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\\\/\\\/www.pschatzmann.ch\\\/home\\\/2023\\\/02\\\/15\\\/a-simple-midi-file-parser-not-only-for-arduino\\\/\"]}]},{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\\\/\\\/www.pschatzmann.ch\\\/home\\\/2023\\\/02\\\/15\\\/a-simple-midi-file-parser-not-only-for-arduino\\\/#primaryimage\",\"url\":\"https:\\\/\\\/www.pschatzmann.ch\\\/wp-content\\\/uploads\\\/2020\\\/09\\\/midi.png\",\"contentUrl\":\"https:\\\/\\\/www.pschatzmann.ch\\\/wp-content\\\/uploads\\\/2020\\\/09\\\/midi.png\",\"width\":1280,\"height\":582},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\\\/\\\/www.pschatzmann.ch\\\/home\\\/2023\\\/02\\\/15\\\/a-simple-midi-file-parser-not-only-for-arduino\\\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\\\/\\\/www.pschatzmann.ch\\\/home\\\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"A simple Midi File Parser (not only) for Arduino\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\\\/\\\/www.pschatzmann.ch\\\/home\\\/#website\",\"url\":\"https:\\\/\\\/www.pschatzmann.ch\\\/home\\\/\",\"name\":\"Phil Schatzmann Consulting\",\"description\":\"\",\"publisher\":{\"@id\":\"https:\\\/\\\/www.pschatzmann.ch\\\/home\\\/#\\\/schema\\\/person\\\/73a53638a4e34e8373405fd737dac9b1\"},\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\\\/\\\/www.pschatzmann.ch\\\/home\\\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":[\"Person\",\"Organization\"],\"@id\":\"https:\\\/\\\/www.pschatzmann.ch\\\/home\\\/#\\\/schema\\\/person\\\/73a53638a4e34e8373405fd737dac9b1\",\"name\":\"pschatzmann\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\\\/\\\/www.pschatzmann.ch\\\/wp-content\\\/uploads\\\/2022\\\/08\\\/pschatzmann.png\",\"url\":\"https:\\\/\\\/www.pschatzmann.ch\\\/wp-content\\\/uploads\\\/2022\\\/08\\\/pschatzmann.png\",\"contentUrl\":\"https:\\\/\\\/www.pschatzmann.ch\\\/wp-content\\\/uploads\\\/2022\\\/08\\\/pschatzmann.png\",\"width\":305,\"height\":305,\"caption\":\"pschatzmann\"},\"logo\":{\"@id\":\"https:\\\/\\\/www.pschatzmann.ch\\\/wp-content\\\/uploads\\\/2022\\\/08\\\/pschatzmann.png\"}}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"A simple Midi File Parser (not only) for Arduino - Phil Schatzmann","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/www.pschatzmann.ch\/home\/2023\/02\/15\/a-simple-midi-file-parser-not-only-for-arduino\/","og_locale":"en_US","og_type":"article","og_title":"A simple Midi File Parser (not only) for Arduino - Phil Schatzmann","og_description":"I was looking for a simple midi file parser library, but I did not find anything that made me happy, so I decided to provide one myself. Here are my design goals: minimal RAM requirements for midi with 1 track incremental writing and parsing of data No external dependencies: compile [&hellip;]","og_url":"https:\/\/www.pschatzmann.ch\/home\/2023\/02\/15\/a-simple-midi-file-parser-not-only-for-arduino\/","og_site_name":"Phil Schatzmann","article_published_time":"2023-02-15T10:24:44+00:00","article_modified_time":"2023-02-15T10:58:08+00:00","og_image":[{"width":1280,"height":582,"url":"https:\/\/www.pschatzmann.ch\/wp-content\/uploads\/2020\/09\/midi.png","type":"image\/png"}],"author":"pschatzmann","twitter_card":"summary_large_image","twitter_misc":{"Written by":"pschatzmann","Est. reading time":"2 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/www.pschatzmann.ch\/home\/2023\/02\/15\/a-simple-midi-file-parser-not-only-for-arduino\/#article","isPartOf":{"@id":"https:\/\/www.pschatzmann.ch\/home\/2023\/02\/15\/a-simple-midi-file-parser-not-only-for-arduino\/"},"author":{"name":"pschatzmann","@id":"https:\/\/www.pschatzmann.ch\/home\/#\/schema\/person\/73a53638a4e34e8373405fd737dac9b1"},"headline":"A simple Midi File Parser (not only) for Arduino","datePublished":"2023-02-15T10:24:44+00:00","dateModified":"2023-02-15T10:58:08+00:00","mainEntityOfPage":{"@id":"https:\/\/www.pschatzmann.ch\/home\/2023\/02\/15\/a-simple-midi-file-parser-not-only-for-arduino\/"},"wordCount":191,"commentCount":0,"publisher":{"@id":"https:\/\/www.pschatzmann.ch\/home\/#\/schema\/person\/73a53638a4e34e8373405fd737dac9b1"},"image":{"@id":"https:\/\/www.pschatzmann.ch\/home\/2023\/02\/15\/a-simple-midi-file-parser-not-only-for-arduino\/#primaryimage"},"thumbnailUrl":"https:\/\/www.pschatzmann.ch\/wp-content\/uploads\/2020\/09\/midi.png","articleSection":["Arduino","Machine Sound"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/www.pschatzmann.ch\/home\/2023\/02\/15\/a-simple-midi-file-parser-not-only-for-arduino\/#respond"]}]},{"@type":"WebPage","@id":"https:\/\/www.pschatzmann.ch\/home\/2023\/02\/15\/a-simple-midi-file-parser-not-only-for-arduino\/","url":"https:\/\/www.pschatzmann.ch\/home\/2023\/02\/15\/a-simple-midi-file-parser-not-only-for-arduino\/","name":"A simple Midi File Parser (not only) for Arduino - Phil Schatzmann","isPartOf":{"@id":"https:\/\/www.pschatzmann.ch\/home\/#website"},"primaryImageOfPage":{"@id":"https:\/\/www.pschatzmann.ch\/home\/2023\/02\/15\/a-simple-midi-file-parser-not-only-for-arduino\/#primaryimage"},"image":{"@id":"https:\/\/www.pschatzmann.ch\/home\/2023\/02\/15\/a-simple-midi-file-parser-not-only-for-arduino\/#primaryimage"},"thumbnailUrl":"https:\/\/www.pschatzmann.ch\/wp-content\/uploads\/2020\/09\/midi.png","datePublished":"2023-02-15T10:24:44+00:00","dateModified":"2023-02-15T10:58:08+00:00","breadcrumb":{"@id":"https:\/\/www.pschatzmann.ch\/home\/2023\/02\/15\/a-simple-midi-file-parser-not-only-for-arduino\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/www.pschatzmann.ch\/home\/2023\/02\/15\/a-simple-midi-file-parser-not-only-for-arduino\/"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/www.pschatzmann.ch\/home\/2023\/02\/15\/a-simple-midi-file-parser-not-only-for-arduino\/#primaryimage","url":"https:\/\/www.pschatzmann.ch\/wp-content\/uploads\/2020\/09\/midi.png","contentUrl":"https:\/\/www.pschatzmann.ch\/wp-content\/uploads\/2020\/09\/midi.png","width":1280,"height":582},{"@type":"BreadcrumbList","@id":"https:\/\/www.pschatzmann.ch\/home\/2023\/02\/15\/a-simple-midi-file-parser-not-only-for-arduino\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/www.pschatzmann.ch\/home\/"},{"@type":"ListItem","position":2,"name":"A simple Midi File Parser (not only) for Arduino"}]},{"@type":"WebSite","@id":"https:\/\/www.pschatzmann.ch\/home\/#website","url":"https:\/\/www.pschatzmann.ch\/home\/","name":"Phil Schatzmann Consulting","description":"","publisher":{"@id":"https:\/\/www.pschatzmann.ch\/home\/#\/schema\/person\/73a53638a4e34e8373405fd737dac9b1"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/www.pschatzmann.ch\/home\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":["Person","Organization"],"@id":"https:\/\/www.pschatzmann.ch\/home\/#\/schema\/person\/73a53638a4e34e8373405fd737dac9b1","name":"pschatzmann","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/www.pschatzmann.ch\/wp-content\/uploads\/2022\/08\/pschatzmann.png","url":"https:\/\/www.pschatzmann.ch\/wp-content\/uploads\/2022\/08\/pschatzmann.png","contentUrl":"https:\/\/www.pschatzmann.ch\/wp-content\/uploads\/2022\/08\/pschatzmann.png","width":305,"height":305,"caption":"pschatzmann"},"logo":{"@id":"https:\/\/www.pschatzmann.ch\/wp-content\/uploads\/2022\/08\/pschatzmann.png"}}]}},"post_mailing_queue_ids":[],"_links":{"self":[{"href":"https:\/\/www.pschatzmann.ch\/home\/wp-json\/wp\/v2\/posts\/5350","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.pschatzmann.ch\/home\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.pschatzmann.ch\/home\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.pschatzmann.ch\/home\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.pschatzmann.ch\/home\/wp-json\/wp\/v2\/comments?post=5350"}],"version-history":[{"count":13,"href":"https:\/\/www.pschatzmann.ch\/home\/wp-json\/wp\/v2\/posts\/5350\/revisions"}],"predecessor-version":[{"id":5363,"href":"https:\/\/www.pschatzmann.ch\/home\/wp-json\/wp\/v2\/posts\/5350\/revisions\/5363"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.pschatzmann.ch\/home\/wp-json\/wp\/v2\/media\/2000"}],"wp:attachment":[{"href":"https:\/\/www.pschatzmann.ch\/home\/wp-json\/wp\/v2\/media?parent=5350"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.pschatzmann.ch\/home\/wp-json\/wp\/v2\/categories?post=5350"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.pschatzmann.ch\/home\/wp-json\/wp\/v2\/tags?post=5350"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}