summaryrefslogtreecommitdiffstats
path: root/tones.c
diff options
context:
space:
mode:
authorroot2009-07-05 22:14:21 +0200
committerroot2009-07-05 22:14:21 +0200
commite9bfe48fc5254e79e59c6ffc1698912175edf4e3 (patch)
tree63c13b3d019b9a45bcd1b48354e243f133292401 /tones.c
parentChanged GSM api to current development tree of OpenBSC. (diff)
downloadlcr-e9bfe48fc5254e79e59c6ffc1698912175edf4e3.tar.gz
lcr-e9bfe48fc5254e79e59c6ffc1698912175edf4e3.tar.xz
lcr-e9bfe48fc5254e79e59c6ffc1698912175edf4e3.zip
Source cleanup.
modified: bootstrap.c modified: cause.c modified: crypt.cpp modified: endpoint.cpp modified: genext.c modified: genrc.c modified: gentones.c modified: genwave.c modified: ie.cpp modified: join.cpp modified: joinpbx.cpp modified: joinremote.cpp modified: lcradmin.c modified: mail.c modified: main.c modified: message.c modified: route.c modified: socket_server.c modified: tones.c modified: trace.c modified: vbox.cpp
Diffstat (limited to 'tones.c')
-rw-r--r--tones.c187
1 files changed, 65 insertions, 122 deletions
diff --git a/tones.c b/tones.c
index 4f8a40f..93d04e9 100644
--- a/tones.c
+++ b/tones.c
@@ -52,46 +52,36 @@ int open_tone(char *file, int *codec, signed int *length, signed int *left)
/* try to open the law file */
SPRINT(filename, "%s.isdn", file);
- if ((fh = open(filename, O_RDONLY)) >= 0)
- {
+ if ((fh = open(filename, O_RDONLY)) >= 0) {
/* stat tone */
l = 0;
- while(42)
- {
- if (l >= 10)
- {
+ while(42) {
+ if (l >= 10) {
close(fh);
PERROR("Link chain too deep: '%s'\n", filename);
return(-1);
}
- if (lstat(filename, &_stat) == -1)
- {
+ if (lstat(filename, &_stat) == -1) {
close(fh);
PERROR("Cannot stat file: '%s'\n", filename);
return(-1);
}
- if (!S_ISLNK(_stat.st_mode))
- {
+ if (!S_ISLNK(_stat.st_mode)) {
break;
}
- if ((linksize=readlink(filename, linkname, sizeof(linkname))) > 0)
- {
+ if ((linksize=readlink(filename, linkname, sizeof(linkname))) > 0) {
linkname[linksize] = '\0';
- } else
- {
+ } else {
close(fh);
PERROR("Cannot read link information: '%s'\n", filename);
return(-1);
}
if (linkname[0] == '/') /* absolute link */
- {
SCPY(filename, linkname);
- } else /* relative link */
- {
+ else { /* relative link */
/* remove filename */
p = filename;
- while(strchr(p, '/'))
- {
+ while(strchr(p, '/')) {
p = strchr(p, '/')+1;
}
*p = 0;
@@ -112,13 +102,11 @@ int open_tone(char *file, int *codec, signed int *length, signed int *left)
/* try to open the wave file */
SPRINT(filename, "%s.wav", file);
- if ((fh = open(filename, O_RDONLY)) >= 0)
- {
+ if ((fh = open(filename, O_RDONLY)) >= 0) {
/* get wave header */
read(fh, buffer, 8);
size=(buffer[4]) + (buffer[5]<<8) + (buffer[6]<<16) + (buffer[7]<<24);
- if (!!strncmp((char *)buffer, "RIFF", 4))
- {
+ if (!!strncmp((char *)buffer, "RIFF", 4)) {
close(fh);
errno = 0;
PERROR("%s is no riff file!\n", filename);
@@ -127,17 +115,14 @@ int open_tone(char *file, int *codec, signed int *length, signed int *left)
// printf("%c%c%c%c size=%ld\n",buffer[0],buffer[1],buffer[2],buffer[3],size);
read(fh, buffer, 4);
size -= 4;
- if (!!strncmp((char *)buffer, "WAVE", 4))
- {
+ if (!!strncmp((char *)buffer, "WAVE", 4)) {
close(fh);
errno = 0;
PERROR("%s is no wave file!\n", filename);
return(-1);
}
- while(size)
- {
- if (size>0 && size<8)
- {
+ while(size) {
+ if (size>0 && size<8) {
close(fh);
errno = 0;
PERROR("Remaining file size %ld not large enough for next chunk.\n",size);
@@ -147,17 +132,14 @@ int open_tone(char *file, int *codec, signed int *length, signed int *left)
chunk=(buffer[4]) + (buffer[5]<<8) + (buffer[6]<<16) + (buffer[7]<<24);
size -= (8+chunk);
// printf("%c%c%c%c length=%d\n",buffer[0],buffer[1],buffer[2],buffer[3],chunk);
- if (size < 0)
- {
+ if (size < 0) {
close(fh);
errno = 0;
PERROR("Chunk '%c%c%c%c' is larger than remainig file size (length=%ld)\n",buffer[0],buffer[1],buffer[2],buffer[3], chunk);
return(-1);
}
- if (!strncmp((char *)buffer, "fmt ", 4))
- {
- if (chunk < 16)
- {
+ if (!strncmp((char *)buffer, "fmt ", 4)) {
+ if (chunk < 16) {
close(fh);
errno = 0;
PERROR("File %s Fmt chunk illegal size.\n", filename);
@@ -165,8 +147,7 @@ int open_tone(char *file, int *codec, signed int *length, signed int *left)
}
read(fh, buffer, chunk);
fmt = (struct fmt *)buffer;
- if (fmt->channels<1 || fmt->channels>2)
- {
+ if (fmt->channels<1 || fmt->channels>2) {
close(fh);
errno = 0;
PERROR("File %s Only support one or two channels file.\n", filename);
@@ -174,13 +155,11 @@ int open_tone(char *file, int *codec, signed int *length, signed int *left)
}
channels = fmt->channels;
// printf("Channels: %d\n", channels);
- if (fmt->sample_rate != 8000)
- {
+ if (fmt->sample_rate != 8000) {
PERROR("Warning: File %s has sample rate of %ld.\n", filename, fmt->sample_rate);
}
// printf("Sample Rate: %ld\n", fmt->sample_rate);
- if (fmt->bits_sample!=8 && fmt->bits_sample!=16)
- {
+ if (fmt->bits_sample!=8 && fmt->bits_sample!=16) {
close(fh);
errno = 0;
PERROR("File %s has neigher 8 nor 16 bit samples.\n", filename);
@@ -190,18 +169,15 @@ int open_tone(char *file, int *codec, signed int *length, signed int *left)
// printf("Bit-Resolution: %d\n", bytes*16-16);
gotfmt = 1;
} else
- if (!strncmp((char *)buffer, "data", 4))
- {
- if (!gotfmt)
- {
+ if (!strncmp((char *)buffer, "data", 4)) {
+ if (!gotfmt) {
close(fh);
errno = 0;
PERROR("File %s No fmt chunk found before data chunk.\n", filename);
return(-1);
}
// printf("Length: %ld samples (%ld.%03ld seconds)\n", chunk/bytes/channels, chunk/bytes/channels/8000, ((chunk/bytes/channels)%8000)*1000/8000);
- if (bytes==2 && channels==1)
- {
+ if (bytes==2 && channels==1) {
if (codec)
*codec = CODEC_MONO;
if (length)
@@ -210,8 +186,7 @@ int open_tone(char *file, int *codec, signed int *length, signed int *left)
*left = ((signed int)chunk)>>1;
// printf("left=%d\n",*left);
} else
- if (bytes==2 && channels==2)
- {
+ if (bytes==2 && channels==2) {
if (codec)
*codec = CODEC_STEREO;
if (length)
@@ -219,27 +194,23 @@ int open_tone(char *file, int *codec, signed int *length, signed int *left)
if (left)
*left = ((signed int)chunk)>>2;
} else
- if (bytes==1 && channels==1)
- {
+ if (bytes==1 && channels==1) {
if (codec)
*codec = CODEC_8BIT;
if (length)
*length = (signed int)chunk;
if (left)
*left = (signed int)chunk;
- } else
- {
+ } else {
close(fh);
errno = 0;
PERROR("File %s Is not MONO8, MONO16 nor STEREO16.\n", filename);
return(-1);
}
return(fh);
- } else
- {
+ } else {
// PDEBUG(DEBUG_PORT, "Unknown chunk '%c%c%c%c'\n",buffer[0],buffer[1],buffer[2],buffer[3]);
- while(chunk > sizeof(buffer))
- {
+ while(chunk > sizeof(buffer)) {
read(fh, buffer, sizeof(buffer));
chunk -= sizeof(buffer);
}
@@ -248,8 +219,7 @@ int open_tone(char *file, int *codec, signed int *length, signed int *left)
}
}
- if (!gotfmt)
- {
+ if (!gotfmt) {
close(fh);
errno = 0;
PERROR("File %s No fmt chunk found in file.\n", filename);
@@ -285,19 +255,16 @@ int read_tone(int fh, unsigned char *buffer, int codec, int len, signed int size
if (!left)
goto unknown_length;
- if (speed!=1)
- {
+ if (speed!=1) {
offset = ((len&(~4)) * (speed-1));
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)*/
*left -= offset; /* correct the current bytes left */
- if (*left < 0)
- {
+ if (*left < 0) {
/* eof */
*left = 0;
return(0);
}
- if (*left >= size)
- {
+ if (*left >= size) {
/* eof */
*left = size;
return(0);
@@ -310,19 +277,16 @@ int read_tone(int fh, unsigned char *buffer, int codec, int len, signed int size
if (*left < len)
len = *left;
unknown_length:
- switch(codec)
- {
+ switch(codec) {
case CODEC_LAW:
l = read(fh, buffer, len); /* as is */
break;
case CODEC_MONO:
l = read(fh, buf16, len<<1);
- if (l>0)
- {
+ if (l>0) {
l = l>>1;
- while(i < l)
- {
+ while(i < l) {
sample = *buf16++;
if (sample < -32767)
sample = -32767;
@@ -335,36 +299,28 @@ int read_tone(int fh, unsigned char *buffer, int codec, int len, signed int size
break;
case CODEC_STEREO:
- {
- l = read(fh, buf32, len<<2);
- if (l>0)
- {
- l = l>>2;
- while(i < l)
- {
- sample = (*buf32++);
- sample += (*buf32++);
- if (sample < -32767)
- sample = -32767;
- if (sample > 32767)
- sample = 32767;
- *buffer++ = audio_s16_to_law[sample & 0xffff];
- i++;
- }
+ l = read(fh, buf32, len<<2);
+ if (l>0) {
+ l = l>>2;
+ while(i < l) {
+ sample = (*buf32++);
+ sample += (*buf32++);
+ if (sample < -32767)
+ sample = -32767;
+ if (sample > 32767)
+ sample = 32767;
+ *buffer++ = audio_s16_to_law[sample & 0xffff];
+ i++;
}
}
break;
case CODEC_8BIT:
- {
- l = read(fh, buf8, len);
- if (l>0)
- {
- while(i < l)
- {
- *buffer++ = audio_s16_to_law[(((*buf8++)<<8)-0x8000) & 0xffff];
- i++;
- }
+ l = read(fh, buf8, len);
+ if (l>0) {
+ while(i < l) {
+ *buffer++ = audio_s16_to_law[(((*buf8++)<<8)-0x8000) & 0xffff];
+ i++;
}
}
break;
@@ -391,11 +347,9 @@ void free_tones(void)
void *temp;
toneset_temp = toneset_first;
- while(toneset_temp)
- {
+ while(toneset_temp) {
tonesettone_temp = toneset_temp->first;
- while(tonesettone_temp)
- {
+ while(tonesettone_temp) {
temp = tonesettone_temp;
tonesettone_temp = tonesettone_temp->next;
FREE(temp, sizeof(struct tonesettone));
@@ -436,13 +390,10 @@ int fetch_tones(void)
if (*p == '\0')
return(1);
- while (*p)
- {
+ while (*p) {
p_next = p;
- while(*p_next)
- {
- if (*p_next == ',')
- {
+ while(*p_next) {
+ if (*p_next == ',') {
*p_next = '\0';
p_next++;
break;
@@ -465,24 +416,20 @@ int fetch_tones(void)
SPRINT(path, "%s/%s", SHARE_DATA, p);
dir = opendir(path);
- if (dir == NULL)
- {
+ if (dir == NULL) {
PERROR("Tone set not found: '%s'\n", path);
return(0);
}
- while((dirent=readdir(dir)))
- {
+ while((dirent=readdir(dir))) {
SPRINT(name, "%s", dirent->d_name);
/* remove .isdn and .wave */
- if (strlen(name) >= 4)
- {
+ if (strlen(name) >= 4) {
if (!strcmp(name+strlen(name)-4, ".wav"))
name[strlen(name)-4] = '\0';
}
- if (strlen(name) >= 5)
- {
+ if (strlen(name) >= 5) {
if (!strcmp(name+strlen(name)-5, ".isdn"))
name[strlen(name)-5] = '\0';
}
@@ -497,15 +444,13 @@ int fetch_tones(void)
/* open file */
fh = open_tone(filename, &tone_codec, &tone_size, &tone_left);
- if (fh < 0)
- {
+ if (fh < 0) {
PERROR("Cannot open file: '%s'\n", filename);
continue;
}
fduse++;
- if (tone_size < 0)
- {
+ if (tone_size < 0) {
PERROR("File has 0-length: '%s'\n", filename);
close(fh);
fduse--;
@@ -556,8 +501,7 @@ void *open_tone_fetched(char *dir, char *file, int *codec, signed int *length, s
/* find set */
toneset = toneset_first;
- while(toneset)
- {
+ while(toneset) {
//printf("1. comparing '%s' with '%s'\n", toneset->directory, dir);
if (!strcmp(toneset->directory, dir))
break;
@@ -568,8 +512,7 @@ void *open_tone_fetched(char *dir, char *file, int *codec, signed int *length, s
/* find tone */
tonesettone = toneset->first;
- while(tonesettone)
- {
+ while(tonesettone) {
//printf("2. comparing '%s' with '%s'\n", tonesettone->name, file);
if (!strcmp(tonesettone->name, file))
break;