Engine directory for ticket #1

This commit is contained in:
DavidWyand-GG 2012-09-19 11:15:01 -04:00
parent 352279af7a
commit 7dbfe6994d
3795 changed files with 1363358 additions and 0 deletions

View file

@ -0,0 +1,283 @@
//-----------------------------------------------------------------------------
// Copyright (c) 2012 GarageGames, LLC
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to
// deal in the Software without restriction, including without limitation the
// rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
// sell copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
// IN THE SOFTWARE.
//-----------------------------------------------------------------------------
#ifdef TORQUE_OGGVORBIS
#include "sfx/media/sfxVorbisStream.h"
#include "core/stream/stream.h"
#include "console/console.h"
SFXVorbisStream* SFXVorbisStream::create( Stream *stream )
{
SFXVorbisStream *sfxStream = new SFXVorbisStream();
if ( sfxStream->open( stream, true ) )
return sfxStream;
delete sfxStream;
return NULL;
}
SFXVorbisStream::SFXVorbisStream()
: mVF( NULL ),
mBytesRead( 0 )
{
}
SFXVorbisStream::SFXVorbisStream( const SFXVorbisStream& cloneFrom )
: Parent( cloneFrom )
{
if( !mStream->hasCapability( Stream::StreamPosition ) )
{
Con::errorf( "SFXVorbisStream::SFXVorbisStream() - Source stream does not allow seeking" );
return;
}
mStream->setPosition( 0 );
if( !_readHeader() )
{
Con::errorf( "SFXVorbisStream::SFXVorbisStream() - Opening Vorbis stream failed" );
return;
}
ov_pcm_seek( mVF, ov_pcm_tell( cloneFrom.mVF ) );
mBitstream = cloneFrom.mBitstream;
mBytesRead = cloneFrom.mBytesRead;
}
SFXVorbisStream::~SFXVorbisStream()
{
// We must call close from our own destructor
// and not the base class... as it causes a
// pure virtual runtime assertion.
close();
}
size_t SFXVorbisStream::_read_func( void *ptr, size_t size, size_t nmemb, void *datasource )
{
Stream *stream = reinterpret_cast<Stream*>( datasource );
// Stream::read() returns true if any data was
// read, so we must track the read bytes ourselves.
U32 startByte = stream->getPosition();
stream->read( size * nmemb, ptr );
U32 endByte = stream->getPosition();
// How many did we actually read?
U32 readBytes = ( endByte - startByte );
U32 readItems = readBytes / size;
return readItems;
}
int SFXVorbisStream::_seek_func( void *datasource, ogg_int64_t offset, int whence )
{
Stream *stream = reinterpret_cast<Stream*>( datasource );
U32 newPos = 0;
if ( whence == SEEK_CUR )
newPos = stream->getPosition() + (U32)offset;
else if ( whence == SEEK_END )
newPos = stream->getStreamSize() - (U32)offset;
else
newPos = (U32)offset;
return stream->setPosition( newPos ) ? 0 : -1;
}
long SFXVorbisStream::_tell_func( void *datasource )
{
Stream *stream = reinterpret_cast<Stream*>( datasource );
return stream->getPosition();
}
bool SFXVorbisStream::_openVorbis()
{
#if defined(TORQUE_OS_XENON)
// For some reason the datasource pointer passed to the callbacks is not the
// same as it is when passed in to ov_open_callbacks
#pragma message("There is a strange bug in ov_open_callbacks as it compiles on the Xbox360. Use FMOD resource loading.")
AssertWarn(false, "There is a strange bug in ov_open_callbacks as it compiles on the Xbox360. Use FMOD resource loading.");
return false;
#endif
mVF = new OggVorbis_File;
dMemset( mVF, 0, sizeof( OggVorbis_File ) );
const bool canSeek = mStream->hasCapability( Stream::StreamPosition );
ov_callbacks cb;
cb.read_func = _read_func;
cb.seek_func = canSeek ? _seek_func : NULL;
cb.close_func = NULL;
cb.tell_func = canSeek ? _tell_func : NULL;
// Open it.
int ovResult = ov_open_callbacks( mStream, mVF, NULL, 0, cb );
if( ovResult != 0 )
return false;
return true;
}
bool SFXVorbisStream::_readHeader()
{
if( !_openVorbis() )
return false;
// Fill in the format info.
const vorbis_info *vi = getInfo();
mFormat.set( vi->channels, vi->channels * 16, vi->rate );
// Set the sample count.
mSamples = getPcmTotal();
// Reset the bitstream.
mBitstream = 0;
return true;
}
void SFXVorbisStream::_close()
{
if ( !mVF )
return;
ov_clear( mVF );
delete mVF;
mVF = NULL;
mBitstream = -1;
}
const vorbis_info* SFXVorbisStream::getInfo( S32 link )
{
AssertFatal( mVF, "SFXVorbisStream::getInfo() - Stream is closed!" );
return ov_info( mVF, link );
}
const vorbis_comment* SFXVorbisStream::getComment( S32 link )
{
AssertFatal( mVF, "SFXVorbisStream::getComment() - Stream is closed!" );
return ov_comment( mVF, link );
}
U64 SFXVorbisStream::getPcmTotal( S32 link )
{
AssertFatal( mVF, "SFXVorbisStream::getInfo() - Stream is closed!" );
return ov_pcm_total( mVF, link );
}
S32 SFXVorbisStream::read( U8 *buffer,
U32 length,
S32 *bitstream )
{
AssertFatal( mVF, "SFXVorbisStream::read() - Stream is closed!" );
mBitstream = *bitstream;
#ifdef TORQUE_BIG_ENDIAN
static const int isBigEndian = 1;
#else
static const int isBigEndian = 0;
#endif
// Vorbis doesn't seem to like reading
// requests longer than this.
const U32 MAXREAD = 4096;
U32 bytesRead = 0;
U32 offset = 0;
U32 bytesToRead = 0;
// Since it only returns the result of one packet
// per call, you generally have to loop to read it all.
while( offset < length )
{
if ( ( length - offset ) < MAXREAD )
bytesToRead = length - offset;
else
bytesToRead = MAXREAD;
bytesRead = ov_read( mVF, (char*)buffer, bytesToRead, isBigEndian, 2, 1, bitstream );
if( bytesRead == 0 ) // EOF
return offset;
else if( bytesRead < 0 )
{
// We got an error... return the result.
return bytesRead;
}
offset += bytesRead;
buffer += bytesRead;
mBytesRead += bytesRead;
}
// Return the total data read.
return offset;
}
bool SFXVorbisStream::isEOS() const
{
return ( Parent::isEOS() || ( mStream && ov_pcm_tell( mVF ) == mSamples ) );
}
void SFXVorbisStream::reset()
{
AssertFatal( mVF, "SFXVorbisStream::reset() - Stream is closed!" );
// There's a bug in libvorbis 1.2.0 that will cause the
// ov_pcm_seek* functions to go into an infinite loop on
// some files (apparently if they contain Theora streams).
// Avoiding to seek when not necessary seems to do the trick
// but if it deadlocks here, that's why.
if( mBytesRead > 0 )
{
ov_pcm_seek_page( mVF, 0 );
mBitstream = 0;
mBytesRead = 0;
}
}
U32 SFXVorbisStream::getPosition() const
{
AssertFatal( mVF, "SFXVorbisStream::getPosition() - Stream is closed!" );
return U32( ov_pcm_tell( mVF ) ) * mFormat.getBytesPerSample();
}
void SFXVorbisStream::setPosition( U32 offset )
{
AssertFatal( mVF, "SFXVorbisStream::setPosition() - Stream is closed!" );
ov_pcm_seek( mVF, offset / mFormat.getBytesPerSample() );
}
U32 SFXVorbisStream::read( U8 *buffer, U32 length )
{
S32 result = read( buffer, length, &mBitstream );
if ( result < 0 )
return 0;
return result;
}
#endif // TORQUE_OGGVORBIS

