-
Notifications
You must be signed in to change notification settings - Fork 21
/
MidiExport.cs
executable file
·392 lines (341 loc) · 14 KB
/
MidiExport.cs
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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
using System;
using System.Collections.Generic;
using UnityEngine;
namespace MidiExport {
public class MidiExport
{
public static System.Text.Encoding ascii = System.Text.Encoding.ASCII;
public int fileType = 1;
public int ticksPerBeat = 960;
public List<MidiTrack> midiTracks = new List<MidiTrack>();
public MidiExport(int fileType = 1, int ticksPerBeat = 960)
{
this.fileType = fileType;
this.ticksPerBeat = ticksPerBeat;
}
public List<byte> createBytes()
{
List<byte> data = new List<byte>();
data.AddRange(createHeader());
foreach (MidiTrack track in midiTracks)
{
data.AddRange(track.createBytes());
}
return data;
}
public List<byte> createHeader()
{
List<byte> data = new List<byte>();
List<byte> header = new List<byte>();
header.AddRange(toBEShort(fileType));
header.AddRange(toBEShort(midiTracks.Count));
header.AddRange(toBEShort(ticksPerBeat));
data.AddRange(writeChunk("MThd",header));
return data;
}
public static List<byte> writeChunk(string name, List<byte> data)
{
List<byte> _data = new List<byte>();
_data.AddRange(ascii.GetBytes(name));
_data.AddRange(toBEULong(data.Count));
_data.AddRange(data);
return _data;
}
public static List<byte> toBEULong(int val)
{
List<byte> data = new List<byte>();
byte[] LEdata = BitConverter.GetBytes((System.UInt32)val);
for (int x = LEdata.Length-1; x >= 0; x--)
{
data.Add(LEdata[x]);
}
return data;
}
public static List<byte> toBEShort(int val)
{
List<byte> data = new List<byte>();
byte[] LEdata = BitConverter.GetBytes((System.Int16)val);
for (int x = LEdata.Length - 1; x >= 0; x--)
{
data.Add(LEdata[x]);
}
return data;
}
public static List<byte> encodeVariableInt(int val)
{
if (val < 0) throw new FormatException("Variable int must be positive.");
List<byte> data = new List<byte>();
while (val > 0)
{
data.Add((byte)(val & 0x7f));
val >>= 7;
}
if (data.Count > 0)
{
data.Reverse();
for (int x = 0; x < data.Count - 1; x++)
{
data[x] |= 0x80;
}
}
else
{
data.Add(0x00);
}
return data;
}
}
public class MidiTrack
{
public List<MidiMessage> messages = new List<MidiMessage>();
public List<byte> createBytes()
{
List<byte> data = new List<byte>();
byte runningStatusByte = 0x00;
bool statusByteSet = false;
foreach (MidiMessage message in messages)
{
if (message.time < 0)
{
message.time = 0;
}
data.AddRange(MidiExport.encodeVariableInt(message.time));
if (message.type.Equals("sysex"))
{
statusByteSet = false;
data.Add(0xf0);
data.AddRange(MidiExport.encodeVariableInt(message.data.Length + 1));
data.AddRange(message.data);
data.Add(0xf7);
} else
{
List<byte> raw = new List<byte>();
raw = message.createBytes();
byte temp = raw[0];
if (statusByteSet && !message.is_meta && raw[0] < 0xf0 && raw[0] == runningStatusByte)
{
raw.RemoveAt(0);
data.AddRange(raw);
} else
{
data.AddRange(raw);
}
runningStatusByte = temp;
statusByteSet = true;
}
}
return MidiExport.writeChunk("MTrk",data);
}
}
public class MidiMessage
{
public string type = "";
public int time = 0;
public bool is_meta = false;
private byte code = 0x00;
//MetaMessages:
//#############
//sequence_number 0x00
public int number = 0;
//text 0x01
public string text = "";
//copyright 0x02 -> text
//track_name 0x03
public string name = "";
//instrument_name 0x04 -> track_name
//lyrics 0x05 -> text
//marker 0x06 -> text
//cue_marker 0x07 -> text
//device_name 0x08 -> track_name
//channel_prefix 0x20
public int channel = 0;
//midi_port 0x21
public int port = 0;
//end_of_track 0x2f
//set_tempo 0x51
public int tempo = 500000;
//smpte_offset 0x54 (Ignore)
//public int frame_rate = 24; public int hours = 0; public int minutes = 0; public int seconds = 0; public int frames = 0; public int sub_frames = 0;
//time_signature 0x58
public int numerator = 4; public int denominator = 2; public int clocks_per_click = 24; public int notated_32nd_notes_per_beat = 8;
//key_signature 0x59
public int key = 0; bool is_major = true;
//Messages:
//#########
//note_off 0x80 (channel, note, velocity)
public int note = 0; public int velocity = 0;
//note_on 0x90 (channel, note, velocity)
//polytouch 0xa0 (channel, note, value)
public int value = 0;
//control_change 0xb0 (channel, control, value)
public int control = 0;
//program_change 0xc0 (channel, program)
public int program = 0;
//aftertouch 0xd0 (channel, value)
//pitchwheel 0xe0 (channel, pitch)
public int pitch = 0;
//sysex 0xf0 (data)
public byte[] data;
//Others not needed..
public MidiMessage(string type, string[] args, int time, byte[] data = null)
{
is_meta = false;
this.type = type;
this.time = time;
//Meta Messages
if (type.Equals("sequence_number")) { is_meta = true; code = 0x00; number = int.Parse(args[0]); }
if (type.Equals("text") || type.Equals("copyright") || type.Equals("lyrics") || type.Equals("marker") || type.Equals("cue_marker"))
{
is_meta = true;
text = args[0];
}
if (type.Equals("text")) code = 0x01;
if (type.Equals("copyright")) code = 0x02;
if (type.Equals("lyrics")) code = 0x05;
if (type.Equals("marker")) code = 0x06;
if (type.Equals("cue_marker")) code = 0x07;
if (type.Equals("track_name") || type.Equals("instrument_name") || type.Equals("device_name"))
{
is_meta = true; code = 0x03;
name = args[0];
}
if (type.Equals("instrument_name")) code = 0x04;
if (type.Equals("device_name")) code = 0x08;
if (type.Equals("channel_prefix")) { code = 0x20; channel = int.Parse(args[0]); is_meta = true; }
if (type.Equals("midi_port")) { code = 0x21; port = int.Parse(args[0]); is_meta = true; }
if (type.Equals("end_of_track")) { code = 0x2f; is_meta = true; }
if (type.Equals("set_tempo")) { code = 0x51; tempo = int.Parse(args[0]); is_meta = true; }
if (type.Equals("time_signature"))
{
is_meta = true; code = 0x58;
numerator = int.Parse(args[0]); //4
denominator = int.Parse(args[1]); //4
clocks_per_click = int.Parse(args[2]); //24
notated_32nd_notes_per_beat = int.Parse(args[3]); //8
}
if (type.Equals("key_signature"))
{
is_meta = true; code = 0x59;
key = int.Parse(args[0]);
is_major = args[1].Equals("0"); //"0" or "1"
}
//Normal Messages
if (type.Equals("note_off")) { code = 0x80; channel = int.Parse(args[0]); note = int.Parse(args[1]); velocity = int.Parse(args[2]); }
if (type.Equals("note_on")) { code = 0x90; channel = int.Parse(args[0]); note = int.Parse(args[1]); velocity = int.Parse(args[2]); }
if (type.Equals("polytouch")) { code = 0xa0; channel = int.Parse(args[0]); note = int.Parse(args[1]); value = int.Parse(args[2]); }
if (type.Equals("control_change")) { code = 0xb0; channel = int.Parse(args[0]); control = int.Parse(args[1]); value = int.Parse(args[2]); }
if (type.Equals("program_change")) { code = 0xc0; channel = int.Parse(args[0]); program = int.Parse(args[1]); }
if (type.Equals("aftertouch")) { code = 0xd0; channel = int.Parse(args[0]); value = int.Parse(args[1]); }
if (type.Equals("pitchwheel")) { code = 0xe0; channel = int.Parse(args[0]); pitch = int.Parse(args[1]); }
if (type.Equals("sysex")) { code = 0xf0; this.data = data; }
}
public List<byte> createBytes()
{
List<byte> data;
if (is_meta) data = createMetaBytes();
else data = createMessageBytes();
return data;
}
public List<byte> createMetaBytes()
{
List<byte> data = new List<byte>();
if (type.Equals("sequence_number"))
{
data.Add((byte)(number >> 8));
data.Add((byte)(number & 0xff));
}
if (type.Equals("text") || type.Equals("copyright") || type.Equals("lyrics") || type.Equals("marker") || type.Equals("cue_marker"))
{
if (text == null) text = "";
data.AddRange(MidiExport.ascii.GetBytes(text));
}
if (type.Equals("track_name") || type.Equals("instrument_name") || type.Equals("device_name"))
{
data.AddRange(MidiExport.ascii.GetBytes(name));
}
if (type.Equals("channel_prefix"))
{
data.Add((byte)channel);
}
if (type.Equals("midi_port"))
{
data.Add((byte)port);
}
if (type.Equals("set_tempo"))
{
//return [tempo >> 16, tempo >> 8 & 0xff, tempo & 0xff]
data.Add((byte)(tempo >> 16));
data.Add((byte)((tempo >> 8)& 0xff));
data.Add((byte)(tempo & 0xff));
}
if (type.Equals("time_signature"))
{
data.Add((byte)numerator);
data.Add((byte)Math.Log(denominator, 2));
data.Add((byte)clocks_per_click);
data.Add((byte)notated_32nd_notes_per_beat);
}
if (type.Equals("key_signature"))
{
data.Add((byte)(key & 0xff));
data.Add(is_major ? (byte)0x00 : (byte)0x01);
}
int dataLength = data.Count;
data.InsertRange(0, MidiExport.encodeVariableInt(dataLength));
data.Insert(0, code);
data.Insert(0, 0xff);
return data;
}
public List<byte> createMessageBytes()
{
List<byte> data = new List<byte>();
/* if (type.Equals("note_off")) { code = 0x80; channel = int.Parse(args[0]); note = int.Parse(args[1]); velocity = int.Parse(args[2]); }
if (type.Equals("note_on")) { code = 0x90; channel = int.Parse(args[0]); note = int.Parse(args[1]); velocity = int.Parse(args[2]); }
if (type.Equals("polytouch")) { code = 0xa0; channel = int.Parse(args[0]); note = int.Parse(args[1]); value = int.Parse(args[2]); }
if (type.Equals("control_change")) { code = 0xb0; channel = int.Parse(args[0]); control = int.Parse(args[1]); value = int.Parse(args[2]); }
if (type.Equals("program_change")) { code = 0xc0; channel = int.Parse(args[0]); program = int.Parse(args[1]); }
if (type.Equals("aftertouch")) { code = 0xd0; channel = int.Parse(args[0]); value = int.Parse(args[1]); }
if (type.Equals("pitchwheel")) { code = 0xe0; channel = int.Parse(args[0]); pitch = int.Parse(args[1]); }
if (type.Equals("sysex")) { code = 0xf0; this.data = data; }
*/
if (type.Equals("note_off") || type.Equals("note_on"))
{
data.Add((byte)(code | (byte)channel));
data.Add((byte)note); data.Add((byte)velocity);
}
if (type.Equals("polytouch"))
{
data.Add((byte)(code | (byte)channel));
data.Add((byte)note); data.Add((byte)value);
}
if (type.Equals("control_change"))
{
data.Add((byte)(code | (byte)channel));
data.Add((byte)control); data.Add((byte)value);
}
if (type.Equals("program_change"))
{
data.Add((byte)(code | (byte)channel));
data.Add((byte)program);
}
if (type.Equals("aftertouch"))
{
data.Add((byte)(code | (byte)channel));
data.Add((byte)value);
}
if (type.Equals("pitchwheel")) //14 bit signed integer
{
data.Add((byte)(code | (byte)channel));
//data.Add((byte)pitch);
pitch -= -8192;
data.Add((byte)(pitch & 0x7f));
data.Add((byte)(pitch >> 7));
}
if (type.Equals("sysex"))
{
data.AddRange(this.data);
}
return data;
}
}
}