1 /*****************************************************************************\
5 **---------------------------------------------------------------------------**
6 ** Copyright: Andreas Eversberg **
8 ** opening and reading tone **
10 \*****************************************************************************/
17 CODEC_OFF is a none accepted value
18 CODEC_LAW is 8 bit (1 byte) data 8khz
19 other codecs are 16 bit (2 bytes) data 8khz
20 the read_tone() will return law or 16bit mono. the read_tone will convert all other formats to 16bit mono.
26 * open the tone (don't increase fhuse, since it is done after calling this function)
27 * NOTE: length and left will be set to the number of samples, NOT bytes
30 unsigned short stereo; /* 1 = pcm, 2 = adpcm */
31 unsigned short channels; /* number of channels */
32 unsigned long sample_rate; /* sample rate */
33 unsigned long data_rate; /* data rate */
34 unsigned short bytes_sample; /* bytes per sample (all channels) */
35 unsigned short bits_sample; /* bits per sample (one channel) */
37 int open_tone(char *file, int *codec, signed long *length, signed long *left)
42 unsigned char buffer[256];
45 unsigned long size, chunk;
53 /* try to open the law file */
54 SPRINT(filename, "%s.isdn", file);
55 if ((fh = open(filename, O_RDONLY)) >= 0)
64 PERROR("Link chain too deep: '%s'\n", filename);
67 if (lstat(filename, &_stat) == -1)
70 PERROR("Cannot stat file: '%s'\n", filename);
73 if (!S_ISLNK(_stat.st_mode))
77 if ((linksize=readlink(filename, linkname, sizeof(linkname))) > 0)
79 linkname[linksize] = '\0';
83 PERROR("Cannot read link information: '%s'\n", filename);
86 if (linkname[0] == '/') /* absolute link */
88 SCPY(filename, linkname);
89 } else /* relative link */
99 SCAT(filename, linkname);
101 //printf("follow link: %s\n", filename);
105 *length = _stat.st_size;
107 *left = _stat.st_size;
113 /* try to open the wave file */
114 SPRINT(filename, "%s.wav", file);
115 if ((fh = open(filename, O_RDONLY)) >= 0)
117 /* get wave header */
119 size=(buffer[4]) + (buffer[5]<<8) + (buffer[6]<<16) + (buffer[7]<<24);
120 if (!!strncmp((char *)buffer, "RIFF", 4))
124 PERROR("%s is no riff file!\n", filename);
127 // printf("%c%c%c%c size=%ld\n",buffer[0],buffer[1],buffer[2],buffer[3],size);
130 if (!!strncmp((char *)buffer, "WAVE", 4))
134 PERROR("%s is no wave file!\n", filename);
139 if (size>0 && size<8)
143 PERROR("Remaining file size %ld not large enough for next chunk.\n",size);
147 chunk=(buffer[4]) + (buffer[5]<<8) + (buffer[6]<<16) + (buffer[7]<<24);
149 // printf("%c%c%c%c length=%d\n",buffer[0],buffer[1],buffer[2],buffer[3],chunk);
154 PERROR("Chunk '%c%c%c%c' is larger than remainig file size (length=%ld)\n",buffer[0],buffer[1],buffer[2],buffer[3], chunk);
157 if (!strncmp((char *)buffer, "fmt ", 4))
163 PERROR("File %s Fmt chunk illegal size.\n", filename);
166 read(fh, buffer, chunk);
167 fmt = (struct fmt *)buffer;
168 if (fmt->channels<1 || fmt->channels>2)
172 PERROR("File %s Only support one or two channels file.\n", filename);
175 channels = fmt->channels;
176 // printf("Channels: %d\n", channels);
177 if (fmt->sample_rate != 8000)
179 PERROR("Warning: File %s has sample rate of %ld.\n", filename, fmt->sample_rate);
181 // printf("Sample Rate: %ld\n", fmt->sample_rate);
182 if (fmt->bits_sample!=8 && fmt->bits_sample!=16)
186 PERROR("File %s has neigher 8 nor 16 bit samples.\n", filename);
189 bytes = (fmt->bits_sample==16)?2:1;
190 // printf("Bit-Resolution: %d\n", bytes*16-16);
193 if (!strncmp((char *)buffer, "data", 4))
199 PERROR("File %s No fmt chunk found before data chunk.\n", filename);
202 // printf("Length: %ld samples (%ld.%03ld seconds)\n", chunk/bytes/channels, chunk/bytes/channels/8000, ((chunk/bytes/channels)%8000)*1000/8000);
203 if (bytes==2 && channels==1)
208 *length = ((signed long)chunk)>>1;
210 *left = ((signed long)chunk)>>1;
211 // printf("left=%d\n",*left);
213 if (bytes==2 && channels==2)
216 *codec = CODEC_STEREO;
218 *length = ((signed long)chunk)>>2;
220 *left = ((signed long)chunk)>>2;
222 if (bytes==1 && channels==1)
227 *length = (signed long)chunk;
229 *left = (signed long)chunk;
234 PERROR("File %s Is not MONO8, MONO16 nor STEREO16.\n", filename);
240 // PDEBUG(DEBUG_PORT, "Unknown chunk '%c%c%c%c'\n",buffer[0],buffer[1],buffer[2],buffer[3]);
241 while(chunk > sizeof(buffer))
243 read(fh, buffer, sizeof(buffer));
244 chunk -= sizeof(buffer);
247 read(fh, buffer, chunk);
255 PERROR("File %s No fmt chunk found in file.\n", filename);
260 PERROR("File %s No data chunk found in file.\n", filename);
269 * read from tone, check size
270 * the len must be the number of samples, NOT for the bytes to read!!
271 * the data returned is law-code
273 int read_tone(int fh, unsigned char *buffer, int codec, int len, signed long size, signed long *left, int speed)
277 signed short buffer16[len], *buf16 = buffer16;
278 signed short buffer32[len<<1], *buf32 = buffer32;
279 unsigned char buffer8[len], *buf8 = buffer8;
282 //printf("left=%ld\n",*left);
284 /* if no *left is given (law has unknown length) */
290 offset = ((len&(~4)) * (speed-1));
291 lseek(fh, offset, SEEK_CUR); /* step fowards, backwards (len must be round to 4 bytes, to be sure, that 16bit stereo will not drift out of sync)*/
292 *left -= offset; /* correct the current bytes left */
316 l = read(fh, buffer, len); /* as is */
320 l = read(fh, buf16, len<<1);
331 *buffer++ = audio_s16_to_law[sample & 0xffff];
339 l = read(fh, buf32, len<<2);
345 sample = (*buf32++) + (*buf32++);
350 *buffer++ = audio_s16_to_law[sample & 0xffff];
359 l = read(fh, buf8, len);
364 *buffer++ = audio_s16_to_law[(((*buf8++)<<8)-0x8000) & 0xffff];
372 FATAL("codec %d is not supported.\n", codec);
381 struct toneset *toneset_first = NULL;
386 void free_tones(void)
388 struct toneset *toneset_temp;
389 struct tonesettone *tonesettone_temp;
392 toneset_temp = toneset_first;
395 tonesettone_temp = toneset_temp->first;
396 while(tonesettone_temp)
398 temp = tonesettone_temp;
399 tonesettone_temp = tonesettone_temp->next;
400 FREE(temp, sizeof(struct tonesettone));
404 toneset_temp = toneset_temp->next;
405 FREE(temp, sizeof(struct toneset));
408 toneset_first = NULL;
412 * fetch tones as specified in options.conf
414 int fetch_tones(void)
417 struct dirent *dirent;
418 struct toneset **toneset_nextpointer;
419 struct tonesettone **tonesettone_nextpointer;
422 char filename[256], name[256];
425 signed long tone_size, tone_left;
426 unsigned long memory = 0;
430 if (!options.fetch_tones)
433 toneset_nextpointer = &toneset_first;
434 p = options.fetch_tones;
452 /* remove trailing / */
453 if (*p) if (p[strlen(p)-1] == '/')
454 p[strlen(p)-1] = '\0';
456 printf("PBX: Fetching tones '%s'\n", p);
457 PDEBUG(DEBUG_PORT, "fetching tones directory '%s'\n", p);
459 *toneset_nextpointer = (struct toneset *)MALLOC(sizeof(struct toneset));
461 memory += sizeof(struct toneset);
462 SCPY((*toneset_nextpointer)->directory, p);
463 tonesettone_nextpointer = &(*toneset_nextpointer)->first;
465 SPRINT(path, "%s/%s", INSTALL_DATA, p);
469 PERROR("Tone set not found: '%s'\n", path);
473 while((dirent=readdir(dir)))
475 SPRINT(name, "%s", dirent->d_name);
477 /* remove .isdn and .wave */
478 if (strlen(name) >= 4)
480 if (!strcmp(name+strlen(name)-4, ".wav"))
481 name[strlen(name)-4] = '\0';
483 if (strlen(name) >= 5)
485 if (!strcmp(name+strlen(name)-5, ".isdn"))
486 name[strlen(name)-5] = '\0';
489 SPRINT(filename, "%s/%s", path, name);
492 if (!strcmp(dirent->d_name, "."))
494 if (!strcmp(dirent->d_name, ".."))
498 fh = open_tone(filename, &tone_codec, &tone_size, &tone_left);
501 PERROR("Cannot open file: '%s'\n", filename);
508 PERROR("File has 0-length: '%s'\n", filename);
515 *tonesettone_nextpointer = (struct tonesettone *)MALLOC(sizeof(struct tonesettone)+tone_size);
517 //printf("tone:%s, %ld bytes\n", name, tone_size);
518 memory += sizeof(struct tonesettone)+tone_size;
522 read_tone(fh, (*tonesettone_nextpointer)->data, tone_codec, tone_size, tone_size, &tone_left, 1);
523 (*tonesettone_nextpointer)->size = tone_size;
524 (*tonesettone_nextpointer)->codec = (tone_codec==CODEC_LAW)?CODEC_LAW:CODEC_MONO;
525 SCPY((*tonesettone_nextpointer)->name, name);
530 tonesettone_nextpointer = &((*tonesettone_nextpointer)->next);
533 toneset_nextpointer = &((*toneset_nextpointer)->next);
537 printf("PBX: Memory used for tones: %ld bytes (%d samples)\n", memory, samples);
538 PDEBUG(DEBUG_PORT, "Memory used for tones: %ld bytes (%d samples)\n", memory, samples);
545 * opens the fetched tone (if available)
547 void *open_tone_fetched(char *dir, char *file, int *codec, signed long *length, signed long *left)
549 struct toneset *toneset;
550 struct tonesettone *tonesettone;
552 /* if anything fetched */
557 toneset = toneset_first;
560 //printf("1. comparing '%s' with '%s'\n", toneset->directory, dir);
561 if (!strcmp(toneset->directory, dir))
563 toneset = toneset->next;
569 tonesettone = toneset->first;
572 //printf("2. comparing '%s' with '%s'\n", tonesettone->name, file);
573 if (!strcmp(tonesettone->name, file))
575 tonesettone = tonesettone->next;
580 /* return information */
582 *length = tonesettone->size;
584 *left = tonesettone->size;
586 *codec = tonesettone->codec;
587 //printf("size=%ld, data=%08x\n", tonesettone->size, tonesettone->data);
588 return(tonesettone->data);
593 * read from fetched tone, check size
594 * the len must be the number of samples, NOT for the bytes to read!!
596 int read_tone_fetched(void **fetched, void *buffer, int len, signed long size, signed long *left, int speed)
599 //printf("left=%ld\n",*left);
601 /* if no *left is given (law has unknown length) */
611 memcpy(buffer, *fetched, len);
612 *((char **)fetched) += len;