FFmpeg
generate_wave_table.c
Go to the documentation of this file.
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 
19 #include <stdint.h>
20 #include "libavutil/avassert.h"
21 #include "avfilter.h"
22 #include "generate_wave_table.h"
23 
24 void ff_generate_wave_table(enum WaveType wave_type,
25  enum AVSampleFormat sample_fmt,
26  void *table, int table_size,
27  double min, double max, double phase)
28 {
29  uint32_t i, phase_offset = phase / M_PI / 2 * table_size + 0.5;
30 
31  for (i = 0; i < table_size; i++) {
32  uint32_t point = (i + phase_offset) % table_size;
33  double d;
34 
35  switch (wave_type) {
36  case WAVE_SIN:
37  d = (sin((double)point / table_size * 2 * M_PI) + 1) / 2;
38  break;
39  case WAVE_TRI:
40  d = (double)point * 2 / table_size;
41  switch (4 * point / table_size) {
42  case 0: d = d + 0.5; break;
43  case 1:
44  case 2: d = 1.5 - d; break;
45  case 3: d = d - 1.5; break;
46  }
47  break;
48  default:
49  av_assert0(0);
50  }
51 
52  d = d * (max - min) + min;
53  switch (sample_fmt) {
54  case AV_SAMPLE_FMT_FLT: {
55  float *fp = (float *)table;
56  *fp++ = (float)d;
57  table = fp;
58  continue; }
59  case AV_SAMPLE_FMT_DBL: {
60  double *dp = (double *)table;
61  *dp++ = d;
62  table = dp;
63  continue; }
64  }
65 
66  d += d < 0 ? -0.5 : 0.5;
67  switch (sample_fmt) {
68  case AV_SAMPLE_FMT_S16: {
69  int16_t *sp = table;
70  *sp++ = (int16_t)d;
71  table = sp;
72  continue; }
73  case AV_SAMPLE_FMT_S32: {
74  int32_t *ip = table;
75  *ip++ = (int32_t)d;
76  table = ip;
77  continue; }
78  default:
79  av_assert0(0);
80  }
81  }
82 }
WaveType
WaveType
Definition: generate_wave_table.h:24
table
static const uint16_t table[]
Definition: prosumer.c:206
max
#define max(a, b)
Definition: cuda_runtime.h:33
WAVE_TRI
@ WAVE_TRI
Definition: generate_wave_table.h:26
avassert.h
av_assert0
#define av_assert0(cond)
assert() equivalent, that is always enabled.
Definition: avassert.h:37
WAVE_SIN
@ WAVE_SIN
Definition: generate_wave_table.h:25
int32_t
int32_t
Definition: audio_convert.c:194
fp
#define fp
Definition: regdef.h:44
ff_generate_wave_table
void ff_generate_wave_table(enum WaveType wave_type, enum AVSampleFormat sample_fmt, void *table, int table_size, double min, double max, double phase)
Definition: generate_wave_table.c:24
sp
#define sp
Definition: regdef.h:63
M_PI
#define M_PI
Definition: mathematics.h:52
i
int i
Definition: input.c:407
AVSampleFormat
AVSampleFormat
Audio sample formats.
Definition: samplefmt.h:58
AV_SAMPLE_FMT_S16
@ AV_SAMPLE_FMT_S16
signed 16 bits
Definition: samplefmt.h:61
generate_wave_table.h
avfilter.h
AV_SAMPLE_FMT_DBL
@ AV_SAMPLE_FMT_DBL
double
Definition: samplefmt.h:64
AV_SAMPLE_FMT_S32
@ AV_SAMPLE_FMT_S32
signed 32 bits
Definition: samplefmt.h:62
AV_SAMPLE_FMT_FLT
@ AV_SAMPLE_FMT_FLT
float
Definition: samplefmt.h:63
min
float min
Definition: vorbis_enc_data.h:456