mirror of https://github.com/OpenMW/openmw.git
Added SampleBuffer (experimental)
parent
6281685f73
commit
4ee198d66c
@ -0,0 +1,57 @@
|
||||
#ifndef MANGLE_SOUND_BUFFER_H
|
||||
#define MANGLE_SOUND_BUFFER_H
|
||||
|
||||
#include "source.h"
|
||||
#include "sources/memsource.h"
|
||||
#include <vector>
|
||||
#include <assert.h>
|
||||
|
||||
namespace Mangle {
|
||||
namespace Sound {
|
||||
|
||||
/** A sample buffer is a factory that creates SampleSources from one
|
||||
single sound source. It is helpful when you have many instances of
|
||||
one sound and want to use one shared memory buffer.
|
||||
|
||||
This is just a helper class - you don't have to include it in your
|
||||
program if you don't need it.
|
||||
*/
|
||||
class SampleBuffer
|
||||
{
|
||||
std::vector<uint8_t> buffer;
|
||||
|
||||
public:
|
||||
/// Reads the source into a memory buffer. Not heavily optimized.
|
||||
SampleBuffer(SampleSource *source)
|
||||
{
|
||||
size_t final = 0;
|
||||
|
||||
while(!source->eof())
|
||||
{
|
||||
const int add = 16*1024;
|
||||
|
||||
// Allocate more memory
|
||||
size_t newSize = final + add;
|
||||
buffer.resize(newSize);
|
||||
|
||||
// Fill in data
|
||||
size_t read = source->read(&buffer[final], add);
|
||||
|
||||
// If we couldn't read enough data, we should be at the end
|
||||
// of the stream
|
||||
assert(read == add || source->eof());
|
||||
|
||||
final += read;
|
||||
}
|
||||
|
||||
// Downsize the buffer to the actual length
|
||||
buffer.resize(final);
|
||||
}
|
||||
|
||||
/// Get a new source
|
||||
SampleSource *get()
|
||||
{ return new MemorySource(&buffer[0], buffer.size()); }
|
||||
};
|
||||
|
||||
}}
|
||||
#endif
|
@ -0,0 +1,49 @@
|
||||
#ifndef MANGLE_SOUND_MEMSOURCE_H
|
||||
#define MANGLE_SOUND_MEMSOURCE_H
|
||||
|
||||
#include "../source.h"
|
||||
|
||||
namespace Mangle {
|
||||
namespace Sound {
|
||||
|
||||
/// A sample source reading directly from a memory buffer
|
||||
class MemorySource : public SampleSource
|
||||
{
|
||||
char *buf;
|
||||
size_t len;
|
||||
size_t pos;
|
||||
|
||||
int32_t rate, channels, bits;
|
||||
|
||||
public:
|
||||
MemorySource(void *_buf, size_t _len, int32_t _rate, int32_t _channels, int32_t _bits)
|
||||
: len(_len), pos(0), rate(_rate), channels(_channels), bits(_bits)
|
||||
{ buf = (char*)_buf; }
|
||||
|
||||
/// Get the sample rate, number of channels, and bits per
|
||||
/// sample. NULL parameters are ignored.
|
||||
void getInfo(int32_t *_rate, int32_t *_channels, int32_t *_bits) const
|
||||
{
|
||||
if(_rate) *_rate = rate;
|
||||
if(_channels) *_channels = channels;
|
||||
if(_bits) *_bits = bits;
|
||||
}
|
||||
|
||||
bool eof() const { return pos == len; }
|
||||
|
||||
size_t read(void *out, size_t count)
|
||||
{
|
||||
assert(len >= pos);
|
||||
|
||||
if(count > (len-pos))
|
||||
count = len-pos;
|
||||
|
||||
if(count) memcpy(out, buf+pos, count);
|
||||
pos += count;
|
||||
|
||||
return count;
|
||||
}
|
||||
};
|
||||
|
||||
}} // namespaces
|
||||
#endif
|
Loading…
Reference in New Issue