gentone-ulaw.c 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  1. /* Generate a header file for a particular
  2. single or double frequency */
  3. #include <stdio.h>
  4. #include <math.h>
  5. #include <string.h>
  6. #include <unistd.h>
  7. #include <stdlib.h>
  8. #define CLIP 32635
  9. #define BIAS 0x84
  10. static float loudness=16384.0;
  11. static int calc_samples(int freq)
  12. {
  13. int x, samples;
  14. /* Calculate the number of samples at 8000hz sampling
  15. we need to have this wave form */
  16. samples = 8000;
  17. /* Take out common 2's up to six times */
  18. for (x=0;x<6;x++)
  19. if (!(freq % 2)) {
  20. freq /= 2;
  21. samples /= 2;
  22. }
  23. /* Take out common 5's (up to three times */
  24. for (x=0;x<3;x++)
  25. if (!(freq % 5)) {
  26. freq /= 5;
  27. samples /=5;
  28. }
  29. /* No more common factors. */
  30. return samples;
  31. }
  32. /*
  33. ** This routine converts from linear to ulaw
  34. **
  35. ** Craig Reese: IDA/Supercomputing Research Center
  36. ** Joe Campbell: Department of Defense
  37. ** 29 September 1989
  38. **
  39. ** References:
  40. ** 1) CCITT Recommendation G.711 (very difficult to follow)
  41. ** 2) "A New Digital Technique for Implementation of Any
  42. ** Continuous PCM Companding Law," Villeret, Michel,
  43. ** et al. 1973 IEEE Int. Conf. on Communications, Vol 1,
  44. ** 1973, pg. 11.12-11.17
  45. ** 3) MIL-STD-188-113,"Interoperability and Performance Standards
  46. ** for Analog-to_Digital Conversion Techniques,"
  47. ** 17 February 1987
  48. **
  49. ** Input: Signed 16 bit linear sample
  50. ** Output: 8 bit ulaw sample
  51. */
  52. #define ZEROTRAP /* turn on the trap as per the MIL-STD */
  53. #define BIAS 0x84 /* define the add-in bias for 16 bit samples */
  54. #define CLIP 32635
  55. static unsigned char linear2ulaw(short sample) {
  56. static int exp_lut[256] = {0,0,1,1,2,2,2,2,3,3,3,3,3,3,3,3,
  57. 4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,
  58. 5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,
  59. 5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,
  60. 6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,
  61. 6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,
  62. 6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,
  63. 6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,
  64. 7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,
  65. 7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,
  66. 7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,
  67. 7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,
  68. 7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,
  69. 7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,
  70. 7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,
  71. 7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7};
  72. int sign, exponent, mantissa;
  73. unsigned char ulawbyte;
  74. /* Get the sample into sign-magnitude. */
  75. sign = (sample >> 8) & 0x80; /* set aside the sign */
  76. if (sign != 0) sample = -sample; /* get magnitude */
  77. if (sample > CLIP) sample = CLIP; /* clip the magnitude */
  78. /* Convert from 16 bit linear to ulaw. */
  79. sample = sample + BIAS;
  80. exponent = exp_lut[(sample >> 7) & 0xFF];
  81. mantissa = (sample >> (exponent + 3)) & 0x0F;
  82. ulawbyte = ~(sign | (exponent << 4) | mantissa);
  83. #ifdef ZEROTRAP
  84. if (ulawbyte == 0) ulawbyte = 0x02; /* optional CCITT trap */
  85. #endif
  86. return(ulawbyte);
  87. }
  88. int main(int argc, char *argv[])
  89. {
  90. FILE *f;
  91. int freq1, freq2;
  92. float wlen1, wlen2;
  93. float val;
  94. int x, samples1, samples2, samples=0;
  95. char fn[256];
  96. if (argc < 3) {
  97. fprintf(stderr, "Usage: gensound <name> <freq1> [freq2]\n");
  98. exit(1);
  99. }
  100. freq1 = atoi(argv[2]);
  101. if (argc > 3)
  102. freq2 = atoi(argv[3]);
  103. else
  104. freq2 = 0;
  105. wlen1 = 8000.0/(float)freq1;
  106. samples1 = calc_samples(freq1);
  107. printf("Wavelength 1 (in samples): %10.5f\n", wlen1);
  108. printf("Minimum samples (1): %d (%f.3 wavelengths)\n", samples1, samples1 / wlen1);
  109. if (freq2) {
  110. wlen2 = 8000.0/(float)freq2;
  111. samples2 = calc_samples(freq2);
  112. printf("Wavelength 1 (in samples): %10.5f\n", wlen2);
  113. printf("Minimum samples (1): %d (%f.3 wavelengths)\n", samples2, samples2 / wlen2);
  114. }
  115. samples = samples1;
  116. if (freq2) {
  117. while(samples % samples2)
  118. samples += samples1;
  119. }
  120. printf("Need %d samples\n", samples);
  121. snprintf(fn, sizeof(fn), "%s.h", argv[1]);
  122. if ((f = fopen(fn, "w"))) {
  123. if (freq2)
  124. fprintf(f, "/* %s: Generated from frequencies %d and %d \n"
  125. " by gentone. %d samples */\n", fn, freq1, freq2, samples);
  126. else
  127. fprintf(f, "/* %s: Generated from frequency %d\n"
  128. " by gentone. %d samples */\n", fn, freq1, samples);
  129. fprintf(f, "static unsigned char %s[%d] = {\n\t", argv[1], samples);
  130. for (x=0;x<samples;x++) {
  131. val = loudness * sin((freq1 * 2.0 * M_PI * x)/8000.0);
  132. if (freq2)
  133. val += loudness * sin((freq2 * 2.0 * M_PI * x)/8000.0);
  134. fprintf(f, "%3d, ", (int) linear2ulaw(val));
  135. if (!((x+1) % 8))
  136. fprintf(f, "\n\t");
  137. }
  138. if (x % 15)
  139. fprintf(f, "\n");
  140. fprintf(f, "};\n");
  141. fclose(f);
  142. printf("Wrote %s\n", fn);
  143. } else {
  144. fprintf(stderr, "Unable to open %s for writing\n", fn);
  145. return 1;
  146. }
  147. return 0;
  148. }