View file

@ -0,0 +1,112 @@
//-----------------------------------------------------------------------------
// Copyright (c) 2012 GarageGames, LLC
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to
// deal in the Software without restriction, including without limitation the
// rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
// sell copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
// IN THE SOFTWARE.
//-----------------------------------------------------------------------------
#ifndef _SFXVORBISSTREAM_H_
#define _SFXVORBISSTREAM_H_
#ifdef TORQUE_OGGVORBIS
#ifndef _SFXFILESTREAM_H_
# include "sfx/sfxFileStream.h"
#endif
#include "core/util/safeDelete.h"
#include "vorbis/vorbisfile.h"
/// An SFXFileStream that loads sample data from a Vorbis file.
class SFXVorbisStream : public SFXFileStream,
public IPositionable< U32 >
{
public:
typedef SFXFileStream Parent;
protected:
/// The vorbis file.
OggVorbis_File *mVF;
/// The current bitstream index.
S32 mBitstream;
/// Total number of bytes read from the Vorbis stream so far.
U32 mBytesRead;
///
bool _openVorbis();
// The ov_callbacks.
static size_t _read_func( void *ptr, size_t size, size_t nmemb, void *datasource );
static int _seek_func( void *datasource, ogg_int64_t offset, int whence );
static long _tell_func( void *datasource );
// SFXStream
virtual bool _readHeader();
virtual void _close();
public:
///
static SFXVorbisStream* create( Stream *stream );
///
SFXVorbisStream();
///
SFXVorbisStream( const SFXVorbisStream& cloneFrom );
virtual ~SFXVorbisStream();
///
const vorbis_info* getInfo( S32 link = -1 );
///
const vorbis_comment* getComment( S32 link = -1 );
///
// TODO: Deal with error cases... like for unseekable streams!
U64 getPcmTotal( S32 link = -1 );
///
S32 read( U8 *buffer,
U32 length,
S32 *bitstream );
// SFXStream
virtual void reset();
virtual U32 read( U8 *buffer, U32 length );
virtual bool isEOS() const;
virtual SFXStream* clone() const
{
SFXVorbisStream* stream = new SFXVorbisStream( *this );
if( !stream->mVF )
SAFE_DELETE( stream );
return stream;
}
// IPositionable
virtual U32 getPosition() const;
virtual void setPosition( U32 offset );
};
#endif // TORQUE_OGGVORBIS
#endif // _SFXVORBISSTREAM_H_

