aboutsummaryrefslogtreecommitdiffstats
path: root/src/libsamplerate/samplerate.c
blob: 002d77d9060cd5ee7234d476240bbeaef987ef9d (plain)
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
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
/* Sample rate conversion
 *
 * (C) 2016 by Andreas Eversberg <jolly@eversberg.eu>
 * All Rights Reserved
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

#include <stdio.h>
#include <stdint.h>
#include <errno.h>
#include <string.h>
#include <stdlib.h>
#include "../libsample/sample.h"
#include "samplerate.h"

int init_samplerate(samplerate_t *state, double low_samplerate, double high_samplerate, double filter_cutoff)
{
	memset(state, 0, sizeof(*state));
	state->factor = high_samplerate / low_samplerate;
	if (state->factor < 1.0) {
		fprintf(stderr, "Software error: Low sample rate must be lower than high sample rate, aborting!\n");
		abort();
	}

	state->filter_cutoff = filter_cutoff;
	if (state->filter_cutoff) {
		iir_lowpass_init(&state->up.lp, filter_cutoff, high_samplerate, 2);
		iir_lowpass_init(&state->down.lp, filter_cutoff, high_samplerate, 2);
	}

	return 0;
}

/* convert high sample rate to low sample rate */
int samplerate_downsample(samplerate_t *state, sample_t *samples, int input_num)
{
	int output_num = 0, i, idx;
	double factor = state->factor, in_index, diff;
	sample_t output[(int)((double)input_num / factor + 0.5) + 10]; /* add some safety */
	sample_t last_sample;

	/* filter down */
	if (state->filter_cutoff)
		iir_process(&state->down.lp, samples, input_num);

	/* get last sample for interpolation */
	last_sample = state->down.last_sample;

	/* resample filtered result */
	in_index = state->down.in_index;

	for (i = 0; ; i++) {
		/* convert index to int */
		idx = (int)in_index;
		/* if index is outside input sample range, we are done */
		if (idx >= input_num)
			break;
		/* linear interpolation */
		diff = in_index - (double)idx;
		if (idx)
			output[i] = samples[idx - 1] * (1.0 - diff) + samples[idx] * diff;
		else
			output[i] = last_sample * (1.0 - diff) + samples[idx] * diff;
		/* count output number */
		output_num++;
		/* increment input index */
		in_index += factor;
	}

	/* store last sample for interpolation */
	if (input_num)
		state->down.last_sample = samples[input_num - 1];

	/* remove number of input samples from index */
	in_index -= (double)input_num;
	/* in_index cannot be negative, except due to rounding error, so... */
	if ((int)in_index < 0)
		in_index = 0.0;

	state->down.in_index = in_index;

	/* copy samples */
	for (i = 0; i < output_num; i++)
		*samples++ = output[i];

	return output_num;
}

int samplerate_upsample_input_num(samplerate_t *state, int output_num)
{
	double factor = 1.0 / state->factor, in_index;
	int idx = 0;

	/* count output */
	in_index = state->up.in_index;

	while (output_num--) {
		/* increment input index */
		in_index += factor;
		/* count index on overflow */
		if (in_index >= 1.0) {
			idx++;
			in_index -= 1.0;
		}
	}

	return idx;
}

int samplerate_upsample_output_num(samplerate_t *state, int input_num)
{
	double factor = 1.0 / state->factor, in_index;
	int output_num = 0, idx = 0;

	/* count output */
	in_index = state->up.in_index;

	while (idx < input_num) {
		/* count output number */
		output_num++;
		/* increment input index */
		in_index += factor;
		/* count index on overflow */
		if (in_index >= 1.0) {
			idx++;
			in_index -= 1.0;
		}
	}

	return output_num;
}

/* convert low sample rate to high sample rate */
void samplerate_upsample(samplerate_t *state, sample_t *input, int input_num, sample_t *output, int output_num)
{
	int i, idx;
	double factor = 1.0 / state->factor, in_index;
	sample_t buff[output_num];
	sample_t *samples, current_sample, last_sample;

	/* get last sample for interpolation */
	current_sample = state->up.current_sample;
	last_sample = state->up.last_sample;

	if (input == output)
		samples = buff;
	else
		samples = output;

	/* resample input */
	in_index = state->up.in_index;
	idx = 0;

	for (i = 0; i < output_num; i++) {
		/* linear interpolation */
		samples[i] = last_sample * (1.0 - in_index) + current_sample * in_index;
		/* increment input index */
		in_index += factor;
		/* get next sample on overflow */
		if (in_index >= 1.0) {
			if (idx == input_num) {
				fprintf(stderr, "Given input_num is too small, please fix!\n");
			}
			last_sample = current_sample;
			current_sample = input[idx++];
			in_index -= 1.0;
		}
	}
	if (idx < input_num) {
		fprintf(stderr, "Given input_num is too large, please fix!\n");
		abort();
	}

	/* store last sample for interpolation */
	state->up.last_sample = last_sample;
	state->up.current_sample = current_sample;
	state->up.in_index = in_index;

	/* filter up */
	if (state->filter_cutoff)
		iir_process(&state->up.lp, samples, output_num);

	if (input == output) {
		/* copy samples */
		for (i = 0; i < output_num; i++)
			*output++ = samples[i];
	}
}