fixes
[lcr.git] / gentones.c
1
2 /* isdn tone generation
3   by jolly
4 */
5
6 #include <stdio.h>
7 #include <math.h>
8 #include <stdlib.h>
9 #include <string.h>
10
11
12 /* ulaw -> signed 16-bit */
13 static short isdn_audio_ulaw_to_s16[] =
14 {
15         0x8284, 0x8684, 0x8a84, 0x8e84, 0x9284, 0x9684, 0x9a84, 0x9e84,
16         0xa284, 0xa684, 0xaa84, 0xae84, 0xb284, 0xb684, 0xba84, 0xbe84,
17         0xc184, 0xc384, 0xc584, 0xc784, 0xc984, 0xcb84, 0xcd84, 0xcf84,
18         0xd184, 0xd384, 0xd584, 0xd784, 0xd984, 0xdb84, 0xdd84, 0xdf84,
19         0xe104, 0xe204, 0xe304, 0xe404, 0xe504, 0xe604, 0xe704, 0xe804,
20         0xe904, 0xea04, 0xeb04, 0xec04, 0xed04, 0xee04, 0xef04, 0xf004,
21         0xf0c4, 0xf144, 0xf1c4, 0xf244, 0xf2c4, 0xf344, 0xf3c4, 0xf444,
22         0xf4c4, 0xf544, 0xf5c4, 0xf644, 0xf6c4, 0xf744, 0xf7c4, 0xf844,
23         0xf8a4, 0xf8e4, 0xf924, 0xf964, 0xf9a4, 0xf9e4, 0xfa24, 0xfa64,
24         0xfaa4, 0xfae4, 0xfb24, 0xfb64, 0xfba4, 0xfbe4, 0xfc24, 0xfc64,
25         0xfc94, 0xfcb4, 0xfcd4, 0xfcf4, 0xfd14, 0xfd34, 0xfd54, 0xfd74,
26         0xfd94, 0xfdb4, 0xfdd4, 0xfdf4, 0xfe14, 0xfe34, 0xfe54, 0xfe74,
27         0xfe8c, 0xfe9c, 0xfeac, 0xfebc, 0xfecc, 0xfedc, 0xfeec, 0xfefc,
28         0xff0c, 0xff1c, 0xff2c, 0xff3c, 0xff4c, 0xff5c, 0xff6c, 0xff7c,
29         0xff88, 0xff90, 0xff98, 0xffa0, 0xffa8, 0xffb0, 0xffb8, 0xffc0,
30         0xffc8, 0xffd0, 0xffd8, 0xffe0, 0xffe8, 0xfff0, 0xfff8, 0x0000,
31         0x7d7c, 0x797c, 0x757c, 0x717c, 0x6d7c, 0x697c, 0x657c, 0x617c,
32         0x5d7c, 0x597c, 0x557c, 0x517c, 0x4d7c, 0x497c, 0x457c, 0x417c,
33         0x3e7c, 0x3c7c, 0x3a7c, 0x387c, 0x367c, 0x347c, 0x327c, 0x307c,
34         0x2e7c, 0x2c7c, 0x2a7c, 0x287c, 0x267c, 0x247c, 0x227c, 0x207c,
35         0x1efc, 0x1dfc, 0x1cfc, 0x1bfc, 0x1afc, 0x19fc, 0x18fc, 0x17fc,
36         0x16fc, 0x15fc, 0x14fc, 0x13fc, 0x12fc, 0x11fc, 0x10fc, 0x0ffc,
37         0x0f3c, 0x0ebc, 0x0e3c, 0x0dbc, 0x0d3c, 0x0cbc, 0x0c3c, 0x0bbc,
38         0x0b3c, 0x0abc, 0x0a3c, 0x09bc, 0x093c, 0x08bc, 0x083c, 0x07bc,
39         0x075c, 0x071c, 0x06dc, 0x069c, 0x065c, 0x061c, 0x05dc, 0x059c,
40         0x055c, 0x051c, 0x04dc, 0x049c, 0x045c, 0x041c, 0x03dc, 0x039c,
41         0x036c, 0x034c, 0x032c, 0x030c, 0x02ec, 0x02cc, 0x02ac, 0x028c,
42         0x026c, 0x024c, 0x022c, 0x020c, 0x01ec, 0x01cc, 0x01ac, 0x018c,
43         0x0174, 0x0164, 0x0154, 0x0144, 0x0134, 0x0124, 0x0114, 0x0104,
44         0x00f4, 0x00e4, 0x00d4, 0x00c4, 0x00b4, 0x00a4, 0x0094, 0x0084,
45         0x0078, 0x0070, 0x0068, 0x0060, 0x0058, 0x0050, 0x0048, 0x0040,
46         0x0038, 0x0030, 0x0028, 0x0020, 0x0018, 0x0010, 0x0008, 0x0000
47 };
48
49 /* alaw -> signed 16-bit */
50 static short isdn_audio_alaw_to_s16[] =
51 {
52         0x13fc, 0xec04, 0x0144, 0xfebc, 0x517c, 0xae84, 0x051c, 0xfae4,
53         0x0a3c, 0xf5c4, 0x0048, 0xffb8, 0x287c, 0xd784, 0x028c, 0xfd74,
54         0x1bfc, 0xe404, 0x01cc, 0xfe34, 0x717c, 0x8e84, 0x071c, 0xf8e4,
55         0x0e3c, 0xf1c4, 0x00c4, 0xff3c, 0x387c, 0xc784, 0x039c, 0xfc64,
56         0x0ffc, 0xf004, 0x0104, 0xfefc, 0x417c, 0xbe84, 0x041c, 0xfbe4,
57         0x083c, 0xf7c4, 0x0008, 0xfff8, 0x207c, 0xdf84, 0x020c, 0xfdf4,
58         0x17fc, 0xe804, 0x018c, 0xfe74, 0x617c, 0x9e84, 0x061c, 0xf9e4,
59         0x0c3c, 0xf3c4, 0x0084, 0xff7c, 0x307c, 0xcf84, 0x030c, 0xfcf4,
60         0x15fc, 0xea04, 0x0164, 0xfe9c, 0x597c, 0xa684, 0x059c, 0xfa64,
61         0x0b3c, 0xf4c4, 0x0068, 0xff98, 0x2c7c, 0xd384, 0x02cc, 0xfd34,
62         0x1dfc, 0xe204, 0x01ec, 0xfe14, 0x797c, 0x8684, 0x07bc, 0xf844,
63         0x0f3c, 0xf0c4, 0x00e4, 0xff1c, 0x3c7c, 0xc384, 0x03dc, 0xfc24,
64         0x11fc, 0xee04, 0x0124, 0xfedc, 0x497c, 0xb684, 0x049c, 0xfb64,
65         0x093c, 0xf6c4, 0x0028, 0xffd8, 0x247c, 0xdb84, 0x024c, 0xfdb4,
66         0x19fc, 0xe604, 0x01ac, 0xfe54, 0x697c, 0x9684, 0x069c, 0xf964,
67         0x0d3c, 0xf2c4, 0x00a4, 0xff5c, 0x347c, 0xcb84, 0x034c, 0xfcb4,
68         0x12fc, 0xed04, 0x0134, 0xfecc, 0x4d7c, 0xb284, 0x04dc, 0xfb24,
69         0x09bc, 0xf644, 0x0038, 0xffc8, 0x267c, 0xd984, 0x026c, 0xfd94,
70         0x1afc, 0xe504, 0x01ac, 0xfe54, 0x6d7c, 0x9284, 0x06dc, 0xf924,
71         0x0dbc, 0xf244, 0x00b4, 0xff4c, 0x367c, 0xc984, 0x036c, 0xfc94,
72         0x0f3c, 0xf0c4, 0x00f4, 0xff0c, 0x3e7c, 0xc184, 0x03dc, 0xfc24,
73         0x07bc, 0xf844, 0x0008, 0xfff8, 0x1efc, 0xe104, 0x01ec, 0xfe14,
74         0x16fc, 0xe904, 0x0174, 0xfe8c, 0x5d7c, 0xa284, 0x05dc, 0xfa24,
75         0x0bbc, 0xf444, 0x0078, 0xff88, 0x2e7c, 0xd184, 0x02ec, 0xfd14,
76         0x14fc, 0xeb04, 0x0154, 0xfeac, 0x557c, 0xaa84, 0x055c, 0xfaa4,
77         0x0abc, 0xf544, 0x0058, 0xffa8, 0x2a7c, 0xd584, 0x02ac, 0xfd54,
78         0x1cfc, 0xe304, 0x01cc, 0xfe34, 0x757c, 0x8a84, 0x075c, 0xf8a4,
79         0x0ebc, 0xf144, 0x00d4, 0xff2c, 0x3a7c, 0xc584, 0x039c, 0xfc64,
80         0x10fc, 0xef04, 0x0114, 0xfeec, 0x457c, 0xba84, 0x045c, 0xfba4,
81         0x08bc, 0xf744, 0x0018, 0xffe8, 0x227c, 0xdd84, 0x022c, 0xfdd4,
82         0x18fc, 0xe704, 0x018c, 0xfe74, 0x657c, 0x9a84, 0x065c, 0xf9a4,
83         0x0cbc, 0xf344, 0x0094, 0xff6c, 0x327c, 0xcd84, 0x032c, 0xfcd4
84 };
85
86
87 unsigned char encode_isdn(short sample, char law)
88 {
89         int best=-1;
90         int i,diff;
91         int best_diff;
92
93         i=0;
94         while(i<256)
95         {
96                 diff = (law=='u')?isdn_audio_ulaw_to_s16[i]:isdn_audio_alaw_to_s16[i]-sample;
97 //printf("s16=%d sample%d diff=%d\n",isdn_audio_to_s16[i],sample,diff);
98                 if (diff<0)
99                         diff=0-diff;
100 //printf("diff=%d\n",diff);
101
102                 if (diff<best_diff || best<0)
103                 {
104 //printf("better %d\n",i);
105                         best_diff=diff;
106                         best=i;
107                 }
108                 i++;
109         }
110         return(best);
111 }
112
113
114 void write_tone(FILE *fp,double t1,double t2,int length,int fade_in,int fade_out, char law)
115 {
116         double x,s,t,fade;
117         int i;
118
119         i=0;
120         while(i<length)
121         {
122                 if (i < fade_in)
123                         fade=(double)i / (double)fade_in;
124                 else    fade=1.0;
125                 if (length-1-i < fade_out)
126                         fade=((double)length-1.0-(double)i) / (double)fade_out;
127
128                 s = (double)i / 8000.0 * (double)t1 * 3.1415927F * 2.0;
129                 t = (double)i / 8000.0 * (double)t2 * 3.1415927F * 2.0;
130                 x = sin(s)+sin(t);
131 //printf("%e,%e,%e\n",s,t,x);
132                 fputc(encode_isdn((short)(x * 6000.0 * fade), law),fp);
133                 i++;
134         }
135 }
136
137 struct fmt {
138         unsigned short  stereo; /* 1 = pcm, 2 = adpcm */
139         unsigned short  channels; /* number of channels */
140         unsigned long   sample_rate; /* sample rate */
141         unsigned long   data_rate; /* data rate */
142         unsigned short  bytes_sample; /* bytes per sample (all channels) */
143         unsigned short  bits_sample; /* bits per sample (one channel) */
144 };
145
146 void write_wav(FILE *fp, char *wav, char law)
147 {
148         unsigned char buffer[256];
149         struct fmt *fmt;
150         FILE *wfp;
151         signed long i;
152         int channels, bytes;
153         short sample, sample2;
154         signed long size, chunk;
155         int gotfmt = 0, gotdata = 0;
156
157         if ((wfp=fopen(wav,"r")))
158         {
159                 fread(buffer,8,1,wfp);
160                 size=(buffer[4]) + (buffer[5]<<8) + (buffer[6]<<16) + (buffer[7]<<24);
161                 if (!!strncmp((char *)buffer, "RIFF", 4))
162                 {
163                         fclose(wfp);
164                         fprintf(stderr, "Error: %s is no riff file!\n", wav);
165                         return;
166                 }
167                 printf("%c%c%c%c size=%ld\n",buffer[0],buffer[1],buffer[2],buffer[3],size);
168                 fread(buffer,4,1,wfp);
169                 size -= 4;
170                 if (!!strncmp((char *)buffer, "WAVE", 4))
171                 {
172                         fclose(wfp);
173                         fprintf(stderr, "Error: %s is no wave file!\n", wav);
174                         return;
175                 }
176                 while(size > 0)
177                 {
178                         if (size>0 && size<8)
179                         {
180                                 fclose(wfp);
181                                 fprintf(stderr, "Error: Remaining file size %ld not large enough for next chunk.\n",size);
182                                 return;
183                         }
184                         fread(buffer,8,1,wfp);
185                         chunk=(buffer[4]) + (buffer[5]<<8) + (buffer[6]<<16) + (buffer[7]<<24);
186 //printf("DEBUG: size(%ld) - (8+chunk(%ld) = size(%ld)\n", size, chunk, size-chunk-8);
187                         size -= (8+chunk);
188                         if (size < 0)
189                         {
190                                 fclose(wfp);
191                                 fprintf(stderr, "Error: Chunk '%c%c%c%c' is larger than remainig file size (length=%ld)\n",buffer[0],buffer[1],buffer[2],buffer[3], chunk);
192                                 return;
193                         }
194 //                      printf("%c%c%c%c lenght=%d\n",buffer[0],buffer[1],buffer[2],buffer[3],chunk);
195                         if (!strncmp((char *)buffer, "fmt ", 4))
196                         {
197                                 if (chunk != 16)
198                                 {
199                                         fclose(wfp);
200                                         fprintf(stderr, "Error: Fmt chunk illegal size.\n");
201                                         return;
202                                 }
203                                 fread(buffer, chunk, 1, wfp);
204                                 fmt = (struct fmt *)buffer;
205                                 if (fmt->channels<1 || fmt->channels>2)
206                                 {
207                                         fclose(wfp);
208                                         fprintf(stderr, "Error: Only support one or two channels file.\n");
209                                         return;
210                                 }
211                                 channels = fmt->channels;
212                                 printf("Channels: %d\n", channels);
213                                 if (fmt->sample_rate != 8000)
214                                 {
215                                         fprintf(stderr, "Warning: File has sample rate of %ld.\n", fmt->sample_rate);
216                                 }
217                                 printf("Sample Rate: %ld\n", fmt->sample_rate);
218                                 if (fmt->bits_sample!=8 && fmt->bits_sample!=16)
219                                 {
220                                         fclose(wfp);
221                                         fprintf(stderr, "Error: File has neigher 8 nor 16 bit samples.\n");
222                                         return;
223                                 }
224                                 bytes = (fmt->bits_sample==16)?2:1;
225                                 printf("Bit-Resolution: %d\n", bytes*16-16);
226                                 gotfmt = 1;
227                         } else
228                         if (!strncmp((char *)buffer, "data", 4))
229                         {
230                                 if (!gotfmt)
231                                 {
232                                         fclose(wfp);
233                                         fprintf(stderr, "Error: No fmt chunk fount in file.\n");
234                                         return;
235                                 }
236                                 printf("Length: %ld samples (%ld.%03ld seconds)\n", chunk/bytes/channels, chunk/bytes/channels/8000, ((chunk/bytes/channels)%8000)*1000/8000);
237                                 i=0;
238                                 if (bytes==2 && channels==1)
239                                 {
240                                         while(i<chunk)
241                                         {
242                                                 fread(buffer, 2, 1, wfp);
243                                                 sample=(buffer[1]<<8) + (buffer[0]);
244                                                 fputc(encode_isdn(sample, law),fp);
245                                                 i+=2;
246                                         }
247                                 }
248                                 if (bytes==2 && channels==2)
249                                 {
250                                         while(i<chunk)
251                                         {
252                                                 fread(buffer, 4, 1, wfp);
253                                                 sample=(buffer[1]<<8) + (buffer[0]);
254                                                 sample2=(buffer[3]<<8) + (buffer[2]);
255                                                 sample = (sample/2) + (sample2/2);
256                                                 fputc(encode_isdn(sample, law),fp);
257                                                 i+=4;
258                                         }
259                                 }
260                                 if (bytes==1 && channels==1)
261                                 {
262                                         while(i<chunk)
263                                         {
264                                                 fread(buffer, 1, 1, wfp);
265                                                 sample=(buffer[0]<<8);
266                                                 fputc(encode_isdn(sample, law),fp);
267                                                 i+=1;
268                                         }
269                                 }
270                                 if (bytes==1 && channels==2)
271                                 {
272                                         while(i<chunk)
273                                         {
274                                                 fread(buffer, 2, 1, wfp);
275                                                 sample=(buffer[0]<<8);
276                                                 sample2=(buffer[1]<<8);
277                                                 sample = (sample/2) + (sample2/2);
278                                                 fputc(encode_isdn(sample, law),fp);
279                                                 i+=2;
280                                         }
281                                 }
282                                 gotdata = 1;
283                         } else
284                         {
285                                 printf("Ignoring chunk '%c%c%c%c' (length=%ld)\n",buffer[0],buffer[1],buffer[2],buffer[3], chunk);
286                                 while(chunk > (signed long)sizeof(buffer))
287                                 {
288                                         fread(buffer, sizeof(buffer), 1, wfp);
289                                         chunk -=  sizeof(buffer);
290                                 }
291                                 if (chunk)
292                                         fread(buffer, chunk, 1, wfp);
293                         }
294                         
295                 }
296                 fclose(wfp);
297                 if (!gotdata)
298                 {
299                         fprintf(stderr, "Error: No data chunk fount in file.\n");
300                         return;
301                 }
302         }
303 }
304
305 int main(int argc, char *argv[])
306 {
307         FILE *fp;
308
309         if (argc <= 1)
310         {
311                 usage:
312                 printf("Usage:\n");
313                 printf("%s wave2alaw <wav file> <alaw file>\n", argv[0]);
314                 printf("%s wave2ulaw <wav file> <ulaw file>\n", argv[0]);
315                 printf("%s tone2alaw <frq1> <frq2> <length> <fade in> <fade out> <alaw file>\n", argv[0]);
316                 printf("%s tone2ulaw <frq1> <frq2> <length> <fade in> <fade out> <ulaw file>\n", argv[0]);
317                 printf("Length and fade lengths must be given in samples (8000 samples are one second).\n");
318                 printf("Tones will append to existing files, wav files don't.\n");
319                 printf("Frequencies may be given as floating point values.\n");
320                 return(0);
321         }
322
323         if (!strcmp(argv[1], "wave2alaw"))
324         {
325                 if (argc <= 3)
326                         goto usage;
327                 if ((fp=fopen(argv[3],"w")))
328                 {
329                         write_wav(fp,argv[2],'a');
330                         fclose(fp);
331                 } else
332                 {
333                         printf("Cannot open isdn file %s\n",argv[3]);
334                 }
335         } else
336         if (!strcmp(argv[1], "wave2ulaw"))
337         {
338                 if (argc <= 3)
339                         goto usage;
340                 if ((fp=fopen(argv[3],"w")))
341                 {
342                         write_wav(fp,argv[2],'u');
343                         fclose(fp);
344                 } else
345                 {
346                         printf("Cannot open isdn file %s\n",argv[3]);
347                 }
348         } else
349         if (!strcmp(argv[1], "tone2alaw"))
350         {
351                 if (argc <= 7)
352                         goto usage;
353                 if ((fp=fopen(argv[7],"a")))
354                 {
355                         write_tone(fp,strtod(argv[2],NULL),strtod(argv[3],NULL),atoi(argv[4]),atoi(argv[5]),atoi(argv[6]),'a');
356                         fclose(fp);
357                 } else
358                 {
359                         printf("Cannot open isdn file %s\n",argv[7]);
360                 }
361
362         } else
363         if (!strcmp(argv[1], "tone2ulaw"))
364         {
365                 if (argc <= 7)
366                         goto usage;
367                 if ((fp=fopen(argv[7],"a")))
368                 {
369                         write_tone(fp,atoi(argv[2]),atoi(argv[3]),atoi(argv[4]),atoi(argv[5]),atoi(argv[6]),'u');
370                         fclose(fp);
371                 } else
372                 {
373                         printf("Cannot open isdn file %s\n",argv[7]);
374                 }
375         } else
376                 goto usage;
377
378         return(0);
379 }