File: cvtsnd.c

package info (click to toggle)
glhack 1.2-1
  • links: PTS
  • area: main
  • in suites: squeeze, wheezy
  • size: 24,604 kB
  • ctags: 18,992
  • sloc: ansic: 208,570; cpp: 13,139; yacc: 2,005; makefile: 1,161; lex: 377; sh: 321; awk: 89; sed: 11
file content (96 lines) | stat: -rw-r--r-- 2,268 bytes parent folder | download | duplicates (29)
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
/*	SCCS Id: @(#)cvtsnd.c	3.2	95/09/10                  */
/* 	Copyright (c) 1995, Andrew Church, Olney, Maryland        */
/* NetHack may be freely redistributed.  See license for details. */

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

typedef struct {
    short namelen;
    char name[62];
    char misc[64];	/* rest of MacBinary header */
    long FORM;
    long flen;
    long AIFF;
    long SSND;
    long sndlen;
} AIFF;

typedef struct {
    char FORM[4];
    long flen;
    char _8SVX[4];
    char VHDR[4];
    long vhlen;
    long oneshot, repeat;
    long samples;	/* 'samplesPerHiCycle' in the docs - usually 32, so
			 *    we'll use that */
    short freq;
    char octaves, compress;
    long volume;
    char NAME[4];
    long nlen;		/* should be 64; see name[] comment */
    char name[64];	/* for simplicity, i.e. just fwrite() entiree header */
    char BODY[4];
    long blen;
} IFF;


main(int ac, char **av)
{
    FILE *in, *out;
    AIFF aiff;
    IFF iff;
    static char buf[16384];
    long n, len;

    if (ac != 3) {
	fprintf(stderr, "Usage: %s input-file output-file\n", av[0]);
	exit(20);
    }
    if (!(in = fopen(av[1], "r"))) {
	fprintf(stderr, "Can't open input file\n");
	exit(20);
    }
    if (!(out = fopen(av[2], "w"))) {
	fprintf(stderr, "Can't open output file\n");
	exit(20);
    }

    fread(&aiff, sizeof(aiff), 1, in);
    memcpy(iff.FORM, "FORM", 4);
    iff.flen	= sizeof(iff) + aiff.sndlen - 8;
    memcpy(iff._8SVX, "8SVX", 4);
    memcpy(iff.VHDR, "VHDR", 4);
    iff.vhlen	= 20;
    iff.oneshot	= aiff.sndlen;
    iff.repeat	= 0;
    iff.samples	= 32;
    iff.freq	= 22000;
    iff.octaves	= 1;
    iff.compress= 0;
    iff.volume	= 0x10000;
    memcpy(iff.NAME, "NAME", 4);
    iff.nlen	= 64;
    strncpy(iff.name, aiff.name, 62); iff.name[aiff.namelen] = 0;
    memcpy(iff.BODY, "BODY", 4);
    iff.blen	= aiff.sndlen;
    fwrite(&iff, sizeof(iff), 1, out);
    len = aiff.sndlen;
    do {
	if (len >= sizeof(buf))
	    n = fread(buf, 1, sizeof(buf), in);
	else
	    n = fread(buf, 1, len, in);
	if (n) {
	    fwrite(buf, 1, n, out);
	    len -= n;
	}
    } while (len && n);

    if (len)
	fprintf(stderr, "Warning: %ld bytes of sample missing\n", len);
    fclose(in); fclose(out);
    exit(0);
}