«Import a midi file easily» by sebastienclara

on 17 Jan'16 09:33 in midi file

Import a midi file on 3 Arrays : notes, durations & dates.

// notes, durations, dates <=

n, d, t = ImportMidiFile("~/Desktop/bwv772.mid");

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
/* Sébastien Clara - Janvier 2016


Import a midi file on 3 Arrays : notes, durations & dates


!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
Put or link this file in a specific directories
Typical user-specific extension directories :
OSX:	~/Library/Application Support/SuperCollider/Extensions/
Linux: 	~/.local/share/SuperCollider/Extensions/

!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
Dependence : wslib on Quarks

!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
typeMidi => For noteOn & noteOff information.
But with MuseScore, we don't have. So put typeMidi to 1.
See the examples below.

*/

ImportMidiFile {
	*new {	arg file, typeMidi=0;

		var midiFile;
		var notes, durees, dates;
		var on, off, datesIndex;

		midiFile = SimpleMIDIFile.read(file);

		if (typeMidi == 0,{
			midiFile.noteEvents.do({ |i| if(i[2] == \noteOn,
				{on = on.add(i)},
				{off = off.add(i)})
			});
		},{
			midiFile.noteEvents.do({ |i| if(i[5] != 0,
				{on = on.add(i)},
				{off = off.add(i)})
			});
		});


		datesIndex = on.collect({|i| i[1]});

		datesIndex.asSet.asArray.sort.do({|date|
			var indices;
			indices =  datesIndex.indicesOfEqual(date);

			notes = notes.add( indices.collect({|i| on[i][4]}) );

			durees = durees.add( indices.collect({|i|
				var offIndice, duree;
				offIndice = off.collect({|j| j[4]}).detectIndex({|j| j == on[i][4]});
				duree = off[offIndice][1] - on[i][1];
				off.removeAt(offIndice);
				duree;
			}) );

			dates = dates.add( date );
		});



		^[notes, durees, dates];
	}
}

/*
//////////////////////////////////////////////////////////////////////////
//	Usage :

m = SimpleMIDIFile.read("~/Desktop/bwv772.mid");
m.noteEvents.collect({ |i| i.postln }); ""
/*
[ 1, 97, noteOn, 0, 60, 127 ]
[ 1, 265, noteOff, 0, 60, 127 ]
[ 1, 278, noteOn, 2, 62, 127 ]
[ 1, 446, noteOff, 2, 62, 127 ]
*/
// => typeMidi by default

// notes, durations, dates <=
# n, d, t = ImportMidiFile("~/Desktop/bwv772.mid");

n
d
t


m = SimpleMIDIFile.read("~/Desktop/cadence.mid");
m.noteEvents.collect({ |i| i.postln }); ""
/*
[ 0, 0, noteOn, 0, 72, 73 ]
[ 0, 479, noteOn, 0, 72, 0 ]
[ 0, 480, noteOn, 0, 69, 69 ]
[ 0, 959, noteOn, 0, 69, 0 ]
*/
// => typeMidi != 0 !!!!!!!!!!!!!!

// notes, durations, dates <=
# n, d, t = ImportMidiFile("~/Desktop/cadence.mid", 1);

n
d
t

*/
raw 2438 chars (focus & ctrl+a+c to copy)
reception
comments