diff src/console/Multi_Buffer.cxx @ 316:fb513e10174e trunk

[svn] - merge libconsole-blargg into mainline libconsole: + obsoletes plugins-ugly:sapplug
author nenolod
date Thu, 30 Nov 2006 19:54:33 -0800
parents 3da1b8942b8b
children
line wrap: on
line diff
--- a/src/console/Multi_Buffer.cxx	Wed Nov 29 14:42:11 2006 -0800
+++ b/src/console/Multi_Buffer.cxx	Thu Nov 30 19:54:33 2006 -0800
@@ -1,5 +1,4 @@
-
-// Blip_Buffer 0.4.0. http://www.slack.net/~ant/
+// Blip_Buffer 0.4.1. http://www.slack.net/~ant/
 
 #include "Multi_Buffer.h"
 
@@ -9,12 +8,16 @@
 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 */
+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_BEGIN
+#include "blargg_source.h"
+
+#ifdef BLARGG_ENABLE_OPTIMIZER
+	#include BLARGG_ENABLE_OPTIMIZER
+#endif
 
 Multi_Buffer::Multi_Buffer( int spf ) : samples_per_frame_( spf )
 {
@@ -23,48 +26,33 @@
 	channels_changed_count_ = 1;
 }
 
-blargg_err_t Multi_Buffer::set_channel_count( int )
-{
-	return blargg_success;
-}
-
-Mono_Buffer::Mono_Buffer() : Multi_Buffer( 1 )
-{
-}
-
-Mono_Buffer::~Mono_Buffer()
-{
-}
-
-blargg_err_t Mono_Buffer::set_sample_rate( long rate, int msec )
-{
-	BLARGG_RETURN_ERR( buf.set_sample_rate( rate, msec ) );
-	return Multi_Buffer::set_sample_rate( buf.sample_rate(), buf.length() );
-}
+blargg_err_t Multi_Buffer::set_channel_count( int ) { return 0; }
 
 // Silent_Buffer
 
 Silent_Buffer::Silent_Buffer() : Multi_Buffer( 1 ) // 0 channels would probably confuse
 {
-	chan.left   = NULL;
-	chan.center = NULL;
-	chan.right  = NULL;
+	// TODO: better to use empty Blip_Buffer so caller never has to check for NULL?
+	chan.left   = 0;
+	chan.center = 0;
+	chan.right  = 0;
 }
 
 // Mono_Buffer
 
-Mono_Buffer::channel_t Mono_Buffer::channel( int )
+Mono_Buffer::Mono_Buffer() : Multi_Buffer( 1 )
 {
-	channel_t ch;
-	ch.center = &buf;
-	ch.left   = &buf;
-	ch.right  = &buf;
-	return ch;
+	chan.center = &buf;
+	chan.left   = &buf;
+	chan.right  = &buf;
 }
 
-void Mono_Buffer::end_frame( blip_time_t t, bool )
+Mono_Buffer::~Mono_Buffer() { }
+
+blargg_err_t Mono_Buffer::set_sample_rate( long rate, int msec )
 {
-	buf.end_frame( t );
+	RETURN_ERR( buf.set_sample_rate( rate, msec ) );
+	return Multi_Buffer::set_sample_rate( buf.sample_rate(), buf.length() );
 }
 
 // Stereo_Buffer
@@ -76,14 +64,12 @@
 	chan.right = &bufs [2];
 }
 
-Stereo_Buffer::~Stereo_Buffer()
-{
-}
+Stereo_Buffer::~Stereo_Buffer() { }
 
 blargg_err_t Stereo_Buffer::set_sample_rate( long rate, int msec )
 {
 	for ( int i = 0; i < buf_count; i++ )
-		BLARGG_RETURN_ERR( bufs [i].set_sample_rate( rate, msec ) );
+		RETURN_ERR( bufs [i].set_sample_rate( rate, msec ) );
 	return Multi_Buffer::set_sample_rate( bufs [0].sample_rate(), bufs [0].length() );
 }
 
@@ -101,18 +87,20 @@
 
 void Stereo_Buffer::clear()
 {
-	stereo_added = false;
-	was_stereo = false;
+	stereo_added = 0;
+	was_stereo   = false;
 	for ( int i = 0; i < buf_count; i++ )
 		bufs [i].clear();
 }
 
-void Stereo_Buffer::end_frame( blip_time_t clock_count, bool stereo )
+void Stereo_Buffer::end_frame( blip_time_t clock_count )
 {
+	stereo_added = 0;
 	for ( unsigned i = 0; i < buf_count; i++ )
+	{
+		stereo_added |= bufs [i].clear_modified() << i;
 		bufs [i].end_frame( clock_count );
-	
-	stereo_added |= stereo;
+	}
 }
 
 long Stereo_Buffer::read_samples( blip_sample_t* out, long count )
