foobar2000 SDK  2015-08-03
audio_sample.h
Go to the documentation of this file.
1 #include <math.h>
2 
3 #define audio_sample_size 32
4 
5 #if audio_sample_size == 32
6 typedef float audio_sample;
7 #define audio_sample_asm dword
8 #elif audio_sample_size == 64
9 typedef double audio_sample;
10 #define audio_sample_asm qword
11 #else
12 #error wrong audio_sample_size
13 #endif
14 
15 #define audio_sample_bytes (audio_sample_size/8)
16 
17 namespace pfc {
18  // made a class so it can be redirected to an alternate class more easily than with namespacing
19  // in win desktop fb2k these are implemented in a DLL
20  class audio_math {
21  public:
22 
24  static void scale(const audio_sample * p_source, t_size p_count, audio_sample * p_output, audio_sample p_scale);
25  static void convert_to_int16(const audio_sample * p_source, t_size p_count, t_int16 * p_output, audio_sample p_scale);
26  static void convert_to_int32(const audio_sample * p_source, t_size p_count, t_int32 * p_output, audio_sample p_scale);
27  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);
28  static void convert_from_int16(const t_int16 * p_source, t_size p_count, audio_sample * p_output, audio_sample p_scale);
29  static void convert_from_int32(const t_int32 * p_source, t_size p_count, audio_sample * p_output, audio_sample p_scale);
30  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);
31  static audio_sample calculate_peak(const audio_sample * p_source, t_size p_count);
32  static void remove_denormals(audio_sample * p_buffer, t_size p_count);
33  static void add_offset(audio_sample * p_buffer, audio_sample p_delta, t_size p_count);
34 
35  static inline t_uint64 time_to_samples(double p_time, t_uint32 p_sample_rate) {
36  return (t_uint64)floor((double)p_sample_rate * p_time + 0.5);
37  }
38 
39  static inline double samples_to_time(t_uint64 p_samples, t_uint32 p_sample_rate) {
40  PFC_ASSERT(p_sample_rate > 0);
41  return (double)p_samples / (double)p_sample_rate;
42  }
43 
44 
45 #if defined(_MSC_VER) && defined(_M_IX86)
46  inline static t_int64 rint64(audio_sample val) {
47  t_int64 rv;
48  _asm {
49  fld val;
50  fistp rv;
51  }
52  return rv;
53  }
54  inline static t_int32 rint32(audio_sample val) {
55  t_int32 rv;
56  _asm {
57  fld val;
58  fistp rv;
59  }
60  return rv;
61  }
62 #elif defined(_MSC_VER) && defined(_M_X64)
63  inline static t_int64 rint64(audio_sample val) { return (t_int64)floor(val + 0.5); }
64  static inline t_int32 rint32(float p_val) {
65  return (t_int32)_mm_cvtss_si32(_mm_load_ss(&p_val));
66  }
67 #else
68  inline static t_int64 rint64(audio_sample val) { return (t_int64)floor(val + 0.5); }
69  inline static t_int32 rint32(audio_sample val) { return (t_int32)floor(val + 0.5); }
70 #endif
71 
72 
73  static inline audio_sample gain_to_scale(double p_gain) { return (audio_sample)pow(10.0, p_gain / 20.0); }
74  static inline double scale_to_gain(double scale) { return 20.0*log10(scale); }
75 
76  static const audio_sample float16scale;
77 
78  static audio_sample decodeFloat24ptr(const void * sourcePtr);
79  static audio_sample decodeFloat24ptrbs(const void * sourcePtr);
80  static audio_sample decodeFloat16(uint16_t source);
81  }; // class audio_math
82 
83 } // namespace pfc
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 t_int32 rint32(float p_val)
Definition: audio_sample.h:64
static audio_sample gain_to_scale(double p_gain)
Definition: audio_sample.h:73
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 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 double samples_to_time(t_uint64 p_samples, t_uint32 p_sample_rate)
Definition: audio_sample.h:39
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 double scale_to_gain(double scale)
Definition: audio_sample.h:74
static t_int64 rint64(audio_sample val)
Definition: audio_sample.h:46
static audio_sample decodeFloat24ptr(const void *sourcePtr)
Definition: audio_sample.cpp:4
static const audio_sample float16scale
Definition: audio_sample.h:76
static audio_sample decodeFloat24ptrbs(const void *sourcePtr)
float audio_sample
Definition: audio_sample.h:6
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 decodeFloat16(uint16_t source)
static t_uint64 time_to_samples(double p_time, t_uint32 p_sample_rate)
Definition: audio_sample.h:35
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