[Scummvm-cvs-logs] CVS: scummvm/sound audiostream.cpp,1.18,1.19 audiostream.h,1.14,1.15 mixer.cpp,1.95,1.96

Max Horn fingolfin at users.sourceforge.net
Fri Aug 1 19:42:01 CEST 2003


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

Modified Files:
	audiostream.cpp audiostream.h mixer.cpp 
Log Message:
converted ChannelMP3 to use new rate conversion scheme, too. Only thing missing now is FLAG_REVERSE_STEREO support (and of course fixing regressions, and the long term goal is to make resample.cpp usable, too)

Index: audiostream.cpp
===================================================================
RCS file: /cvsroot/scummvm/scummvm/sound/audiostream.cpp,v
retrieving revision 1.18
retrieving revision 1.19
diff -u -d -r1.18 -r1.19
--- audiostream.cpp	1 Aug 2003 16:32:11 -0000	1.18
+++ audiostream.cpp	2 Aug 2003 02:41:30 -0000	1.19
@@ -161,15 +161,16 @@
 
 #ifdef USE_MAD
 
-#define MP3_BUFFER_SIZE 131072
-
 /**
  * Playback the MP3 data in the given file for the specified duration.
  *
  * @param file		file containing the MP3 data
- * @param duration	playback duration in frames (1/75th of a second), 0 means playback until EOF
+ * @param duration	playback duration in frames (1/75th of a second), 0 means
+ *					playback until EOF
+ * @param size		optional, if non-zero this limits playback based on the
+ * 					number of input bytes rather then a duration
  */
