From 763f2431276955b16dd64ee61ff77337389f4f99 Mon Sep 17 00:00:00 2001 From: markster Date: Sat, 8 Dec 2001 20:09:24 +0000 Subject: Version 0.1.4 from FTP git-svn-id: http://svn.digium.com/svn/zaptel/trunk@38 5390a7c7-147a-4af0-8ec9-7488f05a26cb --- tonezone.c | 121 +++++++++++-------------------------------------------------- 1 file changed, 21 insertions(+), 100 deletions(-) (limited to 'tonezone.c') diff --git a/tonezone.c b/tonezone.c index e601de9..9c4f289 100755 --- a/tonezone.c +++ b/tonezone.c @@ -37,73 +37,6 @@ #define CLIP 32635 #define BIAS 0x84 -static float loudness=8192.0; - -unsigned char -linear2ulaw(sample) -short sample; { - static int exp_lut[256] = {0,0,1,1,2,2,2,2,3,3,3,3,3,3,3,3, - 4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4, - 5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5, - 5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5, - 6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6, - 6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6, - 6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6, - 6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6, - 7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7, - 7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7, - 7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7, - 7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7, - 7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7, - 7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7, - 7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7, - 7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7}; - int sign, exponent, mantissa; - unsigned char ulawbyte; - - /* Get the sample into sign-magnitude. */ - sign = (sample >> 8) & 0x80; /* set aside the sign */ - if (sign != 0) sample = -sample; /* get magnitude */ - if (sample > CLIP) sample = CLIP; /* clip the magnitude */ - - /* Convert from 16 bit linear to ulaw. */ - sample = sample + BIAS; - exponent = exp_lut[(sample >> 7) & 0xFF]; - mantissa = (sample >> (exponent + 3)) & 0x0F; - ulawbyte = ~(sign | (exponent << 4) | mantissa); -#ifdef ZEROTRAP - if (ulawbyte == 0) ulawbyte = 0x02; /* optional CCITT trap */ -#endif - - return(ulawbyte); -} - - - -static int calc_samples(int freq) -{ - int x, samples; - /* Calculate the number of samples at 8000hz sampling - we need to have this wave form */ - samples = 8000; - /* Take out common 2's up to six times */ - for (x=0;x<6;x++) - if (!(freq % 2)) { - freq /= 2; - samples /= 2; - } - /* Take out common 5's (up to three times */ - for (x=0;x<3;x++) - if (!(freq % 5)) { - freq /= 5; - samples /=5; - } - /* No more common factors. */ - return samples; -} - - - struct tone_zone *tone_zone_find(char *country) { struct tone_zone *z; @@ -128,16 +61,16 @@ struct tone_zone *tone_zone_find_by_num(int id) return NULL; } +#define LEVEL -8 + static int build_tone(char *data, int size, struct tone_zone_sound *t, int *count) { char *dup, *s; struct zt_tone_def *td=NULL; int firstnobang = -1; int freq1, freq2, time; - int samples, samples1, samples2; - int x; int used = 0; - float val; + float gain; dup = strdup(t->data); s = strtok(dup, ","); while(s && strlen(s)) { @@ -176,31 +109,29 @@ static int build_tone(char *data, int size, struct tone_zone_sound *t, int *coun fprintf(stderr, "tone component '%s' of '%s' is a syntax error\n", s,t->data); return -1; } - if (freq1) - samples1 = calc_samples(freq1); - else - samples1 = 40; - if (freq2) - samples2 = calc_samples(freq2); - else - samples2 = 0; - samples = samples1; - if (freq2) { - while(samples % samples2) - samples += samples1; - } #if 0 printf("Using %d samples for %d and %d\n", samples, freq1, freq2); #endif - if (size < samples + sizeof(struct zt_tone_def)) { + if (size < sizeof(struct zt_tone_def)) { fprintf(stderr, "Not enough space for samples\n"); return -1; } td = (struct zt_tone_def *)data; - data += (sizeof(struct zt_tone_def) + samples); - used += (sizeof(struct zt_tone_def) + samples); - size -= (sizeof(struct zt_tone_def) + samples); - td->size = samples; + + /* Bring it down -8 dbm */ + gain = pow(10.0, (LEVEL - 3.14) / 20.0) * 65536.0 / 2.0; + + td->fac1 = 2.0 * cos(2.0 * M_PI * (freq1 / 8000.0)) * 32768.0; + td->init_v2_1 = sin(-4.0 * M_PI * (freq1 / 8000.0)) * gain; + td->init_v3_1 = sin(-2.0 * M_PI * (freq1 / 8000.0)) * gain; + + td->fac2 = 2.0 * cos(2.0 * M_PI * (freq2 / 8000.0)) * 32768.0; + td->init_v2_2 = sin(-4.0 * M_PI * (freq2 / 8000.0)) * gain; + td->init_v3_2 = sin(-2.0 * M_PI * (freq2 / 8000.0)) * gain; + + data += (sizeof(struct zt_tone_def)); + used += (sizeof(struct zt_tone_def)); + size -= (sizeof(struct zt_tone_def)); td->tone = t->toneid; if (time) { /* We should move to the next tone */ @@ -209,15 +140,7 @@ static int build_tone(char *data, int size, struct tone_zone_sound *t, int *coun } else { /* Stay with us */ td->next = *count; - td->samples = samples; - } - for (x=0;xdata[x] = linear2ulaw((int)val); + td->samples = 8000; } (*count)++; s = strtok(NULL, ","); @@ -330,7 +253,6 @@ int tone_zone_register_zone(int fd, struct tone_zone *z) } } h->count = count; - h->size = used - sizeof(struct zt_tone_def_header) - count * sizeof(struct zt_tone_def); h->zone = z->zone; strncpy(h->name, z->description, sizeof(h->name)); x = z->zone; @@ -354,8 +276,6 @@ int tone_zone_register(int fd, char *country) } } - - int tone_zone_set_zone(int fd, char *country) { int res=-1; @@ -387,6 +307,7 @@ int tone_zone_play_tone(int fd, int tone) struct tone_zone *z; int res = -1; int zone; + #if 0 fprintf(stderr, "Playing tone %d (%s) on %d\n", tone, tone_zone_tone_name(tone), fd); #endif -- cgit v1.2.3