-
Notifications
You must be signed in to change notification settings - Fork 0
/
track.c
173 lines (131 loc) · 4.14 KB
/
track.c
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
#include <arpa/inet.h>
#include <stdint.h>
#include <stdlib.h>
#include <string.h>
#include "track.h"
#include "midibuff.h"
#define ARRSIZE(arr) (sizeof(arr)/sizeof(*arr))
enum status {
/* MIDI events: or-ed with channel */
STATUS_PROGRAM_NO = 0xC0,
STATUS_NOTE_ON = 0x90,
STATUS_NOTE_OFF = 0x80,
/* Meta events */
STATUS_META_CHUNK = 0xFF,
};
enum meta_event {
META_TRACK_END = 0x2F,
META_TEMPO = 0x51,
META_TIME_SIGNATURE = 0x58,
META_KEY_SIGNATURE = 0x59,
};
#define TRACK_SIZE_RAW(this) ((uint32_t *)(&(&(this)->buff->bytes[(this)->head])[4]))
static void track_append_bytes(MidiTrack *this, void *bytes, uint32_t num_bytes) {
midibuff_append_raw(this->buff, bytes, num_bytes);
uint32_t *track_sz = TRACK_SIZE_RAW(this);
*track_sz = htonl(ntohl(*track_sz) + num_bytes);
}
static size_t append_as_varwidth(uint8_t *dest, uint32_t val) {
/* Top bit of each byte indicates more bytes.
*/
uint8_t *p = dest;
uint32_t nval = htonl(val);
uint8_t *bytes = (uint8_t *)&nval;
bytes[0] = 0x7F & ((bytes[0] << 3) | (bytes[1] >> 5));
bytes[1] = 0x7F & ((bytes[1] << 2) | (bytes[2] >> 6));
bytes[2] = 0x7F & ((bytes[2] << 1) | (bytes[3] >> 7));
bytes[3] = 0x7F & bytes[3];
if(bytes[0])
*p++ = 0x80 | bytes[0];
if(bytes[0] || bytes[1])
*p++ = 0x80 | bytes[1];
if(bytes[0] || bytes[1] || bytes[2])
*p++ = 0x80 | bytes[2];
*p++ = 0x7F & bytes[3];
return p - dest;
}
static void track_append_chunk(MidiTrack *this, uint32_t delta, void *chunk, size_t chunk_size) {
uint8_t new_bytes[4 + chunk_size];
size_t dt_width = append_as_varwidth(new_bytes, delta);
memcpy(&new_bytes[dt_width], chunk, chunk_size);
track_append_bytes(this, new_bytes, dt_width + chunk_size);
}
MidiTrack *track_start(MidiBuffer *buff) {
MidiTrack *new = malloc(sizeof *new);
if(!new)
return NULL;
new->buff = buff;
new->head = buff->used;
midibuff_append_string(buff, "MTrk");
midibuff_append_uint32(buff, 0);
return new;
}
void track_free(MidiTrack *this) {
free(this);
}
void track_tempo(MidiTrack *this, uint32_t delta, uint32_t quart_micros) {
uint32_t nmicros = htonl(quart_micros);
uint8_t *quart_micros_as_raw = (uint8_t *)&nmicros;
uint8_t chunk[] = {
STATUS_META_CHUNK,
META_TEMPO,
3, /* bytes following... */
quart_micros_as_raw[0],
quart_micros_as_raw[1],
quart_micros_as_raw[2],
};
track_append_chunk(this, delta, chunk, ARRSIZE(chunk));
}
void track_key(MidiTrack *this, uint32_t delta, uint8_t sharps, uint8_t tonality) {
uint8_t chunk[] = {
STATUS_META_CHUNK,
META_KEY_SIGNATURE,
2, /* bytes following... */
sharps,
tonality,
};
track_append_chunk(this, delta, chunk, ARRSIZE(chunk));
}
void track_time_signature(MidiTrack *this, uint32_t delta, uint8_t num, uint8_t denomexp) {
uint8_t chunk[] = {
STATUS_META_CHUNK,
META_TIME_SIGNATURE,
4, /* bytes following... */
num,
denomexp,
24,
8,
};
track_append_chunk(this, delta, chunk, ARRSIZE(chunk));
}
void track_program_no(MidiTrack *this, uint32_t delta, uint8_t channel, uint8_t program_no) {
uint8_t chunk[] = {
STATUS_PROGRAM_NO | channel,
program_no,
};
track_append_chunk(this, delta, chunk, ARRSIZE(chunk));
}
void track_note_on(MidiTrack *this, uint32_t delta, uint8_t channel, uint8_t pitch, uint8_t velocity) {
uint8_t chunk[] = {
STATUS_NOTE_ON | channel,
pitch,
velocity,
};
track_append_chunk(this, delta, chunk, ARRSIZE(chunk));
}
void track_note_off(MidiTrack *this, uint32_t delta, uint8_t channel, uint8_t pitch, uint8_t velocity) {
uint8_t chunk[] = {
STATUS_NOTE_OFF | channel,
pitch,
velocity,
};
track_append_chunk(this, delta, chunk, ARRSIZE(chunk));
}
void track_end(MidiTrack *this, uint32_t delta) {
uint8_t chunk[] = {
STATUS_META_CHUNK,
META_TRACK_END,
0, /* no more bytes */
};
track_append_chunk(this, delta, chunk, ARRSIZE(chunk));
}