Initial Mercurial commit.

This commit is contained in:
rude
2009-07-26 15:46:49 +02:00
commit dcb3dfd83d
417 changed files with 124060 additions and 0 deletions
+69
View File
@@ -0,0 +1,69 @@
/**
* Copyright (c) 2006-2009 LOVE Development Team
*
* This software is provided 'as-is', without any express or implied
* warranty. In no event will the authors be held liable for any damages
* arising from the use of this software.
*
* Permission is granted to anyone to use this software for any purpose,
* including commercial applications, and to alter it and redistribute it
* freely, subject to the following restrictions:
*
* 1. The origin of this software must not be misrepresented; you must not
* claim that you wrote the original software. If you use this software
* in a product, an acknowledgment in the product documentation would be
* appreciated but is not required.
* 2. Altered source versions must be plainly marked as such, and must not be
* misrepresented as being the original software.
* 3. This notice may not be removed or altered from any source distribution.
**/
#include "Decoder.h"
#include <common/Exception.h>
namespace love
{
namespace sound
{
namespace lullaby
{
Decoder::Decoder(Data * data, const std::string & ext, int bufferSize, int sampleRate)
:data(data), ext(ext), bufferSize(bufferSize), sampleRate(sampleRate), buffer(0), eof(false)
{
data->retain();
buffer = new char[bufferSize];
}
Decoder::~Decoder()
{
if(buffer != 0)
delete [] buffer;
if(data != 0)
data->release();
}
void * Decoder::getBuffer() const
{
return buffer;
}
int Decoder::getSize() const
{
return bufferSize;
}
int Decoder::getSampleRate() const
{
return sampleRate;
}
bool Decoder::isFinished()
{
return eof;
}
} // lullaby
} // sound
} // love
+79
View File
@@ -0,0 +1,79 @@
/**
* Copyright (c) 2006-2009 LOVE Development Team
*
* This software is provided 'as-is', without any express or implied
* warranty. In no event will the authors be held liable for any damages
* arising from the use of this software.
*
* Permission is granted to anyone to use this software for any purpose,
* including commercial applications, and to alter it and redistribute it
* freely, subject to the following restrictions:
*
* 1. The origin of this software must not be misrepresented; you must not
* claim that you wrote the original software. If you use this software
* in a product, an acknowledgment in the product documentation would be
* appreciated but is not required.
* 2. Altered source versions must be plainly marked as such, and must not be
* misrepresented as being the original software.
* 3. This notice may not be removed or altered from any source distribution.
**/
#ifndef LOVE_SOUND_LULLABY_DECODER_H
#define LOVE_SOUND_LULLABY_DECODER_H
// LOVE
#include <sound/Decoder.h>
#include <filesystem/File.h>
#include <string>
namespace love
{
namespace sound
{
namespace lullaby
{
class Decoder : public love::sound::Decoder
{
protected:
// The encoded data. This should be replaced with buffered file
// reads in the future.
Data * data;
// File extension.
std::string ext;
// When the decoder decodes data incrementally, it writes
// this many bytes at a time (at most).
int bufferSize;
// The desired frequency of the samples. 44100, 22050, or 11025.
int sampleRate;
// Holds internal memory.
void * buffer;
// Set this to true when eof has been reached.
bool eof;
public:
Decoder(Data * data, const std::string & ext, int bufferSize, int sampleRate);
virtual ~Decoder();
// Implement some of love::sound::Decoder, but allow subclasses
// to override them.
virtual void * getBuffer() const;
virtual int getSize() const;
virtual int getSampleRate() const;
virtual bool isFinished();
}; // Decoder
} // lullaby
} // sound
} // love
#endif // LOVE_SOUND_LULLABY_DECODER_H
+179
View File
@@ -0,0 +1,179 @@
/**
* Copyright (c) 2006-2009 LOVE Development Team
*
* This software is provided 'as-is', without any express or implied
* warranty. In no event will the authors be held liable for any damages
* arising from the use of this software.
*
* Permission is granted to anyone to use this software for any purpose,
* including commercial applications, and to alter it and redistribute it
* freely, subject to the following restrictions:
*
* 1. The origin of this software must not be misrepresented; you must not
* claim that you wrote the original software. If you use this software
* in a product, an acknowledgment in the product documentation would be
* appreciated but is not required.
* 2. Altered source versions must be plainly marked as such, and must not be
* misrepresented as being the original software.
* 3. This notice may not be removed or altered from any source distribution.
**/
#include "FLACDecoder.h"
#include <set>
#include <common/Exception.h>
namespace love
{
namespace sound
{
namespace lullaby
{
FLACDecoder::FLACDecoder(Data * data, const std::string & ext, int nbufferSize, int sampleRate)
: Decoder(data, ext, nbufferSize, sampleRate), pos(0)
{
init();
init_ogg();
process_until_end_of_metadata();
process_single();
seek(0);
bufferSize = 256 * getBits() * getChannels() * 2;
delete[] buffer;
buffer = new char[bufferSize];
}
FLACDecoder::~FLACDecoder()
{
finish();
}
bool FLACDecoder::accepts(const std::string & ext)
{
static const std::string supported[] = {
"flac", ""
};
for(int i = 0; !(supported[i].empty()); i++)
{
if(supported[i].compare(ext) == 0)
return true;
}
return false;
}
love::sound::Decoder *FLACDecoder::clone()
{
return new FLACDecoder(data, ext, bufferSize, sampleRate);
}
int FLACDecoder::decode()
{
process_single();
return bufferSize;
}
bool FLACDecoder::seek(int ms)
{
return seek_absolute(ms);
}
bool FLACDecoder::rewind()
{
return seek_absolute(0);
}
bool FLACDecoder::isSeekable()
{
return true;
}
int FLACDecoder::getChannels() const
{
return get_channels();
}
int FLACDecoder::getBits() const
{
return get_bits_per_sample();
}
int FLACDecoder::getSampleRate() const
{
return get_sample_rate();
}
FLAC__StreamDecoderReadStatus FLACDecoder::read_callback(FLAC__byte buffer[], size_t *bytes)
{
int size = data->getSize();
char *d = (char *) data->getData() + pos;
if (pos >= size)
{
eof = true;
return FLAC__STREAM_DECODER_READ_STATUS_END_OF_STREAM;
}
if (pos+*bytes <= (unsigned int)size)
{
memcpy(buffer, d, *bytes);
pos = pos+*bytes;
}
else
{
memcpy(buffer, d, size-pos);
*bytes = size-pos;
pos = size;
}
return FLAC__STREAM_DECODER_READ_STATUS_CONTINUE;
}
FLAC__StreamDecoderSeekStatus FLACDecoder::seek_callback(FLAC__uint64 offset)
{
pos = (int)offset;
return FLAC__STREAM_DECODER_SEEK_STATUS_OK;
}
FLAC__StreamDecoderTellStatus FLACDecoder::tell_callback(FLAC__uint64 *offset)
{
*offset = pos;
return FLAC__STREAM_DECODER_TELL_STATUS_OK;
}
FLAC__StreamDecoderLengthStatus FLACDecoder::length_callback(FLAC__uint64 *length)
{
*length = data->getSize();
return FLAC__STREAM_DECODER_LENGTH_STATUS_OK;
}
bool FLACDecoder::eof_callback()
{
eof = (pos >= data->getSize());
return eof;
}
FLAC__StreamDecoderWriteStatus FLACDecoder::write_callback(const FLAC__Frame *frame, const FLAC__int32 *const fbuffer[])
{
int i, j;
for (i = 0, j = 0; i < bufferSize; i += 4, j++)
{
((char *)buffer)[i] = fbuffer[0][j];
((char *)buffer)[i+1] = fbuffer[0][j] >> 8;
((char *)buffer)[i+2] = fbuffer[1][j];
((char *)buffer)[i+3] = fbuffer[1][j] >> 8;
}
return FLAC__STREAM_DECODER_WRITE_STATUS_CONTINUE;
}
void FLACDecoder::metadata_callback(const FLAC__StreamMetadata *metadata)
{
//we do nothing with metadata...
return;
}
void FLACDecoder::error_callback(FLAC__StreamDecoderErrorStatus status)
{
//wow.. error, let's throw one (please clean this part up sometime)
throw love::Exception("FLAC error: %s!", FLAC__StreamDecoderErrorStatusString[status]);
}
} // lullaby
} // sound
} // love
+71
View File
@@ -0,0 +1,71 @@
/**
* Copyright (c) 2006-2009 LOVE Development Team
*
* This software is provided 'as-is', without any express or implied
* warranty. In no event will the authors be held liable for any damages
* arising from the use of this software.
*
* Permission is granted to anyone to use this software for any purpose,
* including commercial applications, and to alter it and redistribute it
* freely, subject to the following restrictions:
*
* 1. The origin of this software must not be misrepresented; you must not
* claim that you wrote the original software. If you use this software
* in a product, an acknowledgment in the product documentation would be
* appreciated but is not required.
* 2. Altered source versions must be plainly marked as such, and must not be
* misrepresented as being the original software.
* 3. This notice may not be removed or altered from any source distribution.
**/
#ifndef LOVE_SOUND_LULLABY_FLAC_DECODER_H
#define LOVE_SOUND_LULLABY_FLAC_DECODER_H
// LOVE
#include <common/Data.h>
#include "Decoder.h"
#include <FLAC++/decoder.h>
#include <string.h>
namespace love
{
namespace sound
{
namespace lullaby
{
class FLACDecoder : public Decoder, public FLAC::Decoder::Stream
{
private:
int pos;
public:
FLACDecoder(Data * data, const std::string & ext, int bufferSize, int sampleRate);
~FLACDecoder();
static bool accepts(const std::string &ext);
love::sound::Decoder *clone();
int decode();
bool seek(int ms);
bool rewind();
bool isSeekable();
int getChannels() const;
int getBits() const;
int getSampleRate() const;
//needed for FLAC
FLAC__StreamDecoderReadStatus read_callback(FLAC__byte buffer[], size_t *bytes);
FLAC__StreamDecoderSeekStatus seek_callback(FLAC__uint64 offset);
FLAC__StreamDecoderTellStatus tell_callback(FLAC__uint64 *offset);
FLAC__StreamDecoderLengthStatus length_callback(FLAC__uint64 *length);
bool eof_callback();
FLAC__StreamDecoderWriteStatus write_callback(const FLAC__Frame *frame, const FLAC__int32 *const buffer[]);
void metadata_callback(const FLAC__StreamMetadata *metadata);
void error_callback(FLAC__StreamDecoderErrorStatus status);
}; // Decoder
} // lullaby
} // sound
} // love
#endif // LOVE_SOUND_LULLABY_FLAC_DECODER_H
@@ -0,0 +1,117 @@
/**
* Copyright (c) 2006-2009 LOVE Development Team
*
* This software is provided 'as-is', without any express or implied
* warranty. In no event will the authors be held liable for any damages
* arising from the use of this software.
*
* Permission is granted to anyone to use this software for any purpose,
* including commercial applications, and to alter it and redistribute it
* freely, subject to the following restrictions:
*
* 1. The origin of this software must not be misrepresented; you must not
* claim that you wrote the original software. If you use this software
* in a product, an acknowledgment in the product documentation would be
* appreciated but is not required.
* 2. Altered source versions must be plainly marked as such, and must not be
* misrepresented as being the original software.
* 3. This notice may not be removed or altered from any source distribution.
**/
#include "ModPlugDecoder.h"
#include <set>
#include <common/Exception.h>
namespace love
{
namespace sound
{
namespace lullaby
{
ModPlugDecoder::ModPlugDecoder(Data * data, const std::string & ext, int bufferSize, int sampleRate)
: Decoder(data, ext, bufferSize, sampleRate), plug(0)
{
// Set some ModPlug settings.
settings.mFlags = MODPLUG_ENABLE_OVERSAMPLING | MODPLUG_ENABLE_NOISE_REDUCTION;
settings.mChannels = 2;
settings.mBits = 16;
settings.mFrequency = sampleRate;
settings.mResamplingMode = MODPLUG_RESAMPLE_LINEAR;
settings.mLoopCount = 0;
ModPlug_SetSettings(&settings);
// Load the module.
plug = ModPlug_Load(data->getData(), data->getSize());
if(plug == 0)
throw love::Exception("Could not load file with ModPlug.");
}
ModPlugDecoder::~ModPlugDecoder()
{
if(plug != 0)
ModPlug_Unload(plug);
}
bool ModPlugDecoder::accepts(const std::string & ext)
{
static const std::string supported[] = {
"it", "xm", "mod", "wav", ""
};
for(int i = 0; !(supported[i].empty()); i++)
{
if(supported[i].compare(ext) == 0)
return true;
}
return false;
}
love::sound::Decoder * ModPlugDecoder::clone()
{
return new ModPlugDecoder(data, ext, bufferSize, settings.mFrequency);
}
int ModPlugDecoder::decode()
{
int r = ModPlug_Read(plug, buffer, bufferSize);
if(r == 0)
eof = true;
return r;
}
bool ModPlugDecoder::seek(int ms)
{
ModPlug_Seek(plug, ms);
return true;
}
bool ModPlugDecoder::rewind()
{
ModPlug_Seek(plug, 0);
return true;
}
bool ModPlugDecoder::isSeekable()
{
return true;
}
int ModPlugDecoder::getChannels() const
{
return 2;
}
int ModPlugDecoder::getBits() const
{
return 16;
}
} // lullaby
} // sound
} // love
@@ -0,0 +1,65 @@
/**
* Copyright (c) 2006-2009 LOVE Development Team
*
* This software is provided 'as-is', without any express or implied
* warranty. In no event will the authors be held liable for any damages
* arising from the use of this software.
*
* Permission is granted to anyone to use this software for any purpose,
* including commercial applications, and to alter it and redistribute it
* freely, subject to the following restrictions:
*
* 1. The origin of this software must not be misrepresented; you must not
* claim that you wrote the original software. If you use this software
* in a product, an acknowledgment in the product documentation would be
* appreciated but is not required.
* 2. Altered source versions must be plainly marked as such, and must not be
* misrepresented as being the original software.
* 3. This notice may not be removed or altered from any source distribution.
**/
#ifndef LOVE_SOUND_LULLABY_MODPLUG_DECODER_H
#define LOVE_SOUND_LULLABY_MODPLUG_DECODER_H
// LOVE
#include <common/Data.h>
#include "Decoder.h"
// SDL_sound
#include <libmodplug/modplug.h>
namespace love
{
namespace sound
{
namespace lullaby
{
class ModPlugDecoder : public Decoder
{
private:
ModPlugFile * plug;
ModPlug_Settings settings;
public:
ModPlugDecoder(Data * data, const std::string & ext, int bufferSize, int sampleRate);
~ModPlugDecoder();
static bool accepts(const std::string & ext);
love::sound::Decoder * clone();
int decode();
bool seek(int ms);
bool rewind();
bool isSeekable();
int getChannels() const;
int getBits() const;
}; // Decoder
} // lullaby
} // sound
} // love
#endif // LOVE_SOUND_LULLABY_MODPLUG_DECODER_H
+141
View File
@@ -0,0 +1,141 @@
/**
* Copyright (c) 2006-2009 LOVE Development Team
*
* This software is provided 'as-is', without any express or implied
* warranty. In no event will the authors be held liable for any damages
* arising from the use of this software.
*
* Permission is granted to anyone to use this software for any purpose,
* including commercial applications, and to alter it and redistribute it
* freely, subject to the following restrictions:
*
* 1. The origin of this software must not be misrepresented; you must not
* claim that you wrote the original software. If you use this software
* in a product, an acknowledgment in the product documentation would be
* appreciated but is not required.
* 2. Altered source versions must be plainly marked as such, and must not be
* misrepresented as being the original software.
* 3. This notice may not be removed or altered from any source distribution.
**/
#include "Mpg123Decoder.h"
#include <common/Exception.h>
namespace love
{
namespace sound
{
namespace lullaby
{
bool Mpg123Decoder::inited = false;
Mpg123Decoder::Mpg123Decoder(Data * data, const std::string & ext, int bufferSize, int sampleRate)
: Decoder(data, ext, bufferSize, sampleRate), handle(0)
{
int ret;
if(!inited)
{
ret = mpg123_init();
if (ret != MPG123_OK)
throw love::Exception("Could not initialize mpg123.");
inited = (ret == MPG123_OK);
}
//Intialize the handle
handle = mpg123_new(NULL, &ret);
if (handle == NULL)
throw love::Exception("Could not create handle.");
ret = mpg123_open_feed(handle);
if (ret != MPG123_OK)
throw love::Exception("Could not open feed.");
ret = mpg123_format(handle, sampleRate, MPG123_STEREO, MPG123_ENC_SIGNED_16);
if (ret != MPG123_OK)
throw love::Exception("Could not set output format.");
size_t numbytes = 0;
ret = mpg123_decode(handle, (unsigned char*) data->getData(), data->getSize(), NULL, 0, &numbytes);
if (ret != MPG123_DONE && ret != MPG123_NEW_FORMAT)
throw love::Exception("Could not decode feed.");
}
Mpg123Decoder::~Mpg123Decoder()
{
mpg123_close(handle);
}
bool Mpg123Decoder::accepts(const std::string & ext)
{
static const std::string supported[] = {
"mp3", ""
};
for(int i = 0; !(supported[i].empty()); i++)
{
if(supported[i].compare(ext) == 0)
return true;
}
return false;
}
void Mpg123Decoder::quit()
{
if(inited)
mpg123_exit();
}
love::sound::Decoder * Mpg123Decoder::clone()
{
return new Mpg123Decoder(data, ext, bufferSize, sampleRate);
}
int Mpg123Decoder::decode()
{
size_t numbytes;
int r = mpg123_read(handle, (unsigned char*) buffer, bufferSize, &numbytes);
// If we're done, then EOF. If some error occurred, pretend we EOF'd.
if (r == MPG123_DONE || r != MPG123_OK)
{
eof = true;
numbytes = 0;
}
return numbytes;
}
bool Mpg123Decoder::seek(int ms)
{
mpg123_seek(handle, SEEK_SET, ms);
return true;
}
bool Mpg123Decoder::rewind()
{
mpg123_seek(handle, SEEK_SET, 0);
return true;
}
bool Mpg123Decoder::isSeekable()
{
return true;
}
int Mpg123Decoder::getChannels() const
{
return 2;
}
int Mpg123Decoder::getBits() const
{
return 16;
}
} // lullaby
} // sound
} // love
+67
View File
@@ -0,0 +1,67 @@
/**
* Copyright (c) 2006-2009 LOVE Development Team
*
* This software is provided 'as-is', without any express or implied
* warranty. In no event will the authors be held liable for any damages
* arising from the use of this software.
*
* Permission is granted to anyone to use this software for any purpose,
* including commercial applications, and to alter it and redistribute it
* freely, subject to the following restrictions:
*
* 1. The origin of this software must not be misrepresented; you must not
* claim that you wrote the original software. If you use this software
* in a product, an acknowledgment in the product documentation would be
* appreciated but is not required.
* 2. Altered source versions must be plainly marked as such, and must not be
* misrepresented as being the original software.
* 3. This notice may not be removed or altered from any source distribution.
**/
#ifndef LOVE_SOUND_LULLABY_LIBMPG123_DECODER_H
#define LOVE_SOUND_LULLABY_LIBMPG123_DECODER_H
// LOVE
#include <common/Data.h>
#include "Decoder.h"
// libmpg123
#include <mpg123.h>
namespace love
{
namespace sound
{
namespace lullaby
{
class Mpg123Decoder : public Decoder
{
private:
mpg123_handle * handle;
static bool inited;
public:
Mpg123Decoder(Data * data, const std::string & ext, int bufferSize, int sampleRate);
~Mpg123Decoder();
static bool accepts(const std::string & ext);
static void quit();
love::sound::Decoder * clone();
int decode();
bool seek(int ms);
bool rewind();
bool isSeekable();
int getChannels() const;
int getBits() const;
}; // Decoder
} // lullaby
} // sound
} // love
#endif // LOVE_SOUND_LULLABY_LIBMPG123_DECODER_H
+74
View File
@@ -0,0 +1,74 @@
/**
* Copyright (c) 2006-2009 LOVE Development Team
*
* This software is provided 'as-is', without any express or implied
* warranty. In no event will the authors be held liable for any damages
* arising from the use of this software.
*
* Permission is granted to anyone to use this software for any purpose,
* including commercial applications, and to alter it and redistribute it
* freely, subject to the following restrictions:
*
* 1. The origin of this software must not be misrepresented; you must not
* claim that you wrote the original software. If you use this software
* in a product, an acknowledgment in the product documentation would be
* appreciated but is not required.
* 2. Altered source versions must be plainly marked as such, and must not be
* misrepresented as being the original software.
* 3. This notice may not be removed or altered from any source distribution.
**/
#include "Sound.h"
#include "ModPlugDecoder.h"
#include "Mpg123Decoder.h"
#include "VorbisDecoder.h"
#include "FLACDecoder.h"
namespace love
{
namespace sound
{
namespace lullaby
{
Sound::Sound()
{
}
Sound::~Sound()
{
Mpg123Decoder::quit();
}
const char * Sound::getName() const
{
return "love.sound.lullaby";
}
sound::Decoder * Sound::newDecoder(love::filesystem::File * file, int bufferSize, int sampleRate)
{
Data * data = file->read();
std::string ext = file->getExtension();
sound::Decoder * decoder = 0;
// Find a suitable decoder here, and return it.
if(ModPlugDecoder::accepts(ext))
decoder = new ModPlugDecoder(data, ext, bufferSize, sampleRate);
else if (Mpg123Decoder::accepts(ext))
decoder = new Mpg123Decoder(data, ext, bufferSize, sampleRate);
else if (VorbisDecoder::accepts(ext))
decoder = new VorbisDecoder(data, ext, bufferSize, sampleRate);
else if (FLACDecoder::accepts(ext))
decoder = new FLACDecoder(data, ext, bufferSize, sampleRate);
// else if(OtherDecoder::accept(ext))
data->release();
return decoder;
}
} // lullaby
} // sound
} // love
+69
View File
@@ -0,0 +1,69 @@
/**
* Copyright (c) 2006-2009 LOVE Development Team
*
* This software is provided 'as-is', without any express or implied
* warranty. In no event will the authors be held liable for any damages
* arising from the use of this software.
*
* Permission is granted to anyone to use this software for any purpose,
* including commercial applications, and to alter it and redistribute it
* freely, subject to the following restrictions:
*
* 1. The origin of this software must not be misrepresented; you must not
* claim that you wrote the original software. If you use this software
* in a product, an acknowledgment in the product documentation would be
* appreciated but is not required.
* 2. Altered source versions must be plainly marked as such, and must not be
* misrepresented as being the original software.
* 3. This notice may not be removed or altered from any source distribution.
**/
#ifndef LOVE_SOUND_LULLABY_SOUND_H
#define LOVE_SOUND_LULLABY_SOUND_H
// LOVE
#include <sound/Sound.h>
#include <sound/Decoder.h>
namespace love
{
namespace sound
{
namespace lullaby
{
/**
* The love.sound.lullaby module is the custom sound decoder module for LOVE. Instead
* of using an intermediate library like SDL_sound, it interfaces with relevant libraries
* directly (libmpg123, libmodplug, libFLAC, etc).
*
* It was Mike that came up with the name Lullaby, which we both instantly recognized as awesome.
**/
class Sound : public love::sound::Sound
{
public:
/**
* Constructor. Initializes relevant libraries.
**/
Sound();
/**
* Destructor. Deinitializes relevant libraries.
**/
virtual ~Sound();
/// @copydoc love::Module::getName
const char * getName() const;
/// @copydoc love::sound::Sound::newDecoder
sound::Decoder * newDecoder(love::filesystem::File * file, int bufferSize, int sampleRate);
}; // Sound
} // lullaby
} // sound
} // love
#endif // LOVE_SOUND_LULLABY_SOUND_H
+278
View File
@@ -0,0 +1,278 @@
/**
* Copyright (c) 2006-2009 LOVE Development Team
*
* This software is provided 'as-is', without any express or implied
* warranty. In no event will the authors be held liable for any damages
* arising from the use of this software.
*
* Permission is granted to anyone to use this software for any purpose,
* including commercial applications, and to alter it and redistribute it
* freely, subject to the following restrictions:
*
* 1. The origin of this software must not be misrepresented; you must not
* claim that you wrote the original software. If you use this software
* in a product, an acknowledgment in the product documentation would be
* appreciated but is not required.
* 2. Altered source versions must be plainly marked as such, and must not be
* misrepresented as being the original software.
* 3. This notice may not be removed or altered from any source distribution.
**/
#include "VorbisDecoder.h"
#include <string.h>
#include <common/Exception.h>
namespace love
{
namespace sound
{
namespace lullaby
{
/**
* CALLBACK FUNCTIONS
**/
int vorbisClose(void * datasource /* ptr to the data that the vorbis files need*/)
{
// Does nothing (handled elsewhere)
return 1;
}
size_t vorbisRead(void * ptr /* ptr to the data that the vorbis files need*/,
size_t byteSize /* how big a byte is*/,
size_t sizeToRead /* How much we can read*/,
void * datasource /* this is a pointer to the data we passed into ov_open_callbacks (our SOggFile struct*/)
{
size_t spaceToEOF; // How much more we can read till we hit the EOF marker
size_t actualSizeToRead; // How much data we are actually going to read from memory
SOggFile* vorbisData; // Our vorbis data, for the typecast
// Get the data in the right format
vorbisData = (SOggFile*)datasource;
// Calculate how much we need to read. This can be sizeToRead*byteSize or less depending on how near the EOF marker we are
spaceToEOF = vorbisData->dataSize - vorbisData->dataRead;
if ((sizeToRead*byteSize) < spaceToEOF)
actualSizeToRead = (sizeToRead*byteSize);
else
actualSizeToRead = spaceToEOF;
// A simple copy of the data from memory to the datastruct that the vorbis libs will use
if (actualSizeToRead)
{
// Copy the data from the start of the file PLUS how much we have already read in
memcpy(ptr, (char*)vorbisData->dataPtr + vorbisData->dataRead, actualSizeToRead);
// Increase by how much we have read by
vorbisData->dataRead += (actualSizeToRead);
}
// Return how much we read (in the same way fread would)
return actualSizeToRead;
}
int vorbisSeek(void * datasource /* ptr to the data that the vorbis files need*/,
ogg_int64_t offset /*offset from the point we wish to seek to*/,
int whence /*where we want to seek to*/)
{
size_t spaceToEOF; // How much more we can read till we hit the EOF marker
ogg_int64_t actualOffset; // How much we can actually offset it by
SOggFile* vorbisData; // The data we passed in (for the typecast)
// Get the data in the right format
vorbisData = (SOggFile*) datasource;
// Goto where we wish to seek to
switch (whence)
{
case SEEK_SET: // Seek to the start of the data file
// Make sure we are not going to the end of the file
if (vorbisData->dataSize >= offset)
actualOffset = offset;
else
actualOffset = vorbisData->dataSize;
// Set where we now are
vorbisData->dataRead = (int)actualOffset;
break;
case SEEK_CUR: // Seek from where we are
// Make sure we dont go past the end
spaceToEOF = vorbisData->dataSize - vorbisData->dataRead;
if (offset < spaceToEOF)
actualOffset = (offset);
else
actualOffset = spaceToEOF;
// Seek from our currrent location
vorbisData->dataRead += (int)actualOffset;
break;
case SEEK_END: // Seek from the end of the file
vorbisData->dataRead = vorbisData->dataSize+1;
break;
default:
throw love::Exception("Unknown seek command in vorbisSeek\n");
break;
};
return 0;
}
long vorbisTell(void * datasource /* ptr to the data that the vorbis files need*/)
{
SOggFile * vorbisData;
vorbisData = (SOggFile*) datasource;
return vorbisData->dataRead;
}
/**
* END CALLBACK FUNCTIONS
**/
VorbisDecoder::VorbisDecoder(Data * data, const std::string & ext, int bufferSize, int sampleRate)
: Decoder(data, ext, bufferSize, sampleRate)
{
// Initialize callbacks
vorbisCallbacks.close_func = vorbisClose;
vorbisCallbacks.seek_func = vorbisSeek;
vorbisCallbacks.read_func = vorbisRead;
vorbisCallbacks.tell_func = vorbisTell;
// Initialize OGG file
oggFile.dataPtr = (char *) data->getData();
oggFile.dataSize = data->getSize();
oggFile.dataRead = 0;
// Open Vorbis handle
if(ov_open_callbacks(&oggFile, &handle, NULL, 0, vorbisCallbacks) < 0)
throw love::Exception("Could not read Ogg bitstream");
// Get info and comments
vorbisInfo = ov_info(&handle, -1);
vorbisComment = ov_comment(&handle, -1);
}
VorbisDecoder::~VorbisDecoder()
{
ov_clear(&handle);
}
bool VorbisDecoder::accepts(const std::string & ext)
{
static const std::string supported[] = {
"ogg", "oga", ""
};
for(int i = 0; !(supported[i].empty()); i++)
{
if(supported[i].compare(ext) == 0)
return true;
}
return false;
}
love::sound::Decoder * VorbisDecoder::clone()
{
return new VorbisDecoder(data, ext, bufferSize, sampleRate);
}
int VorbisDecoder::decode()
{
int bits = this->getBits();
int size = 0;
int section;
int result;
if(bits == 16)
bits = 2;
else
bits = 1;
while(size < bufferSize)
{
result = ov_read(&handle, (char*) buffer + size, bufferSize - size, 0, bits, 1, &section);
if(result > 0)
size += result;
else if(result < 0)
{
switch(result)
{
case OV_EREAD:
throw love::Exception("Vorbis read: Read from media");
case OV_ENOTVORBIS:
throw love::Exception("Vorbis read: Not Vorbis data");
case OV_EVERSION:
throw love::Exception("Vorbis read: Vorbis version mismatch");
case OV_EBADHEADER:
throw love::Exception("Vorbis read: Invalid Vorbis header");
case OV_EFAULT:
throw love::Exception("Vorbis read: Internal logic fault (bug or heap/stack corruption)");
default:
throw love::Exception("Vorbis read: Unknown error");
}
}
else
break;
}
if(oggFile.dataSize - oggFile.dataRead == 0)
eof = true;
return size;
}
bool VorbisDecoder::seek(int ms)
{
eof = false;
int result = ov_time_seek(&handle, ms / 100);
if(result == 0)
return true;
switch(result)
{
case OV_ENOSEEK:
throw love::Exception("Vorbis seek: Bitstream is unseekable");
case OV_EINVAL:
throw love::Exception("Vorbis seek: Invalid argument value");
case OV_EREAD:
throw love::Exception("Vorbis seek: Read from media");
case OV_EFAULT:
throw love::Exception("Vorbis seek: Internal logic fault (bug or heap/stack corruption)");
case OV_EBADLINK:
throw love::Exception("Vorbis seek: Invalid stream section supplied or link is corrupt");
default:
throw love::Exception("Vorbis seek: Unknown error");
}
return false;
}
bool VorbisDecoder::rewind()
{
eof = false;
return this->seek(0);
}
bool VorbisDecoder::isSeekable()
{
long result = ov_seekable(&handle);
return (result != 0);
}
int VorbisDecoder::getChannels() const
{
return vorbisInfo->channels;
}
int VorbisDecoder::getBits() const
{
//return vorbisInfo->bitrate_nominal / 10000;
return 16;
}
int VorbisDecoder::getSampleRate() const
{
return vorbisInfo->rate;
}
} // lullaby
} // sound
} // love
+78
View File
@@ -0,0 +1,78 @@
/**
* Copyright (c) 2006-2009 LOVE Development Team
*
* This software is provided 'as-is', without any express or implied
* warranty. In no event will the authors be held liable for any damages
* arising from the use of this software.
*
* Permission is granted to anyone to use this software for any purpose,
* including commercial applications, and to alter it and redistribute it
* freely, subject to the following restrictions:
*
* 1. The origin of this software must not be misrepresented; you must not
* claim that you wrote the original software. If you use this software
* in a product, an acknowledgment in the product documentation would be
* appreciated but is not required.
* 2. Altered source versions must be plainly marked as such, and must not be
* misrepresented as being the original software.
* 3. This notice may not be removed or altered from any source distribution.
**/
#ifndef LOVE_SOUND_LULLABY_VORBIS_DECODER_H
#define LOVE_SOUND_LULLABY_VORBIS_DECODER_H
// LOVE
#include <common/Data.h>
#include "Decoder.h"
// vorbis
#include <vorbis/codec.h>
#include <vorbis/vorbisfile.h>
namespace love
{
namespace sound
{
namespace lullaby
{
// Struct for handling data
struct SOggFile
{
char * dataPtr; // Pointer to the data in memory
int dataSize; // Size of the data
int dataRead; // How much we've read so far
};
class VorbisDecoder : public Decoder
{
private:
SOggFile oggFile; // (see struct)
ov_callbacks vorbisCallbacks; // Callbacks used to read the file from mem
OggVorbis_File handle; // Handle to the file
vorbis_info * vorbisInfo; // Info
vorbis_comment * vorbisComment; // Comments
bool eof; // Whether we have reached the end of the file
public:
VorbisDecoder(Data * data, const std::string & ext, int bufferSize, int sampleRate);
~VorbisDecoder();
static bool accepts(const std::string & ext);
love::sound::Decoder * clone();
int decode();
bool seek(int ms);
bool rewind();
bool isSeekable();
int getChannels() const;
int getBits() const;
int getSampleRate() const;
}; // VorbisDecoder
} // lullaby
} // sound
} // love
#endif // LOVE_SOUND_LULLABY_VORBIS_DECODER_H