ascii2aifc.c
3.02 KB
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
#include <stdio.h>
#include <stdlib.h>
#include <bstring.h>
#include <assert.h>
#include <getopt.h>
#include <audiofile.h>
#include <math.h>
#define MIN(a,b) (((a)<(b))?(a):(b))
#define MAX(a,b) (((a)>(b))?(a):(b))
static char usage[] = "[-stereo] [-r sample rate] infile outfile";
main(int argc, char **argv)
{
char *ofile = NULL;
char *ifile = NULL;
extern char *optarg;
extern int optind;
int errflg=0;
int c;
int verbose;
int nchannels = 1;
double rate = 44100.0;
int bytesPerSample = 2;
int bitsPerSample = bytesPerSample << 3;
AFfilesetup setup;
AFfilehandle file;
FILE *asciifile;
int count;
short buf[2];
long w0, w1;
while ((c = getopt(argc, argv, "svr:")) != EOF) {
switch (c) {
case 's':
nchannels = 2;
break;
case 'r':
rate = atof(optarg);
break;
case 'v':
verbose = 1;
break;
case '?':
errflg++;
break;
}
}
if (errflg || optind == argc) {
(void)fprintf(stderr, "%s %s\n", argv[0], usage);
exit (2);
}
/*
* open the input file
*/
ifile = argv[optind++];
if (optind != argc - 1) {
(void)fprintf(stderr, "%s %s\n", argv[0], usage);
}
asciifile = fopen(ifile, "r");
if (!asciifile) {
(void)fprintf(stderr, "error opening file %s\n", ifile);
}
/*
* open the output file
*/
ofile = argv[optind++];
if (optind != argc) {
(void)fprintf(stderr, "%s %s\n", argv[0], usage);
}
setup = AFnewfilesetup();
AFinitchannels(setup, AF_DEFAULT_TRACK, nchannels);
AFinitsampfmt(setup, AF_DEFAULT_TRACK, AF_SAMPFMT_TWOSCOMP, bitsPerSample);
AFinitrate(setup, AF_DEFAULT_TRACK, rate);
AFinitcompression(setup, AF_DEFAULT_TRACK, AF_COMPRESSION_NONE);
AFinitfilefmt(setup, AF_FILE_AIFFC);
file = AFopenfile(ofile, "w", setup);
if (file == AF_NULL_FILEHANDLE) {
printf("error opening file %s\n", ofile);
exit(1);
}
/*
* ### not very efficient, but quick to code
*/
while (1) {
if (nchannels == 1) {
count = fscanf(asciifile, "%x\n", &w0);
buf[0] = (short)w0;
} else {
count = fscanf(asciifile, "%x %x\n", &w0, &w1);
buf[0] = (short)w0;
buf[1] = (short)w1;
}
if (count == EOF)
break;
count = AFwriteframes(file, AF_DEFAULT_TRACK, buf, 1);
if (count <= 0) {
printf("error writing file, file incomplete\n");
exit(1);
}
}
AFfreefilesetup(setup);
AFclosefile(file);
fclose(asciifile);
}