summaryrefslogtreecommitdiff
path: root/apps/codecs/libgme/resampler.c
blob: a69a92cc53b23045e86831ca8c964ea6e85cb8fd (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
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
// Game_Music_Emu 0.5.5. http://www.slack.net/~ant/

#include "resampler.h"

#include <stdlib.h>
#include <string.h>

/* Copyright (C) 2003-2006 Shay Green. This module is free software; you
can redistribute it and/or modify it under the terms of the GNU Lesser
General Public License as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) any later version. This
module 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 Lesser General Public License for more
details. You should have received a copy of the GNU Lesser General Public
License along with this module; if not, write to the Free Software Foundation,
Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA */

#include "blargg_source.h"

// TODO: fix this. hack since resampler holds back some output.
unsigned const resampler_extra = 34;

enum { shift = 14 };
int const unit = 1 << shift;

blargg_err_t Resampler_setup( struct Resampler* this, int fm_rate, int fm_gain, int rate, int gain )
 {
	this->gain_ = (int)( ((1LL << gain_bits) * fm_gain * gain) / FP_ONE_GAIN );
	this->step  = (int)( ((1LL << shift) * fm_rate) / rate + 1);
	this->rate_ = this->step;
 	return 0;
 }

blargg_err_t Resampler_reset( struct Resampler* this, int pairs )
{
	// expand allocations a bit
	Resampler_resize( this, pairs );
	this->resampler_size = this->oversamples_per_frame + (this->oversamples_per_frame >> 2);

	this->buffer_size = this->resampler_size;
	this->pos = 0;
	this->write_pos = 0;
	return 0;
}

void Resampler_resize( struct Resampler* this, int pairs )
{
	int new_sample_buf_size = pairs * 2;
	if ( this->sample_buf_size != new_sample_buf_size )
	{
		this->sample_buf_size = new_sample_buf_size;
		this->oversamples_per_frame = (int) ((pairs * this->rate_ * 2LL) / unit) + 2;
		Resampler_clear( this );
	}
}

static void mix_mono( struct Resampler* this, struct Stereo_Buffer* stereo_buf, dsample_t* out_ )
{
	int const bass = BLIP_READER_BASS( stereo_buf->bufs [0] );
	BLIP_READER_BEGIN( sn, stereo_buf->bufs [0] );
	
	int count = this->sample_buf_size >> 1;
	BLIP_READER_ADJ_( sn, count );
	
	typedef dsample_t stereo_dsample_t [2];
	stereo_dsample_t* BLARGG_RESTRICT out = (stereo_dsample_t*) out_ + count;
	stereo_dsample_t const* BLARGG_RESTRICT in =
			(stereo_dsample_t const*) this->sample_buf + count;
	int offset = -count;
	int const gain = this->gain_;
	do
	{
		int s = BLIP_READER_READ_RAW( sn ) >> (blip_sample_bits - 16);
		BLIP_READER_NEXT_IDX_( sn, bass, offset );
		
		int l = (in [offset] [0] * gain >> gain_bits) + s;
		int r = (in [offset] [1] * gain >> gain_bits) + s;
		
		BLIP_CLAMP( l, l );
		out [offset] [0] = (blip_sample_t) l;
		
		BLIP_CLAMP( r, r );
		out [offset] [1] = (blip_sample_t) r;
	}
	while ( ++offset );
	
	BLIP_READER_END( sn, stereo_buf->bufs [0] );
}

static void mix_stereo( struct Resampler* this, struct Stereo_Buffer* stereo_buf, dsample_t* out_ )
{
	int const bass = BLIP_READER_BASS( stereo_buf->bufs [0] );
	BLIP_READER_BEGIN( snc, stereo_buf->bufs [0] );
	BLIP_READER_BEGIN( snl, stereo_buf->bufs [1] );
	BLIP_READER_BEGIN( snr, stereo_buf->bufs [2] );
	
	int count = this->sample_buf_size >> 1;
	BLIP_READER_ADJ_( snc, count );
	BLIP_READER_ADJ_( snl, count );
	BLIP_READER_ADJ_( snr, count );
	
	typedef dsample_t stereo_dsample_t [2];
	stereo_dsample_t* BLARGG_RESTRICT out = (stereo_dsample_t*) out_ + count;
	stereo_dsample_t const* BLARGG_RESTRICT in =
			(stereo_dsample_t const*) this->sample_buf + count;
	int offset = -count;
	int const gain = this->gain_;
	do
	{
		int sc = BLIP_READER_READ_RAW( snc ) >> (blip_sample_bits - 16);
		int sl = BLIP_READER_READ_RAW( snl ) >> (blip_sample_bits - 16);
		int sr = BLIP_READER_READ_RAW( snr ) >> (blip_sample_bits - 16);
		BLIP_READER_NEXT_IDX_( snc, bass, offset );
		BLIP_READER_NEXT_IDX_( snl, bass, offset );
		BLIP_READER_NEXT_IDX_( snr, bass, offset );
		
		int l = (in [offset] [0] * gain >> gain_bits) + sl + sc;
		int r = (in [offset] [1] * gain >> gain_bits) + sr + sc;
		
		BLIP_CLAMP( l, l );
		out [offset] [0] = (blip_sample_t) l;
		
		BLIP_CLAMP( r, r );
		out [offset] [1] = (blip_sample_t) r;
	}
	while ( ++offset );
	
	BLIP_READER_END( snc, stereo_buf->bufs [0] );
	BLIP_READER_END( snl, stereo_buf->bufs [1] );
	BLIP_READER_END( snr, stereo_buf->bufs [2] );
}

static void mix_stereo_no_center( struct Resampler* this, struct Stereo_Buffer* stereo_buf, dsample_t* out_ )
{
	int const bass = BLIP_READER_BASS( stereo_buf->bufs [0] );
	BLIP_READER_BEGIN( snl, stereo_buf->bufs [1] );
	BLIP_READER_BEGIN( snr, stereo_buf->bufs [2] );
	
	int count = this->sample_buf_size >> 1;
	BLIP_READER_ADJ_( snl, count );
	BLIP_READER_ADJ_( snr, count );
	
	typedef dsample_t stereo_dsample_t [2];
	stereo_dsample_t* BLARGG_RESTRICT out = (stereo_dsample_t*) out_ + count;
	stereo_dsample_t const* BLARGG_RESTRICT in =
			(stereo_dsample_t const*) this->sample_buf + count;
	int offset = -count;
	int const gain = this->gain_;
	do
	{
		int sl = BLIP_READER_READ_RAW( snl ) >> (blip_sample_bits - 16);
		int sr = BLIP_READER_READ_RAW( snr ) >> (blip_sample_bits - 16);
		BLIP_READER_NEXT_IDX_( snl, bass, offset );
		BLIP_READER_NEXT_IDX_( snr, bass, offset );
		
		int l = (in [offset] [0] * gain >> gain_bits) + sl;
		int r = (in [offset] [1] * gain >> gain_bits) + sr;
		
		BLIP_CLAMP( l, l );
		out [offset] [0] = (blip_sample_t) l;
		
		BLIP_CLAMP( r, r );
		out [offset] [1] = (blip_sample_t) r;
	}
	while ( ++offset );
	
	BLIP_READER_END( snl, stereo_buf->bufs [1] );
	BLIP_READER_END( snr, stereo_buf->bufs [2] );
}

static dsample_t const* resample_( struct Resampler* this, dsample_t** out_,
		dsample_t const* out_end, dsample_t const in [], int in_size )
{
	in_size -= write_offset;
	if ( in_size > 0 )
	{
		dsample_t* BLIP_RESTRICT out = *out_;
		dsample_t const* const in_end = in + in_size;
		
		int const step = this->step;
		int       pos  = this->pos;
		
		// TODO: IIR filter, then linear resample
		// TODO: detect skipped sample, allowing merging of IIR and resample?
		
		do
		{
			#define INTERP( i, out )\
				out = (in [0 + i] * (unit - pos) + ((in [2 + i] + in [4 + i] + in [6 + i]) << shift) +\
				in [8 + i] * pos) >> (shift + 2);
			
			int out_0;
			INTERP( 0,                  out_0 )
			INTERP( 1, out [0] = out_0; out [1] )
			out += stereo;
			
			pos += step;
			in += ((unsigned) pos >> shift) * stereo;
			pos &= unit - 1;
		}
		while ( in < in_end && out < out_end );
		
		this->pos = pos;
		*out_ = out;
	}
	return in;
}

static inline int resample_wrapper( struct Resampler* this, dsample_t out [], int* out_size,
		dsample_t const in [], int in_size )
{
	assert( Resampler_rate( this ) );
	
	dsample_t* out_ = out;
	int result = resample_( this, &out_, out + *out_size, in, in_size ) - in;
	assert( out_ <= out + *out_size );
	assert( result <= in_size );
	
	*out_size = out_ - out;
	return result;
}

static int skip_input( struct Resampler* this, int count )
{
	this->write_pos -= count;
	if ( this->write_pos < 0 ) // occurs when downsampling
	{
		count += this->write_pos;
		this->write_pos = 0;
	}
	memmove( this->buf, &this->buf [count], this->write_pos * sizeof this->buf [0] );
	return count;
}

static void play_frame_( struct Resampler* this, struct Stereo_Buffer* stereo_buf, dsample_t* out )
{
	long pair_count = this->sample_buf_size >> 1;
	blip_time_t blip_time = Blip_count_clocks( &stereo_buf->bufs [0], pair_count );
	int sample_count = this->oversamples_per_frame - this->write_pos + resampler_extra;
	
	int new_count = this->callback( this->callback_data, blip_time, sample_count, &this->buf [this->write_pos] );
	assert( new_count < resampler_size );
	
	Buffer_end_frame( stereo_buf, blip_time );
	/* Blip_end_frame( &stereo_buf->bufs [0], blip_time ); */
	assert( Blip_samples_avail( &stereo_buf->bufs [0] ) == pair_count * 2 );
	
	this->write_pos += new_count;
	assert( (unsigned) this->write_pos <= this->buffer_size );
	
	new_count = this->sample_buf_size;
	if ( new_count )
		skip_input( this, resample_wrapper( this, this->sample_buf, &new_count, this->buf, this->write_pos ) );
	assert( new_count == (long) this->sample_buf_size );
	
	int bufs_used = stereo_buf->stereo_added | stereo_buf->was_stereo;
	if ( bufs_used <= 1 ) {
		mix_mono( this, stereo_buf, out );
		Blip_remove_samples( &stereo_buf->bufs [0], pair_count );
		Blip_remove_silence( &stereo_buf->bufs [1], pair_count );
		Blip_remove_silence( &stereo_buf->bufs [2], pair_count );
	}
	else if ( bufs_used & 1 ) {
		mix_stereo( this, stereo_buf, out );
		Blip_remove_samples( &stereo_buf->bufs [0], pair_count );
		Blip_remove_samples( &stereo_buf->bufs [1], pair_count );
		Blip_remove_samples( &stereo_buf->bufs [2], pair_count );
	}
	else {
		mix_stereo_no_center( this, stereo_buf, out );
		Blip_remove_silence( &stereo_buf->bufs [0], pair_count );
		Blip_remove_samples( &stereo_buf->bufs [1], pair_count );
		Blip_remove_samples( &stereo_buf->bufs [2], pair_count );
	}
	
	// to do: this might miss opportunities for optimization
	if ( !Blip_samples_avail( &stereo_buf->bufs [0] ) )
	{
		stereo_buf->was_stereo   = stereo_buf->stereo_added;
		stereo_buf->stereo_added = 0;
	}
	
	/* mix_mono( this, stereo_buf, out );
	Blip_remove_samples( &stereo_buf->bufs [0], pair_count ); */
}

void Resampler_play( struct Resampler* this, long count, dsample_t* out, struct Stereo_Buffer* stereo_buf )
{
	// empty extra buffer
	long remain = this->sample_buf_size - this->buf_pos;
	if ( remain )
	{
		if ( remain > count )
			remain = count;
		count -= remain;
		memcpy( out, &this->sample_buf [this->buf_pos], remain * sizeof *out );
		out += remain;
		this->buf_pos += remain;
	}
	
	// entire frames
	while ( count >= (long) this->sample_buf_size )
	{
		play_frame_( this, stereo_buf, out );
		out += this->sample_buf_size;
		count -= this->sample_buf_size;
	}
	
	// extra
	if ( count )
	{
		play_frame_( this, stereo_buf, this->sample_buf );
		this->buf_pos = count;
		memcpy( out, this->sample_buf, count * sizeof *out );
		out += count;
	}
}