A few small bug fixes related to bad pointer arithmetic

This commit is contained in:
casey langen 2016-11-27 21:38:27 -08:00
parent d28cfcfd52
commit 72eb1cb74d
7 changed files with 43 additions and 50 deletions

View File

@ -313,17 +313,17 @@ char *strchr(), *strrchr();
#if defined(_WIN32) && !defined(__MINGW32__)
#define HAS_LRINTF
static INLINE int lrintf(float f)
{
int i;
__asm
{
fld f
fistp i
}
return i;
}
//#define HAS_LRINTF
//static INLINE int lrintf(float f)
//{
// int i;
// __asm
// {
// fld f
// fistp i
// }
// return i;
//}
#elif (defined(__i386__) && defined(__GNUC__) && \
!defined(__CYGWIN__) && !defined(__MINGW32__))
#ifndef HAVE_LRINTF

View File

@ -57,7 +57,6 @@ Buffer::Buffer(void)
, internalBufferSize(0)
, sampleRate(44100)
, channels(2) {
}
Buffer::~Buffer() {
@ -105,11 +104,7 @@ void Buffer::CopyFormat(BufferPtr fromBuffer) {
void Buffer::ResizeBuffer() {
if (this->sampleSize > this->internalBufferSize) {
if (this->buffer) {
delete[] this->buffer;
this->buffer = nullptr;
}
delete[] this->buffer;
this->buffer = new float[this->sampleSize];
this->internalBufferSize = this->sampleSize;
}
@ -145,42 +140,31 @@ void Buffer::Copy(float* buffer, long samples) {
void Buffer::Append(float* src, long samples) {
/* number of floats (not bytes) in buffer */
long newBufferSize = (this->Samples() + samples);
long newBufferSize = this->sampleSize + samples;
if (newBufferSize > this->internalBufferSize) { /* resize, then copy, if too small */
if (newBufferSize > this->internalBufferSize) {
/* resize, then copy, if too small */
float *newBuffer = new float[newBufferSize];
CopyFloat(newBuffer, this->buffer, this->sampleSize);
float *dst = &newBuffer[this->sampleSize];
CopyFloat(dst, src, samples);
CopyFloat(newBuffer + this->sampleSize, src, samples);
delete[] this->buffer;
this->buffer = newBuffer;
this->internalBufferSize = newBufferSize;
}
else { /* append, no resize required */
float *dst = &this->buffer[this->sampleSize];
CopyFloat(dst, src, samples);
else {
/* append, no resize required */
CopyFloat(this->buffer + this->sampleSize, src, samples);
this->internalBufferSize += samples;
}
this->sampleSize = newBufferSize;
}
bool Buffer::Append(BufferPtr appendBuffer) {
if (this->SampleRate() == appendBuffer->SampleRate() &&
this->Channels() == appendBuffer->Channels())
{
this->Append(appendBuffer->BufferPointer(), appendBuffer->Samples());
return true;
}
return false;
}
bool Buffer::Fft(float* buffer, int size) {
if (this->sampleSize < FFT_BUFFER_SIZE || size != FFT_BUFFER_SIZE) {
if (this->sampleSize / this->channels < FFT_BUFFER_SIZE || size != FFT_BUFFER_SIZE) {
return false;
}
@ -188,8 +172,9 @@ bool Buffer::Fft(float* buffer, int size) {
/* de-interleave the audio first */
float* deinterleaved = new float[FFT_BUFFER_SIZE * count];
int to;
for (int i = 0; i < count * FFT_BUFFER_SIZE; i++) {
const int to = ((i % this->channels) * FFT_BUFFER_SIZE) + (i / count);
to = ((i % this->channels) * FFT_BUFFER_SIZE) + (i / count);
deinterleaved[to] = this->buffer[i];
}
@ -207,7 +192,7 @@ bool Buffer::Fft(float* buffer, int size) {
fft_perform(this->buffer, buffer, state);
for (int i = 1; i < count; i++) {
fft_perform(&deinterleaved[i * FFT_BUFFER_SIZE], scratch, state);
fft_perform(deinterleaved + (i * FFT_BUFFER_SIZE), scratch, state);
/* average with the previous read */
for (int j = 0; j < FFT_BUFFER_SIZE; j++) {

View File

@ -67,7 +67,6 @@ namespace musik { namespace core { namespace audio {
void SetPosition(double position);
void Copy(float* buffer, long samples);
void Append(float* buffer, long samples);
bool Append(BufferPtr appendBuffer);
void CopyFormat(BufferPtr fromBuffer);
private:

View File

@ -46,12 +46,11 @@ using musik::core::PluginFactory;
static std::string TAG = "DynamicStream";
DynamicStream::DynamicStream(unsigned int options)
: preferedBufferSampleSize(4096)
, options(options)
, decoderSampleRate(0)
, decoderChannels(0)
, decoderSamplePosition(0)
{
: preferedBufferSampleSize(4096)
, options(options)
, decoderSampleRate(0)
, decoderChannels(0)
, decoderSamplePosition(0) {
if ((this->options & NoDSP) == 0) {
typedef PluginFactory::DestroyDeleter<IDSP> Deleter;
this->dsps = PluginFactory::Instance().QueryInterface<IDSP, Deleter>("GetDSP");
@ -164,13 +163,18 @@ BufferPtr DynamicStream::GetNextProcessedOutputBuffer() {
BufferPtr currentBuffer = this->GetNextBufferFromDecoder();
if (currentBuffer) {
void * f = &free;
/* try to fill the buffer to its optimal size; if the decoder didn't return
a full buffer, ask it for some more data. */
bool moreBuffers = true;
while (currentBuffer->Samples() < this->preferedBufferSampleSize && moreBuffers) {
BufferPtr bufferToAppend = this->GetNextBufferFromDecoder();
if (bufferToAppend) {
currentBuffer->Append(bufferToAppend);
currentBuffer->Append(
bufferToAppend->BufferPointer(),
bufferToAppend->Samples());
this->RecycleBuffer(bufferToAppend);
}
else {
@ -184,7 +188,7 @@ BufferPtr DynamicStream::GetNextProcessedOutputBuffer() {
for (Dsps::iterator dsp = this->dsps.begin(); dsp != this->dsps.end(); ++dsp) {
oldBuffer->CopyFormat(currentBuffer);
currentBuffer->SetPosition(oldBuffer->Position());
oldBuffer->SetPosition(currentBuffer->Position());
if ((*dsp)->Process(currentBuffer.get(), oldBuffer.get())) {
currentBuffer.swap(oldBuffer);

View File

@ -52,7 +52,7 @@ static std::string TAG = "FixedSizeStream";
((double) this->decoderSampleRate));
#define COPY_BUFFER(target, current, count, offset) \
target->Copy(&current->BufferPointer()[offset], count); \
target->Copy(current->BufferPointer() + offset, count); \
SET_OFFSET(target, offset) \
FixedSizeStream::FixedSizeStream(int samplesPerChannel, int bufferCount, unsigned int options)

View File

@ -138,8 +138,12 @@ int Player::State() {
}
void Player::ThreadLoop() {
/* create and open the stream */
this->stream = FixedSizeStream::Create();
/* fixed size streams are better for visualizations because they provide
consistent buffer sizes. dynamic streams are more efficient otherwise
because we don't need to worry about manually chunking data, we can just
send audio data to the output straight from the decoder. */
this->stream = vis::SelectedVisualizer()
? FixedSizeStream::Create() : DynamicStream::Create();
BufferPtr buffer;

View File

@ -84,6 +84,7 @@ Indexer::Indexer(const std::string& libraryPath, const std::string& dbFilename)
: thread(nullptr)
, restart(false)
, filesSaved(0)
, exit(false)
, prefs(Preferences::ForComponent(prefs::components::Settings))
, readSemaphore(prefs->GetInt(prefs::keys::MaxTagReadThreads, MAX_THREADS)) {
this->dbFilename = dbFilename;