| 1 |
/* |
| 2 |
syn123_int: internal header for libsyn123 |
| 3 |
|
| 4 |
copyright 2018-2023 by the mpg123 project, |
| 5 |
licensed under the terms of the LGPL 2.1 |
| 6 |
see COPYING and AUTHORS files in distribution or http://mpg123.org |
| 7 |
|
| 8 |
initially written by Thomas Orgis |
| 9 |
*/ |
| 10 |
|
| 11 |
#ifndef _MPG123_SYN123_INT_H_ |
| 12 |
#define _MPG123_SYN123_INT_H_ |
| 13 |
|
| 14 |
#define _ISOC99_SOURCE |
| 15 |
|
| 16 |
#include "config.h" |
| 17 |
#ifdef LFS_LARGEFILE_64 |
| 18 |
#define _LARGEFILE64_SOURCE |
| 19 |
#endif |
| 20 |
#include "../common/abi_align.h" |
| 21 |
#include "../compat/compat.h" |
| 22 |
|
| 23 |
#define SYN123_NO_LARGEFUNC |
| 24 |
#include "syn123.h" |
| 25 |
// A little hack to help MSVC not having ssize_t, duplicated in external header with largefuncs. |
| 26 |
#ifdef _MSC_VER |
| 27 |
typedef ptrdiff_t syn123_ssize_t; |
| 28 |
#else |
| 29 |
typedef ssize_t syn123_ssize_t; |
| 30 |
#endif |
| 31 |
|
| 32 |
|
| 33 |
// Generally, a number of samples we work on in one go to |
| 34 |
// allow the compiler to know our loops. |
| 35 |
// An enum is the best integer constant you can define in plain C. |
| 36 |
// This sets an upper limit to the number of channels in some functions. |
| 37 |
enum { bufblock = 512 }; |
| 38 |
|
| 39 |
struct syn123_wave |
| 40 |
{ |
| 41 |
enum syn123_wave_id id; |
| 42 |
int backwards; /* TRUE or FALSE */ |
| 43 |
double freq; /* actual frequency */ |
| 44 |
double phase; /* current phase */ |
| 45 |
}; |
| 46 |
|
| 47 |
struct syn123_sweep |
| 48 |
{ |
| 49 |
struct syn123_wave wave; |
| 50 |
double f1, f2; // begin/end frequencies (or logarithms of the same) |
| 51 |
enum syn123_sweep_id id; |
| 52 |
size_t i; // position counter |
| 53 |
size_t d; // duration |
| 54 |
size_t post; // amount of samples after sweep to finish period |
| 55 |
double endphase; // phase for continuing, just after sweep end |
| 56 |
}; |
| 57 |
|
| 58 |
// Only a forward declaration of the resampler state. An instance |
| 59 |
// is allocated and a pointer stored if it is configured. The |
| 60 |
// resampler is pretty disjunct from the other parts of syn123. |
| 61 |
// Not sure if synergies will emerge eventually. |
| 62 |
struct resample_data; |
| 63 |
|
| 64 |
struct filter_chain |
| 65 |
{ |
| 66 |
int mixenc; // double or float |
| 67 |
int channels; |
| 68 |
size_t count; // of filters |
| 69 |
size_t maxcount; // storage allocated for that many, to avoid |
| 70 |
// realloc in syn123_drop_filter() |
| 71 |
// Only one type is configured! |
| 72 |
struct d_filter *df; // double precision filters |
| 73 |
struct f_filter *ff; // single precision filters |
| 74 |
}; |
| 75 |
|
| 76 |
struct syn123_struct |
| 77 |
{ |
| 78 |
// Temporary storage in internal precision. |
| 79 |
// This is a set of two to accomodate x and y=function(x, y). |
| 80 |
// Working in blocks reduces function call overhead and gives |
| 81 |
// chance of vectorization. |
| 82 |
// This may also be used as buffer for data with various encodings, |
| 83 |
// hence a union to express this in the nicest possible way. This storage |
| 84 |
// is used in a number of ways. |
| 85 |
// All members have to be the same size, utilizing the whole storage |
| 86 |
// block. Code using it assumes that sizeof(workbuf) gives the numbers |
| 87 |
// of bytes accessible via any member. |
| 88 |
union { |
| 89 |
double f64[2][bufblock]; |
| 90 |
float f32[2][2*bufblock]; |
| 91 |
uint32_t u32[2][2*bufblock]; |
| 92 |
int32_t i32[2][2*bufblock]; |
| 93 |
uint16_t u16[2][4*bufblock]; |
| 94 |
int16_t i16[2][4*bufblock]; |
| 95 |
uint8_t u8[2][8*bufblock]; |
| 96 |
int8_t i8[2][8*bufblock]; |
| 97 |
// Yes, again the same, to be overly explicit about type names. |
| 98 |
char c[2][8*bufblock]; |
| 99 |
unsigned char uc[2][8*bufblock]; |
| 100 |
} workbuf; |
| 101 |
struct mpg123_fmt fmt; |
| 102 |
int dither; // if dithering is activated for the handle |
| 103 |
int do_dither; // flag for recursive calls of syn123_conv() |
| 104 |
uint32_t dither_seed; |
| 105 |
// Pointer to a generator function that writes a bit of samples |
| 106 |
// into workbuf[1], possibly using workbuf[0] internally. |
| 107 |
// Given count of samples <= bufblock! |
| 108 |
void (*generator)(syn123_handle*, int); |
| 109 |
// Generator configuration. |
| 110 |
// wave generator |
| 111 |
size_t wave_count; |
| 112 |
struct syn123_wave* waves; |
| 113 |
// pink noise, maybe others: simple structs that can be |
| 114 |
// simply free()d |
| 115 |
void* handle; |
| 116 |
uint32_t seed; // random seed for some RNGs |
| 117 |
// Extraction of initially-computed waveform from buffer. |
| 118 |
void *buf; // period buffer |
| 119 |
size_t bufs; // allocated size of buffer in bytes |
| 120 |
size_t maxbuf; // maximum period buffer size in bytes |
| 121 |
size_t samples; // samples (PCM frames) in period buffer |
| 122 |
size_t offset; // offset in buffer for extraction helper |
| 123 |
struct resample_data *rd; // resampler data, if initialized |
| 124 |
struct filter_chain fc; |
| 125 |
}; |
| 126 |
|
| 127 |
#ifndef NO_SMIN |
| 128 |
static size_t smin(size_t a, size_t b) |
| 129 |
{ |
| 130 |
return a < b ? a : b; |
| 131 |
} |
| 132 |
#endif |
| 133 |
|
| 134 |
#ifndef NO_SMAX |
| 135 |
static size_t smax(size_t a, size_t b) |
| 136 |
{ |
| 137 |
return a > b ? a : b; |
| 138 |
} |
| 139 |
#endif |
| 140 |
|
| 141 |
#ifndef NO_GROW_BUF |
| 142 |
// Grow period buffer to at least given size. |
| 143 |
// Content is not preserved. |
| 144 |
static void grow_buf(syn123_handle *sh, size_t bytes) |
| 145 |
{ |
| 146 |
if(sh->bufs >= bytes) |
| 147 |
return; |
| 148 |
if(sh->buf) |
| 149 |
free(sh->buf); |
| 150 |
sh->buf = NULL; |
| 151 |
if(bytes && bytes <= sh->maxbuf) |
| 152 |
sh->buf = malloc(bytes); |
| 153 |
sh->bufs = sh->buf ? bytes : 0; |
| 154 |
} |
| 155 |
#endif |
| 156 |
|
| 157 |
#ifdef FILL_PERIOD |
| 158 |
static int fill_period(syn123_handle *sh) |
| 159 |
{ |
| 160 |
sh->samples = 0; |
| 161 |
if(!sh->maxbuf) |
| 162 |
return SYN123_OK; |
| 163 |
size_t samplesize = MPG123_SAMPLESIZE(sh->fmt.encoding); |
| 164 |
size_t buffer_samples = sh->maxbuf/samplesize; |
| 165 |
grow_buf(sh, buffer_samples*samplesize); |
| 166 |
if(buffer_samples > sh->bufs/samplesize) |
| 167 |
return SYN123_DOOM; |
| 168 |
int outchannels = sh->fmt.channels; |
| 169 |
sh->fmt.channels = 1; |
| 170 |
size_t buffer_bytes = syn123_read(sh, sh->buf, buffer_samples*samplesize); |
| 171 |
sh->fmt.channels = outchannels; |
| 172 |
if(buffer_bytes != buffer_samples*samplesize) |
| 173 |
return SYN123_WEIRD; |
| 174 |
sh->samples = buffer_samples; |
| 175 |
return SYN123_OK; |
| 176 |
} |
| 177 |
#endif |
| 178 |
|
| 179 |
#ifdef RAND_XORSHIFT32 |
| 180 |
// Borrowing the random number algorithm from the libmpg123 dither code. |
| 181 |
// xorshift random number generator |
| 182 |
// See http://www.jstatsoft.org/v08/i14/paper on XOR shift random number generators. |
| 183 |
static float rand_xorshift32(uint32_t *seed) |
| 184 |
{ |
| 185 |
union |
| 186 |
{ |
| 187 |
uint32_t i; |
| 188 |
float f; |
| 189 |
} fi; |
| 190 |
|
| 191 |
fi.i = *seed; |
| 192 |
fi.i ^= (fi.i<<13); |
| 193 |
fi.i ^= (fi.i>>17); |
| 194 |
fi.i ^= (fi.i<<5); |
| 195 |
*seed = fi.i; |
| 196 |
/* scale the number to [-0.5, 0.5] */ |
| 197 |
#ifdef IEEE_FLOAT |
| 198 |
fi.i = (fi.i>>9)|0x3f800000; |
| 199 |
fi.f -= 1.5f; |
| 200 |
#else |
| 201 |
fi.f = (double)fi.i / 4294967295.0; |
| 202 |
fi.f -= 0.5f; |
| 203 |
#endif |
| 204 |
return fi.f; |
| 205 |
} |
| 206 |
#endif |
| 207 |
|
| 208 |
#endif |