/
midi_file.c
146 lines (122 loc) · 3.41 KB
/
midi_file.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
/*
(C) Copyright 2005,2006, Stephen M. Cameron.
This file is part of Gneutronica.
Gneutronica is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
Gneutronica is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with Gneutronica; if not, write to the Free Software
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*/
#include <stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <netinet/in.h> /* for htonl(), etc. */
#include <sys/time.h>
#include <unistd.h>
#include "sched.h"
#include "write_bytes.h"
static void write_weird_midi_int(int fd, unsigned int value)
{
unsigned int buf;
buf = value & 0x7F;
unsigned char *x;
int count;
/* printf("Writing %d: ", value); */
while ( (value >>= 7) ) {
buf <<= 8;
buf |= ((value & 0x7F) | 0x80);
}
count = 0;
x = (unsigned char *) &buf;
while (1) {
write_bytes(fd, x, 1);
/* printf(" 0x%02x", *x); */
if (*x & 0x80) {
x++;
count++;
if (count >= 4) /* sanity check to keep from buffer overrun */
break;
} else
break;
}
/* printf("\n"); */
}
int write_MThd(int fd)
{
/* write the MThd chunk */
unsigned int length;
int rc;
unsigned short format = htons(0);
unsigned short tracks = htons(1);
unsigned short divisions = htons(0xE728); /* millisecond resolution */
length = htonl(6);
rc = write(fd, "MThd", 4);
if (rc != 4)
return -1;
rc = write_bytes(fd, &length, sizeof(length));
if (rc != sizeof(length))
return -1;
rc = write_bytes(fd, &format, sizeof(format));
if (rc != sizeof(format)) return -1;
rc = write_bytes(fd, &tracks, sizeof(tracks));
if (rc != sizeof(tracks)) return -1;
rc = write_bytes(fd, &divisions, sizeof(divisions));
if (rc != sizeof(divisions)) return -1;
return 0;
}
int write_end_of_track(int fd)
{
unsigned char eot[] = { 0xFF,0x2F,0x00 };
unsigned long ms = 0;
write_weird_midi_int(fd, ms);
return (write_bytes(fd, eot, 3) != 3);
}
int write_tempo_change(int fd, int microsecs_per_quarternote)
{
unsigned int buf;
unsigned char *x;
unsigned char msg[] = { 0xFF, 0x51, 0x03 };
int rc;
microsecs_per_quarternote &= 0x00ffffff;
buf = htonl(microsecs_per_quarternote);
x = (unsigned char *) &buf;
rc = write_bytes(fd, msg, 3);
rc += write_bytes(fd, &x[1], 3);
return (rc != 6);
}
#if 0
int write_MThd(int fd)
{
unsigned int length = htonl(6);
unsigned short format, numtracks, divisions;
write(fd, "MThd", 4);
write(fd, &length, 4);
}
#endif
int write_MTrk(int fd)
{
int length = htonl(0);
write_bytes(fd, "MTrk", 4);
write_bytes(fd, &length, 4);
return 0;
}
static struct timeval prevtime = { 0L, 0L };
void write_note(int fd, struct timeval *tm, unsigned char opcode,
unsigned char note, unsigned char velocity)
{
unsigned long ms;
ms = msdiff(tm, &prevtime);
write_weird_midi_int(fd, ms);
/* printf("writing opcode/note/velocity: %02x %02x %02x\n", opcode, note, velocity); */
write_bytes(fd, &opcode, 1);
write_bytes(fd, ¬e, 1);
write_bytes(fd, &velocity, 1);
prevtime = *tm;
}