View file

@ -0,0 +1,299 @@
//-----------------------------------------------------------------------------
// Copyright (c) 2012 GarageGames, LLC
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to
// deal in the Software without restriction, including without limitation the
// rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
// sell copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
// IN THE SOFTWARE.
//-----------------------------------------------------------------------------
#include "sfx/media/sfxWavStream.h"
#include "core/stream/stream.h"
#include "core/strings/stringFunctions.h"
/// WAV File-header
struct WAVFileHdr
{
U8 id[4];
U32 size;
U8 type[4];
};
//// WAV Fmt-header
struct WAVFmtHdr
{
U16 format;
U16 channels;
U32 samplesPerSec;
U32 bytesPerSec;
U16 blockAlign;
U16 bitsPerSample;
};
/// WAV FmtEx-header
struct WAVFmtExHdr
{
U16 size;
U16 samplesPerBlock;
};
/// WAV Smpl-header
struct WAVSmplHdr
{
U32 manufacturer;
U32 product;
U32 samplePeriod;
U32 note;
U32 fineTune;
U32 SMPTEFormat;
U32 SMPTEOffest;
U32 loops;
U32 samplerData;
struct
{
U32 identifier;
U32 type;
U32 start;
U32 end;
U32 fraction;
U32 count;
} loop[1];
};
/// WAV Chunk-header
struct WAVChunkHdr
{
U8 id[4];
U32 size;
};
SFXWavStream* SFXWavStream::create( Stream *stream )
{
SFXWavStream *sfxStream = new SFXWavStream();
if ( sfxStream->open( stream, true ) )
return sfxStream;
delete sfxStream;
return NULL;
}
SFXWavStream::SFXWavStream()
{
}
SFXWavStream::SFXWavStream( const SFXWavStream& cloneFrom )
: Parent( cloneFrom ),
mDataStart( cloneFrom.mDataStart )
{
}
SFXWavStream::~SFXWavStream()
{
// We must call close from our own destructor
// and not the base class... as it causes a
// pure virtual runtime assertion.
close();
}
void SFXWavStream::_close()
{
mDataStart = -1;
}
bool SFXWavStream::_readHeader()
{
// We read the wav chunks to gather than header info
// and find the start and end position of the data chunk.
mDataStart = -1;
WAVFileHdr fileHdr;
mStream->read( 4, &fileHdr.id[0] );
mStream->read( &fileHdr.size );
mStream->read( 4, &fileHdr.type[0] );
fileHdr.size=((fileHdr.size+1)&~1)-4;
WAVChunkHdr chunkHdr;
mStream->read( 4, &chunkHdr.id[0] );
mStream->read( &chunkHdr.size );
// Unread chunk data rounded up to nearest WORD.
S32 chunkRemaining = chunkHdr.size + ( chunkHdr.size & 1 );
WAVFmtHdr fmtHdr;
WAVFmtExHdr fmtExHdr;
WAVSmplHdr smplHdr;
dMemset(&fmtHdr, 0, sizeof(fmtHdr));
while ((fileHdr.size!=0) && (mStream->getStatus() != Stream::EOS))
{
// WAV format header chunk.
if ( !dStrncmp( (const char*)chunkHdr.id, "fmt ", 4 ) )
{
mStream->read(&fmtHdr.format);
mStream->read(&fmtHdr.channels);
mStream->read(&fmtHdr.samplesPerSec);
mStream->read(&fmtHdr.bytesPerSec);
mStream->read(&fmtHdr.blockAlign);
mStream->read(&fmtHdr.bitsPerSample);
if ( fmtHdr.format == 0x0001 )
{
mFormat.set( fmtHdr.channels, fmtHdr.bitsPerSample * fmtHdr.channels, fmtHdr.samplesPerSec );
chunkRemaining -= sizeof( WAVFmtHdr );
}
else
{
mStream->read(sizeof(WAVFmtExHdr), &fmtExHdr);
chunkRemaining -= sizeof(WAVFmtExHdr);
}
}
// WAV data chunk
else if (!dStrncmp((const char*)chunkHdr.id,"data",4))
{
// TODO: Handle these other formats in a more graceful manner!
if (fmtHdr.format==0x0001)
{
mDataStart = mStream->getPosition();
mStream->setPosition( mDataStart + chunkHdr.size );
chunkRemaining -= chunkHdr.size;
mSamples = chunkHdr.size / mFormat.getBytesPerSample();
}
else if (fmtHdr.format==0x0011)
{
//IMA ADPCM
}
else if (fmtHdr.format==0x0055)
{
//MP3 WAVE
}
}
// WAV sample header
else if (!dStrncmp((const char*)chunkHdr.id,"smpl",4))
{
// this struct read is NOT endian safe but it is ok because
// we are only testing the loops field against ZERO
mStream->read(sizeof(WAVSmplHdr), &smplHdr);
// This has never been hooked up and its usefulness is
// dubious. Do we really want the audio file overriding
// the SFXDescription setting?
//mLooping = ( smplHdr.loops ? true : false );
chunkRemaining -= sizeof(WAVSmplHdr);
}
// either we have unread chunk data or we found an unknown chunk type
// loop and read up to 1K bytes at a time until we have
// read to the end of this chunk
AssertFatal(chunkRemaining >= 0, "AudioBuffer::readWAV: remaining chunk data should never be less than zero.");
if ( chunkRemaining > 0 )
{
U32 pos = mStream->getPosition();
mStream->setPosition( pos + chunkRemaining );
chunkRemaining = 0;
}
fileHdr.size-=(((chunkHdr.size+1)&~1)+8);
// read next chunk header...
mStream->read(4, &chunkHdr.id[0]);
mStream->read(&chunkHdr.size);
// unread chunk data rounded up to nearest WORD
chunkRemaining = chunkHdr.size + (chunkHdr.size&1);
}
return ( mDataStart != -1 );
}
void SFXWavStream::reset()
{
AssertFatal( mStream, "SFXWavStream::reset() - Stream is null!" );
AssertFatal( mDataStart != -1, "SFXWavStream::seek() - Data start offset is invalid!" );
mStream->setPosition( mDataStart );
}
U32 SFXWavStream::getPosition() const
{
AssertFatal( mStream, "SFXWavStream::getPosition() - Stream is null!" );
return ( mStream->getPosition() - mDataStart );
}
void SFXWavStream::setPosition( U32 offset )
{
AssertFatal( mStream, "SFXWavStream::setPosition() - Stream is null!" );
offset -= offset % mFormat.getBytesPerSample();
const U32 dataLength = mSamples * mFormat.getBytesPerSample();
if( offset > dataLength )
offset = dataLength;
AssertFatal( mDataStart != -1, "SFXWavStream::getPosition() - Data start offset is invalid!" );
U32 byte = mDataStart + offset;
mStream->setPosition( byte );
}
U32 SFXWavStream::read( U8 *buffer, U32 bytes )
{
AssertFatal( mStream, "SFXWavStream::seek() - Stream is null!" );
// Read in even sample chunks.
bytes -= bytes % mFormat.getBytesPerSample();
// Read the data and determine how much we've read.
// FileStreams apparently report positions past
// the actual stream length, so manually cap the
// numbers here.
const U32 oldPosition = mStream->getPosition();
mStream->read( bytes, buffer );
U32 newPosition = mStream->getPosition();
const U32 maxPosition = getDataLength() + mDataStart;
if( newPosition > maxPosition )
newPosition = maxPosition;
const U32 numBytesRead = newPosition - oldPosition;
// TODO: Is it *just* 16 bit samples that needs to
// be flipped? What about 32 bit samples?
#ifdef TORQUE_BIG_ENDIAN
// We need to endian-flip 16-bit data.
if ( getFormat().getBytesPerChannel() == 2 )
{
U16 *ds = (U16*)buffer;
U16 *de = (U16*)(buffer+bytes);
while (ds<de)
{
*ds = convertLEndianToHost(*ds);
ds++;
}
}
#endif
return numBytesRead;
}

