[Scummvm-cvs-logs] CVS: scummvm/sound mixer.cpp,1.20,1.21 mixer.h,1.10,1.11

Jonathan Gray khalek at users.sourceforge.net
Sat Oct 26 18:13:03 CEST 2002


Update of /cvsroot/scummvm/scummvm/sound
In directory usw-pr-cvs1:/tmp/cvs-serv31536/sound

Modified Files:
	mixer.cpp mixer.h 
Log Message:
patch #628997 support for ogg vorbis instead of cd tracks by Daniel Schepler. Uncomment the relevant lines in the makefile to use

Index: mixer.cpp
===================================================================
RCS file: /cvsroot/scummvm/scummvm/sound/mixer.cpp,v
retrieving revision 1.20
retrieving revision 1.21
diff -u -d -r1.20 -r1.21
--- mixer.cpp	23 Oct 2002 06:48:10 -0000	1.20
+++ mixer.cpp	27 Oct 2002 01:12:10 -0000	1.21
@@ -120,7 +120,7 @@
 	_beginSlots = index;
 }
 
-#ifdef COMPRESSED_SOUND_FILE
+#ifdef USE_MAD
 int SoundMixer::playMP3(PlayingSoundHandle * handle, void *sound, uint32 size, byte flags) {
 	for (int i = _beginSlots; i != NUM_CHANNELS; i++) {
 		if (_channels[i] == NULL) {
@@ -144,6 +144,19 @@
 }
 #endif
 
+#ifdef USE_VORBIS
+int SoundMixer::playVorbisCDTrack(PlayingSoundHandle * handle, OggVorbis_File * ov_file, double duration) {
+	for (int i = _beginSlots; i != NUM_CHANNELS; i++) {
+		if (_channels[i] == NULL) {
+			return insertAt(handle, i, new ChannelVorbis(this, ov_file, duration));
+		}
+	}
+
+	warning("SoundMixer::out of mixer slots");
+	return -1;
+}
+#endif
+
 void SoundMixer::mix(int16 *buf, uint len) {
 	if (_paused) {
 		memset(buf, 0, 2 * len * sizeof(int16));
@@ -713,7 +726,7 @@
 	delete this;
 }
 
-#ifdef COMPRESSED_SOUND_FILE
+#ifdef USE_MAD
 SoundMixer::ChannelMP3::ChannelMP3(SoundMixer * mixer, void * sound, uint size, byte flags) {
 	_mixer = mixer;
 	_flags = flags;
@@ -959,6 +972,89 @@
 	mad_stream_finish(&_stream);
 
 	delete this;
+}
+
+#endif
+
+#ifdef USE_VORBIS
+SoundMixer::ChannelVorbis::ChannelVorbis(SoundMixer * mixer, OggVorbis_File * ov_file, double duration) {
+	_mixer = mixer;
+	_ov_file = ov_file;
+
+	if (duration)
+		_end_pos = ov_time_tell(ov_file) + duration;
+	else
+		_end_pos = 0;
+
+	_eof_flag = false;
+	_toBeDestroyed = false;
+}
+
+void SoundMixer::ChannelVorbis::mix(int16 * data, uint len) {
+	if (_toBeDestroyed) {
+		realDestroy();
+		return;
+	}
+
+	if (_eof_flag) {
+		memset(data, 0, sizeof(int16) * 2 * len);
+		return;
+	}
+
+	int channels = ov_info(_ov_file, -1)->channels;
+	uint len_left = len * channels * 2;
+	int16 *samples = new int16[len_left / 2];
+	char *read_pos = (char *) samples;
+	int volume = _mixer->_musicVolume;
+
+	// Read the samples
+	while (len_left > 0) {
+		long result = ov_read(_ov_file, read_pos, len_left,
+#ifdef SCUMM_BIG_ENDIAN
+				      1,
+#else
+				      0,
+#endif
+				      2, 1, NULL);
+		if (result == 0) {
+			_eof_flag = true;
+			memset(read_pos, 0, len_left);
+			break;
+		}
+		else if (result < 0) {
+			debug(1, "Decode error %d in Vorbis file", result);
+			// Don't delete it yet, that causes problems in
+			// the CD player emulation code.
+			_eof_flag = true;
+			memset(read_pos, 0, len_left);
+			break;
+		}
+		else {
+			len_left -= result;
+			read_pos += result;
+		}
+	}
+
+	// Mix the samples in
+	for (uint i = 0; i < len; i++) {
+		int16 sample = (int16) ((int32) samples[i * channels] * volume / 256);
+		*data++ += sample;
+		if (channels > 1)
+			sample = (int16) ((int32) samples[i * channels + 1] * volume / 256);
+		*data++ += sample;
+	}
+
+	delete [] samples;
+}
+
+void SoundMixer::ChannelVorbis::realDestroy() {
+	_mixer->unInsert(this);
+	delete this;
+}
+
+bool SoundMixer::ChannelVorbis::soundFinished() {
+	return _eof_flag || (_end_pos > 0 &&
+			     ov_time_tell(_ov_file) >= _end_pos);
 }
 
 #endif

Index: mixer.h
===================================================================
RCS file: /cvsroot/scummvm/scummvm/sound/mixer.h,v
retrieving revision 1.10
retrieving revision 1.11
diff -u -d -r1.10 -r1.11
--- mixer.h	16 Oct 2002 00:24:45 -0000	1.10
+++ mixer.h	27 Oct 2002 01:12:10 -0000	1.11
@@ -25,10 +25,14 @@
 
 #include <stdio.h>
 
-#ifdef COMPRESSED_SOUND_FILE
+#ifdef USE_MAD
 #include <mad.h>
 #endif
 
+#ifdef USE_VORBIS
+#include <vorbis/vorbisfile.h>
+#endif
+
 #include "common/scummsys.h"
 #include "common/system.h"
 
@@ -92,7 +96,7 @@
 		void realDestroy();
 	};
 
-#ifdef COMPRESSED_SOUND_FILE
+#ifdef USE_MAD
 
 	class ChannelMP3 : public Channel {
 		SoundMixer * _mixer;
@@ -138,6 +142,22 @@
 
 #endif
 
+#ifdef USE_VORBIS
+	class ChannelVorbis : public Channel {
+		SoundMixer * _mixer;
+		OggVorbis_File * _ov_file;
+		double _end_pos;
+		bool _eof_flag;
+
+	public:
+		ChannelVorbis(SoundMixer * mixer, OggVorbis_File * ov_file, double duration);
+
+		void mix(int16 * data, uint len);
+		void realDestroy();
+		bool soundFinished();
+	};
+#endif
+
 	static void onGenerateSamples(void * s, byte * samples, int len);
 
 public:
@@ -187,9 +207,12 @@
 	int playRaw(PlayingSoundHandle * handle, void * sound, uint32 size, uint rate, byte flags, int id);
 	int playStream(PlayingSoundHandle * handle, int index, void * sound, uint32 size, uint rate,
 									byte flags, int32 timeout = 3, int32 buffer_size = 2000000);
-#ifdef COMPRESSED_SOUND_FILE
+#ifdef USE_MAD
 	int playMP3(PlayingSoundHandle * handle, void * sound, uint32 size, byte flags);
 	int playMP3CDTrack(PlayingSoundHandle * handle, File * file, mad_timer_t duration);
+#endif
+#ifdef USE_VORBIS
+	int playVorbisCDTrack(PlayingSoundHandle * handle, OggVorbis_File * ov_file, double duration);
 #endif
 
 	/* Premix procedure, useful when using fmopl adlib */





More information about the Scummvm-git-logs mailing list