summaryrefslogtreecommitdiff
path: root/lib/rbcodec/codecs/libgme/resampler.c
diff options
context:
space:
mode:
Diffstat (limited to 'lib/rbcodec/codecs/libgme/resampler.c')
-rw-r--r--lib/rbcodec/codecs/libgme/resampler.c218
1 files changed, 218 insertions, 0 deletions
diff --git a/lib/rbcodec/codecs/libgme/resampler.c b/lib/rbcodec/codecs/libgme/resampler.c
new file mode 100644
index 0000000000..c4f42a9794
--- /dev/null
+++ b/lib/rbcodec/codecs/libgme/resampler.c
@@ -0,0 +1,218 @@
1// Game_Music_Emu 0.5.5. http://www.slack.net/~ant/
2
3#include "resampler.h"
4
5#include <stdlib.h>
6#include <string.h>
7
8/* Copyright (C) 2003-2006 Shay Green. This module is free software; you
9can redistribute it and/or modify it under the terms of the GNU Lesser
10General Public License as published by the Free Software Foundation; either
11version 2.1 of the License, or (at your option) any later version. This
12module is distributed in the hope that it will be useful, but WITHOUT ANY
13WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
14FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
15details. You should have received a copy of the GNU Lesser General Public
16License along with this module; if not, write to the Free Software Foundation,
17Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA */
18
19#include "blargg_source.h"
20
21// TODO: fix this. hack since resampler holds back some output.
22unsigned const resampler_extra = 34;
23
24enum { shift = 14 };
25int const unit = 1 << shift;
26
27blargg_err_t Resampler_setup( struct Resampler* this, int fm_rate, int fm_gain, int rate, int gain )
28{
29 this->gain_ = (int)( ((1LL << gain_bits) * fm_gain * gain) / FP_ONE_GAIN );
30 this->step = (int)( ((1LL << shift) * fm_rate) / rate + 1);
31 this->rate_ = this->step;
32 return 0;
33}
34
35blargg_err_t Resampler_reset( struct Resampler* this, int pairs )
36{
37 // expand allocations a bit
38 this->sample_buffer_size = (pairs + (pairs >> 2)) * 2;
39 Resampler_resize( this, pairs );
40 this->resampler_size = this->oversamples_per_frame + (this->oversamples_per_frame >> 2);
41
42 this->buffer_size = this->resampler_size;
43 this->pos = 0;
44 this->write_pos = 0;
45
46 Resampler_clear( this );
47 return 0;
48}
49
50void Resampler_resize( struct Resampler* this, int pairs )
51{
52 int new_sample_buf_size = pairs * 2;
53 if ( this->sample_buf_size != new_sample_buf_size )
54 {
55 if ( new_sample_buf_size > this->sample_buffer_size )
56 {
57 check(false);
58 return;
59 }
60
61 this->sample_buf_size = new_sample_buf_size;
62 this->oversamples_per_frame = (int) ((pairs * this->rate_ * 2LL) / unit) + 2;
63 Resampler_clear( this );
64 }
65}
66
67static void mix_samples( struct Resampler* this, struct Blip_Buffer* blip_buf, dsample_t out_ [] )
68{
69 int const bass = BLIP_READER_BASS( *blip_buf );
70 BLIP_READER_BEGIN( sn, *blip_buf );
71
72 int count = this->sample_buf_size >> 1;
73 BLIP_READER_ADJ_( sn, count );
74
75 typedef dsample_t stereo_dsample_t [2];
76 stereo_dsample_t* BLARGG_RESTRICT out = (stereo_dsample_t*) out_ + count;
77 stereo_dsample_t const* BLARGG_RESTRICT in =
78 (stereo_dsample_t const*) this->sample_buf + count;
79 int offset = -count;
80 int const gain = this->gain_;
81 do
82 {
83 int s = BLIP_READER_READ_RAW( sn ) >> (blip_sample_bits - 16);
84 BLIP_READER_NEXT_IDX_( sn, bass, offset );
85
86 int l = (in [offset] [0] * gain >> gain_bits) + s;
87 int r = (in [offset] [1] * gain >> gain_bits) + s;
88
89 BLIP_CLAMP( l, l );
90 out [offset] [0] = (blip_sample_t) l;
91
92 BLIP_CLAMP( r, r );
93 out [offset] [1] = (blip_sample_t) r;
94 }
95 while ( ++offset );
96
97 BLIP_READER_END( sn, *blip_buf );
98}
99
100static dsample_t const* resample_( struct Resampler* this, dsample_t** out_,
101 dsample_t const* out_end, dsample_t const in [], int in_size )
102{
103 in_size -= write_offset;
104 if ( in_size > 0 )
105 {
106 dsample_t* BLARGG_RESTRICT out = *out_;
107 dsample_t const* const in_end = in + in_size;
108
109 int const step = this->step;
110 int pos = this->pos;
111
112 // TODO: IIR filter, then linear resample
113 // TODO: detect skipped sample, allowing merging of IIR and resample?
114
115 do
116 {
117 #define INTERP( i, out )\
118 out = (in [0 + i] * (unit - pos) + ((in [2 + i] + in [4 + i] + in [6 + i]) << shift) +\
119 in [8 + i] * pos) >> (shift + 2);
120
121 int out_0;
122 INTERP( 0, out_0 )
123 INTERP( 1, out [0] = out_0; out [1] )
124 out += stereo;
125
126 pos += step;
127 in += ((unsigned) pos >> shift) * stereo;
128 pos &= unit - 1;
129 }
130 while ( in < in_end && out < out_end );
131
132 this->pos = pos;
133 *out_ = out;
134 }
135 return in;
136}
137
138static inline int resample_wrapper( struct Resampler* this, dsample_t out [], int* out_size,
139 dsample_t const in [], int in_size )
140{
141 assert( Resampler_rate( this ) );
142
143 dsample_t* out_ = out;
144 int result = resample_( this, &out_, out + *out_size, in, in_size ) - in;
145 assert( out_ <= out + *out_size );
146 assert( result <= in_size );
147
148 *out_size = out_ - out;
149 return result;
150}
151
152static int skip_input( struct Resampler* this, int count )
153{
154 this->write_pos -= count;
155 if ( this->write_pos < 0 ) // occurs when downsampling
156 {
157 count += this->write_pos;
158 this->write_pos = 0;
159 }
160 memmove( this->buf, &this->buf [count], this->write_pos * sizeof this->buf [0] );
161 return count;
162}
163
164static void play_frame_( struct Resampler* this, struct Blip_Buffer* blip_buf, dsample_t* out )
165{
166 int pair_count = this->sample_buf_size >> 1;
167 blip_time_t blip_time = Blip_count_clocks( blip_buf, pair_count );
168 int sample_count = this->oversamples_per_frame - this->write_pos + resampler_extra;
169
170 int new_count = this->callback( this->callback_data, blip_time, sample_count, &this->buf [this->write_pos] );
171 assert( new_count < this->resampler_size );
172
173 Blip_end_frame( blip_buf, blip_time );
174 assert( Blip_samples_avail( blip_buf ) == pair_count );
175
176 this->write_pos += new_count;
177 assert( (unsigned) this->write_pos <= this->buffer_size );
178
179 int count = this->sample_buf_size;
180 if ( count )
181 skip_input( this, resample_wrapper( this, this->sample_buf, &count, this->buf, this->write_pos ) );
182 assert( count == this->sample_buf_size );
183
184 mix_samples( this, blip_buf, out );
185 Blip_remove_samples( blip_buf, pair_count );
186}
187
188void Resampler_play( struct Resampler* this, int count, dsample_t* out, struct Blip_Buffer* blip_buf )
189{
190 // empty extra buffer
191 int remain = this->sample_buf_size - this->buf_pos;
192 if ( remain )
193 {
194 if ( remain > count )
195 remain = count;
196 count -= remain;
197 memcpy( out, &this->sample_buf [this->buf_pos], remain * sizeof *out );
198 out += remain;
199 this->buf_pos += remain;
200 }
201
202 // entire frames
203 while ( count >= this->sample_buf_size )
204 {
205 play_frame_( this, blip_buf, out );
206 out += this->sample_buf_size;
207 count -= this->sample_buf_size;
208 }
209
210 // extra
211 if ( count )
212 {
213 play_frame_( this, blip_buf, this->sample_buf );
214 this->buf_pos = count;
215 memcpy( out, this->sample_buf, count * sizeof *out );
216 out += count;
217 }
218}