Asterisk.org: Developer Documentation (28 Oct 2009)

Wed Oct 28 15:47:54 2009

## Asterisk developer's documentation

# gentone.c

Go to the documentation of this file.

    00001 /* Generate a header file for a particular
    00002    single or double frequency */
    00003
    00004 #include <stdio.h>
    00005 #include <math.h>
    00006 #include <string.h>
    00007 #include <unistd.h>
    00008 #include <stdlib.h>
    00009 #define CLIP 32635
    00010 #define BIAS 0x84
    00011 static float loudness=16384.0;
    00012
    00013 static int calc_samples(int freq)
    00014 {
    00015    int x, samples;
    00016    /* Calculate the number of samples at 8000hz sampling
    00017       we need to have this wave form */
    00018    samples = 8000;
    00019    /* Take out common 2's up to six times */
    00020    for (x=0;x<6;x++)
    00021       if (!(freq % 2)) {
    00022          freq /= 2;
    00023          samples /= 2;
    00024       }
    00025    /* Take out common 5's (up to three times */
    00026    for (x=0;x<3;x++)
    00027       if (!(freq % 5)) {
    00028          freq /= 5;
    00029          samples /=5;
    00030       }
    00031    /* No more common factors. */
    00032    return samples;
    00033 }
    00034
    00035 int main(int argc, char *argv[])
    00036 {
    00037    FILE *f;
    00038    int freq1, freq2;
    00039    float wlen1, wlen2;
    00040    float val;
    00041    int x, samples1, samples2=0, samples=0;
    00042    char fn[256];
    00043    if (argc < 3) {
    00044       fprintf(stderr, "Usage: gensound <name> <freq1> [freq2]\n");
    00045       exit(1);
    00046    }
    00047    freq1 = atoi(argv[2]);
    00048    if (argc > 3)
    00049       freq2 = atoi(argv[3]);
    00050    else
    00051       freq2 = 0;
    00052    wlen1 = 8000.0/(float)freq1;
    00053    samples1 = calc_samples(freq1);
    00054    printf("Wavelength 1 (in samples): %10.5f\n", wlen1);
    00055    printf("Minimum samples (1): %d (%f.3 wavelengths)\n", samples1, samples1 / wlen1);
    00056    if (freq2) {
    00057       wlen2 = 8000.0/(float)freq2;
    00058       samples2 = calc_samples(freq2);
    00059       printf("Wavelength 1 (in samples): %10.5f\n", wlen2);
    00060       printf("Minimum samples (1): %d (%f.3 wavelengths)\n", samples2, samples2 / wlen2);
    00061    }
    00062    samples = samples1;
    00063    if (freq2) {
    00064       while(samples % samples2)
    00065          samples += samples1;
    00066    }
    00067    printf("Need %d samples\n", samples);
    00068    snprintf(fn, sizeof(fn), "%s.h", argv[1]);
    00069    if ((f = fopen(fn, "w"))) {
    00070       if (freq2)
    00071          fprintf(f, "/* %s: Generated from frequencies %d and %d \n"
    00072                     "   by gentone.  %d samples  */\n", fn, freq1, freq2, samples);
    00073       else
    00074          fprintf(f, "/* %s: Generated from frequency %d\n"
    00075                     "   by gentone.  %d samples  */\n", fn, freq1, samples);
    00076       fprintf(f, "static short %s[%d] = {\n\t", argv[1], samples);
    00077       for (x=0;x<samples;x++) {
    00078          val = loudness * sin((freq1 * 2.0 * M_PI * x)/8000.0);
    00079          if (freq2)
    00080             val += loudness * sin((freq2 * 2.0 * M_PI * x)/8000.0);
    00081          fprintf(f, "%5d, ", (int)val);
    00082          if (!((x+1) % 8))
    00083             fprintf(f, "\n\t");
    00084       }
    00085       if (x % 15)
    00086          fprintf(f, "\n");
    00087       fprintf(f, "};\n");
    00088       fclose(f);
    00089       printf("Wrote %s\n", fn);
    00090    } else {
    00091       fprintf(stderr, "Unable to open %s for writing\n", fn);
    00092       return 1;
    00093    }
    00094    return 0;
    00095 }