summaryrefslogtreecommitdiff
path: root/maestropond.c
blob: 7fbbfc04cd34a149427d54afe5fb91dfdf3285c6 (plain)
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
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
/*
 * maestropond.c
 *
 * Convert Acorn Maestro files to LilyPond files
 *
 * (c) 2011 Thomas White <taw@physics.org>
 *
 */


#ifdef HAVE_CONFIG_H
#include <config.h>
#endif

#include <stdarg.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include <getopt.h>
#include <sys/types.h>
#include <sys/stat.h>


static const int bpm[] = {
	40, 50, 60, 65, 70, 80, 90, 100, 115,
	130, 145, 160, 175, 190, 210
};

static void show_syntax(const char *s)
{
	printf("Syntax: %s [options]\n", s);
}


static void show_help(const char *s)
{
	show_syntax(s);
	printf(
"\nConvert Acorn Maestro files to LilyPond files.\n"
"\n"
"  -h, --help                 Display this help message.\n"
"\n"
);
}


static unsigned int get_basic_int(unsigned char *f, size_t *pptr)
{
	unsigned int v;
	size_t ptr = *pptr;
	int sig;

	v = 0;
	sig = f[ptr++];
	if ( sig != 0x40 ) {
		fprintf(stderr, "Not a BASIC integer (sig %i, val %i)\n",
		                sig, v);
		goto out;
	}

	v += f[ptr++] << 24;
	v += f[ptr++] << 16;
	v += f[ptr++] << 8;
	v += f[ptr++];

out:
	*pptr = ptr;
	return v;
}


static void music_attribute(unsigned char ma)
{
	if ( (ma & 0x7f) == 0x40 ) {

		printf("Warning: reserved gate type.\n");

	} else if ( (ma & 0x3f) == 0x20 ) {

		printf("Bar line\n");

	} else if ( (ma & 0x1f) == 0x10 ) {

		printf("Bar line\n");

	} else if ( (ma & 0xf) == 0x8 ) {

		printf("Octave shift\n");

	} else if ( (ma & 0x7) == 0x4 ) {

		int st;

		st = 1 + ((ma & 0xc0) >> 6);
		if ( ma & 0x10 ) {
			printf("Slur on (stave %i)\n", st);
		} else {
			printf("Slur off (stave %i)\n", st);
		}

	} else if ( (ma & 0x3) == 0x2 ) {

		int ct, st;

		ct = (ma & 0x18) >> 3;
		switch ( ct ) {

			case 0 :
			printf("Treble clef:");
			break;

			case 1 :
			printf("Alto clef:");
			break;

			case 2 :
			printf("Tenor clef:");
			break;

			case 3 :
			printf("Bass clef:");
			break;
		}

		st = (ma & 0x60) >> 5;
		printf(" stave %i\n", st);

	} else if ( (ma & 0x1) == 0x1 ) {

		int tn, td;

		tn = 1 + ((ma & 0x1e) >> 1);
		td = 1 + ((ma & 0xe0) >> 5);
		printf("Time signature %i/%i\n", tn, td);
	}
}



static void get_note(unsigned char **notes, int *nptrs, int ch)
{
	unsigned char n1, n2;
	int rest = 0;

	n1 = notes[ch][nptrs[ch]++];

	if ( n1 & 0xf8 ) {
		n2 = notes[ch][nptrs[ch]++];
	} else {
		rest = 1;
	}

	if ( rest ) {
		printf("rest %i\n", n1);
	} else {
		printf("note %i:%i\n", n1, n2);
	}
}


static void interpret_gates(unsigned char *gates, unsigned char **notes,
                            int n_gates)
{
	int i;
	int ma = 0;
	int nptrs[8];

	for ( i=0; i<8; i++ ) nptrs[i] = 0;

	for ( i=0; i<n_gates; i++ ) {

		if ( ma ) {
			music_attribute(gates[i]);
			ma = 0;
			continue;
		}

		if ( gates[i] == 0 ) {
			ma = 1;
			continue;
		} else {

			if ( gates[i] & 0x1 ) get_note(notes, nptrs, 0);
			if ( gates[i] & 0x2 ) get_note(notes, nptrs, 1);
			if ( gates[i] & 0x4 ) get_note(notes, nptrs, 2);
			if ( gates[i] & 0x8 ) get_note(notes, nptrs, 3);
			if ( gates[i] & 0x10 ) get_note(notes, nptrs, 4);
			if ( gates[i] & 0x20 ) get_note(notes, nptrs, 5);
			if ( gates[i] & 0x40 ) get_note(notes, nptrs, 6);
			if ( gates[i] & 0x80 ) get_note(notes, nptrs, 7);

		}
	}
}


