foobar2000 SDK  2015-08-03
audio_math.cpp
Go to the documentation of this file.
1 #include "pfc.h"
2 
4 {
5  audio_sample peak = 0;
6  t_size num = p_num;
7  for(;num;num--)
8  {
9  audio_sample temp = (audio_sample)fabs(*(p_src++));
10  if (temp>peak) peak = temp;
11  }
12  return peak;
13 }
14 
15 
16 
17 
18 static void noopt_convert_to_32bit(const audio_sample * p_source,t_size p_count,t_int32 * p_output,float p_scale)
19 {
20  t_size num = p_count;
21  for(;num;--num)
22  {
23  t_int64 val = pfc::audio_math::rint64( *(p_source++) * p_scale );
24  if (val < -(t_int64)0x80000000) val = -(t_int64)0x80000000;
25  else if (val > 0x7FFFFFFF) val = 0x7FFFFFFF;
26  *(p_output++) = (t_int32) val;
27  }
28 }
29 
30 inline static void noopt_convert_to_16bit(const audio_sample * p_source,t_size p_count,t_int16 * p_output,float p_scale) {
31  for(t_size n=0;n<p_count;n++) {
32  *(p_output++) = (t_int16) pfc::clip_t(pfc::audio_math::rint32(*(p_source++)*p_scale),-0x8000,0x7FFF);
33  }
34 }
35 
36 inline static void noopt_convert_from_int16(const t_int16 * p_source,t_size p_count,audio_sample * p_output,float p_scale)
37 {
38  t_size num = p_count;
39  for(;num;num--)
40  *(p_output++) = (audio_sample)*(p_source++) * p_scale;
41 }
42 
43 
44 
45 inline static void noopt_convert_from_int32(const t_int32 * p_source,t_size p_count,audio_sample * p_output,float p_scale)
46 {
47  t_size num = p_count;
48  for(;num;num--)
49  *(p_output++) = (audio_sample)*(p_source++) * p_scale;
50 }
51 
52 inline static void noopt_scale(const audio_sample * p_source,t_size p_count,audio_sample * p_output,audio_sample p_scale)
53 {
54  for(t_size n=0;n<p_count;n++)
55  p_output[n] = p_source[n] * p_scale;
56 }
57 
58 
59 namespace pfc {
60 
61  void audio_math::scale(const audio_sample * p_source,t_size p_count,audio_sample * p_output,audio_sample p_scale)
62  {
63  noopt_scale(p_source,p_count,p_output,p_scale);
64  }
65 
66  void audio_math::convert_to_int16(const audio_sample * p_source,t_size p_count,t_int16 * p_output,audio_sample p_scale)
67  {
68  audio_sample scale = (audio_sample)(p_scale * 0x8000);
69  noopt_convert_to_16bit(p_source,p_count,p_output,scale);
70  }
71 
73  {
74  //todo?
75  convert_to_int16(p_source,p_count,p_output,p_scale);
76  return p_scale * calculate_peak(p_source,p_count);
77  }
78 
79  void audio_math::convert_from_int16(const t_int16 * p_source,t_size p_count,audio_sample * p_output,audio_sample p_scale)
80  {
81  audio_sample scale = (audio_sample) ( p_scale / (double) 0x8000 );
82  noopt_convert_from_int16(p_source,p_count,p_output,scale);
83  }
84 
85  void audio_math::convert_to_int32(const audio_sample * p_source,t_size p_count,t_int32 * p_output,audio_sample p_scale)
86  {
87  audio_sample scale = (audio_sample)(p_scale * 0x80000000);
88  {
89  noopt_convert_to_32bit(p_source,p_count,p_output,scale);
90  }
91  }
92 
94  {
95  convert_to_int32(p_source,p_count,p_output,p_scale);
96  return p_scale * calculate_peak(p_source,p_count);
97  }
98 
99  void audio_math::convert_from_int32(const t_int32 * p_source,t_size p_count,audio_sample * p_output,audio_sample p_scale)
100  {
101  audio_sample scale = (audio_sample) ( p_scale / (double) 0x80000000 );
102  noopt_convert_from_int32(p_source,p_count,p_output,scale);
103  }
104 
105 
107  {
108  return noopt_calculate_peak(p_source,p_count);
109  }
110 
112 #if audio_sample_size == 32
113  t_uint32 * ptr = reinterpret_cast<t_uint32*>(p_buffer);
114  for(;p_count;p_count--)
115  {
116  t_uint32 t = *ptr;
117  if ((t & 0x007FFFFF) && !(t & 0x7F800000)) *ptr=0;
118  ptr++;
119  }
120 #elif audio_sample_size == 64
121  t_uint64 * ptr = reinterpret_cast<t_uint64*>(p_buffer);
122  for(;p_count;p_count--)
123  {
124  t_uint64 t = *ptr;
125  if ((t & 0x000FFFFFFFFFFFFF) && !(t & 0x7FF0000000000000)) *ptr=0;
126  ptr++;
127  }
128 #else
129 #error unsupported
130 #endif
131  }
132 
133  void audio_math::add_offset(audio_sample * p_buffer,audio_sample p_delta,t_size p_count) {
134  for(t_size n=0;n<p_count;n++) {
135  p_buffer[n] += p_delta;
136  }
137  }
138 
139 
140  const audio_sample audio_math::float16scale = 65536.f;
141 }
static audio_sample convert_to_int32_calculate_peak(const audio_sample *p_source, t_size p_count, t_int32 *p_output, audio_sample p_scale)
Definition: audio_math.cpp:93
static audio_sample calculate_peak(const audio_sample *p_source, t_size p_count)
Definition: audio_math.cpp:106
uint64_t t_uint64
Definition: int_types.h:3
static void noopt_convert_from_int16(const t_int16 *p_source, t_size p_count, audio_sample *p_output, float p_scale)
Definition: audio_math.cpp:36
static void convert_from_int32(const t_int32 *p_source, t_size p_count, audio_sample *p_output, audio_sample p_scale)
Definition: audio_math.cpp:99
static void add_offset(audio_sample *p_buffer, audio_sample p_delta, t_size p_count)
Definition: audio_math.cpp:133
static void convert_from_int16(const t_int16 *p_source, t_size p_count, audio_sample *p_output, audio_sample p_scale)
Definition: audio_math.cpp:79
static t_int32 rint32(audio_sample val)
Definition: audio_sample.h:54
int16_t t_int16
Definition: int_types.h:6
static void remove_denormals(audio_sample *p_buffer, t_size p_count)
Definition: audio_math.cpp:111
static void convert_to_int16(const audio_sample *p_source, t_size p_count, t_int16 *p_output, audio_sample p_scale)
Definition: audio_math.cpp:66
int32_t t_int32
Definition: int_types.h:4
static void convert_to_int32(const audio_sample *p_source, t_size p_count, t_int32 *p_output, audio_sample p_scale)
Definition: audio_math.cpp:85
size_t t_size
Definition: int_types.h:48
static t_int64 rint64(audio_sample val)
Definition: audio_sample.h:46
static void noopt_scale(const audio_sample *p_source, t_size p_count, audio_sample *p_output, audio_sample p_scale)
Definition: audio_math.cpp:52
static void noopt_convert_to_16bit(const audio_sample *p_source, t_size p_count, t_int16 *p_output, float p_scale)
Definition: audio_math.cpp:30
static const audio_sample float16scale
Definition: audio_sample.h:76
T clip_t(const T &p_item, const T &p_min, const T &p_max)
Definition: primitives.h:565
float audio_sample
Definition: audio_sample.h:6
static void noopt_convert_to_32bit(const audio_sample *p_source, t_size p_count, t_int32 *p_output, float p_scale)
Definition: audio_math.cpp:18
static void noopt_convert_from_int32(const t_int32 *p_source, t_size p_count, audio_sample *p_output, float p_scale)
Definition: audio_math.cpp:45
static void scale(const audio_sample *p_source, t_size p_count, audio_sample *p_output, audio_sample p_scale)
p_source/p_output can point to same buffer
Definition: audio_math.cpp:61
static audio_sample noopt_calculate_peak(const audio_sample *p_src, t_size p_num)
Definition: audio_math.cpp:3
uint32_t t_uint32
Definition: int_types.h:5
static audio_sample convert_to_int16_calculate_peak(const audio_sample *p_source, t_size p_count, t_int16 *p_output, audio_sample p_scale)
Definition: audio_math.cpp:72
int64_t t_int64
Definition: int_types.h:2