1 // $package. http://www.slack.net/~ant/
2 
3 #include "Resampler.h"
4 
5 /* Copyright (C) 2004-2008 Shay Green. This module is free software; you
6 can redistribute it and/or modify it under the terms of the GNU Lesser
7 General Public License as published by the Free Software Foundation; either
8 version 2.1 of the License, or (at your option) any later version. This
9 module is distributed in the hope that it will be useful, but WITHOUT ANY
10 WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
11 FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
12 details. You should have received a copy of the GNU Lesser General Public
13 License along with this module; if not, write to the Free Software Foundation,
14 Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA */
15 
16 #include "blargg_source.h"
17 
Resampler()18 Resampler::Resampler()
19 {
20 	write_pos = 0;
21 	rate_     = 0;
22 }
23 
~Resampler()24 Resampler::~Resampler() { }
25 
clear()26 void Resampler::clear()
27 {
28 	write_pos = 0;
29 	clear_();
30 }
31 
resample_wrapper(sample_t out[],int * out_size,sample_t const in[],int in_size)32 inline int Resampler::resample_wrapper( sample_t out [], int* out_size,
33 		sample_t const in [], int in_size )
34 {
35 	assert( rate() );
36 
37 	sample_t* out_ = out;
38 	int result = resample_( &out_, out + *out_size, in, in_size ) - in;
39 	assert( out_ <= out + *out_size );
40 	assert( result <= in_size );
41 
42 	*out_size = out_ - out;
43 	return result;
44 }
45 
resample(sample_t out[],int out_size,sample_t const in[],int * in_size)46 int Resampler::resample( sample_t out [], int out_size, sample_t const in [], int* in_size )
47 {
48 	*in_size = resample_wrapper( out, &out_size, in, *in_size );
49 	return out_size;
50 }
51 
52 
53 //// Buffering
54 
resize_buffer(int new_size)55 blargg_err_t Resampler::resize_buffer( int new_size )
56 {
57 	RETURN_ERR( buf.resize( new_size ) );
58 	clear();
59 	return blargg_ok;
60 }
61 
skip_input(int count)62 int Resampler::skip_input( int count )
63 {
64 	write_pos -= count;
65 	if ( write_pos < 0 ) // occurs when downsampling
66 	{
67 		count += write_pos;
68 		write_pos = 0;
69 	}
70 	memmove( buf.begin(), &buf [count], write_pos * sizeof buf [0] );
71 	return count;
72 }
73 
read(sample_t out[],int out_size)74 int Resampler::read( sample_t out [], int out_size )
75 {
76 	if ( out_size )
77 		skip_input( resample_wrapper( out, &out_size, buf.begin(), write_pos ) );
78 	return out_size;
79 }
80