summaryrefslogtreecommitdiff
path: root/plugins/gme/game-music-emu-0.6pre/gme/Resampler.h
blob: c8f96f72eb0afd1f57b3cd979699e432c3d241d1 (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
// Common interface for resamplers

// Game_Music_Emu 0.6-pre
#ifndef RESAMPLER_H
#define RESAMPLER_H

#include "blargg_common.h"

class Resampler {
public:
	
	virtual ~Resampler();
	
	// Sets input/output resampling ratio
	blargg_err_t set_rate( double );
	
	// Current input/output ratio
	double rate() const             { return rate_; }
	
	// Samples are 16-bit signed
	typedef short sample_t;

// One of two different buffering schemes can be used, as decided by the caller:

// External buffering (caller provides input buffer)
	
	// Resamples in to at most n out samples and returns number of samples actually
	// written. Sets *in_size to number of input samples that aren't needed anymore
	// and should be removed from input.
	int resample( sample_t out [], int n, sample_t const in [], int* in_size );

// Internal buffering (resampler manages buffer)
	
	// Resizes input buffer to n samples, then clears it
	blargg_err_t resize_buffer( int n );
	
	// Clears input buffer
	void clear();
	
	// Writes at most n samples to input buffer and returns number actually written.
	// Result will be less than n if there isn't enough free space in buffer.
	int write( sample_t const in [], int n );
	
	// Number of input samples in buffer
	int written() const             { return write_pos; }
	
	// Removes first n input samples from buffer, fewer if there aren't that many.
	// Returns number of samples actually removed.
	int skip_input( int n );
	
	// Resamples input to at most n output samples. Returns number of samples
	// actually written to out. Result will be less than n if there aren't
	// enough input samples in buffer.
	int read( sample_t out [], int n );

// Direct writing to input buffer, instead of using write( in, n ) above

	// Pointer to place to write input samples
	sample_t* buffer()              { return &buf [write_pos]; }
	
	// Number of samples that can be written to buffer()
	int buffer_free() const         { return buf.size() - write_pos; }
	
	// Notifies resampler that n input samples have been written to buffer().
	// N must not be greater than buffer_free().
	void write( int n );

// Derived interface
protected:
	virtual blargg_err_t set_rate_( double rate ) BLARGG_PURE( ; )
	
	virtual void clear_() { }
	
	// Resample as many available in samples as will fit within out_size and
	// return pointer past last input sample read and set *out just past
	// the last output sample.
	virtual sample_t const* resample_( sample_t** out, sample_t const* out_end,
			sample_t const in [], int in_size ) BLARGG_PURE( { return in; } )

// Implementation
public:
	Resampler();

private:
	blargg_vector<sample_t> buf;
	int write_pos;
	double rate_;
	
	int resample_wrapper( sample_t out [], int* out_size,
			sample_t const in [], int in_size );
};

inline void Resampler::write( int count )
{
	write_pos += count;
	assert( (unsigned) write_pos <= buf.size() );
}

inline blargg_err_t Resampler::set_rate_( double r )
{
	rate_ = r;
	return blargg_ok;
}

inline blargg_err_t Resampler::set_rate( double r )
{
	return set_rate_( r );
}

#endif