-
Notifications
You must be signed in to change notification settings - Fork 30
/
Copy pathutil.h
143 lines (131 loc) · 4.41 KB
/
util.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
/*
* This file is part of dsp.
*
* Copyright (c) 2013-2024 Michael Barbour <[email protected]>
*
* Permission to use, copy, modify, and distribute this software for any
* purpose with or without fee is hereby granted, provided that the above
* copyright notice and this permission notice appear in all copies.
*
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
* ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
* ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
* OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
*/
#ifndef DSP_UTIL_H
#define DSP_UTIL_H
#include <stdint.h>
#include <string.h>
#include "dsp.h"
#define LENGTH(x) (sizeof(x) / sizeof((x)[0]))
#define MAXIMUM(a, b) (((a) > (b)) ? (a) : (b))
#define MINIMUM(a, b) (((a) < (b)) ? (a) : (b))
#define CHECK_RANGE(cond, name, action) \
do { if (!(cond)) { \
LOG_FMT(LL_ERROR, "%s: error: %s out of range", argv[0], name); \
action; \
} } while (0)
#define CHECK_FREQ(var, fs, name, action) \
CHECK_RANGE((var) >= 0.0 && (var) < (double) (fs) / 2.0, name, action)
#define CHECK_ENDPTR(str, endptr, param_name, action) \
do { if (check_endptr(argv[0], str, endptr, param_name)) { action; } } while (0)
#if 0
#define GET_BIT(x, o) (((char *) x)[(int) (o) / 8] & (1 << ((int) (o) % 8)))
#define SET_BIT(x, o) ((char *) x)[(int) (o) / 8] |= (1 << ((int) (o) % 8))
#define SET_SELECTOR(x, n) memset(x, 0xff, ((n) - 1) / 8 + 1)
#define CLEAR_SELECTOR(x, n) memset(x, 0x00, ((n) - 1) / 8 + 1)
#define NEW_SELECTOR(n) calloc(((n) - 1) / 8 + 1, sizeof(char))
#define COPY_SELECTOR(dest, src, n) memcpy(dest, src, ((n) - 1) / 8 + 1)
#else
#define GET_BIT(x, o) ((x)[(o)])
#define SET_BIT(x, o) (x)[(o)] = 1
#define SET_SELECTOR(x, n) memset(x, 0x01, n)
#define CLEAR_SELECTOR(x, n) memset(x, 0x00, n)
#define NEW_SELECTOR(n) calloc(n, sizeof(char))
#define COPY_SELECTOR(dest, src, n) memcpy(dest, src, n)
#endif
#define TEST_BIT(x, o, s) (!!GET_BIT(x, o) == !!(s))
#define IS_POWER_OF_2(x) ((x) && !((x)&((x)-1)))
#define PM_RAND_MAX 0x7fffffff
int check_endptr(const char *, const char *, const char *, const char *);
double parse_freq(const char *, char **);
ssize_t parse_len(const char *, int, char **);
double parse_len_frac(const char *, double, char **);
int parse_selector(const char *, char *, int);
int parse_selector_masked(const char *, char *, const char *, int);
void print_selector(const char *, int);
int num_bits_set(const char *, int);
int gen_argv_from_string(const char *, int *, char ***);
char * get_file_contents(const char *);
char * construct_full_path(const char *, const char *);
char * isolate(char *, char);
#ifdef HAVE_FFTW3
ssize_t next_fast_fftw_len(ssize_t);
#endif
#ifdef INT64_MAX
#define PM_RAND_R_DEFINE_FUNC(func_name, A) \
static inline uint32_t func_name(uint32_t *s) \
{ \
uint64_t p = (uint64_t) *s * (A); \
uint32_t r = (p & 0x7fffffff) + (p >> 31); \
r = (r & 0x7fffffff) + (r >> 31); \
return *s = r; \
}
#else
#define PM_RAND_R_DEFINE_FUNC(func_name, A) \
static inline uint32_t func_name(uint32_t *s) \
{ \
uint32_t l = (*s & 0x7fff) * (A); \
uint32_t h = (*s >> 15) * (A); \
uint32_t r = l + ((h & 0xffff) << 15) + (h >> 16); \
r = (r & 0x7fffffff) + (r >> 31); \
return *s = r; \
}
#endif
PM_RAND_R_DEFINE_FUNC(pm_rand1_r, 48271)
PM_RAND_R_DEFINE_FUNC(pm_rand2_r, 16807)
static inline uint32_t pm_rand(void)
{
static uint32_t s = 1;
return pm_rand1_r(&s);
}
static inline sample_t tpdf_dither_get_mult(int prec)
{
if (prec < 1 || prec > 32)
return 0.0;
uint32_t d = ((uint32_t) 1) << (prec - 1);
return 1.0 / ((sample_t) PM_RAND_MAX * d);
}
static inline sample_t tpdf_noise(sample_t mult)
{
#if 1
/* Faster and gives better quality noise */
static uint32_t s0 = 1, s1 = 1;
int32_t n1 = pm_rand1_r(&s0);
int32_t n2 = pm_rand2_r(&s1);
return (n1 - n2) * mult;
#else
int32_t n1 = pm_rand();
int32_t n2 = pm_rand();
return (n1 - n2) * mult;
#endif
}
static inline ssize_t ratio_mult_ceil(ssize_t v, int n, int d)
{
long long int r = (long long int) v * n;
return (ssize_t) ((r % d != 0) ? r / d + 1 : r / d);
}
static inline int find_gcd(int a, int b)
{
int c;
while (b != 0) {
c = b;
b = a % b;
a = c;
}
return a;
}
#endif