@@ -125,91 +113,120 @@
 		count = avail;
 	if ( count )
 	{
-		if ( stereo_added || was_stereo )
+		int bufs_used = stereo_added | was_stereo;
+		//dprintf( "%X\n", bufs_used );
+		if ( bufs_used <= 1 )
+		{
+			mix_mono( out, count );
+			bufs [0].remove_samples( count );
+			bufs [1].remove_silence( count );
+			bufs [2].remove_silence( count );
+		}
+		else if ( bufs_used & 1 )
 		{
 			mix_stereo( out, count );
-			
 			bufs [0].remove_samples( count );
 			bufs [1].remove_samples( count );
 			bufs [2].remove_samples( count );
 		}
 		else
 		{
-			mix_mono( out, count );
-			
-			bufs [0].remove_samples( count );
-			
-			bufs [1].remove_silence( count );
-			bufs [2].remove_silence( count );
+			mix_stereo_no_center( out, count );
+			bufs [0].remove_silence( count );
+			bufs [1].remove_samples( count );
+			bufs [2].remove_samples( count );
 		}
 		
 		// to do: this might miss opportunities for optimization
-		if ( !bufs [0].samples_avail() ) {
-			was_stereo = stereo_added;
-			stereo_added = false;
+		if ( !bufs [0].samples_avail() )
+		{
+			was_stereo   = stereo_added;
+			stereo_added = 0;
 		}
 	}
 	
 	return count * 2;
 }
 
-#include BLARGG_ENABLE_OPTIMIZER
-
-void Stereo_Buffer::mix_stereo( blip_sample_t* out, long count )
+void Stereo_Buffer::mix_stereo( blip_sample_t* out_, blargg_long count )
 {
-	Blip_Reader left; 
-	Blip_Reader right; 
-	Blip_Reader center;
+	blip_sample_t* BLIP_RESTRICT out = out_;
+	int const bass = BLIP_READER_BASS( bufs [1] );
+	BLIP_READER_BEGIN( left, bufs [1] );
+	BLIP_READER_BEGIN( right, bufs [2] );
+	BLIP_READER_BEGIN( center, bufs [0] );
 	
-	left.begin( bufs [1] );
-	right.begin( bufs [2] );
-	int bass = center.begin( bufs [0] );
-	
-	while ( count-- )
+	for ( ; count; --count )
 	{
-		int c = center.read();
-		long l = c + left.read();
-		long r = c + right.read();
-		center.next( bass );
+		int c = BLIP_READER_READ( center );
+		blargg_long l = c + BLIP_READER_READ( left );
+		blargg_long r = c + BLIP_READER_READ( right );
+		if ( (BOOST::int16_t) l != l )
+			l = 0x7FFF - (l >> 24);
+		
+		BLIP_READER_NEXT( center, bass );
+		if ( (BOOST::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;
-		
-		if ( (BOOST::int16_t) l != l )
-			out [-2] = 0x7FFF - (l >> 24);
-		
-		left.next( bass );
-		right.next( bass );
-		
-		if ( (BOOST::int16_t) r != r )
-			out [-1] = 0x7FFF - (r >> 24);
 	}
 	
-	center.end( bufs [0] );
-	right.end( bufs [2] );
-	left.end( bufs [1] );
+	BLIP_READER_END( center, bufs [0] );
+	BLIP_READER_END( right, bufs [2] );
+	BLIP_READER_END( left, bufs [1] );
 }
 
-void Stereo_Buffer::mix_mono( blip_sample_t* out, long count )
+void Stereo_Buffer::mix_stereo_no_center( blip_sample_t* out_, blargg_long count )
 {
-	Blip_Reader in;
-	int bass = in.begin( bufs [0] );
+	blip_sample_t* BLIP_RESTRICT out = out_;
+	int const bass = BLIP_READER_BASS( bufs [1] );
+	BLIP_READER_BEGIN( left, bufs [1] );
+	BLIP_READER_BEGIN( right, bufs [2] );
 	
-	while ( count-- )
+	for ( ; count; --count )
 	{
-		long s = in.read();
-		in.next( bass );
+		blargg_long l = BLIP_READER_READ( left );
+		if ( (BOOST::int16_t) l != l )
+			l = 0x7FFF - (l >> 24);
+		
+		blargg_long r = BLIP_READER_READ( right );
+		if ( (BOOST::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, bufs [2] );
+	BLIP_READER_END( left, bufs [1] );
+}
+
+void Stereo_Buffer::mix_mono( blip_sample_t* out_, blargg_long count )
+{
+	blip_sample_t* BLIP_RESTRICT out = out_;
+	int const bass = BLIP_READER_BASS( bufs [0] );
+	BLIP_READER_BEGIN( center, bufs [0] );
+	
+	for ( ; count; --count )
+	{
+		blargg_long s = BLIP_READER_READ( center );
+		if ( (BOOST::int16_t) s != s )
+			s = 0x7FFF - (s >> 24);
+		
+		BLIP_READER_NEXT( center, bass );
 		out [0] = s;
 		out [1] = s;
 		out += 2;
-		
-		if ( (BOOST::int16_t) s != s ) {
-			s = 0x7FFF - (s >> 24);
-			out [-2] = s;
-			out [-1] = s;
-		}
 	}
 	
-	in.end( bufs [0] );
+	BLIP_READER_END( center, bufs [0] );
 }
-