-MP3InputStream::MP3InputStream(File *file, mad_timer_t duration) {
+MP3InputStream::MP3InputStream(File *file, mad_timer_t duration, uint size) {
 	// duration == 0 means: play everything till end of file
 	
 	_isStereo = false;
@@ -177,6 +178,7 @@
 	_file = file;
 	_rate = 0;
 	_posInFrame = 0;
+	_bufferSize = size ? size : (128 * 1024);	// Default buffer size is 128K
 	
 	_duration = duration;
 
@@ -184,9 +186,15 @@
 	mad_frame_init(&_frame);
 	mad_synth_init(&_synth);
 	
-	_ptr = (byte *)malloc(MP3_BUFFER_SIZE + MAD_BUFFER_GUARD);
-	
+	_ptr = (byte *)malloc(_bufferSize + MAD_BUFFER_GUARD);
+
 	_initialized = init();
+
+	// If a size is specified, we do not perform any further read operations
+	if (size) {
+		_file = 0;
+	}
+	
 }
 
 MP3InputStream::~MP3InputStream() {
@@ -201,7 +209,7 @@
 	// TODO
 	
 	// Read in the first chunk of the MP3 file
-	_size = _file->read(_ptr, MP3_BUFFER_SIZE);
+	_size = _file->read(_ptr, _bufferSize);
 	if (_size <= 0) {
 		warning("MP3InputStream: Failed to read MP3 data");
 		return false;
@@ -244,19 +252,22 @@
 		if (_stream.error == MAD_ERROR_BUFLEN) {
 			int offset;
 
+			if (!_file)
+				_size = -1;
+		
 			// Give up immediately if we are at the EOF already
 			if (_size <= 0)
 				return;
 
 			if (!_stream.next_frame) {
 				offset = 0;
-				memset(_ptr, 0, MP3_BUFFER_SIZE + MAD_BUFFER_GUARD);
+				memset(_ptr, 0, _bufferSize + MAD_BUFFER_GUARD);
 			} else {
 				offset = _stream.bufend - _stream.next_frame;
 				memcpy(_ptr, _stream.next_frame, offset);
 			}
 			// Read in more data from the input file
-			_size = _file->read(_ptr + offset, MP3_BUFFER_SIZE - offset);
+			_size = _file->read(_ptr + offset, _bufferSize - offset);
 			
 			// Nothing read -> EOF -> bail out
 			if (_size <= 0) {
@@ -286,8 +297,10 @@
 }
 
 bool MP3InputStream::eof() const {
-	// Time over -> input steam ends
-	if (mad_timer_compare(_duration, mad_timer_zero) <= 0)
+	// Time over -> input steam ends. Unless _file is 0, which
+	// means that playback is based on the number of input bytes
+	// rather than a duration.
+	if (_file && mad_timer_compare(_duration, mad_timer_zero) <= 0)
 		return true;
 	return (_posInFrame >= _synth.pcm.length);
 }
@@ -307,8 +320,9 @@
 }
 
 int16 MP3InputStream::read() {
-	if (_size < 0 || _posInFrame >= _synth.pcm.length)	// EOF
+	if (_size < 0 || _posInFrame >= _synth.pcm.length) {	// EOF
 		return 0;
+	}
 
 	int16 sample;
 	if (_isStereo) {

Index: audiostream.h
===================================================================
RCS file: /cvsroot/scummvm/scummvm/sound/audiostream.h,v
retrieving revision 1.14
retrieving revision 1.15
diff -u -d -r1.14 -r1.15
--- audiostream.h	1 Aug 2003 16:32:11 -0000	1.14
+++ audiostream.h	2 Aug 2003 02:41:31 -0000	1.15
@@ -51,6 +51,8 @@
 	//virtual int size() const = 0;
 	virtual bool isStereo() const = 0;
 	virtual bool eof() const = 0;
+
+	virtual int getRate() const { return -1; }
 };
 
 class WrappedAudioInputStream : public AudioInputStream {
@@ -75,6 +77,7 @@
 	struct mad_frame _frame;
 	struct mad_synth _synth;
 	uint32 _posInFrame;
+	uint32 _bufferSize;
 	int _size;
 	bool _isStereo;
 	int _curChannel;
@@ -87,7 +90,7 @@
 	bool init();
 	void refill();
 public:
-	MP3InputStream(File *file, mad_timer_t duration);
+	MP3InputStream(File *file, mad_timer_t duration, uint size = 0);
 	~MP3InputStream();
 	int16 read();
 	bool eof() const;

Index: mixer.cpp
===================================================================
RCS file: /cvsroot/scummvm/scummvm/sound/mixer.cpp,v
retrieving revision 1.95
retrieving revision 1.96
diff -u -d -r1.95 -r1.96
--- mixer.cpp	2 Aug 2003 02:12:26 -0000	1.95
+++ mixer.cpp	2 Aug 2003 02:41:31 -0000	1.96
@@ -37,6 +37,10 @@
 protected:
 	SoundMixer *_mixer;
 	PlayingSoundHandle *_handle;
+#ifdef SOX_HACK
+	RateConverter *_converter;
+	AudioInputStream *_input;
+#endif
 public:
 	int _id;
 	Channel(SoundMixer *mixer, PlayingSoundHandle *handle)
@@ -44,6 +48,8 @@
 		assert(mixer);
 	}
 	virtual ~Channel() {
+		delete _converter;
+		delete _input;
 		if (_handle)
 			*_handle = 0;
 	}
@@ -65,10 +71,7 @@
 class ChannelRaw : public Channel {
 	byte *_ptr;
 	byte _flags;
-#ifdef SOX_HACK
-	RateConverter *_converter;
-	AudioInputStream *_input;
-#else
+#ifndef SOX_HACK
 	uint32 _pos;
 	uint32 _size;
 	uint32 _fpSpeed;
@@ -89,10 +92,7 @@
 };
 
 class ChannelStream : public Channel {
-#ifdef SOX_HACK
-	RateConverter *_converter;
-	WrappedAudioInputStream *_input;
-#else
+#ifndef SOX_HACK
 	byte *_ptr;
 	byte *_endOfData;
 	byte *_endOfBuffer;
@@ -134,8 +134,12 @@
 	~ChannelMP3Common();
 };
 
+#ifdef SOX_HACK
+class ChannelMP3 : public Channel {
+#else
 class ChannelMP3 : public ChannelMP3Common {
 	uint32 _position;
+#endif
 
 public:
 	ChannelMP3(SoundMixer *mixer, PlayingSoundHandle *handle, File *file, uint size);
@@ -146,8 +150,6 @@
 
 #ifdef SOX_HACK
 class ChannelMP3CDMusic : public Channel {
-	RateConverter *_converter;
-	MP3InputStream *_input;
 #else
 class ChannelMP3CDMusic : public ChannelMP3Common {
 	uint32 _bufferSize;
@@ -157,7 +159,6 @@
 
 public:
 	ChannelMP3CDMusic(SoundMixer *mixer, PlayingSoundHandle *handle, File *file, mad_timer_t duration);
-	~ChannelMP3CDMusic();
 
 	void mix(int16 *data, uint len);
 	bool isMusicChannel() { return true; }
@@ -167,10 +168,7 @@
 
 #ifdef USE_VORBIS
 class ChannelVorbis : public Channel {
-#ifdef SOX_HACK
-	RateConverter *_converter;
-	AudioInputStream *_input;
-#else
+#ifndef SOX_HACK
 	OggVorbis_File *_ov_file;
 	int _end_pos;
 #endif
@@ -178,7 +176,6 @@
 
 public:
 	ChannelVorbis(SoundMixer *mixer, PlayingSoundHandle *handle, OggVorbis_File *ov_file, int duration, bool is_cd_track);
-	~ChannelVorbis();
 
 	void mix(int16 *data, uint len);
 	bool isMusicChannel() {
@@ -702,10 +699,6 @@
 }
 
 ChannelRaw::~ChannelRaw() {
-#ifdef SOX_HACK
-	delete _converter;
-	delete _input;
-#endif
 	if (_flags & SoundMixer::FLAG_AUTOFREE)
 		free(_ptr);
 }
@@ -762,7 +755,7 @@
 	
 	// Create the input stream
 	_input = makeWrappedInputStream(flags, buffer_size);
-	_input->append((const byte *)sound, size);
+	((WrappedAudioInputStream *)_input)->append((const byte *)sound, size);
 	// TODO: add support for SoundMixer::FLAG_REVERSE_STEREO
 
 	// Get a rate converter instance
@@ -791,17 +784,14 @@
 }
 
 ChannelStream::~ChannelStream() {
-#ifdef SOX_HACK
-	delete _converter;
-	delete _input;
-#else
+#ifndef SOX_HACK
 	free(_ptr);
 #endif
 }
 
 void ChannelStream::append(void *data, uint32 len) {
 #ifdef SOX_HACK
-	_input->append((const byte *)data, len);
+	((WrappedAudioInputStream *)_input)->append((const byte *)data, len);
 #else
 	if (_endOfData + len > _endOfBuffer) {
 		/* Wrap-around case */
@@ -936,6 +926,17 @@
 	return sample >> (MAD_F_FRACBITS + 1 - 16);
 }
 
+#ifdef SOX_HACK
+ChannelMP3::ChannelMP3(SoundMixer *mixer, PlayingSoundHandle *handle, File *file, uint size)
+	: Channel(mixer, handle) {
+	// Create the input stream
+	_input = new MP3InputStream(file, mad_timer_zero, size);
+
+	// Get a rate converter instance
+//printf("ChannelMP3: inrate %d, outrate %d, stereo %d\n", _input->getRate(), mixer->getOutputRate(), _input->isStereo());
+	_converter = makeRateConverter(_input->getRate(), mixer->getOutputRate(), _input->isStereo());
+}
+#else
 ChannelMP3::ChannelMP3(SoundMixer *mixer, PlayingSoundHandle *handle, File *file, uint size)
 	: ChannelMP3Common(mixer, handle) {
 	_posInFrame = 0xFFFFFFFF;
@@ -944,8 +945,23 @@
 
 	_size = file->read(_ptr, size);
 }
+#endif
 
 void ChannelMP3::mix(int16 *data, uint len) {
+#ifdef SOX_HACK
+	assert(_input);
+	assert(_converter);
+
+	if (_input->eof()) {
+		// TODO: call drain method
+		destroy();
+		return;
+	}
+
+	const int volume = _mixer->getVolume();
+	st_size_t tmpLen = len;
+	_converter->flow(*_input, data, &tmpLen, volume);
+#else
 	const int volume = _mixer->getVolume();
 
 	// Exit if all data is used up (this also covers the case were reading from the file failed).
@@ -990,6 +1006,7 @@
 		_posInFrame = 0;
 		_position = _stream.next_frame - _ptr;
 	}
+#endif
 }
 
 #define MP3CD_BUFFERING_SIZE 131072
@@ -998,7 +1015,7 @@
 ChannelMP3CDMusic::ChannelMP3CDMusic(SoundMixer *mixer, PlayingSoundHandle *handle, File *file, mad_timer_t duration) 
 	: Channel(mixer, handle) {
 	// Create the input stream
-	_input = new MP3InputStream(file, duration);
+	_input = new MP3InputStream(file, duration, 0);
 
 	// Get a rate converter instance
 //printf("ChannelMP3CDMusic: inrate %d, outrate %d, stereo %d\n", _input->getRate(), mixer->getOutputRate(), _input->isStereo());
@@ -1014,13 +1031,6 @@
 }
 #endif
 
-ChannelMP3CDMusic::~ChannelMP3CDMusic() {
-#ifdef SOX_HACK
-	delete _converter;
-	delete _input;
-#endif
-}
-
 void ChannelMP3CDMusic::mix(int16 *data, uint len) {
 #ifdef SOX_HACK
 	assert(_input);
@@ -1169,13 +1179,6 @@
 		_end_pos = 0;
 #endif
 	_is_cd_track = is_cd_track;
-}
-
-ChannelVorbis::~ChannelVorbis() {
-#ifdef SOX_HACK
-	delete _converter;
-	delete _input;
-#endif
 }
 
 #ifdef CHUNKSIZE





More information about the Scummvm-git-logs mailing list