tonegen.c
4.54 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
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
#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[] = "[-f frequency] [-a db attenuation] [-stereo] \
[-n <samples>] [-r sample rate] [-hex] outfile";
main(int argc, char **argv)
{
char *ofile = NULL;
extern char *optarg;
extern int optind;
int errflg=0;
int c;
int asciihex=0;
int nchannels = 1;
double frequency = 1000.0;
double gain = (double)0x7fff;
double rate = 44100.0;
double pi = M_PI;
double sec = 5.0;
int bytesPerSample = 2;
int bitsPerSample = bytesPerSample << 3;
double db;
AFfilesetup setup;
AFfilehandle file;
FILE *hfile;
long framesPerPass;
int bytes;
short *buf;
long frames;
double phase;
double pi2_N;
int i;
long count;
int useTime = 0;
while ((c = getopt(argc, argv, "hsf:t:a:n:")) != EOF) {
switch (c) {
case 'h':
asciihex = 1;
break;
case 'f':
frequency = atof(optarg);
break;
case 'a':
db = atof(optarg);
if (db > 0)
db = -db;
gain = gain * pow(10, db/10);
break;
case 's':
nchannels = 2;
break;
case 'n':
frames = atoi(optarg);
break;
case 't':
sec = atof(optarg);
useTime = 1;
break;
case '?':
errflg++;
break;
}
}
if (errflg || optind == argc) {
(void)fprintf(stderr, "%s %s\n", argv[0], usage);
exit (2);
}
ofile = argv[optind++];
if (optind != argc) {
fprintf(stderr, "warning: only first file (%s) used, rest ignored\n",
ofile);
}
/*
* open output file and initialize
*/
if (asciihex) {
hfile = fopen(ofile, "w");
if (!hfile) {
printf("error opening file %s\n", ofile);
exit(1);
}
} else {
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);
}
}
framesPerPass = 512;
bytes = (int)(framesPerPass*nchannels*bytesPerSample);
buf = (short *)malloc(bytes);
if (!buf) {
printf("memory allocation error\n");
exit(1);
}
if (useTime)
frames = (long)(sec*rate);
phase = 0;
pi2_N = 2*pi*frequency/rate;
while (frames > 0) {
long curFrames = MIN(frames, framesPerPass);
if (nchannels == 1) {
for (i = 0; i < curFrames; i++) {
buf[i] = (short) (gain*sin(pi2_N*phase));
if (asciihex) {
fprintf(hfile, "%x \n", buf[i]);
}
phase++;
}
} else {
for (i = 0; i < curFrames*2; i+=2) {
buf[i] = buf[i+1] = (short) (gain*sin(pi2_N*phase));
if (asciihex) {
fprintf(hfile, "%x %x \n", buf[i], buf[i+1]);
}
phase++;
}
}
if (asciihex) {
count = curFrames;
} else {
count = AFwriteframes(file, AF_DEFAULT_TRACK, buf, curFrames);
if (count <= 0) {
printf("error writing file, file incomplete\n");
exit(1);
}
}
frames -= count;
}
if (asciihex) {
fclose(hfile);
} else {
AFfreefilesetup(setup);
AFclosefile(file);
}
}