gentone.c 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  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. int main(int argc, char *argv[])
  33. {
  34. FILE *f;
  35. int freq1, freq2;
  36. float wlen1, wlen2;
  37. float val;
  38. int x, samples1, samples2=0, samples=0;
  39. char fn[256];
  40. if (argc < 3) {
  41. fprintf(stderr, "Usage: gensound <name> <freq1> [freq2]\n");
  42. exit(1);
  43. }
  44. freq1 = atoi(argv[2]);
  45. if (argc > 3)
  46. freq2 = atoi(argv[3]);
  47. else
  48. freq2 = 0;
  49. wlen1 = 8000.0/(float)freq1;
  50. samples1 = calc_samples(freq1);
  51. printf("Wavelength 1 (in samples): %10.5f\n", wlen1);
  52. printf("Minimum samples (1): %d (%f.3 wavelengths)\n", samples1, samples1 / wlen1);
  53. if (freq2) {
  54. wlen2 = 8000.0/(float)freq2;
  55. samples2 = calc_samples(freq2);
  56. printf("Wavelength 1 (in samples): %10.5f\n", wlen2);
  57. printf("Minimum samples (1): %d (%f.3 wavelengths)\n", samples2, samples2 / wlen2);
  58. }
  59. samples = samples1;
  60. if (freq2) {
  61. while(samples % samples2)
  62. samples += samples1;
  63. }
  64. printf("Need %d samples\n", samples);
  65. snprintf(fn, sizeof(fn), "%s.h", argv[1]);
  66. if ((f = fopen(fn, "w"))) {
  67. if (freq2)
  68. fprintf(f, "/* %s: Generated from frequencies %d and %d \n"
  69. " by gentone. %d samples */\n", fn, freq1, freq2, samples);
  70. else
  71. fprintf(f, "/* %s: Generated from frequency %d\n"
  72. " by gentone. %d samples */\n", fn, freq1, samples);
  73. fprintf(f, "static short %s[%d] = {\n\t", argv[1], samples);
  74. for (x=0;x<samples;x++) {
  75. val = loudness * sin((freq1 * 2.0 * M_PI * x)/8000.0);
  76. if (freq2)
  77. val += loudness * sin((freq2 * 2.0 * M_PI * x)/8000.0);
  78. fprintf(f, "%5d, ", (int)val);
  79. if (!((x+1) % 8))
  80. fprintf(f, "\n\t");
  81. }
  82. if (x % 15)
  83. fprintf(f, "\n");
  84. fprintf(f, "};\n");
  85. fclose(f);
  86. printf("Wrote %s\n", fn);
  87. } else {
  88. fprintf(stderr, "Unable to open %s for writing\n", fn);
  89. return 1;
  90. }
  91. return 0;
  92. }