~q3cpma/mus

ref: cf29c2237d0c99e5240a55895c07564ef4635392 mus/mus_player/filter.c -rw-r--r-- 873 bytes
cf29c223q3cpma Add a #pragma once to the new xoshiro128plus.h header 1 year, 7 months ago
                                                                                
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
#include <math.h>

#include "filter.h"
#include "misc.h"
#include "xoshiro128plus.h"


void interleave16_stereo(const int32_t *restrict inbuf[],
	int16_t *restrict outbuf, size_t nbsample)
{
	for (size_t i = 0; i < nbsample; ++i)
	{
		*outbuf++ = *inbuf[0]++;
		*outbuf++ = *inbuf[1]++;
	}
}

#define INT16_HEADROOM_MULT (32767.497f / 32768.f)
static inline int16_t triangle_dither(float x)
{
	static float prev_rand = 0.f;
	const float rand = frand();
	const float tmp = x * INT16_HEADROOM_MULT + rand - prev_rand;
	prev_rand = rand;
	return lrintf(tmp);
}

void apply_gain(int16_t *restrict buf, size_t nbsample, float gain)
{
	if (gain == 1.f)
		return;

	for (size_t i = 0; i < nbsample; ++i)
		buf[i] = triangle_dither(buf[i] * gain);
}

bool write_pcm(int16_t *restrict buf, int outfd, size_t nbsample)
{
	return write_full(outfd, buf, nbsample * sizeof(int16_t));
}