1 /*	SCCS Id: @(#)cvtsnd.c	3.2	95/09/10                  */
2 /* 	Copyright (c) 1995, Andrew Church, Olney, Maryland        */
3 /* NetHack may be freely redistributed.  See license for details. */
4 
5 #include <stdio.h>
6 #include <stdlib.h>
7 #include <string.h>
8 
9 typedef struct {
10     short namelen;
11     char name[62];
12     char misc[64];	/* rest of MacBinary header */
13     long FORM;
14     long flen;
15     long AIFF;
16     long SSND;
17     long sndlen;
18 } AIFF;
19 
20 typedef struct {
21     char FORM[4];
22     long flen;
23     char _8SVX[4];
24     char VHDR[4];
25     long vhlen;
26     long oneshot, repeat;
27     long samples;	/* 'samplesPerHiCycle' in the docs - usually 32, so
28 			 *    we'll use that */
29     short freq;
30     char octaves, compress;
31     long volume;
32     char NAME[4];
33     long nlen;		/* should be 64; see name[] comment */
34     char name[64];	/* for simplicity, i.e. just fwrite() entiree header */
35     char BODY[4];
36     long blen;
37 } IFF;
38 
39 
main(int ac,char ** av)40 main(int ac, char **av)
41 {
42     FILE *in, *out;
43     AIFF aiff;
44     IFF iff;
45     static char buf[16384];
46     long n, len;
47 
48     if (ac != 3) {
49 	fprintf(stderr, "Usage: %s input-file output-file\n", av[0]);
50 	exit(20);
51     }
52     if (!(in = fopen(av[1], "r"))) {
53 	fprintf(stderr, "Can't open input file\n");
54 	exit(20);
55     }
56     if (!(out = fopen(av[2], "w"))) {
57 	fprintf(stderr, "Can't open output file\n");
58 	exit(20);
59     }
60 
61     fread(&aiff, sizeof(aiff), 1, in);
62     memcpy(iff.FORM, "FORM", 4);
63     iff.flen	= sizeof(iff) + aiff.sndlen - 8;
64     memcpy(iff._8SVX, "8SVX", 4);
65     memcpy(iff.VHDR, "VHDR", 4);
66     iff.vhlen	= 20;
67     iff.oneshot	= aiff.sndlen;
68     iff.repeat	= 0;
69     iff.samples	= 32;
70     iff.freq	= 22000;
71     iff.octaves	= 1;
72     iff.compress= 0;
73     iff.volume	= 0x10000;
74     memcpy(iff.NAME, "NAME", 4);
75     iff.nlen	= 64;
76     strncpy(iff.name, aiff.name, 62); iff.name[aiff.namelen] = 0;
77     memcpy(iff.BODY, "BODY", 4);
78     iff.blen	= aiff.sndlen;
79     fwrite(&iff, sizeof(iff), 1, out);
80     len = aiff.sndlen;
81     do {
82 	if (len >= sizeof(buf))
83 	    n = fread(buf, 1, sizeof(buf), in);
84 	else
85 	    n = fread(buf, 1, len, in);
86 	if (n) {
87 	    fwrite(buf, 1, n, out);
88 	    len -= n;
89 	}
90     } while (len && n);
91 
92     if (len)
93 	fprintf(stderr, "Warning: %ld bytes of sample missing\n", len);
94     fclose(in); fclose(out);
95     exit(0);
96 }
97