- use size_t in Audio::DataWindow and be more strict in memory management
This commit is contained in:
@@ -9,11 +9,7 @@
|
|||||||
using namespace Audio;
|
using namespace Audio;
|
||||||
|
|
||||||
DataWindow::DataWindow()
|
DataWindow::DataWindow()
|
||||||
{
|
{}
|
||||||
mFilled = 0;
|
|
||||||
mData = nullptr;
|
|
||||||
mCapacity = 0;
|
|
||||||
}
|
|
||||||
|
|
||||||
DataWindow::~DataWindow()
|
DataWindow::~DataWindow()
|
||||||
{
|
{
|
||||||
@@ -24,10 +20,13 @@ DataWindow::~DataWindow()
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void DataWindow::setCapacity(int capacity)
|
void DataWindow::setCapacity(size_t capacity)
|
||||||
{
|
{
|
||||||
Lock l(mMutex);
|
Lock l(mMutex);
|
||||||
int tail = capacity - mCapacity;
|
|
||||||
|
if (capacity >= mCapacity)
|
||||||
|
{
|
||||||
|
size_t tail = capacity - mCapacity;
|
||||||
char* buffer = mData;
|
char* buffer = mData;
|
||||||
mData = (char*)realloc(mData, capacity);
|
mData = (char*)realloc(mData, capacity);
|
||||||
if (!mData)
|
if (!mData)
|
||||||
@@ -39,9 +38,12 @@ void DataWindow::setCapacity(int capacity)
|
|||||||
if (tail > 0)
|
if (tail > 0)
|
||||||
memset(mData + mCapacity, 0, tail);
|
memset(mData + mCapacity, 0, tail);
|
||||||
mCapacity = capacity;
|
mCapacity = capacity;
|
||||||
|
}
|
||||||
|
else
|
||||||
|
throw std::bad_alloc();
|
||||||
}
|
}
|
||||||
|
|
||||||
void DataWindow::addZero(int length)
|
void DataWindow::addZero(size_t length)
|
||||||
{
|
{
|
||||||
Lock l(mMutex);
|
Lock l(mMutex);
|
||||||
|
|
||||||
@@ -60,7 +62,7 @@ void DataWindow::addZero(int length)
|
|||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
void DataWindow::add(const void* data, int length)
|
void DataWindow::add(const void* data, size_t length)
|
||||||
{
|
{
|
||||||
Lock l(mMutex);
|
Lock l(mMutex);
|
||||||
|
|
||||||
@@ -94,7 +96,7 @@ void DataWindow::add(short sample)
|
|||||||
add(&sample, sizeof sample);
|
add(&sample, sizeof sample);
|
||||||
}
|
}
|
||||||
|
|
||||||
void DataWindow::erase(int length)
|
void DataWindow::erase(size_t length)
|
||||||
{
|
{
|
||||||
Lock l(mMutex);
|
Lock l(mMutex);
|
||||||
if (length > mFilled)
|
if (length > mFilled)
|
||||||
@@ -120,21 +122,21 @@ void DataWindow::clear()
|
|||||||
mFilled = 0;
|
mFilled = 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
short DataWindow::shortAt(int index) const
|
short DataWindow::shortAt(size_t index) const
|
||||||
{
|
{
|
||||||
Lock l(mMutex);
|
Lock l(mMutex);
|
||||||
assert(index < mFilled / 2);
|
assert(index < mFilled / 2);
|
||||||
return ((short*)mData)[index];
|
return ((short*)mData)[index];
|
||||||
}
|
}
|
||||||
|
|
||||||
void DataWindow::setShortAt(short value, int index)
|
void DataWindow::setShortAt(short value, size_t index)
|
||||||
{
|
{
|
||||||
Lock l(mMutex);
|
Lock l(mMutex);
|
||||||
assert(index < mFilled / 2);
|
assert(index < mFilled / 2);
|
||||||
((short*)mData)[index] = value;
|
((short*)mData)[index] = value;
|
||||||
}
|
}
|
||||||
|
|
||||||
int DataWindow::read(void* buffer, int length)
|
size_t DataWindow::read(void* buffer, size_t length)
|
||||||
{
|
{
|
||||||
Lock l(mMutex);
|
Lock l(mMutex);
|
||||||
if (length > mFilled)
|
if (length > mFilled)
|
||||||
@@ -150,25 +152,27 @@ int DataWindow::read(void* buffer, int length)
|
|||||||
return length;
|
return length;
|
||||||
}
|
}
|
||||||
|
|
||||||
int DataWindow::filled() const
|
size_t DataWindow::filled() const
|
||||||
{
|
{
|
||||||
Lock l(mMutex);
|
Lock l(mMutex);
|
||||||
return mFilled;
|
return mFilled;
|
||||||
}
|
}
|
||||||
|
|
||||||
void DataWindow::setFilled(int filled)
|
void DataWindow::setFilled(size_t filled)
|
||||||
{
|
{
|
||||||
Lock l(mMutex);
|
Lock l(mMutex);
|
||||||
|
if (filled > mCapacity)
|
||||||
|
throw std::bad_alloc();
|
||||||
mFilled = filled;
|
mFilled = filled;
|
||||||
}
|
}
|
||||||
|
|
||||||
int DataWindow::capacity() const
|
size_t DataWindow::capacity() const
|
||||||
{
|
{
|
||||||
Lock l(mMutex);
|
Lock l(mMutex);
|
||||||
return mCapacity;
|
return mCapacity;
|
||||||
}
|
}
|
||||||
|
|
||||||
void DataWindow::zero(int length)
|
void DataWindow::zero(size_t length)
|
||||||
{
|
{
|
||||||
Lock l(mMutex);
|
Lock l(mMutex);
|
||||||
assert(length <= mCapacity);
|
assert(length <= mCapacity);
|
||||||
@@ -189,10 +193,10 @@ size_t DataWindow::moveTo(DataWindow& dst, size_t size)
|
|||||||
return avail;
|
return avail;
|
||||||
}
|
}
|
||||||
|
|
||||||
std::chrono::milliseconds DataWindow::getTimeLength(int samplerate, int channels) const
|
std::chrono::milliseconds DataWindow::getTimeLength(const Audio::Format& fmt) const
|
||||||
{
|
{
|
||||||
Lock l(mMutex);
|
Lock l(mMutex);
|
||||||
return std::chrono::milliseconds(mFilled / sizeof(short) / channels / (samplerate / 1000));
|
return std::chrono::milliseconds(mFilled / sizeof(short) / fmt.channels() / (fmt.rate()/ 1000));
|
||||||
}
|
}
|
||||||
|
|
||||||
void DataWindow::makeStereoFromMono(DataWindow& dst, DataWindow& src)
|
void DataWindow::makeStereoFromMono(DataWindow& dst, DataWindow& src)
|
||||||
|
|||||||
@@ -8,6 +8,7 @@
|
|||||||
|
|
||||||
#include "../helper/HL_ByteBuffer.h"
|
#include "../helper/HL_ByteBuffer.h"
|
||||||
#include "../helper/HL_Sync.h"
|
#include "../helper/HL_Sync.h"
|
||||||
|
#include "Audio_Interface.h"
|
||||||
|
|
||||||
namespace Audio
|
namespace Audio
|
||||||
{
|
{
|
||||||
@@ -17,34 +18,34 @@ public:
|
|||||||
DataWindow();
|
DataWindow();
|
||||||
~DataWindow();
|
~DataWindow();
|
||||||
|
|
||||||
void setCapacity(int capacity);
|
void setCapacity(size_t capacity);
|
||||||
int capacity() const;
|
size_t capacity() const;
|
||||||
|
|
||||||
void addZero(int length);
|
void addZero(size_t length);
|
||||||
void add(const void* data, int length);
|
void add(const void* data, size_t length);
|
||||||
void add(short sample);
|
void add(short sample);
|
||||||
int read(void* buffer, int length);
|
size_t read(void* buffer, size_t length);
|
||||||
void erase(int length = -1);
|
void erase(size_t length);
|
||||||
const char* data() const;
|
const char* data() const;
|
||||||
char* mutableData();
|
char* mutableData();
|
||||||
int filled() const;
|
size_t filled() const;
|
||||||
void setFilled(int filled);
|
void setFilled(size_t filled);
|
||||||
void clear();
|
void clear();
|
||||||
|
|
||||||
short shortAt(int index) const;
|
short shortAt(size_t index) const;
|
||||||
void setShortAt(short value, int index);
|
void setShortAt(short value, size_t index);
|
||||||
void zero(int length);
|
void zero(size_t length);
|
||||||
size_t moveTo(DataWindow& dst, size_t size);
|
size_t moveTo(DataWindow& dst, size_t size /* in bytes*/ );
|
||||||
|
|
||||||
std::chrono::milliseconds getTimeLength(int samplerate, int channels) const;
|
std::chrono::milliseconds getTimeLength(const Format& fmt) const;
|
||||||
|
|
||||||
static void makeStereoFromMono(DataWindow& dst, DataWindow& src);
|
static void makeStereoFromMono(DataWindow& dst, DataWindow& src);
|
||||||
|
|
||||||
protected:
|
protected:
|
||||||
mutable Mutex mMutex;
|
mutable Mutex mMutex;
|
||||||
char* mData;
|
char* mData = nullptr;
|
||||||
int mFilled;
|
size_t mFilled = 0;
|
||||||
int mCapacity;
|
size_t mCapacity = 0;
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
#endif
|
#endif
|
||||||
|
|||||||
Reference in New Issue
Block a user