[Scummvm-cvs-logs] CVS: scummvm/sound rate.cpp,1.1,1.2 rate.h,1.1,1.2 resample.cpp,1.1,1.2 audiostream.h,1.1,1.2

Max Horn fingolfin at users.sourceforge.net
Thu Jul 24 11:21:06 CEST 2003


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

Modified Files:
	rate.cpp rate.h resample.cpp audiostream.h 
Log Message:
added RateConverter classes as thin wrappers around st_* functions; renamed InputStream -> AudioInputStream and cleaned up the hierarchy of its subclasses; added makeInputStream() auxillary function

Index: rate.cpp
===================================================================
RCS file: /cvsroot/scummvm/scummvm/sound/rate.cpp,v
retrieving revision 1.1
retrieving revision 1.2
diff -u -d -r1.1 -r1.2
--- rate.cpp	24 Jul 2003 17:46:38 -0000	1.1
+++ rate.cpp	24 Jul 2003 18:20:12 -0000	1.2
@@ -104,7 +104,7 @@
  * Processed signed long samples from ibuf to obuf.
  * Return number of samples processed.
  */
-int st_rate_flow(eff_t effp, InputStream &input, st_sample_t *obuf, st_size_t *osamp)
+int st_rate_flow(eff_t effp, AudioInputStream &input, st_sample_t *obuf, st_size_t *osamp)
 {
 	rate_t rate = (rate_t) effp->priv;
 	st_sample_t *ostart, *oend;

Index: rate.h
===================================================================
RCS file: /cvsroot/scummvm/scummvm/sound/rate.h,v
retrieving revision 1.1
retrieving revision 1.2
diff -u -d -r1.1 -r1.2
--- rate.h	24 Jul 2003 17:46:38 -0000	1.1
+++ rate.h	24 Jul 2003 18:20:12 -0000	1.2
@@ -59,14 +59,62 @@
 // Resample (high quality)
 int st_resample_getopts(eff_t effp, int n, char **argv);
 int st_resample_start(eff_t effp, st_rate_t inrate, st_rate_t outrate);
-int st_resample_flow(eff_t effp, InputStream &input, st_sample_t *obuf, st_size_t *osamp);
+int st_resample_flow(eff_t effp, AudioInputStream &input, st_sample_t *obuf, st_size_t *osamp);
 int st_resample_drain(eff_t effp, st_sample_t *obuf, st_size_t *osamp);
 int st_resample_stop(eff_t effp);
 
 // Rate (linear filter, low quality)
 int st_rate_getopts(eff_t effp, int n, char **argv);
 int st_rate_start(eff_t effp, st_rate_t inrate, st_rate_t outrate);
-int st_rate_flow(eff_t effp, InputStream &input, st_sample_t *obuf, st_size_t *osamp);
+int st_rate_flow(eff_t effp, AudioInputStream &input, st_sample_t *obuf, st_size_t *osamp);
 int st_rate_stop(eff_t effp);
+
+#if 1
+class RateConverter {
+protected:
+	eff_struct effp;
+public:
+	RateConverter() {}
+	virtual ~RateConverter() {}
+	virtual int flow(AudioInputStream &input, st_sample_t *obuf, st_size_t *osamp) = 0;
+	virtual int drain(st_sample_t *obuf, st_size_t *osamp) = 0;
+};
+
+class LinearRateConverter : public RateConverter {
+public:
+	LinearRateConverter(st_rate_t inrate, st_rate_t outrate) {
+		st_rate_getopts(&effp, 0, NULL);
+		st_rate_start(&effp, inrate, outrate);
+	}
+	~LinearRateConverter() {
+		st_rate_stop(&effp);
+	}
+	virtual int flow(AudioInputStream &input, st_sample_t *obuf, st_size_t *osamp) {
+		return st_rate_flow(&effp, input, obuf, osamp);
+	}
+	virtual int drain(st_sample_t *obuf, st_size_t *osamp) {
+		return (ST_SUCCESS);
+	}
+};
+
+class ResampleRateConverter : public RateConverter {
+public:
+	ResampleRateConverter(st_rate_t inrate, st_rate_t outrate) {
+		st_resample_getopts(&effp, 0, NULL);
+		st_resample_start(&effp, inrate, outrate);
+	}
+	~ResampleRateConverter() {
+		st_resample_stop(&effp);
+	}
+	virtual int flow(AudioInputStream &input, st_sample_t *obuf, st_size_t *osamp) {
+		return st_resample_flow(&effp, input, obuf, osamp);
+	}
+	virtual int drain(st_sample_t *obuf, st_size_t *osamp) {
+		return st_resample_drain(&effp, obuf, osamp);
+	}
+};
+
+#endif
+
 
 #endif

Index: resample.cpp
===================================================================
RCS file: /cvsroot/scummvm/scummvm/sound/resample.cpp,v
retrieving revision 1.1
retrieving revision 1.2
diff -u -d -r1.1 -r1.2
--- resample.cpp	24 Jul 2003 17:46:38 -0000	1.1
+++ resample.cpp	24 Jul 2003 18:20:12 -0000	1.2
@@ -262,7 +262,7 @@
  * Processed signed long samples from ibuf to obuf.
  * Return number of samples processed.
  */
-int st_resample_flow(eff_t effp, InputStream &input, st_sample_t *obuf, st_size_t *osamp) {
+int st_resample_flow(eff_t effp, AudioInputStream &input, st_sample_t *obuf, st_size_t *osamp) {
 	resample_t r = (resample_t) effp->priv;
 	long i, k, last;
 	long Nout;		// The number of bytes we effectively output

Index: audiostream.h
===================================================================
RCS file: /cvsroot/scummvm/scummvm/sound/audiostream.h,v
retrieving revision 1.1
retrieving revision 1.2
diff -u -d -r1.1 -r1.2
--- audiostream.h	24 Jul 2003 17:46:38 -0000	1.1
+++ audiostream.h	24 Jul 2003 18:20:12 -0000	1.2
@@ -22,14 +22,19 @@
 #ifndef AUDIOSTREAM_H
 #define AUDIOSTREAM_H
 
+#include "mixer.h"
+
+// TODO:
+// * maybe make readIntern return 16.16 or 24.8 fixed point values
+//   since MAD (and maybe OggVorbis?) gives us those -> higher quality.
+//   The rate converters should be able to deal with those just fine, too.
+// * possibly add MADInputStream and VorbisInputStream
+
 /**
  * Generic input stream for the resampling code.
  */
-class InputStream {
-public:
-	byte *_ptr;
-	byte *_end;
-	InputStream(byte *ptr, uint len) : _ptr(ptr), _end(ptr+len) { }
+class AudioInputStream {
+protected:
 	virtual int16 readIntern() = 0;
 	virtual void advance() = 0;
 public:
@@ -39,53 +44,86 @@
 	bool eof() { return size() <= 0; }
 };
 
-class ZeroInputStream : public InputStream {
+class ZeroInputStream : public AudioInputStream {
 protected:
+	uint _len;
 	int16 readIntern() { return 0; }
-	void advance() { _ptr++; }
+	void advance() { _len--; }
 public:
-	ZeroInputStream(uint len) : InputStream(0, len) { }
-	virtual int size() { return _end - _ptr; }
+	ZeroInputStream(uint len) : _len(len) { }
+	virtual int size() { return _len; }
+};
+
+class MemoryAudioInputStream : public AudioInputStream {
+protected:
+	const byte *_ptr;
+	const byte *_end;
+public:
+	MemoryAudioInputStream(const byte *ptr, uint len) : _ptr(ptr), _end(ptr+len) { }
 };
 
+
 template<int channels>
-class Input8bitSignedStream : public InputStream {
+class Input8bitSignedStream : public MemoryAudioInputStream {
 protected:
 	int16 readIntern() { int8 v = (int8)*_ptr; return v << 8; }
 	void advance() { _ptr += channels; }
 public:
-	Input8bitSignedStream(byte *ptr, int len) : InputStream(ptr, len) { }
+	Input8bitSignedStream(const byte *ptr, int len) : MemoryAudioInputStream(ptr, len) { }
 	virtual int size() { return (_end - _ptr) / channels; }
 };
 
 template<int channels>
-class Input8bitUnsignedStream : public InputStream {
+class Input8bitUnsignedStream : public MemoryAudioInputStream {
 protected:
 	int16 readIntern() { int8 v = (int8)(*_ptr ^ 0x80); return v << 8; }
 	void advance() { _ptr += channels; }
 public:
-	Input8bitUnsignedStream(byte *ptr, int len) : InputStream(ptr, len) { }
+	Input8bitUnsignedStream(const byte *ptr, int len) : MemoryAudioInputStream(ptr, len) { }
 	virtual int size() { return (_end - _ptr) / channels; }
 };
 
 template<int channels>
-class Input16bitSignedStream : public InputStream {
+class Input16bitSignedStream : public MemoryAudioInputStream {
 protected:
 	int16 readIntern() { return (int16)READ_BE_UINT16(_ptr); }
 	void advance() { _ptr += 2*channels; }
 public:
-	Input16bitSignedStream(byte *ptr, int len) : InputStream(ptr, len) { }
+	Input16bitSignedStream(const byte *ptr, int len) : MemoryAudioInputStream(ptr, len) { }
 	virtual int size() { return (_end - _ptr) / (2 * channels); }
 };
 
 template<int channels>
-class Input16bitUnsignedStream : public InputStream {
+class Input16bitUnsignedStream : public MemoryAudioInputStream {
 protected:
 	int16 readIntern() { return (int16)(READ_BE_UINT16(_ptr) ^ 0x8000); }
 	void advance() { _ptr += 2*channels; }
 public:
-	Input16bitUnsignedStream(byte *ptr, int len) : InputStream(ptr, len) { }
+	Input16bitUnsignedStream(const byte *ptr, int len) : MemoryAudioInputStream(ptr, len) { }
 	virtual int size() { return (_end - _ptr) / (2 * channels); }
 };
+
+
+template<int channels>
+static AudioInputStream *makeInputStream(const byte *ptr, uint32 len, bool isUnsigned, bool is16Bit) {
+	if (isUnsigned) {
+		if (is16Bit)
+			return new Input16bitUnsignedStream<channels>(ptr, len);
+		else
+			return new Input8bitUnsignedStream<channels>(ptr, len);
+	} else {
+		if (is16Bit)
+			return new Input16bitSignedStream<channels>(ptr, len);
+		else
+			return new Input8bitSignedStream<channels>(ptr, len);
+	}
+}
+
+static inline AudioInputStream *makeInputStream(byte _flags, const byte *ptr, uint32 len) {
+	if (_flags & SoundMixer::FLAG_STEREO)
+		return makeInputStream<2>(ptr, len, _flags & SoundMixer::FLAG_UNSIGNED, _flags & SoundMixer::FLAG_16BITS);
+	else
+		return makeInputStream<1>(ptr, len, _flags & SoundMixer::FLAG_UNSIGNED, _flags & SoundMixer::FLAG_16BITS);
+}
 
 #endif





More information about the Scummvm-git-logs mailing list