static size_t process_music_data(unsigned char *f, size_t ptr, size_t len)
{
	unsigned int n_gates;
	unsigned int i;
	unsigned int lengths[8];
	unsigned char *gates;
	unsigned char *notes[8];

	n_gates = get_basic_int(f, &ptr);
	printf("%i gates\n", n_gates);

	for ( i=0; i<8; i++ ) {
		lengths[i] = get_basic_int(f, &ptr);
		printf("Channel %i, length %i\n", i+1, lengths[i]);
	}

	gates = malloc(n_gates);
	if ( gates == NULL ) {
		fprintf(stderr, "Failed to allocate gates\n");
	}
	for ( i=0; i<n_gates; i++ ) {
		gates[i] = f[ptr++];
	}

	for ( i=0; i<8; i++ ) {

		unsigned int j;

		notes[i] = malloc(lengths[i]);
		if ( notes[i] == NULL ) {
			fprintf(stderr, "Failed to allocate notes\n");
		}
		for ( j=0; j<lengths[i]; j++ ) {
			notes[i][j] = f[ptr++];
		}
	}

	interpret_gates(gates, notes, n_gates);

	return ptr;
}


static size_t process_stave_data(unsigned char *f, size_t ptr)
{
	int n_staves, n_perc;

	n_staves = 1 + f[ptr++];
	n_perc = f[ptr++];

	printf("%i staves, %i percussion\n", n_staves, n_perc);

	return ptr;
}


static size_t process_instrument_data(unsigned char *f, size_t ptr)
{
	int i;

	for ( i=0; i<8; i++ ) {
		int ch, v;
		ch = f[ptr++];  v = f[ptr++];
		//printf("Channel %i, voice %i\n", ch+1, v);
	}

	return ptr;
}


static size_t process_volume_data(unsigned char *f, size_t ptr)
{
	int i;

	for ( i=0; i<8; i++ ) {
		int v;
		v = f[ptr++];
		//printf("Channel %i, volume %i\n", i+1, v);
	}

	return ptr;
}


static size_t process_stereo_data(unsigned char *f, size_t ptr)
{
	int i;

	for ( i=0; i<8; i++ ) {
		int st;
		st = f[ptr++];
		//printf("Channel %i, stereo %i\n", i+1, st);
	}

	return ptr;
}


static size_t process_tempo_data(unsigned char *f, size_t ptr)
{
	int tempo;

	tempo = f[ptr++];
	printf("Tempo = %i bpm\n", bpm[tempo]);

	return ptr;
}


static void convert_file(const char *filename)
{
	struct stat statbuf;
	FILE *fh;
	unsigned char *f;
	size_t r, ptr;

	if ( stat(filename, &statbuf) == -1 ) {
		fprintf(stderr, "Couldn't file file '%s'\n", filename);
		return;
	}
	f = malloc(statbuf.st_size);
	if ( f == NULL ) {
		fprintf(stderr, "Couldn't allocate memory.\n");
		return;
	}

	/* Load data */
	fh = fopen(filename, "rb");
	if ( fh == NULL ) {
		fprintf(stderr, "Failed to open file '%s'\n", filename);
		return;
	}
	r = fread(f, 1, statbuf.st_size, fh);
	if ( r != (size_t)statbuf.st_size ) {
		fprintf(stderr, "Failed to read file"
		                " (got %lli out of %lli bytes).\n",
		                (long long int)r,
		                (long long int)statbuf.st_size);
		fclose(fh);
		free(f);
		return;
	}
	fclose(fh);

	if ( memcmp(f, "Maestro\n", 8) != 0 ) {
		fprintf(stderr, "Not a Maestro file.\n");
		free(f);
		return;
	}

	if ( f[8] != 2 ) {
		fprintf(stderr, "Unrecognised Maestro file type (%i)\n", f[8]);
		free(f);
		return;
	}

	ptr = 9;
	while ( ptr < r ) {
		switch ( f[ptr++] ) {

			case 1 :
			ptr = process_music_data(f, ptr, r);
			break;

			case 2 :
			ptr = process_stave_data(f, ptr);
			break;

			case 3 :
			ptr = process_instrument_data(f, ptr);
			break;

			case 4 :
			ptr = process_volume_data(f, ptr);
			break;

			case 5 :
			ptr = process_stereo_data(f, ptr);
			break;

			case 6 :
			ptr = process_tempo_data(f, ptr);
			break;

		}
	}
}


int main(int argc, char *argv[])
{
	int c;
	char *infile;

	/* Long options */
	const struct option longopts[] = {
		{"help",               0, NULL,               'h'},
		{0, 0, NULL, 0}
	};

	/* Short options */
	while ((c = getopt_long(argc, argv, "h",
	                        longopts, NULL)) != -1) {

		switch (c) {
		case 'h' :
			show_help(argv[0]);
			return 0;

		case 0 :
			break;

		default :
			return 1;
		}

	}
	if ( optind >= argc ) {
		show_syntax(argv[0]);
		return 1;
	}

	infile = argv[optind++];
	printf("Input: '%s'\n", infile);
	convert_file(infile);

	return 0;
}