View file

@ -0,0 +1,80 @@
//-----------------------------------------------------------------------------
// Copyright (c) 2012 GarageGames, LLC
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to
// deal in the Software without restriction, including without limitation the
// rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
// sell copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
// IN THE SOFTWARE.
//-----------------------------------------------------------------------------
#ifndef _SFXWAVSTREAM_H_
#define _SFXWAVSTREAM_H_
#ifndef _SFXFILESTREAM_H_
#include "sfx/sfxFileStream.h"
#endif
#include "core/util/safeDelete.h"
/// An SFXFileStream that loads sample data from a WAV file.
class SFXWavStream : public SFXFileStream,
public IPositionable< U32 >
{
public:
typedef SFXFileStream Parent;
protected:
/// The file position of the start of
/// the PCM data for fast reset().
U32 mDataStart;
// SFXFileStream
virtual bool _readHeader();
virtual void _close();
public:
///
static SFXWavStream* create( Stream *stream );
///
SFXWavStream();
///
SFXWavStream( const SFXWavStream& cloneFrom );
/// Destructor.
virtual ~SFXWavStream();
// SFXStream
virtual void reset();
virtual U32 read( U8 *buffer, U32 length );
virtual SFXStream* clone() const
{
SFXWavStream* stream = new SFXWavStream( *this );
if( !stream->mStream )
SAFE_DELETE( stream );
return stream;
}
// IPositionable
virtual U32 getPosition() const;
virtual void setPosition( U32 offset );
};
#endif // _SFXWAVSTREAM_H_