Copy 1.4.22-rc5 to 1.4.22
[asterisk-bristuff.git] / channels / gentone-ulaw.c
blobb290d76c7a608a63878d2b775cded372ae20e4ae
1 /* Generate a header file for a particular
2 single or double frequency */
4 #include <stdio.h>
5 #include <math.h>
6 #include <string.h>
7 #include <unistd.h>
8 #include <stdlib.h>
9 #define CLIP 32635
10 #define BIAS 0x84
11 static float loudness=16384.0;
13 static int calc_samples(int freq)
15 int x, samples;
16 /* Calculate the number of samples at 8000hz sampling
17 we need to have this wave form */
18 samples = 8000;
19 /* Take out common 2's up to six times */
20 for (x=0;x<6;x++)
21 if (!(freq % 2)) {
22 freq /= 2;
23 samples /= 2;
25 /* Take out common 5's (up to three times */
26 for (x=0;x<3;x++)
27 if (!(freq % 5)) {
28 freq /= 5;
29 samples /=5;
31 /* No more common factors. */
32 return samples;
36 ** This routine converts from linear to ulaw
38 ** Craig Reese: IDA/Supercomputing Research Center
39 ** Joe Campbell: Department of Defense
40 ** 29 September 1989
42 ** References:
43 ** 1) CCITT Recommendation G.711 (very difficult to follow)
44 ** 2) "A New Digital Technique for Implementation of Any
45 ** Continuous PCM Companding Law," Villeret, Michel,
46 ** et al. 1973 IEEE Int. Conf. on Communications, Vol 1,
47 ** 1973, pg. 11.12-11.17
48 ** 3) MIL-STD-188-113,"Interoperability and Performance Standards
49 ** for Analog-to_Digital Conversion Techniques,"
50 ** 17 February 1987
52 ** Input: Signed 16 bit linear sample
53 ** Output: 8 bit ulaw sample
56 #define ZEROTRAP /* turn on the trap as per the MIL-STD */
57 #define BIAS 0x84 /* define the add-in bias for 16 bit samples */
58 #define CLIP 32635
60 static unsigned char linear2ulaw(short sample) {
61 static int exp_lut[256] = {0,0,1,1,2,2,2,2,3,3,3,3,3,3,3,3,
62 4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,
63 5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,
64 5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,
65 6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,
66 6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,
67 6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,
68 6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,
69 7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,
70 7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,
71 7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,
72 7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,
73 7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,
74 7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,
75 7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,
76 7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7};
77 int sign, exponent, mantissa;
78 unsigned char ulawbyte;
80 /* Get the sample into sign-magnitude. */
81 sign = (sample >> 8) & 0x80; /* set aside the sign */
82 if (sign != 0) sample = -sample; /* get magnitude */
83 if (sample > CLIP) sample = CLIP; /* clip the magnitude */
85 /* Convert from 16 bit linear to ulaw. */
86 sample = sample + BIAS;
87 exponent = exp_lut[(sample >> 7) & 0xFF];
88 mantissa = (sample >> (exponent + 3)) & 0x0F;
89 ulawbyte = ~(sign | (exponent << 4) | mantissa);
90 #ifdef ZEROTRAP
91 if (ulawbyte == 0) ulawbyte = 0x02; /* optional CCITT trap */
92 #endif
94 return(ulawbyte);
97 int main(int argc, char *argv[])
99 FILE *f;
100 int freq1, freq2;
101 float wlen1, wlen2;
102 float val;
103 int x, samples1, samples2, samples=0;
104 char fn[256];
105 if (argc < 3) {
106 fprintf(stderr, "Usage: gensound <name> <freq1> [freq2]\n");
107 exit(1);
109 freq1 = atoi(argv[2]);
110 if (argc > 3)
111 freq2 = atoi(argv[3]);
112 else
113 freq2 = 0;
114 wlen1 = 8000.0/(float)freq1;
115 samples1 = calc_samples(freq1);
116 printf("Wavelength 1 (in samples): %10.5f\n", wlen1);
117 printf("Minimum samples (1): %d (%f.3 wavelengths)\n", samples1, samples1 / wlen1);
118 if (freq2) {
119 wlen2 = 8000.0/(float)freq2;
120 samples2 = calc_samples(freq2);
121 printf("Wavelength 1 (in samples): %10.5f\n", wlen2);
122 printf("Minimum samples (1): %d (%f.3 wavelengths)\n", samples2, samples2 / wlen2);
124 samples = samples1;
125 if (freq2) {
126 while(samples % samples2)
127 samples += samples1;
129 printf("Need %d samples\n", samples);
130 snprintf(fn, sizeof(fn), "%s.h", argv[1]);
131 if ((f = fopen(fn, "w"))) {
132 if (freq2)
133 fprintf(f, "/* %s: Generated from frequencies %d and %d \n"
134 " by gentone. %d samples */\n", fn, freq1, freq2, samples);
135 else
136 fprintf(f, "/* %s: Generated from frequency %d\n"
137 " by gentone. %d samples */\n", fn, freq1, samples);
138 fprintf(f, "static unsigned char %s[%d] = {\n\t", argv[1], samples);
139 for (x=0;x<samples;x++) {
140 val = loudness * sin((freq1 * 2.0 * M_PI * x)/8000.0);
141 if (freq2)
142 val += loudness * sin((freq2 * 2.0 * M_PI * x)/8000.0);
143 fprintf(f, "%3d, ", (int) linear2ulaw(val));
144 if (!((x+1) % 8))
145 fprintf(f, "\n\t");
147 if (x % 15)
148 fprintf(f, "\n");
149 fprintf(f, "};\n");
150 fclose(f);
151 printf("Wrote %s\n", fn);
152 } else {
153 fprintf(stderr, "Unable to open %s for writing\n", fn);
154 return 1;
156 return 0;