summaryrefslogtreecommitdiff
path: root/apps/codecs/libgme/multi_buffer.c
blob: 26cb8cdec69c3047d8a084c579a2c7f51c8bdf7b (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
// Blip_Buffer 0.4.1. http://www.slack.net/~ant/

#include "multi_buffer.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"

#ifdef BLARGG_ENABLE_OPTIMIZER
	#include BLARGG_ENABLE_OPTIMIZER
#endif

// Stereo_Buffer
 
void Buffer_init( struct Stereo_Buffer* this )
{
	Blip_init( &this->bufs [0] );
	Blip_init( &this->bufs [1] );
	Blip_init( &this->bufs [2] );
			
	this->chan.center = &this->bufs [0];
	this->chan.left = &this->bufs [1];
	this->chan.right = &this->bufs [2];
	
	this->length_ = 0;
	this->sample_rate_ = 0;
	this->channels_changed_count_ = 1;
	this->samples_per_frame_ = 2;
}

blargg_err_t Buffer_set_sample_rate( struct Stereo_Buffer* this, long rate, int msec )
{
	int i;
	for ( i = 0; i < buf_count; i++ )
		RETURN_ERR( Blip_set_sample_rate( &this->bufs[i], rate, msec ) );
		
	this->sample_rate_ = Blip_sample_rate( &this->bufs [0] );
	this->length_ = Blip_length( &this->bufs [0] );
	return 0;
}

void Buffer_clock_rate( struct Stereo_Buffer* this, long rate )
{
	int i;
	for ( i = 0; i < buf_count; i++ )
		Blip_set_clock_rate( &this->bufs [i], rate );
}

void Buffer_bass_freq( struct Stereo_Buffer* this, int bass )
{
	unsigned i;
	for ( i = 0; i < buf_count; i++ )
		Blip_bass_freq( &this->bufs [i], bass );
}

struct channel_t Buffer_channel( struct Stereo_Buffer* this )
{
	return this->chan;
}

void Buffer_clear( struct Stereo_Buffer* this )
{
	this->stereo_added = 0;
	this->was_stereo   = false;
	int i;
	for ( i = 0; i < buf_count; i++ )
		Blip_clear( &this->bufs [i], 1 );
}

void Buffer_end_frame( struct Stereo_Buffer* this, blip_time_t clock_count )
{
	this->stereo_added = 0;
	unsigned i;
	for ( i = 0; i < buf_count; i++ )
	{
		this->stereo_added |= Blip_clear_modified( &this->bufs [i] ) << i;
		Blip_end_frame( &this->bufs [i], clock_count );
	}
}

long Buffer_read_samples( struct Stereo_Buffer* this, blip_sample_t* out, long count )
{
	require( !(count & 1) ); // count must be even
	count = (unsigned) count / 2;
	
	long avail = Blip_samples_avail( &this->bufs [0] );
	if ( count > avail )
		count = avail;
	if ( count )
	{
		int bufs_used = this->stereo_added | this->was_stereo;
		//dprintf( "%X\n", bufs_used );
		if ( bufs_used <= 1 )
		{
			Buffer_mix_mono( this, out, count );
			Blip_remove_samples( &this->bufs [0], count );
			Blip_remove_silence( &this->bufs [1], count );
			Blip_remove_silence( &this->bufs [2], count );
		}
		else if ( bufs_used & 1 )
		{
			Buffer_mix_stereo( this, out, count );
			Blip_remove_samples( &this->bufs [0], count );
			Blip_remove_samples( &this->bufs [1], count );
			Blip_remove_samples( &this->bufs [2], count );
		}
		else
		{
			Buffer_mix_stereo_no_center( this, out, count );
			Blip_remove_silence( &this->bufs [0], count );
			Blip_remove_samples( &this->bufs [1], count );
			Blip_remove_samples( &this->bufs [2], count );
		}
		
		// to do: this might miss opportunities for optimization
		if ( !Blip_samples_avail( &this->bufs [0] ) )
		{
			this->was_stereo   = this->stereo_added;
			this->stereo_added = 0;
		}
	}
	
	return count * 2;
}

unsigned Buffer_channels_changed_count( struct Stereo_Buffer* this )
{
	return this->channels_changed_count_;
}

void Buffer_channels_changed( struct Stereo_Buffer* this )
{
	this->channels_changed_count_++;
}

void Buffer_mix_stereo( struct Stereo_Buffer* this, blip_sample_t* out_, blargg_long count )
{
	blip_sample_t* BLIP_RESTRICT out = out_;
	int const bass = BLIP_READER_BASS( this->bufs [1] );
	BLIP_READER_BEGIN( left, this->bufs [1] );
	BLIP_READER_BEGIN( right, this->bufs [2] );
	BLIP_READER_BEGIN( center, this->bufs [0] );
	
	for ( ; count; --count )
	{
		int c = BLIP_READER_READ( center );
		blargg_long l = c + BLIP_READER_READ( left );
		blargg_long r = c + BLIP_READER_READ( right );
		if ( (int16_t) l != l )
			l = 0x7FFF - (l >> 24);
		
		BLIP_READER_NEXT( center, bass );
		if ( (int16_t) r != r )
			r = 0x7FFF - (r >> 24);
		
		BLIP_READER_NEXT( left, bass );
		BLIP_READER_NEXT( right, bass );
		
		out [0] = l;
		out [1] = r;
		out += 2;
	}
	
	BLIP_READER_END( center, this->bufs [0] );
	BLIP_READER_END( right, this->bufs [2] );
	BLIP_READER_END( left, this->bufs [1] );
}

void Buffer_mix_stereo_no_center( struct Stereo_Buffer* this, blip_sample_t* out_, blargg_long count )
{
	blip_sample_t* BLIP_RESTRICT out = out_;
	int const bass = BLIP_READER_BASS( this->bufs [1] );
	BLIP_READER_BEGIN( left, this->bufs [1] );
	BLIP_READER_BEGIN( right, this->bufs [2] );
	
	for ( ; count; --count )
	{
		blargg_long l = BLIP_READER_READ( left );
		if ( (int16_t) l != l )
			l = 0x7FFF - (l >> 24);
		
		blargg_long r = BLIP_READER_READ( right );
		if ( (int16_t) r != r )
			r = 0x7FFF - (r >> 24);
		
		BLIP_READER_NEXT( left, bass );
		BLIP_READER_NEXT( right, bass );
		
		out [0] = l;
		out [1] = r;
		out += 2;
	}
	
	BLIP_READER_END( right, this->bufs [2] );
	BLIP_READER_END( left, this->bufs [1] );
}

void Buffer_mix_mono( struct Stereo_Buffer* this, blip_sample_t* out_, blargg_long count )
{
	blip_sample_t* BLIP_RESTRICT out = out_;
	int const bass = BLIP_READER_BASS( this->bufs [0] );
	BLIP_READER_BEGIN( center, this->bufs [0] );
	
	for ( ; count; --count )
	{
		blargg_long s = BLIP_READER_READ( center );
		if ( (int16_t) s != s )
			s = 0x7FFF - (s >> 24);
		
		BLIP_READER_NEXT( center, bass );
		out [0] = s;
		out [1] = s;
		out += 2;
	}
	
	BLIP_READER_END( center, this->bufs [0] );
}