1
0
Fork 0
mirror of https://github.com/OpenMW/openmw.git synced 2025-01-21 06:23:53 +00:00

Nearly finished shared_ptr, and getPtr(). Tests not rewritten yet.

This commit is contained in:
Nicolay Korslund 2010-01-01 14:34:46 +01:00
parent 38501777b0
commit eaf93691d5
8 changed files with 47 additions and 49 deletions

View file

@ -108,10 +108,6 @@ void OpenAL_Sound::setRepeat(bool rep)
alSourcei(Source, AL_LOOPING, rep?AL_TRUE:AL_FALSE); alSourcei(Source, AL_LOOPING, rep?AL_TRUE:AL_FALSE);
} }
void OpenAL_Sound::setup()
{
}
// Constructor used for cloned sounds // Constructor used for cloned sounds
OpenAL_Sound::OpenAL_Sound(ALuint buf, int *ref) OpenAL_Sound::OpenAL_Sound(ALuint buf, int *ref)
: refCnt(ref), bufferID(buf) : refCnt(ref), bufferID(buf)
@ -125,19 +121,31 @@ OpenAL_Sound::OpenAL_Sound(ALuint buf, int *ref)
alSourcei(inst, AL_BUFFER, bufferID); alSourcei(inst, AL_BUFFER, bufferID);
} }
OpenAL_Sound::OpenAL_Sound(SampleSource *input) OpenAL_Sound::OpenAL_Sound(SampleSourcePtr input)
{ {
// Get the format // Get the format
int fmt, rate; int fmt, rate;
getALFormat(inp, fmt, rate); getALFormat(inp, fmt, rate);
// Read the entire stream into a buffer // Set up the OpenAL buffer
BufferStream buf(input);
// Move the data into OpenAL
alGenBuffers(1, &bufferID); alGenBuffers(1, &bufferID);
assert(bufferID != 0); assert(bufferID != 0);
// Does the stream support pointer operations?
if(input->hasPtr)
{
// If so, we can read the data directly from the stream
alBufferData(bufferID, fmt, &input.getPtr(), input.size(), rate);
}
else
{
// Read the entire stream into a temporary buffer first
BufferStream buf(input);
// Then copy that into OpenAL
alBufferData(bufferID, fmt, &buf.getPtr(), buf.size(), rate); alBufferData(bufferID, fmt, &buf.getPtr(), buf.size(), rate);
}
checkALError("loading sound buffer"); checkALError("loading sound buffer");
// Create a source // Create a source

View file

@ -22,7 +22,7 @@ class OpenAL_Sound : public Sound
int *refCnt; int *refCnt;
public: public:
OpenAL_Sound(SampleSource *input); OpenAL_Sound(SampleSourcePtr input);
OpenAL_Sound(ALuint buf, int *ref); // Used for cloning OpenAL_Sound(ALuint buf, int *ref); // Used for cloning
~OpenAL_Sound(); ~OpenAL_Sound();
@ -83,10 +83,10 @@ class OpenAL_Factory : public SoundFactory
} }
} }
Sound *load(const std::string &file, bool stream=false) { assert(0); } SoundPtr load(const std::string &file, bool stream=false) { assert(0); }
Sound *load(Stream::Stream *input, bool stream=false) { assert(0); } SoundPtr load(Stream::StreamPtr input, bool stream=false) { assert(0); }
Sound *load(SampleSource* input, bool stream=false) SoundPtr load(SampleSourcePtr input, bool stream=false)
{ return new OpenAL_Sound(input); } { return SoundPtr(new OpenAL_Sound(input)); }
void update() {} void update() {}
setListenerPos(float x, float y, float z, setListenerPos(float x, float y, float z,

View file

@ -17,15 +17,7 @@ class SampleSource : public Stream::Stream
bool isEof; bool isEof;
public: public:
SampleSource() SampleSource() : isEof(false) {}
{
// These are usually not needed for sound data
isSeekable = false;
hasPosition = false;
hasSize = false;
isEof = false;
}
/// Get the sample rate, number of channels, and bits per /// Get the sample rate, number of channels, and bits per
/// sample. NULL parameters are ignored. /// sample. NULL parameters are ignored.

View file

@ -109,7 +109,7 @@ AudiereSource::AudiereSource(const std::string &file)
setup(); setup();
} }
AudiereSource::AudiereSource(Stream::Stream *input) AudiereSource::AudiereSource(Stream::StreamPtr input)
{ {
// Use our Stream::AudiereFile implementation to convert a Mangle // Use our Stream::AudiereFile implementation to convert a Mangle
// 'Stream' to an Audiere 'File' // 'Stream' to an Audiere 'File'

View file

@ -33,7 +33,7 @@ class AudiereSource : public SampleSource
AudiereSource(const std::string &file); AudiereSource(const std::string &file);
/// Decode the given sound stream /// Decode the given sound stream
AudiereSource(Stream::Stream *src); AudiereSource(Stream::StreamPtr src);
/// Read directly from an existing audiere::SampleSource /// Read directly from an existing audiere::SampleSource
AudiereSource(audiere::SampleSourcePtr src); AudiereSource(audiere::SampleSourcePtr src);

View file

@ -28,7 +28,7 @@ class FFMpegSource : public SampleSource
FFMpegSource(const std::string &file); FFMpegSource(const std::string &file);
/// Decode the given sound stream (not supported by FFmpeg) /// Decode the given sound stream (not supported by FFmpeg)
FFMpegSource(Stream::Stream *src) { assert(0); } FFMpegSource(Stream::StreamPtr src) { assert(0); }
~FFMpegSource(); ~FFMpegSource();

View file

@ -16,7 +16,7 @@ class SSL_Template : public SampleSourceLoader
return new SourceT(file); return new SourceT(file);
} }
SampleSource *load(Stream::Stream *input) SampleSource *load(Stream::StreamPtr input)
{ {
assert(canLoadStream); assert(canLoadStream);
return new SourceT(input); return new SourceT(input);

View file

@ -6,19 +6,21 @@
namespace Mangle { namespace Mangle {
namespace Sound { namespace Sound {
/// A sample source reading directly from a memory buffer /// A class for reading raw samples directly from a stream.
class MemorySource : public SampleSource class Stream2Samples : public SampleSource
{ {
char *buf;
size_t len;
size_t pos;
int32_t rate, channels, bits; int32_t rate, channels, bits;
Stream::StreamPtr inp;
public: public:
MemorySource(void *_buf, size_t _len, int32_t _rate, int32_t _channels, int32_t _bits) Stream2Samples(Stream::StreamPtr _inp, int32_t _rate, int32_t _channels, int32_t _bits)
: len(_len), pos(0), rate(_rate), channels(_channels), bits(_bits) : inp(_inp), rate(_rate), channels(_channels), bits(_bits)
{ buf = (char*)_buf; } {
isSeekable = inp->isSeekable;
hasPosition = inp->hasPosition;
hasSize = inp->hasSize;
hasPtr = inp->hasPtr;
}
/// Get the sample rate, number of channels, and bits per /// Get the sample rate, number of channels, and bits per
/// sample. NULL parameters are ignored. /// sample. NULL parameters are ignored.
@ -29,20 +31,16 @@ class MemorySource : public SampleSource
if(_bits) *_bits = bits; if(_bits) *_bits = bits;
} }
bool eof() const { return pos == len; }
size_t read(void *out, size_t count) size_t read(void *out, size_t count)
{ { return inp->read(out, count); }
assert(len >= pos);
if(count > (len-pos)) void seek(size_t pos) { inp->seek(pos); }
count = len-pos; size_t tell() const { return inp->tell(); }
size_t size() const { return inp->size(); }
if(count) memcpy(out, buf+pos, count); bool eof() const { return inp->eof(); }
pos += count; void *getPtr() const { return inp->getPtr(); }
void *getPtr(size_t size) const { return inp->getPtr(size); }
return count; void *getPtr(size_t pos, size_t size) const { return inp->getPtr(pos, size); }
}
}; };
}} // namespaces }} // namespaces