| 1 |
#pragma once |
| 2 |
|
| 3 |
#include "stream/ScopedArchiveBuffer.h" |
| 4 |
|
| 5 |
/** greebo: A wrapper class for use with the ov_open_callbacks() method |
| 6 |
* in vorbsfile.h. This provides the four callback |
| 7 |
* functions required by the OGG libs, simulating a file stream. |
| 8 |
* |
| 9 |
* Parts of this code has been written after |
| 10 |
* http://www.devmaster.net/articles/openal-ogg-file/ |
| 11 |
*/ |
| 12 |
namespace sound { |
| 13 |
|
| 14 |
class OggFileStream |
| 15 |
{ |
| 16 |
archive::ScopedArchiveBuffer _source; |
| 17 |
|
| 18 |
unsigned char* _curPtr; |
| 19 |
public: |
| 20 |
OggFileStream(ArchiveFile& file) : |
| 21 |
_source(file) |
| 22 |
{ |
| 23 |
// Set the pointer to the beginning of the buffer |
| 24 |
_curPtr = _source.buffer; |
| 25 |
} |
| 26 |
|
| 27 |
static std::size_t oggReadFunc(void* ptr, std::size_t byteSize, |
| 28 |
std::size_t sizeToRead, void* datasource) |
| 29 |
{ |
| 30 |
OggFileStream* self = reinterpret_cast<OggFileStream*>(datasource); |
| 31 |
|
| 32 |
// Calculate how much we need to read. |
| 33 |
// This can be sizeToRead*byteSize or less depending on how near the EOF marker we are |
| 34 |
std::size_t bytesRequested = sizeToRead*byteSize; |
| 35 |
std::size_t bytesLeft = self->_source.buffer + self->_source.length - self->_curPtr; |
| 36 |
|
| 37 |
// Clamp the number of bytes to read to the number of bytes left |
| 38 |
std::size_t actualSizeToRead = (bytesRequested < bytesLeft) ? bytesRequested : bytesLeft; |
| 39 |
|
| 40 |
if (actualSizeToRead > 0) { |
| 41 |
// Copy the data |
| 42 |
memcpy(ptr, self->_curPtr, actualSizeToRead); |
| 43 |
// Increase the local pointer |
| 44 |
self->_curPtr += actualSizeToRead; |
| 45 |
} |
| 46 |
|
| 47 |
// Return how much we read (in the same way fread would) |
| 48 |
return actualSizeToRead; |
| 49 |
} |
| 50 |
|
| 51 |
static int oggSeekFunc(void* datasource, ogg_int64_t offset, int whence) { |
| 52 |
OggFileStream* self = reinterpret_cast<OggFileStream*>(datasource); |
| 53 |
|
| 54 |
// Goto where we wish to seek to |
| 55 |
switch (whence) { |
| 56 |
case SEEK_SET: |
| 57 |
// Seek to the start of the data file |
| 58 |
self->_curPtr = self->_source.buffer + offset; |
| 59 |
break; |
| 60 |
case SEEK_CUR: |
| 61 |
// Seek relatively to the current position |
| 62 |
self->_curPtr += offset; |
| 63 |
break; |
| 64 |
case SEEK_END: |
| 65 |
// Seek the end of the file |
| 66 |
self->_curPtr = self->_source.buffer + self->_source.length; |
| 67 |
break; |
| 68 |
default: |
| 69 |
// Unknown SEEK case |
| 70 |
break; |
| 71 |
}; |
| 72 |
|
| 73 |
// Clamp the curPtr to allowed values |
| 74 |
if (self->_curPtr > self->_source.buffer + self->_source.length) { |
| 75 |
self->_curPtr = self->_source.buffer + self->_source.length; |
| 76 |
} |
| 77 |
|
| 78 |
return 0; |
| 79 |
} |
| 80 |
|
| 81 |
static int oggCloseFunc(void* datasource) { |
| 82 |
return 1; |
| 83 |
} |
| 84 |
|
| 85 |
static long oggTellFunc(void* datasource) { |
| 86 |
OggFileStream* self = reinterpret_cast<OggFileStream*>(datasource); |
| 87 |
return static_cast<long>(self->_curPtr - self->_source.buffer); |
| 88 |
} |
| 89 |
}; |
| 90 |
|
| 91 |
} // namespace sound |