audiogen.c 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248
  1. /*
  2. * Generate a synthetic stereo sound.
  3. * NOTE: No floats are used to guarantee bitexact output.
  4. *
  5. * Copyright (c) 2002 Fabrice Bellard
  6. *
  7. * This file is part of FFmpeg.
  8. *
  9. * FFmpeg is free software; you can redistribute it and/or
  10. * modify it under the terms of the GNU Lesser General Public
  11. * License as published by the Free Software Foundation; either
  12. * version 2.1 of the License, or (at your option) any later version.
  13. *
  14. * FFmpeg is distributed in the hope that it will be useful,
  15. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  17. * Lesser General Public License for more details.
  18. *
  19. * You should have received a copy of the GNU Lesser General Public
  20. * License along with FFmpeg; if not, write to the Free Software
  21. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  22. */
  23. #include <stdlib.h>
  24. #include <stdint.h>
  25. #include <stdio.h>
  26. #include <string.h>
  27. #define MAX_CHANNELS 12
  28. static unsigned int myrnd(unsigned int *seed_ptr, int n)
  29. {
  30. unsigned int seed, val;
  31. seed = *seed_ptr;
  32. seed = (seed * 314159) + 1;
  33. if (n == 256) {
  34. val = seed >> 24;
  35. } else {
  36. val = seed % n;
  37. }
  38. *seed_ptr = seed;
  39. return val;
  40. }
  41. #define FRAC_BITS 16
  42. #define FRAC_ONE (1 << FRAC_BITS)
  43. #define COS_TABLE_BITS 7
  44. /* integer cosine */
  45. static const unsigned short cos_table[(1 << COS_TABLE_BITS) + 2] = {
  46. 0x8000, 0x7ffe, 0x7ff6, 0x7fea, 0x7fd9, 0x7fc2, 0x7fa7, 0x7f87,
  47. 0x7f62, 0x7f38, 0x7f0a, 0x7ed6, 0x7e9d, 0x7e60, 0x7e1e, 0x7dd6,
  48. 0x7d8a, 0x7d3a, 0x7ce4, 0x7c89, 0x7c2a, 0x7bc6, 0x7b5d, 0x7aef,
  49. 0x7a7d, 0x7a06, 0x798a, 0x790a, 0x7885, 0x77fb, 0x776c, 0x76d9,
  50. 0x7642, 0x75a6, 0x7505, 0x7460, 0x73b6, 0x7308, 0x7255, 0x719e,
  51. 0x70e3, 0x7023, 0x6f5f, 0x6e97, 0x6dca, 0x6cf9, 0x6c24, 0x6b4b,
  52. 0x6a6e, 0x698c, 0x68a7, 0x67bd, 0x66d0, 0x65de, 0x64e9, 0x63ef,
  53. 0x62f2, 0x61f1, 0x60ec, 0x5fe4, 0x5ed7, 0x5dc8, 0x5cb4, 0x5b9d,
  54. 0x5a82, 0x5964, 0x5843, 0x571e, 0x55f6, 0x54ca, 0x539b, 0x5269,
  55. 0x5134, 0x4ffb, 0x4ec0, 0x4d81, 0x4c40, 0x4afb, 0x49b4, 0x486a,
  56. 0x471d, 0x45cd, 0x447b, 0x4326, 0x41ce, 0x4074, 0x3f17, 0x3db8,
  57. 0x3c57, 0x3af3, 0x398d, 0x3825, 0x36ba, 0x354e, 0x33df, 0x326e,
  58. 0x30fc, 0x2f87, 0x2e11, 0x2c99, 0x2b1f, 0x29a4, 0x2827, 0x26a8,
  59. 0x2528, 0x23a7, 0x2224, 0x209f, 0x1f1a, 0x1d93, 0x1c0c, 0x1a83,
  60. 0x18f9, 0x176e, 0x15e2, 0x1455, 0x12c8, 0x113a, 0x0fab, 0x0e1c,
  61. 0x0c8c, 0x0afb, 0x096b, 0x07d9, 0x0648, 0x04b6, 0x0324, 0x0192,
  62. 0x0000, 0x0000,
  63. };
  64. #define CSHIFT (FRAC_BITS - COS_TABLE_BITS - 2)
  65. static int int_cos(int a)
  66. {
  67. int neg, v, f;
  68. const unsigned short *p;
  69. a = a & (FRAC_ONE - 1); /* modulo 2 * pi */
  70. if (a >= (FRAC_ONE / 2))
  71. a = FRAC_ONE - a;
  72. neg = 0;
  73. if (a > (FRAC_ONE / 4)) {
  74. neg = -1;
  75. a = (FRAC_ONE / 2) - a;
  76. }
  77. p = cos_table + (a >> CSHIFT);
  78. /* linear interpolation */
  79. f = a & ((1 << CSHIFT) - 1);
  80. v = p[0] + (((p[1] - p[0]) * f + (1 << (CSHIFT - 1))) >> CSHIFT);
  81. v = (v ^ neg) - neg;
  82. v = v << (FRAC_BITS - 15);
  83. return v;
  84. }
  85. FILE *outfile;
  86. static void put16(int16_t v)
  87. {
  88. fputc( v & 0xff, outfile);
  89. fputc((v >> 8) & 0xff, outfile);
  90. }
  91. static void put32(uint32_t v)
  92. {
  93. fputc( v & 0xff, outfile);
  94. fputc((v >> 8) & 0xff, outfile);
  95. fputc((v >> 16) & 0xff, outfile);
  96. fputc((v >> 24) & 0xff, outfile);
  97. }
  98. #define HEADER_SIZE 38
  99. #define FMT_SIZE 18
  100. #define SAMPLE_SIZE 2
  101. #define WFORMAT_PCM 0x0001
  102. static void put_wav_header(int sample_rate, int channels, int nb_samples)
  103. {
  104. int block_align = SAMPLE_SIZE * channels;
  105. int data_size = block_align * nb_samples;
  106. fputs("RIFF", outfile);
  107. put32(HEADER_SIZE + data_size);
  108. fputs("WAVEfmt ", outfile);
  109. put32(FMT_SIZE);
  110. put16(WFORMAT_PCM);
  111. put16(channels);
  112. put32(sample_rate);
  113. put32(block_align * sample_rate);
  114. put16(block_align);
  115. put16(SAMPLE_SIZE * 8);
  116. put16(0);
  117. fputs("data", outfile);
  118. put32(data_size);
  119. }
  120. int main(int argc, char **argv)
  121. {
  122. int i, a, v, j, f, amp, ampa;
  123. unsigned int seed = 1;
  124. int tabf1[MAX_CHANNELS], tabf2[MAX_CHANNELS];
  125. int taba[MAX_CHANNELS];
  126. int sample_rate = 44100;
  127. int nb_channels = 2;
  128. char *ext;
  129. if (argc < 2 || argc > 5) {
  130. printf("usage: %s file [<sample rate> [<channels>] [<random seed>]]\n"
  131. "generate a test raw 16 bit audio stream\n"
  132. "If the file extension is .wav a WAVE header will be added.\n"
  133. "default: 44100 Hz stereo\n", argv[0]);
  134. exit(1);
  135. }
  136. if (argc > 2) {
  137. sample_rate = atoi(argv[2]);
  138. if (sample_rate <= 0) {
  139. fprintf(stderr, "invalid sample rate: %d\n", sample_rate);
  140. return 1;
  141. }
  142. }
  143. if (argc > 3) {
  144. nb_channels = atoi(argv[3]);
  145. if (nb_channels < 1 || nb_channels > MAX_CHANNELS) {
  146. fprintf(stderr, "invalid number of channels: %d\n", nb_channels);
  147. return 1;
  148. }
  149. }
  150. if (argc > 4)
  151. seed = atoi(argv[4]);
  152. outfile = fopen(argv[1], "wb");
  153. if (!outfile) {
  154. perror(argv[1]);
  155. return 1;
  156. }
  157. if ((ext = strrchr(argv[1], '.')) && !strcmp(ext, ".wav"))
  158. put_wav_header(sample_rate, nb_channels, 6 * sample_rate);
  159. /* 1 second of single freq sine at 1000 Hz */
  160. a = 0;
  161. for (i = 0; i < 1 * sample_rate; i++) {
  162. v = (int_cos(a) * 10000) >> FRAC_BITS;
  163. for (j = 0; j < nb_channels; j++)
  164. put16(v);
  165. a += (1000 * FRAC_ONE) / sample_rate;
  166. }
  167. /* 1 second of varying frequency between 100 and 10000 Hz */
  168. a = 0;
  169. for (i = 0; i < 1 * sample_rate; i++) {
  170. v = (int_cos(a) * 10000) >> FRAC_BITS;
  171. for (j = 0; j < nb_channels; j++)
  172. put16(v);
  173. f = 100 + (((10000 - 100) * i) / sample_rate);
  174. a += (f * FRAC_ONE) / sample_rate;
  175. }
  176. /* 0.5 second of low amplitude white noise */
  177. for (i = 0; i < sample_rate / 2; i++) {
  178. v = myrnd(&seed, 20000) - 10000;
  179. for (j = 0; j < nb_channels; j++)
  180. put16(v);
  181. }
  182. /* 0.5 second of high amplitude white noise */
  183. for (i = 0; i < sample_rate / 2; i++) {
  184. v = myrnd(&seed, 65535) - 32768;
  185. for (j = 0; j < nb_channels; j++)
  186. put16(v);
  187. }
  188. /* 1 second of unrelated ramps for each channel */
  189. for (j = 0; j < nb_channels; j++) {
  190. taba[j] = 0;
  191. tabf1[j] = 100 + myrnd(&seed, 5000);
  192. tabf2[j] = 100 + myrnd(&seed, 5000);
  193. }
  194. for (i = 0; i < 1 * sample_rate; i++) {
  195. for (j = 0; j < nb_channels; j++) {
  196. v = (int_cos(taba[j]) * 10000) >> FRAC_BITS;
  197. put16(v);
  198. f = tabf1[j] + (((tabf2[j] - tabf1[j]) * i) / sample_rate);
  199. taba[j] += (f * FRAC_ONE) / sample_rate;
  200. }
  201. }
  202. /* 2 seconds of 500 Hz with varying volume */
  203. a = 0;
  204. ampa = 0;
  205. for (i = 0; i < 2 * sample_rate; i++) {
  206. for (j = 0; j < nb_channels; j++) {
  207. amp = ((FRAC_ONE + int_cos(ampa)) * 5000) >> FRAC_BITS;
  208. if (j & 1)
  209. amp = 10000 - amp;
  210. v = (int_cos(a) * amp) >> FRAC_BITS;
  211. put16(v);
  212. a += (500 * FRAC_ONE) / sample_rate;
  213. ampa += (2 * FRAC_ONE) / sample_rate;
  214. }
  215. }
  216. fclose(outfile);
  217. return 0;
  218. }