[Scummvm-cvs-logs] CVS: scummvm/sound mixer.cpp,1.80,1.81

Max Horn fingolfin at users.sourceforge.net
Mon Jul 28 19:22:01 CEST 2003


Update of /cvsroot/scummvm/scummvm/sound
In directory sc8-pr-cvs1:/tmp/cvs-serv25004

Modified Files:
	mixer.cpp 
Log Message:
some cleanup; moved around some stuff, preparing to unify more code of ChannelMP3CDMusic and ChannelMP3

Index: mixer.cpp
===================================================================
RCS file: /cvsroot/scummvm/scummvm/sound/mixer.cpp,v
retrieving revision 1.80
retrieving revision 1.81
diff -u -d -r1.80 -r1.81
--- mixer.cpp	29 Jul 2003 01:43:35 -0000	1.80
+++ mixer.cpp	29 Jul 2003 02:21:07 -0000	1.81
@@ -128,6 +128,7 @@
 	struct mad_synth _synth;
 	uint32 _posInFrame;
 	uint32 _size;
+	bool _initialized;
 
 public:
 	ChannelMP3Common(SoundMixer *mixer, PlayingSoundHandle *handle);
@@ -149,7 +150,6 @@
 	uint32 _bufferSize;
 	mad_timer_t _duration;
 	File *_file;
-	bool _initialized;
 
 public:
 	ChannelMP3CDMusic(SoundMixer *mixer, PlayingSoundHandle *handle, File *file, mad_timer_t duration);
@@ -920,6 +920,8 @@
 #endif
 	mad_frame_init(&_frame);
 	mad_synth_init(&_synth);
+
+	_initialized = false;
 }
 
 ChannelMP3Common::~ChannelMP3Common() {
@@ -930,6 +932,20 @@
 	mad_stream_finish(&_stream);
 }
 
+static inline int scale_sample(mad_fixed_t sample) {
+	/* round */
+	sample += (1L << (MAD_F_FRACBITS - 16));
+
+	/* clip */
+	if (sample > MAD_F_ONE - 1)
+		sample = MAD_F_ONE - 1;
+	else if (sample < -MAD_F_ONE)
+		sample = -MAD_F_ONE;
+
+	/* quantize and scale to not saturate when mixing a lot of channels */
+	return sample >> (MAD_F_FRACBITS + 1 - 16);
+}
+
 ChannelMP3::ChannelMP3(SoundMixer *mixer, PlayingSoundHandle *handle, void *sound, uint size, byte flags)
 	: ChannelMP3Common(mixer, handle) {
 	_posInFrame = 0xFFFFFFFF;
@@ -940,7 +956,7 @@
 
 	/* This variable is the number of samples to cut at the start of the MP3
 	   file. This is needed to have lip-sync as the MP3 file have some miliseconds
-	   of blank at the start (as, I suppose, the MP3 compression algorithm need to
+	   of blank at the start (as, I suppose, the MP3 compression algorithm needs to
 	   have some silence at the start to really be efficient and to not distort
 	   too much the start of the sample).
 
@@ -953,23 +969,14 @@
 	_silenceCut = 576 * 2;
 }
 
-static inline int scale_sample(mad_fixed_t sample) {
-	/* round */
-	sample += (1L << (MAD_F_FRACBITS - 16));
-
-	/* clip */
-	if (sample > MAD_F_ONE - 1)
-		sample = MAD_F_ONE - 1;
-	else if (sample < -MAD_F_ONE)
-		sample = -MAD_F_ONE;
-
-	/* quantize and scale to not saturate when mixing a lot of channels */
-	return sample >> (MAD_F_FRACBITS + 1 - 16);
-}
-
 void ChannelMP3::mix(int16 *data, uint len) {
 	const int volume = _mixer->getVolume();
 
+	if (!_initialized) {
+		// TODO: instead of using _silenceCut, skip first two frames like
+		// it is done in ChannelMP3CDMusic::mix()
+	}
+
 	while (1) {
 
 		/* Skip _silence_cut a the start */
@@ -1024,7 +1031,6 @@
 	: ChannelMP3Common(mixer, handle) {
 	_file = file;
 	_duration = duration;
-	_initialized = false;
 	_bufferSize = MP3CD_BUFFERING_SIZE;
 	_ptr = (byte *)malloc(MP3CD_BUFFERING_SIZE);
 	_releasePtr = true;
@@ -1032,10 +1038,9 @@
 
 void ChannelMP3CDMusic::mix(int16 *data, uint len) {
 	mad_timer_t frame_duration;
-	int volume = _mixer->getMusicVolume();
+	const int volume = _mixer->getMusicVolume();
 
 	if (!_initialized) {
-		int skip_loop;
 		// just skipped
 		memset(_ptr, 0, _bufferSize);
 		_size = _file->read(_ptr, _bufferSize);
@@ -1046,14 +1051,13 @@
 		}
 		// Resync
 		mad_stream_buffer(&_stream, _ptr, _size);
-		skip_loop = 2;
+		
+		// Skip the first two frames (see ChannelMP3::ChannelMP3 for an explanation)
+		int skip_loop = 2;
 		while (skip_loop != 0) {
 			if (mad_frame_decode(&_frame, &_stream) == 0) {
 				/* Do not decrease duration - see if it's a problem */
 				skip_loop--;
-				if (skip_loop == 0) {
-					mad_synth_frame(&_synth, &_frame);
-				}
 			} else {
 				if (!MAD_RECOVERABLE(_stream.error)) {
 					debug(1, "Unrecoverable error while skipping !");
@@ -1062,6 +1066,8 @@
 				}
 			}
 		}
+		mad_synth_frame(&_synth, &_frame);
+
 		// We are supposed to be in synch
 		mad_frame_mute(&_frame);
 		mad_synth_mute(&_synth);
@@ -1106,14 +1112,13 @@
 				int not_decoded;
 
 				if (!_stream.next_frame) {
-					memset(_ptr, 0, _bufferSize + MAD_BUFFER_GUARD);
-					_size = _file->read(_ptr, _bufferSize);
 					not_decoded = 0;
+					memset(_ptr, 0, _bufferSize + MAD_BUFFER_GUARD);
 				} else {
 					not_decoded = _stream.bufend - _stream.next_frame;
 					memcpy(_ptr, _stream.next_frame, not_decoded);
-					_size = _file->read(_ptr + not_decoded, _bufferSize - not_decoded);
 				}
+				_size = _file->read(_ptr + not_decoded, _bufferSize - not_decoded);
 				if (_size <= 0) {
 					return;
 				}





More information about the Scummvm-git-logs mailing list