-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmidiFileWriter.cpp
More file actions
174 lines (138 loc) · 3.9 KB
/
midiFileWriter.cpp
File metadata and controls
174 lines (138 loc) · 3.9 KB
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
//
// A C++ class for writing midi files
// initial C code from https://github.com/mru00/dsp
//
#include "midiFileWriter.h"
static inline unsigned short reorder_word(unsigned short in) {
return ((in>>8)&0xff) | ((in&0xff) << 8);
}
static inline unsigned int reorder_dword(unsigned int in) {
return (reorder_word((in >> 16)&0xffff)) | (reorder_word(in&0xffff)<<16);
}
MidiFileWriter::MidiFileWriter(const char* filename, short timeDivision): _difftime(0), _runningStatus(0xff) {
_handle = fopen (filename, "wb+");
if (canWrite())
{
writeHeader(timeDivision, 1); // division = 120
writeTrackHeader();
}
}
MidiFileWriter::~MidiFileWriter()
{
if (_handle)
{
writeTrackEnd();
fclose(_handle);
}
}
void MidiFileWriter::increaseDiffTime(unsigned long delta)
{
_difftime += delta;
}
void MidiFileWriter::writeTimestamp()
{
writeVarLength(_difftime);
_difftime = 0;
}
//http://www.omega-art.com/midi/mfiles.html
void MidiFileWriter::writeVarLength(unsigned long number)
{
unsigned char b;
unsigned long buffer;
buffer = number & 0x7f;
while ((number >>= 7) > 0) {
buffer <<= 8;
buffer |= 0x80 + (number & 0x7f);
}
while (1) {
b = buffer & 0xff;
fwrite(&b, 1, 1, _handle);
if (buffer & 0x80) buffer >>= 8;
else break;
}
}
void MidiFileWriter::addCopyright(const char* copyright)
{
writeTimestamp();
unsigned char b;
b = 0xFF; fwrite(&b, 1, 1, _handle);
b = 0x2; fwrite(&b, 1, 1, _handle);
b = strlen(copyright); fwrite(&b, 1, 1, _handle);
fwrite(copyright, b, 1, _handle);
}
void MidiFileWriter::addTempo(unsigned long bpm)
{
writeTimestamp();
unsigned char b;
b = 0xFF; fwrite(&b, 1, 1, _handle);
b = 0x51; fwrite(&b, 1, 1, _handle);
b = 0x03; fwrite(&b, 1, 1, _handle);
bpm = 60000000 / bpm;
b = bpm >> 16; fwrite(&b, 1, 1, _handle);
b = (bpm & 0xFFFFF) >> 8; fwrite(&b, 1, 1, _handle);
b = (bpm & 0xFF); fwrite(&b, 1, 1, _handle);
}
void MidiFileWriter::addTimeSignature(unsigned char numerator, unsigned char denominator, unsigned char numclocks, unsigned char num32)
{
writeTimestamp();
unsigned char b;
b = 0xFF; fwrite(&b, 1, 1, _handle);
b = 0x58; fwrite(&b, 1, 1, _handle);
b = 0x04; fwrite(&b, 1, 1, _handle);
fwrite(&numerator, 1, 1, _handle);
fwrite(&denominator, 1, 1, _handle);
fwrite(&numclocks, 1, 1, _handle);
fwrite(&num32, 1, 1, _handle);
}
void MidiFileWriter::addTrackName(const char* name)
{
writeTimestamp();
unsigned char b;
b = 0xFF; fwrite(&b, 1, 1, _handle);
b = 0x3; fwrite(&b, 1, 1, _handle);
b = (unsigned char)strlen(name); fwrite(&b, 1, 1, _handle);
fwrite(name, b, 1, _handle);
}
void MidiFileWriter::writeNoteEvent(unsigned char eventType, unsigned char note, unsigned char velocity)
{
writeTimestamp();
// running status
if ( eventType != _runningStatus ) {
fwrite(&eventType, 1, 1, _handle);
_runningStatus = eventType;
}
fwrite(¬e, 1, 1, _handle);
fwrite(&velocity, 1, 1, _handle);
}
void MidiFileWriter::writeHeader(short timedivision, short numberoftracks)
{
int chunksize = reorder_dword(6);
short formattype = reorder_word(0);
numberoftracks = reorder_word(numberoftracks);
timedivision= reorder_word(timedivision);
fwrite("MThd", 4, 1, _handle);
fwrite(&chunksize, 4, 1, _handle);
fwrite(&formattype, 2, 1, _handle);
fwrite(&numberoftracks, 2, 1, _handle);
fwrite(&timedivision, 2, 1, _handle);
}
void MidiFileWriter::writeTrackHeader()
{
int chunksize = reorder_dword(60000);
fwrite("MTrk", 4, 1, _handle);
_trackChunksizeFixup = ftell(_handle);
fwrite(&chunksize, 4, 1, _handle);
}
void MidiFileWriter::writeTrackEnd()
{
unsigned char b;
writeTimestamp();
b = 0xFF; fwrite(&b, 1, 1, _handle);
b = 0x2F; fwrite(&b, 1, 1, _handle);
writeVarLength(0);
long pos = ftell(_handle);
int chunksize = reorder_dword(pos - _trackChunksizeFixup - sizeof(unsigned int));
fseek(_handle, _trackChunksizeFixup, SEEK_SET);
fwrite(&chunksize, 4, 1, _handle);
fseek(_handle, pos, SEEK_SET);
}