Removed obsolete logging code
[lcr.git] / genwave.c
1
2 /* isdn 2 wave
3   by jolly
4 */
5
6 #include <stdio.h>
7 #include <string.h>
8 #include <math.h>
9
10
11 /* ulaw -> signed 16-bit */
12 static short isdn_audio_ulaw_to_s16[] =
13 {
14         0x8284, 0x8684, 0x8a84, 0x8e84, 0x9284, 0x9684, 0x9a84, 0x9e84,
15         0xa284, 0xa684, 0xaa84, 0xae84, 0xb284, 0xb684, 0xba84, 0xbe84,
16         0xc184, 0xc384, 0xc584, 0xc784, 0xc984, 0xcb84, 0xcd84, 0xcf84,
17         0xd184, 0xd384, 0xd584, 0xd784, 0xd984, 0xdb84, 0xdd84, 0xdf84,
18         0xe104, 0xe204, 0xe304, 0xe404, 0xe504, 0xe604, 0xe704, 0xe804,
19         0xe904, 0xea04, 0xeb04, 0xec04, 0xed04, 0xee04, 0xef04, 0xf004,
20         0xf0c4, 0xf144, 0xf1c4, 0xf244, 0xf2c4, 0xf344, 0xf3c4, 0xf444,
21         0xf4c4, 0xf544, 0xf5c4, 0xf644, 0xf6c4, 0xf744, 0xf7c4, 0xf844,
22         0xf8a4, 0xf8e4, 0xf924, 0xf964, 0xf9a4, 0xf9e4, 0xfa24, 0xfa64,
23         0xfaa4, 0xfae4, 0xfb24, 0xfb64, 0xfba4, 0xfbe4, 0xfc24, 0xfc64,
24         0xfc94, 0xfcb4, 0xfcd4, 0xfcf4, 0xfd14, 0xfd34, 0xfd54, 0xfd74,
25         0xfd94, 0xfdb4, 0xfdd4, 0xfdf4, 0xfe14, 0xfe34, 0xfe54, 0xfe74,
26         0xfe8c, 0xfe9c, 0xfeac, 0xfebc, 0xfecc, 0xfedc, 0xfeec, 0xfefc,
27         0xff0c, 0xff1c, 0xff2c, 0xff3c, 0xff4c, 0xff5c, 0xff6c, 0xff7c,
28         0xff88, 0xff90, 0xff98, 0xffa0, 0xffa8, 0xffb0, 0xffb8, 0xffc0,
29         0xffc8, 0xffd0, 0xffd8, 0xffe0, 0xffe8, 0xfff0, 0xfff8, 0x0000,
30         0x7d7c, 0x797c, 0x757c, 0x717c, 0x6d7c, 0x697c, 0x657c, 0x617c,
31         0x5d7c, 0x597c, 0x557c, 0x517c, 0x4d7c, 0x497c, 0x457c, 0x417c,
32         0x3e7c, 0x3c7c, 0x3a7c, 0x387c, 0x367c, 0x347c, 0x327c, 0x307c,
33         0x2e7c, 0x2c7c, 0x2a7c, 0x287c, 0x267c, 0x247c, 0x227c, 0x207c,
34         0x1efc, 0x1dfc, 0x1cfc, 0x1bfc, 0x1afc, 0x19fc, 0x18fc, 0x17fc,
35         0x16fc, 0x15fc, 0x14fc, 0x13fc, 0x12fc, 0x11fc, 0x10fc, 0x0ffc,
36         0x0f3c, 0x0ebc, 0x0e3c, 0x0dbc, 0x0d3c, 0x0cbc, 0x0c3c, 0x0bbc,
37         0x0b3c, 0x0abc, 0x0a3c, 0x09bc, 0x093c, 0x08bc, 0x083c, 0x07bc,
38         0x075c, 0x071c, 0x06dc, 0x069c, 0x065c, 0x061c, 0x05dc, 0x059c,
39         0x055c, 0x051c, 0x04dc, 0x049c, 0x045c, 0x041c, 0x03dc, 0x039c,
40         0x036c, 0x034c, 0x032c, 0x030c, 0x02ec, 0x02cc, 0x02ac, 0x028c,
41         0x026c, 0x024c, 0x022c, 0x020c, 0x01ec, 0x01cc, 0x01ac, 0x018c,
42         0x0174, 0x0164, 0x0154, 0x0144, 0x0134, 0x0124, 0x0114, 0x0104,
43         0x00f4, 0x00e4, 0x00d4, 0x00c4, 0x00b4, 0x00a4, 0x0094, 0x0084,
44         0x0078, 0x0070, 0x0068, 0x0060, 0x0058, 0x0050, 0x0048, 0x0040,
45         0x0038, 0x0030, 0x0028, 0x0020, 0x0018, 0x0010, 0x0008, 0x0000
46 };
47
48 /* alaw -> signed 16-bit */
49 static short isdn_audio_alaw_to_s16[] =
50 {
51         0x13fc, 0xec04, 0x0144, 0xfebc, 0x517c, 0xae84, 0x051c, 0xfae4,
52         0x0a3c, 0xf5c4, 0x0048, 0xffb8, 0x287c, 0xd784, 0x028c, 0xfd74,
53         0x1bfc, 0xe404, 0x01cc, 0xfe34, 0x717c, 0x8e84, 0x071c, 0xf8e4,
54         0x0e3c, 0xf1c4, 0x00c4, 0xff3c, 0x387c, 0xc784, 0x039c, 0xfc64,
55         0x0ffc, 0xf004, 0x0104, 0xfefc, 0x417c, 0xbe84, 0x041c, 0xfbe4,
56         0x083c, 0xf7c4, 0x0008, 0xfff8, 0x207c, 0xdf84, 0x020c, 0xfdf4,
57         0x17fc, 0xe804, 0x018c, 0xfe74, 0x617c, 0x9e84, 0x061c, 0xf9e4,
58         0x0c3c, 0xf3c4, 0x0084, 0xff7c, 0x307c, 0xcf84, 0x030c, 0xfcf4,
59         0x15fc, 0xea04, 0x0164, 0xfe9c, 0x597c, 0xa684, 0x059c, 0xfa64,
60         0x0b3c, 0xf4c4, 0x0068, 0xff98, 0x2c7c, 0xd384, 0x02cc, 0xfd34,
61         0x1dfc, 0xe204, 0x01ec, 0xfe14, 0x797c, 0x8684, 0x07bc, 0xf844,
62         0x0f3c, 0xf0c4, 0x00e4, 0xff1c, 0x3c7c, 0xc384, 0x03dc, 0xfc24,
63         0x11fc, 0xee04, 0x0124, 0xfedc, 0x497c, 0xb684, 0x049c, 0xfb64,
64         0x093c, 0xf6c4, 0x0028, 0xffd8, 0x247c, 0xdb84, 0x024c, 0xfdb4,
65         0x19fc, 0xe604, 0x01ac, 0xfe54, 0x697c, 0x9684, 0x069c, 0xf964,
66         0x0d3c, 0xf2c4, 0x00a4, 0xff5c, 0x347c, 0xcb84, 0x034c, 0xfcb4,
67         0x12fc, 0xed04, 0x0134, 0xfecc, 0x4d7c, 0xb284, 0x04dc, 0xfb24,
68         0x09bc, 0xf644, 0x0038, 0xffc8, 0x267c, 0xd984, 0x026c, 0xfd94,
69         0x1afc, 0xe504, 0x01ac, 0xfe54, 0x6d7c, 0x9284, 0x06dc, 0xf924,
70         0x0dbc, 0xf244, 0x00b4, 0xff4c, 0x367c, 0xc984, 0x036c, 0xfc94,
71         0x0f3c, 0xf0c4, 0x00f4, 0xff0c, 0x3e7c, 0xc184, 0x03dc, 0xfc24,
72         0x07bc, 0xf844, 0x0008, 0xfff8, 0x1efc, 0xe104, 0x01ec, 0xfe14,
73         0x16fc, 0xe904, 0x0174, 0xfe8c, 0x5d7c, 0xa284, 0x05dc, 0xfa24,
74         0x0bbc, 0xf444, 0x0078, 0xff88, 0x2e7c, 0xd184, 0x02ec, 0xfd14,
75         0x14fc, 0xeb04, 0x0154, 0xfeac, 0x557c, 0xaa84, 0x055c, 0xfaa4,
76         0x0abc, 0xf544, 0x0058, 0xffa8, 0x2a7c, 0xd584, 0x02ac, 0xfd54,
77         0x1cfc, 0xe304, 0x01cc, 0xfe34, 0x757c, 0x8a84, 0x075c, 0xf8a4,
78         0x0ebc, 0xf144, 0x00d4, 0xff2c, 0x3a7c, 0xc584, 0x039c, 0xfc64,
79         0x10fc, 0xef04, 0x0114, 0xfeec, 0x457c, 0xba84, 0x045c, 0xfba4,
80         0x08bc, 0xf744, 0x0018, 0xffe8, 0x227c, 0xdd84, 0x022c, 0xfdd4,
81         0x18fc, 0xe704, 0x018c, 0xfe74, 0x657c, 0x9a84, 0x065c, 0xf9a4,
82         0x0cbc, 0xf344, 0x0094, 0xff6c, 0x327c, 0xcd84, 0x032c, 0xfcd4
83 };
84
85
86 struct fmt {
87         unsigned short  stereo; /* 1 = pcm, 2 = adpcm */
88         unsigned short  channels; /* number of channels */
89         unsigned int    sample_rate; /* sample rate */
90         unsigned int    data_rate; /* data rate */
91         unsigned short  bytes_sample; /* bytes per sample (all channels) */
92         unsigned short  bits_sample; /* bits per sample (one channel) */
93 };
94
95 void write_law(FILE *fp, char *name, char law)
96 {
97         unsigned char buffer[256];
98         struct fmt fmt;
99         FILE *lfp;
100         unsigned int i;
101         short sample;
102         unsigned int size, wsize;
103         int ret;
104
105         if ((lfp=fopen(name,"r"))) {
106                 /* get size */
107                 fseek(lfp, 0, SEEK_END);
108                 size = ftell(lfp);
109                 printf("samples: %d\n", size);
110                 size += size;
111                 fseek(lfp, 0, SEEK_SET);
112
113                 wsize = 4+8+sizeof(fmt)+8+size+8+4+8+4;
114
115                 /* RIFF */
116                 fprintf(fp, "RIFF%c%c%c%c", (char)(wsize&0xff), (char)((wsize>>8)&0xff), (char)((wsize>>16)&0xff), (char)(wsize>>24));
117
118                 /* WAVE */
119                 fprintf(fp, "WAVE");
120
121                 /* fmt */
122                 fprintf(fp, "fmt %c%c%c%c", (unsigned int)sizeof(fmt), 0, 0, 0);
123                 fmt.stereo = 1;
124                 fmt.channels = 1;
125                 fmt.sample_rate = 8000;
126                 fmt.data_rate = 16000;
127                 fmt.bytes_sample = 2;
128                 fmt.bits_sample = 16;
129                 ret = fwrite(&fmt, sizeof(fmt), 1, fp);
130
131                 /* data */
132                 fprintf(fp, "data%c%c%c%c", (char)(size&0xff), (char)((size>>8)&0xff), (char)((size>>16)&0xff), (char)(size>>24));
133                 i = 0;
134                 while(i < size) {
135                         ret = fread(buffer, 1, 1, lfp);
136                         if (law == 'a')
137                                 sample = isdn_audio_alaw_to_s16[*buffer];
138                         else
139                                 sample = isdn_audio_ulaw_to_s16[*buffer];
140                         ret = fwrite(&sample, 2, 1, fp);
141                         i+=2;
142                 }
143
144                 /* cue */
145                 fprintf(fp, "cue %c%c%c%c%c%c%c%c", 4, 0, 0, 0, 0,0,0,0);
146         
147                 /* LIST */
148                 fprintf(fp, "LIST%c%c%c%cadtl", 4, 0, 0, 0);
149         
150                 fclose(lfp);
151         }
152 }
153
154 int main(int argc, char *argv[])
155 {
156         FILE *fp;
157
158         if (argc <= 1) {
159                 usage:
160                 printf("Usage:\n");
161                 printf("%s ulaw2wave <alaw file> <wav file>\n", argv[0]);
162                 printf("%s alaw2wave <alaw file> <wav file>\n", argv[0]);
163                 return(0);
164         }
165
166         if (!strcmp(argv[1], "alaw2wave")) {
167                 if (argc <= 3)
168                         goto usage;
169                 if ((fp=fopen(argv[3],"w"))) {
170                         write_law(fp,argv[2],'a');
171                         fclose(fp);
172                 } else {
173                         printf("Cannot open wave file %s\n",argv[3]);
174                 }
175         } else
176         if (!strcmp(argv[1], "ulaw2wave")) {
177                 if (argc <= 3)
178                         goto usage;
179                 if ((fp=fopen(argv[3],"w"))) {
180                         write_law(fp,argv[2],'u');
181                         fclose(fp);
182                 } else {
183                         printf("Cannot open wave file %s\n",argv[3]);
184                 }
185         } else
186                 goto usage;
187
188         return(0);
189 }