mirror of
https://github.com/TorqueGameEngines/Torque3D.git
synced 2026-01-19 20:24:49 +00:00
while it still remains a good idea to port as many NULL compares and assignments over to nullPtr as feasable, we do still need to sort out how to better support scripted empty, false, and zero assigns for things like objectIDs. this means we'll need to both fully convert the backend of the parser to support that kind of thing, but also alter most if not all exisiting NULLs. up to and including things like SAFE_DELETE. while that's certainly feasable, given there's aproximatel 400 nullptr assigns/checks prior to this commit, and roughly 1800 of the prior, if it terminates in a script call and not an aip one direct, we'll be dialing that back until such time as fork fully fopcused on converting and resolving any lingering mismatches is completed.
225 lines
5.6 KiB
C++
225 lines
5.6 KiB
C++
//-----------------------------------------------------------------------------
|
|
// 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/sfxSndStream.h"
|
|
#include "core/stream/fileStream.h"
|
|
#include "core/stream/stream.h"
|
|
#include "console/console.h"
|
|
|
|
#define MAX_BUFFER 4096
|
|
|
|
bool SFXSndStream::_readHeader()
|
|
{
|
|
mCurPos = 0;
|
|
mBytesRead = 0;
|
|
|
|
dMemset(&sfinfo, 0, sizeof(SF_INFO));
|
|
|
|
vio.get_filelen = sndFileLen;
|
|
vio.seek = sndSeek;
|
|
vio.read = sndRead;
|
|
vio.write = sndWrite;
|
|
vio.tell = sndTell;
|
|
|
|
vio_data.length = 0;
|
|
vio_data.offset = 0;
|
|
vio_data.data = mStream;
|
|
|
|
if ((sndFile = sf_open_virtual(&vio, SFM_READ, &sfinfo, &vio_data)) == NULL)
|
|
{
|
|
Con::printf("SFXSndStream - _readHeader failed: %s", sf_strerror(sndFile));
|
|
return false;
|
|
}
|
|
|
|
S32 bitsPerSample = 0;
|
|
switch ((sfinfo.format & SF_FORMAT_SUBMASK))
|
|
{
|
|
case SF_FORMAT_PCM_S8:
|
|
case SF_FORMAT_PCM_U8:
|
|
bitsPerSample = 8;
|
|
break;
|
|
case SF_FORMAT_PCM_16:
|
|
bitsPerSample = 16;
|
|
break;
|
|
case SF_FORMAT_VORBIS:
|
|
case SF_FORMAT_PCM_24:
|
|
case SF_FORMAT_PCM_32:
|
|
case SF_FORMAT_FLOAT:
|
|
bitsPerSample = 16;
|
|
sf_command(sndFile, SFC_SET_SCALE_FLOAT_INT_READ, NULL, SF_TRUE);
|
|
break;
|
|
default:
|
|
// missed, set it to 16 anyway.
|
|
bitsPerSample = 16;
|
|
break;
|
|
}
|
|
|
|
mFormat.set(sfinfo.channels, bitsPerSample * sfinfo.channels, sfinfo.samplerate);
|
|
|
|
mSamples = sfinfo.frames;
|
|
|
|
return true;
|
|
}
|
|
|
|
void SFXSndStream::_close()
|
|
{
|
|
if (!sndFile)
|
|
return;
|
|
|
|
sf_close(sndFile);
|
|
}
|
|
|
|
SFXSndStream* SFXSndStream::create(Stream* stream)
|
|
{
|
|
SFXSndStream* sfxStream = new SFXSndStream();
|
|
if (sfxStream->open(stream, true))
|
|
return sfxStream;
|
|
|
|
delete sfxStream;
|
|
return NULL;
|
|
}
|
|
|
|
void SFXSndStream::reset()
|
|
{
|
|
vio_data.offset = 0;
|
|
}
|
|
|
|
U32 SFXSndStream::read(U8* buffer, U32 length)
|
|
{
|
|
if (!sndFile)
|
|
{
|
|
Con::errorf("SFXSndStream - read: Called on uninitialized stream.");
|
|
return 0;
|
|
}
|
|
|
|
U32 framesToRead = length / mFormat.getBytesPerSample();
|
|
U32 framesRead = 0;
|
|
|
|
switch (sfinfo.format & SF_FORMAT_SUBMASK)
|
|
{
|
|
case SF_FORMAT_PCM_S8:
|
|
case SF_FORMAT_PCM_U8:
|
|
framesRead = sf_readf_int(sndFile, (int*)buffer, framesToRead);
|
|
break;
|
|
case SF_FORMAT_PCM_16:
|
|
case SF_FORMAT_VORBIS:
|
|
case SF_FORMAT_PCM_24:
|
|
case SF_FORMAT_PCM_32:
|
|
case SF_FORMAT_FLOAT:
|
|
framesRead = sf_readf_short(sndFile, (short*)buffer, framesToRead);
|
|
break;
|
|
default:
|
|
Con::errorf("SFXSndStream - read: Unsupported format.");
|
|
return 0;
|
|
}
|
|
|
|
if (framesRead != framesToRead)
|
|
{
|
|
Con::errorf("SFXSndStream - read: %s", sf_strerror(sndFile));
|
|
}
|
|
|
|
// (convert to frames) - number of frames available < MAX_BUFFER? reset
|
|
if (((getPosition() / mFormat.getBytesPerSample()) - sfinfo.frames) < MAX_BUFFER)
|
|
{
|
|
// reset stream
|
|
setPosition(0);
|
|
}
|
|
|
|
|
|
return framesRead * mFormat.getBytesPerSample();
|
|
}
|
|
|
|
bool SFXSndStream::isEOS() const
|
|
{
|
|
return (Parent::isEOS() || (mStream && vio_data.length == vio_data.offset));
|
|
}
|
|
|
|
U32 SFXSndStream::getPosition() const
|
|
{
|
|
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)
|
|
{
|
|
VIO_DATA* vf = (VIO_DATA*)user_data;
|
|
Stream* stream = reinterpret_cast<Stream*>(vf->data);
|
|
|
|
switch (whence)
|
|
{
|
|
case SEEK_SET:
|
|
vf->offset = offset;
|
|
break;
|
|
|
|
case SEEK_CUR:
|
|
vf->offset = vf->offset + offset;
|
|
break;
|
|
|
|
case SEEK_END:
|
|
vf->offset = vf->length - offset;
|
|
break;
|
|
default:
|
|
break;
|
|
};
|
|
|
|
return stream->setPosition(vf->offset) ? 0 : -1;
|
|
}
|
|
|
|
sf_count_t SFXSndStream::sndRead(void* ptr, sf_count_t count, void* user_data)
|
|
{
|
|
VIO_DATA* vf = (VIO_DATA*)user_data;
|
|
Stream* stream = reinterpret_cast<Stream*>(vf->data);
|
|
|
|
if (vf->offset + count > vf->length)
|
|
count = vf->length - vf->offset;
|
|
|
|
stream->read((U32)(count), ptr);
|
|
vf->offset += count;
|
|
|
|
return count;
|
|
}
|
|
|
|
sf_count_t SFXSndStream::sndWrite(const void* ptr, sf_count_t count, void* user_data)
|
|
{
|
|
return sf_count_t();
|
|
}
|
|
|
|
sf_count_t SFXSndStream::sndTell(void* user_data)
|
|
{
|
|
VIO_DATA* vf = (VIO_DATA*)user_data;
|
|
return vf->offset;
|
|
}
|
|
|
|
sf_count_t SFXSndStream::sndFileLen(void* user_data)
|
|
{
|
|
VIO_DATA* vf = (VIO_DATA*)user_data;
|
|
Stream* stream = reinterpret_cast<Stream*>(vf->data);
|
|
|
|
vf->length = stream->getStreamSize();
|
|
|
|
return vf->length;
|
|
}
|
|
|