-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfile.c
267 lines (210 loc) · 7.63 KB
/
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
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
#include "file.h"
#include <stdio.h>
#include <string.h>
#define TAPE_PATH_SIZE 16
#define FMT_CHUNK_ID "fmt "
#define CUE_CHUNK_ID "cue "
#define DATA_CHUNK_ID "data"
#define FMT_CHUNK_SIZE 16
// 4 byte count + 2 cue points (24 bytes per cue)
#define CUE_CHUNK_SIZE 52
#define IN_CUE_ID 1
#define OUT_CUE_ID 2
// https://sites.google.com/site/musicgapi/technical-documents/wav-file-format
// http://www-mmsp.ece.mcgill.ca/Documents/AudioFormats/WAVE/WAVE.html
int read_file(FILE * file, Tape * tape);
void read_fourcc(FILE * file, char * fourcc);
// return length
unsigned int read_chunk_head(FILE * file, char * id);
// little endian
unsigned short read_uint16(FILE * file);
unsigned int read_uint32(FILE * file);
void read_chunk_fmt(FILE * file);
int read_chunk_cue(FILE * file, Tape * tape);
int read_chunk_data(FILE * file, unsigned int chunk_size, Tape * tape);
int write_file(FILE * file, Tape * tape);
void write_fourcc(FILE * file, char * fourcc);
void write_uint16(FILE * file, unsigned short value);
void write_uint32(FILE * file, unsigned int value);
void write_cue(FILE * file, unsigned int id, unsigned int sample_num);
void path_for_tape(Tape * tape, char * path_out) {
path_out[0] = "ESLC"[tape->tape_num / 4]; // effect sample loop cue
path_out[1] = "1234"[tape->tape_num % 4];
path_out[2] = '.'; // yes this is good code
path_out[3] = 'w';
path_out[4] = 'a';
path_out[5] = 'v';
path_out[6] = 0;
}
/* LOADING */
int load_tape(Tape * tape) {
tape_reset(tape);
char path[TAPE_PATH_SIZE];
path_for_tape(tape, path);
FILE * file = fopen(path, "rb");
if (file == NULL) {
return 0; // probably doesn't exist
}
int error = read_file(file, tape);
if (fclose(file)) {
fprintf(stderr, "Error closing file\n");
}
return error;
}
int read_file(FILE * file, Tape * tape) {
int error = 0;
char chunk_id[5] = {0,0,0,0,0};
unsigned int riff_size = read_chunk_head(file, chunk_id);
if (strcmp(chunk_id, "RIFF")) {
fprintf(stderr, "Invalid WAV file\n");
return 1;
}
//printf("Size: %d\n", riff_size);
long riff_start = ftell(file);
read_fourcc(file, chunk_id);
if (strcmp(chunk_id, "WAVE")) {
fprintf(stderr, "Invalid WAV file\n");
return 1;
}
long pos = riff_start + 4;
while (pos < riff_start + riff_size) {
fseek(file, pos, SEEK_SET);
unsigned int chunk_size = read_chunk_head(file, chunk_id);
//printf("%s %d\n", chunk_id, chunk_size);
if (chunk_size % 2 == 1)
chunk_size ++; // chunks are always word-aligned
if (!strcmp(chunk_id, FMT_CHUNK_ID)) {
//read_chunk_fmt(file);
// ignore fmt chunk
} else if (!strcmp(chunk_id, CUE_CHUNK_ID)) {
if (read_chunk_cue(file, tape))
error = 1;
} else if (!strcmp(chunk_id, DATA_CHUNK_ID)) {
if (read_chunk_data(file, chunk_size, tape))
error = 1;
}
pos += 8 + chunk_size;
}
return error;
}
void read_fourcc(FILE * file, char * fourcc) {
fread(fourcc, 1, 4, file);
}
unsigned int read_chunk_head(FILE * file, char * id) {
read_fourcc(file, id);
return read_uint32(file);
}
unsigned short read_uint16(FILE * file) {
unsigned short v = 0;
for (int i = 0; i < 2; i++)
v += fgetc(file) << (i * 8);
return v;
}
unsigned int read_uint32(FILE * file) {
unsigned int v = 0;
for (int i = 0; i < 4; i++)
v += fgetc(file) << (i * 8);
return v;
}
void read_chunk_fmt(FILE * file) {
unsigned short format = read_uint16(file);
unsigned short n_channels = read_uint16(file);
unsigned short sample_rate = read_uint32(file);
unsigned short data_rate = read_uint32(file);
unsigned short block_size = read_uint16(file);
unsigned short bits_per_sample = read_uint16(file);
printf("Format: %d\nNum channels: %d\nSample rate: %d\nData rate: %d\nBlock size: %d\nBits per sample: %d\n", format, n_channels, sample_rate, data_rate, block_size, bits_per_sample);
}
int read_chunk_cue(FILE * file, Tape * tape) {
int error = 0;
unsigned int num_cues = read_uint32(file);
for (int i = 0; i < num_cues; i++) {
unsigned int cue_id = read_uint32(file);
fseek(file, 12, SEEK_CUR);
unsigned int sample_num = read_uint32(file) / sizeof(sample);
fseek(file, 4, SEEK_CUR);
if (sample_num > TAPE_MAX(tape) - tape->pt_start) {
fprintf(stderr, "Cue past end of tape!\n");
error = 1;
sample_num = TAPE_MAX(tape) - tape->pt_start;
}
if (cue_id == IN_CUE_ID)
tape->pt_in = sample_num + tape->pt_start;
else if (cue_id = OUT_CUE_ID)
tape->pt_out = sample_num + tape->pt_start;
}
return error;
}
int read_chunk_data(FILE * file, unsigned int chunk_size, Tape * tape) {
int error = 0;
int chunk_samples = chunk_size / sizeof(sample);
if (chunk_samples > TAPE_SAMPLES) {
error = 1;
fprintf(stderr, "File is too large!\n");
move_all_tape_points(tape, tape->audio_data - tape->pt_start);
chunk_samples = TAPE_SAMPLES;
} else if (chunk_samples > TAPE_SAMPLES / 2) {
move_all_tape_points(tape, -(chunk_samples - TAPE_SAMPLES/2) / 2);
}
fread(tape->pt_start, 1, chunk_samples * sizeof(sample), file);
tape->pt_end = tape->pt_start + chunk_samples;
return error;
}
/* SAVING */
int save_tape(Tape * tape) {
char path[TAPE_PATH_SIZE];
path_for_tape(tape, path);
FILE * file = fopen(path, "wb");
if (file == NULL) {
fprintf(stderr, "Couldn't write file %s\n", path);
return 1;
}
write_file(file, tape);
if (fclose(file)) {
fprintf(stderr, "Error closing file\n");
}
return 0;
}
int write_file(FILE * file, Tape * tape) {
write_fourcc(file, "RIFF");
int data_chunk_size = (tape->pt_end - tape->pt_start) * sizeof(sample);
int riff_size = 4 + (8 + FMT_CHUNK_SIZE) + (8 + data_chunk_size)
+ (8 + CUE_CHUNK_SIZE);
write_uint32(file, riff_size);
write_fourcc(file, "WAVE");
write_fourcc(file, FMT_CHUNK_ID); // begin format chunk
write_uint32(file, FMT_CHUNK_SIZE);
write_uint16(file, 3); // format code: IEEE float
write_uint16(file, TAPE_CHANNELS); // num channels
write_uint32(file, FRAME_RATE); // sample rate
write_uint32(file, FRAME_RATE * TAPE_CHANNELS * sizeof(sample)); // data rate
write_uint16(file, TAPE_CHANNELS * sizeof(sample)); // block size
write_uint16(file, sizeof(sample) * 8); // bits per sample
write_fourcc(file, CUE_CHUNK_ID); // begin cue chunk
write_uint32(file, CUE_CHUNK_SIZE);
write_uint32(file, 2); // num cue points
write_cue(file, IN_CUE_ID, tape->pt_in - tape->pt_start);
write_cue(file, OUT_CUE_ID, tape->pt_out - tape->pt_start);
write_fourcc(file, DATA_CHUNK_ID); // begin data chunk
write_uint32(file, data_chunk_size); // size of data chunk
fwrite(tape->pt_start, 1, data_chunk_size, file);
}
void write_fourcc(FILE * file, char * fourcc) {
fwrite(fourcc, 1, 4, file);
}
void write_uint16(FILE * file, unsigned short value) {
for (int i = 0; i < 2; i++)
fputc((value >> (i * 8)) & 0xFF, file);
}
void write_uint32(FILE * file, unsigned int value) {
for (int i = 0; i < 4; i++)
fputc((value >> (i * 8)) & 0xFF, file);
}
void write_cue(FILE * file, unsigned int id, unsigned int sample_num) {
write_uint32(file, id);
write_uint32(file, 0);
write_fourcc(file, DATA_CHUNK_ID);
write_uint32(file, 0);
write_uint32(file, sample_num * sizeof(sample));
write_uint32(file, sample_num * sizeof(sample));
}