generate_wave_table.c 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. /*
  2. * This file is part of FFmpeg.
  3. *
  4. * FFmpeg is free software; you can redistribute it and/or
  5. * modify it under the terms of the GNU Lesser General Public
  6. * License as published by the Free Software Foundation; either
  7. * version 2.1 of the License, or (at your option) any later version.
  8. *
  9. * FFmpeg is distributed in the hope that it will be useful,
  10. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  12. * Lesser General Public License for more details.
  13. *
  14. * You should have received a copy of the GNU Lesser General Public
  15. * License along with FFmpeg; if not, write to the Free Software
  16. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  17. */
  18. #include <stdint.h>
  19. #include "libavutil/avassert.h"
  20. #include "avfilter.h"
  21. #include "generate_wave_table.h"
  22. void ff_generate_wave_table(enum WaveType wave_type,
  23. enum AVSampleFormat sample_fmt,
  24. void *table, int table_size,
  25. double min, double max, double phase)
  26. {
  27. uint32_t i, phase_offset = phase / M_PI / 2 * table_size + 0.5;
  28. for (i = 0; i < table_size; i++) {
  29. uint32_t point = (i + phase_offset) % table_size;
  30. double d;
  31. switch (wave_type) {
  32. case WAVE_SIN:
  33. d = (sin((double)point / table_size * 2 * M_PI) + 1) / 2;
  34. break;
  35. case WAVE_TRI:
  36. d = (double)point * 2 / table_size;
  37. switch (4 * point / table_size) {
  38. case 0: d = d + 0.5; break;
  39. case 1:
  40. case 2: d = 1.5 - d; break;
  41. case 3: d = d - 1.5; break;
  42. }
  43. break;
  44. default:
  45. av_assert0(0);
  46. }
  47. d = d * (max - min) + min;
  48. switch (sample_fmt) {
  49. case AV_SAMPLE_FMT_FLT: {
  50. float *fp = (float *)table;
  51. *fp++ = (float)d;
  52. table = fp;
  53. continue; }
  54. case AV_SAMPLE_FMT_DBL: {
  55. double *dp = (double *)table;
  56. *dp++ = d;
  57. table = dp;
  58. continue; }
  59. }
  60. d += d < 0 ? -0.5 : 0.5;
  61. switch (sample_fmt) {
  62. case AV_SAMPLE_FMT_S16: {
  63. int16_t *sp = table;
  64. *sp++ = (int16_t)d;
  65. table = sp;
  66. continue; }
  67. case AV_SAMPLE_FMT_S32: {
  68. int32_t *ip = table;
  69. *ip++ = (int32_t)d;
  70. table = ip;
  71. continue; }
  72. default:
  73. av_assert0(0);
  74. }
  75. }
  76. }