{
   "labels" : [
      "midi file"
   ],
   "code" : "/* Sébastien Clara - Janvier 2016\r\n\r\n\r\nImport a midi file on 3 Arrays : notes, durations & dates\r\n\r\n\r\n!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!\r\nPut or link this file in a specific directories\r\nTypical user-specific extension directories :\r\nOSX:\t~/Library/Application Support/SuperCollider/Extensions/\r\nLinux: \t~/.local/share/SuperCollider/Extensions/\r\n\r\n!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!\r\nDependence : wslib on Quarks\r\n\r\n!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!\r\ntypeMidi => For noteOn & noteOff information.\r\nBut with MuseScore, we don't have. So put typeMidi to 1.\r\nSee the examples below.\r\n\r\n*/\r\n\r\nImportMidiFile {\r\n\t*new {\targ file, typeMidi=0;\r\n\r\n\t\tvar midiFile;\r\n\t\tvar notes, durees, dates;\r\n\t\tvar on, off, datesIndex;\r\n\r\n\t\tmidiFile = SimpleMIDIFile.read(file);\r\n\r\n\t\tif (typeMidi == 0,{\r\n\t\t\tmidiFile.noteEvents.do({ |i| if(i[2] == \\noteOn,\r\n\t\t\t\t{on = on.add(i)},\r\n\t\t\t\t{off = off.add(i)})\r\n\t\t\t});\r\n\t\t},{\r\n\t\t\tmidiFile.noteEvents.do({ |i| if(i[5] != 0,\r\n\t\t\t\t{on = on.add(i)},\r\n\t\t\t\t{off = off.add(i)})\r\n\t\t\t});\r\n\t\t});\r\n\r\n\r\n\t\tdatesIndex = on.collect({|i| i[1]});\r\n\r\n\t\tdatesIndex.asSet.asArray.sort.do({|date|\r\n\t\t\tvar indices;\r\n\t\t\tindices =  datesIndex.indicesOfEqual(date);\r\n\r\n\t\t\tnotes = notes.add( indices.collect({|i| on[i][4]}) );\r\n\r\n\t\t\tdurees = durees.add( indices.collect({|i|\r\n\t\t\t\tvar offIndice, duree;\r\n\t\t\t\toffIndice = off.collect({|j| j[4]}).detectIndex({|j| j == on[i][4]});\r\n\t\t\t\tduree = off[offIndice][1] - on[i][1];\r\n\t\t\t\toff.removeAt(offIndice);\r\n\t\t\t\tduree;\r\n\t\t\t}) );\r\n\r\n\t\t\tdates = dates.add( date );\r\n\t\t});\r\n\r\n\r\n\r\n\t\t^[notes, durees, dates];\r\n\t}\r\n}\r\n\r\n/*\r\n//////////////////////////////////////////////////////////////////////////\r\n//\tUsage :\r\n\r\nm = SimpleMIDIFile.read(\"~/Desktop/bwv772.mid\");\r\nm.noteEvents.collect({ |i| i.postln }); \"\"\r\n/*\r\n[ 1, 97, noteOn, 0, 60, 127 ]\r\n[ 1, 265, noteOff, 0, 60, 127 ]\r\n[ 1, 278, noteOn, 2, 62, 127 ]\r\n[ 1, 446, noteOff, 2, 62, 127 ]\r\n*/\r\n// => typeMidi by default\r\n\r\n// notes, durations, dates <=\r\n# n, d, t = ImportMidiFile(\"~/Desktop/bwv772.mid\");\r\n\r\nn\r\nd\r\nt\r\n\r\n\r\nm = SimpleMIDIFile.read(\"~/Desktop/cadence.mid\");\r\nm.noteEvents.collect({ |i| i.postln }); \"\"\r\n/*\r\n[ 0, 0, noteOn, 0, 72, 73 ]\r\n[ 0, 479, noteOn, 0, 72, 0 ]\r\n[ 0, 480, noteOn, 0, 69, 69 ]\r\n[ 0, 959, noteOn, 0, 69, 0 ]\r\n*/\r\n// => typeMidi != 0 !!!!!!!!!!!!!!\r\n\r\n// notes, durations, dates <=\r\n# n, d, t = ImportMidiFile(\"~/Desktop/cadence.mid\", 1);\r\n\r\nn\r\nd\r\nt\r\n\r\n*/",
   "id" : "1-50O",
   "is_private" : null,
   "author" : "sebastienclara",
   "name" : "Import a midi file easily",
   "description" : "Import a midi file on 3 Arrays : notes, durations & dates.\r\n\r\n// notes, durations, dates <=\r\n# n, d, t = ImportMidiFile(\"~/Desktop/bwv772.mid\");",
   "ancestor_list" : []
}
