mirror of
https://github.com/TorqueGameEngines/Torque3D.git
synced 2026-01-19 20:24:49 +00:00
replace other classes
now all sfxFileStreams create a libsndfile stream class. At the moment we only read as short
This commit is contained in:
parent
147044796b
commit
0342eb6f31
|
|
@ -38,7 +38,7 @@
|
|||
#include "sfx/sfxMemoryStream.h"
|
||||
#include "sfx/sfxSound.h"
|
||||
#ifdef SPLIT_VORBIS
|
||||
#include "sfx/media/sfxVorbisStream.h"
|
||||
#include "sfx/media/sfxSndStream.h"
|
||||
#endif
|
||||
|
||||
#include "core/stream/fileStream.h"
|
||||
|
|
@ -524,7 +524,7 @@ bool TheoraTexture::setFile( const String& filename, SFXDescription* desc )
|
|||
stream = FileStream::createAndOpen( filename, Torque::FS::File::Read );
|
||||
if( stream )
|
||||
{
|
||||
ThreadSafeRef< SFXStream > vorbisStream = SFXVorbisStream::create( stream );
|
||||
ThreadSafeRef< SFXStream > vorbisStream = SFXSndStream::create( stream );
|
||||
if( !vorbisStream )
|
||||
{
|
||||
Con::errorf( "TheoraTexture - could not create Vorbis stream for '%s'", filename.c_str() );
|
||||
|
|
|
|||
|
|
@ -64,11 +64,12 @@ bool SFXSndStream::_readHeader()
|
|||
bitsPerSample = 24;
|
||||
break;
|
||||
case SF_FORMAT_PCM_32:
|
||||
case SF_FORMAT_FLOAT:
|
||||
bitsPerSample = 32;
|
||||
break;
|
||||
default:
|
||||
// Other formats not supported or not recognized
|
||||
bitsPerSample = -1;
|
||||
// missed, set it to 16 anyway.
|
||||
bitsPerSample = 16;
|
||||
break;
|
||||
}
|
||||
|
||||
|
|
@ -107,22 +108,27 @@ U32 SFXSndStream::read(U8* buffer, U32 length)
|
|||
U32 framesRead = 0;
|
||||
|
||||
framesRead = sf_readf_short(sndFile, (short*)buffer, sfinfo.frames);
|
||||
if (framesRead != sfinfo.frames)
|
||||
{
|
||||
Con::errorf("SFXSndStream - read: %s", sf_strerror(sndFile));
|
||||
}
|
||||
|
||||
return framesRead;
|
||||
}
|
||||
|
||||
bool SFXSndStream::isEOS() const
|
||||
{
|
||||
return false;
|
||||
return (Parent::isEOS() || (mStream && vio_data.length == vio_data.offset));
|
||||
}
|
||||
|
||||
U32 SFXSndStream::getPosition() const
|
||||
{
|
||||
return U32();
|
||||
return vio_data.offset;
|
||||
}
|
||||
|
||||
void SFXSndStream::setPosition(U32 offset)
|
||||
{
|
||||
sf_seek(sndFile, offset / mFormat.getBytesPerSample(), SEEK_SET);
|
||||
}
|
||||
|
||||
sf_count_t SFXSndStream::sndSeek(sf_count_t offset, int whence, void* user_data)
|
||||
|
|
|
|||
|
|
@ -1,280 +0,0 @@
|
|||
//-----------------------------------------------------------------------------
|
||||
// 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 ),
|
||||
mBitstream(-1),
|
||||
mBytesRead( 0 )
|
||||
{
|
||||
}
|
||||
|
||||
SFXVorbisStream::SFXVorbisStream( const SFXVorbisStream& cloneFrom )
|
||||
: Parent( cloneFrom )
|
||||
{
|
||||
mVF = NULL;
|
||||
mBitstream = -1;
|
||||
mBytesRead = 0;
|
||||
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((U32)(size * nmemb), ptr );
|
||||
U32 endByte = stream->getPosition();
|
||||
|
||||
// How many did we actually read?
|
||||
U32 readBytes = ( endByte - startByte );
|
||||
U32 readItems = (U32)(readBytes / size);
|
||||
|
||||
return readItems;
|
||||
}
|
||||
|
||||
S32 SFXVorbisStream::_seek_func( void *datasource, ogg_int64_t offset, S32 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()
|
||||
{
|
||||
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.
|
||||
S32 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 S32 isBigEndian = 1;
|
||||
#else
|
||||
static const S32 isBigEndian = 0;
|
||||
#endif
|
||||
|
||||
// Vorbis doesn't seem to like reading
|
||||
// requests longer than this.
|
||||
const U32 MAXREAD = 4096;
|
||||
|
||||
S64 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
|
||||
|
|
@ -1,112 +0,0 @@
|
|||
//-----------------------------------------------------------------------------
|
||||
// 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 S32 _seek_func( void *datasource, ogg_int64_t offset, S32 whence );
|
||||
static long _tell_func( void *datasource );
|
||||
|
||||
// SFXStream
|
||||
bool _readHeader() override;
|
||||
void _close() override;
|
||||
|
||||
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
|
||||
void reset() override;
|
||||
U32 read( U8 *buffer, U32 length ) override;
|
||||
bool isEOS() const override;
|
||||
SFXStream* clone() const override
|
||||
{
|
||||
SFXVorbisStream* stream = new SFXVorbisStream( *this );
|
||||
if( !stream->mVF )
|
||||
SAFE_DELETE( stream );
|
||||
return stream;
|
||||
}
|
||||
|
||||
// IPositionable
|
||||
U32 getPosition() const override;
|
||||
void setPosition( U32 offset ) override;
|
||||
};
|
||||
|
||||
#endif // TORQUE_OGGVORBIS
|
||||
#endif // _SFXVORBISSTREAM_H_
|
||||
|
|
@ -1,300 +0,0 @@
|
|||
//-----------------------------------------------------------------------------
|
||||
// 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()
|
||||
:mDataStart(-1)
|
||||
{
|
||||
}
|
||||
|
||||
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;
|
||||
}
|
||||
|
|
@ -1,80 +0,0 @@
|
|||
//-----------------------------------------------------------------------------
|
||||
// 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
|
||||
bool _readHeader() override;
|
||||
void _close() override;
|
||||
|
||||
public:
|
||||
|
||||
///
|
||||
static SFXWavStream* create( Stream *stream );
|
||||
|
||||
///
|
||||
SFXWavStream();
|
||||
|
||||
///
|
||||
SFXWavStream( const SFXWavStream& cloneFrom );
|
||||
|
||||
/// Destructor.
|
||||
virtual ~SFXWavStream();
|
||||
|
||||
// SFXStream
|
||||
void reset() override;
|
||||
U32 read( U8 *buffer, U32 length ) override;
|
||||
SFXStream* clone() const override
|
||||
{
|
||||
SFXWavStream* stream = new SFXWavStream( *this );
|
||||
if( !stream->mStream )
|
||||
SAFE_DELETE( stream );
|
||||
return stream;
|
||||
}
|
||||
|
||||
// IPositionable
|
||||
U32 getPosition() const override;
|
||||
void setPosition( U32 offset ) override;
|
||||
};
|
||||
|
||||
#endif // _SFXWAVSTREAM_H_
|
||||
|
|
@ -24,75 +24,29 @@
|
|||
#include "core/stream/fileStream.h"
|
||||
#include "console/console.h"
|
||||
#include "core/util/safeDelete.h"
|
||||
|
||||
|
||||
SFXFileStream::ExtensionsVector SFXFileStream::smExtensions( __FILE__, __LINE__ );
|
||||
SFXFileStream::CreateFnsVector SFXFileStream::smCreateFns( __FILE__, __LINE__ );
|
||||
|
||||
|
||||
void SFXFileStream::registerExtension( String ext, SFXFILESTREAM_CREATE_FN create_fn )
|
||||
{
|
||||
// Register the stream creation first.
|
||||
smExtensions.push_back( ext );
|
||||
smCreateFns.push_back( create_fn );
|
||||
}
|
||||
|
||||
void SFXFileStream::unregisterExtension( String ext )
|
||||
{
|
||||
for( ExtensionsVector::iterator iter = smExtensions.begin();
|
||||
iter != smExtensions.end(); ++ iter )
|
||||
if( ( *iter ).equal( ext, String::NoCase ) )
|
||||
{
|
||||
smExtensions.erase( iter );
|
||||
return;
|
||||
}
|
||||
}
|
||||
#include "sfx/media/sfxSndStream.h"
|
||||
|
||||
SFXFileStream* SFXFileStream::create( String filename )
|
||||
{
|
||||
//RDTODO: if original file has an extension, we should try that first
|
||||
|
||||
// First strip off our current extension (validating
|
||||
// against a list of known extensions so that we don't
|
||||
// strip off the last part of a file name with a dot in it.
|
||||
SFXFileStream *sfxStream = NULL;
|
||||
|
||||
String noExtension = Platform::stripExtension( filename, smExtensions );
|
||||
Stream *stream = FileStream::createAndOpen(filename, Torque::FS::File::Read );
|
||||
if ( !stream )
|
||||
return NULL;
|
||||
|
||||
SFXFileStream *sfxStream = NULL;
|
||||
|
||||
for( U32 i = 0; i < smExtensions.size(); i++ )
|
||||
{
|
||||
String testName = noExtension + smExtensions[ i ];
|
||||
|
||||
Stream *stream = FileStream::createAndOpen( testName, Torque::FS::File::Read );
|
||||
if ( !stream )
|
||||
continue;
|
||||
|
||||
// Note that the creation function swallows up the
|
||||
// resource stream and will take care of deleting it.
|
||||
sfxStream = smCreateFns[i]( stream );
|
||||
if ( sfxStream )
|
||||
return sfxStream;
|
||||
}
|
||||
// Note that the creation function swallows up the
|
||||
// resource stream and will take care of deleting it.
|
||||
sfxStream = SFXSndStream::create( stream );
|
||||
if ( sfxStream )
|
||||
return sfxStream;
|
||||
|
||||
return NULL;
|
||||
}
|
||||
|
||||
bool SFXFileStream::exists( String filename )
|
||||
{
|
||||
// First strip off our current extension (validating
|
||||
// against a list of known extensions so that we don't
|
||||
// strip off the last part of a file name with a dot in it.
|
||||
|
||||
String noExtension = Platform::stripExtension( filename, smExtensions );
|
||||
|
||||
for( U32 i = 0; i < smExtensions.size(); i++ )
|
||||
{
|
||||
String testName = noExtension + smExtensions[ i ];
|
||||
if( Torque::FS::IsFile( testName ) )
|
||||
if( Torque::FS::IsFile(filename) )
|
||||
return true;
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -44,12 +44,6 @@ typedef SFXFileStream* ( *SFXFILESTREAM_CREATE_FN )( Stream *stream );
|
|||
class SFXFileStream : public SFXStream
|
||||
{
|
||||
protected:
|
||||
typedef Vector< String > ExtensionsVector;
|
||||
typedef Vector< SFXFILESTREAM_CREATE_FN > CreateFnsVector;
|
||||
|
||||
static ExtensionsVector smExtensions;
|
||||
static CreateFnsVector smCreateFns;
|
||||
|
||||
/// The file stream we're reading from.
|
||||
Stream *mStream;
|
||||
|
||||
|
|
@ -78,13 +72,6 @@ class SFXFileStream : public SFXStream
|
|||
virtual void _close() = 0;
|
||||
|
||||
public:
|
||||
|
||||
///
|
||||
static void registerExtension( String ext, SFXFILESTREAM_CREATE_FN create_fn );
|
||||
|
||||
///
|
||||
static void unregisterExtension( String ext );
|
||||
|
||||
/// This is a helper function used to create an appropriate SFXStream
|
||||
/// for the requested sound file.
|
||||
///
|
||||
|
|
|
|||
|
|
@ -43,12 +43,7 @@
|
|||
#include "core/util/autoPtr.h"
|
||||
#include "core/module.h"
|
||||
|
||||
#include "sfx/media/sfxWavStream.h"
|
||||
#ifdef TORQUE_OGGVORBIS
|
||||
#include "sfx/media/sfxVorbisStream.h"
|
||||
#endif
|
||||
|
||||
#include "sfx/media/sfxSndStream.h"
|
||||
|
||||
|
||||
MODULE_BEGIN( SFX )
|
||||
|
|
@ -352,13 +347,6 @@ void SFXSystem::init()
|
|||
AssertWarn( smSingleton == NULL, "SFX has already been initialized!" );
|
||||
|
||||
SFXProvider::initializeAllProviders();
|
||||
|
||||
// Register the streams and resources. Note that
|
||||
// the order here does matter!
|
||||
SFXFileStream::registerExtension( ".wav", ( SFXFILESTREAM_CREATE_FN ) SFXWavStream::create );
|
||||
#ifdef TORQUE_OGGVORBIS
|
||||
SFXFileStream::registerExtension( ".ogg", ( SFXFILESTREAM_CREATE_FN ) SFXSndStream::create );
|
||||
#endif
|
||||
|
||||
// Create the stream thread pool.
|
||||
|
||||
|
|
@ -378,11 +366,6 @@ void SFXSystem::destroy()
|
|||
{
|
||||
AssertWarn( smSingleton != NULL, "SFX has not been initialized!" );
|
||||
|
||||
SFXFileStream::unregisterExtension( ".wav" );
|
||||
#ifdef TORQUE_OGGVORBIS
|
||||
SFXFileStream::unregisterExtension( ".ogg" );
|
||||
#endif
|
||||
|
||||
delete smSingleton;
|
||||
smSingleton = NULL;
|
||||
|
||||
|
|
|
|||
Loading…
Reference in a new issue