- minor cleanups
This commit is contained in:
parent
694ced4d25
commit
d535e5312b
|
|
@ -135,9 +135,9 @@ void DevicePair::onMicData(const Format& f, const void* buffer, int length)
|
||||||
for (int blockIndex = 0; blockIndex < blocks; blockIndex++)
|
for (int blockIndex = 0; blockIndex < blocks; blockIndex++)
|
||||||
{
|
{
|
||||||
|
|
||||||
int wasProcessed = 0;
|
size_t wasProcessed = 0;
|
||||||
|
|
||||||
int wasProduced = mMicResampler.resample(f.mRate, // Source rate
|
size_t wasProduced = mMicResampler.resample(f.mRate, // Source rate
|
||||||
mInputResampingData.data(), // Source data
|
mInputResampingData.data(), // Source data
|
||||||
(int)f.sizeFromTime(AUDIO_MIC_BUFFER_LENGTH), // Source size
|
(int)f.sizeFromTime(AUDIO_MIC_BUFFER_LENGTH), // Source size
|
||||||
wasProcessed,
|
wasProcessed,
|
||||||
|
|
@ -196,8 +196,8 @@ void DevicePair::onSpkData(const Format& f, void* buffer, int length)
|
||||||
mAecSpkBuffer.add(mOutput10msBuffer.data(), mOutput10msBuffer.capacity());
|
mAecSpkBuffer.add(mOutput10msBuffer.data(), mOutput10msBuffer.capacity());
|
||||||
|
|
||||||
// Resample these 10 milliseconds it to native format
|
// Resample these 10 milliseconds it to native format
|
||||||
int wasProcessed = 0;
|
size_t wasProcessed = 0;
|
||||||
int wasProduced = mSpkResampler.resample(AUDIO_SAMPLERATE, mOutput10msBuffer.data(), mOutput10msBuffer.capacity(), wasProcessed, f.mRate,
|
size_t wasProduced = mSpkResampler.resample(AUDIO_SAMPLERATE, mOutput10msBuffer.data(), mOutput10msBuffer.capacity(), wasProcessed, f.mRate,
|
||||||
mOutputNativeData.mutableData() + mOutputNativeData.filled(), mOutputNativeData.capacity() - mOutputNativeData.filled());
|
mOutputNativeData.mutableData() + mOutputNativeData.filled(), mOutputNativeData.capacity() - mOutputNativeData.filled());
|
||||||
mOutputNativeData.setFilled(mOutputNativeData.filled() + wasProduced);
|
mOutputNativeData.setFilled(mOutputNativeData.filled() + wasProduced);
|
||||||
#ifdef CONSOLE_LOGGING
|
#ifdef CONSOLE_LOGGING
|
||||||
|
|
|
||||||
|
|
@ -69,12 +69,12 @@ void Mixer::Stream::setActive(bool active)
|
||||||
void Mixer::Stream::addPcm(int rate, const void* input, int length)
|
void Mixer::Stream::addPcm(int rate, const void* input, int length)
|
||||||
{
|
{
|
||||||
// Resample to internal sample rate
|
// Resample to internal sample rate
|
||||||
unsigned outputSize = unsigned(0.5 + length * ((float)AUDIO_SAMPLERATE / rate));
|
size_t outputSize = size_t(0.5 + length * ((float)AUDIO_SAMPLERATE / rate));
|
||||||
if (mTempBuffer.size() < outputSize)
|
if (mTempBuffer.size() < outputSize)
|
||||||
mTempBuffer.resize(outputSize);
|
mTempBuffer.resize(outputSize);
|
||||||
|
|
||||||
Resampler* resampler = (rate == 8000) ? &mResampler8 : ((rate == 16000) ? &mResampler16 : ((rate == 32000) ? &mResampler32 : &mResampler48));
|
Resampler* resampler = (rate == 8000) ? &mResampler8 : ((rate == 16000) ? &mResampler16 : ((rate == 32000) ? &mResampler32 : &mResampler48));
|
||||||
int inputProcessed = 0;
|
size_t inputProcessed = 0;
|
||||||
resampler->processBuffer(input, length, inputProcessed, mTempBuffer.mutableData(), outputSize);
|
resampler->processBuffer(input, length, inputProcessed, mTempBuffer.mutableData(), outputSize);
|
||||||
// inputProcessed result value is ignored here - rate will be 8/16/32/48k, inputProcessed is equal to length
|
// inputProcessed result value is ignored here - rate will be 8/16/32/48k, inputProcessed is equal to length
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -54,21 +54,22 @@ SpeexResampler::~SpeexResampler()
|
||||||
stop();
|
stop();
|
||||||
}
|
}
|
||||||
|
|
||||||
int SpeexResampler::processBuffer(const void* src, int sourceLength, int& sourceProcessed, void* dest, int destCapacity)
|
size_t SpeexResampler::processBuffer(const void* src, size_t sourceLength, size_t& sourceProcessed, void* dest, size_t destCapacity)
|
||||||
{
|
{
|
||||||
assert(mSourceRate != 0 && mDestRate != 0);
|
assert(mSourceRate != 0 && mDestRate != 0);
|
||||||
|
|
||||||
if (mDestRate == mSourceRate)
|
if (mDestRate == mSourceRate)
|
||||||
{
|
{
|
||||||
assert(destCapacity >= sourceLength);
|
assert(destCapacity >= sourceLength);
|
||||||
memcpy(dest, src, (size_t)sourceLength);
|
memcpy(dest, src, sourceLength);
|
||||||
sourceProcessed = sourceLength;
|
sourceProcessed = sourceLength;
|
||||||
return sourceLength;
|
return sourceLength;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!mContext)
|
if (!mContext)
|
||||||
{
|
{
|
||||||
mContext = speex_resampler_init(mChannels, mSourceRate, mDestRate, AUDIO_RESAMPLER_QUALITY, &mErrorCode);
|
mContext = speex_resampler_init(mChannels, mSourceRate, mDestRate,
|
||||||
|
AUDIO_RESAMPLER_QUALITY, &mErrorCode);
|
||||||
if (!mContext)
|
if (!mContext)
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
@ -83,20 +84,27 @@ int SpeexResampler::processBuffer(const void* src, int sourceLength, int& source
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
unsigned outLen = getDestLength(sourceLength);
|
size_t outLen = getDestLength(sourceLength);
|
||||||
if (outLen > (unsigned)destCapacity)
|
if (outLen > destCapacity)
|
||||||
return 0; // Skip resampling if not enough space
|
return 0; // Skip resampling if not enough space
|
||||||
|
|
||||||
assert((unsigned)destCapacity >= outLen);
|
assert(destCapacity >= outLen);
|
||||||
|
|
||||||
// Calculate number of samples - input length is in bytes
|
// Calculate number of samples - input length is in bytes
|
||||||
unsigned inLen = sourceLength / (sizeof(short) * mChannels);
|
unsigned inLen = sourceLength / (sizeof(short) * mChannels);
|
||||||
outLen /= sizeof(short) * mChannels;
|
outLen /= sizeof(short) * mChannels;
|
||||||
assert(mContext != NULL);
|
assert(mContext != NULL);
|
||||||
int speexCode = speex_resampler_process_interleaved_int((SpeexResamplerState *)mContext, (spx_int16_t*)src, &inLen,
|
spx_uint32_t in_len = static_cast<spx_uint32_t>(inLen),
|
||||||
(spx_int16_t*)dest, &outLen);
|
out_len = static_cast<spx_uint32_t>(outLen);
|
||||||
|
|
||||||
|
int speexCode = speex_resampler_process_interleaved_int((SpeexResamplerState *)mContext,
|
||||||
|
(spx_int16_t*)src, &in_len,
|
||||||
|
(spx_int16_t*)dest, &out_len);
|
||||||
assert(speexCode == RESAMPLER_ERR_SUCCESS);
|
assert(speexCode == RESAMPLER_ERR_SUCCESS);
|
||||||
|
|
||||||
|
inLen = static_cast<size_t>(in_len);
|
||||||
|
outLen = static_cast<size_t>(out_len);
|
||||||
|
|
||||||
// Return results in bytes
|
// Return results in bytes
|
||||||
sourceProcessed = inLen * sizeof(short) * mChannels;
|
sourceProcessed = inLen * sizeof(short) * mChannels;
|
||||||
return outLen * sizeof(short) * mChannels;
|
return outLen * sizeof(short) * mChannels;
|
||||||
|
|
@ -112,18 +120,18 @@ int SpeexResampler::destRate()
|
||||||
return mDestRate;
|
return mDestRate;
|
||||||
}
|
}
|
||||||
|
|
||||||
int SpeexResampler::getDestLength(int sourceLen)
|
size_t SpeexResampler::getDestLength(size_t sourceLen)
|
||||||
{
|
{
|
||||||
return int(sourceLen * (float(mDestRate) / mSourceRate) + 0.5) / 2 * 2;
|
return size_t(sourceLen * (float(mDestRate) / mSourceRate) + 0.5f) / 2 * 2;
|
||||||
}
|
}
|
||||||
|
|
||||||
int SpeexResampler::getSourceLength(int destLen)
|
size_t SpeexResampler::getSourceLength(size_t destLen)
|
||||||
{
|
{
|
||||||
return int(destLen * (float(mSourceRate) / mDestRate) + 0.5) / 2 * 2;
|
return size_t(destLen * (float(mSourceRate) / mDestRate) + 0.5f) / 2 * 2;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Returns instance + speex resampler size in bytes
|
// Returns instance + speex resampler size in bytes
|
||||||
int SpeexResampler::getSize() const
|
size_t SpeexResampler::getSize() const
|
||||||
{
|
{
|
||||||
return sizeof(*this) + 200; // 200 is approximate size of speex resample structure
|
return sizeof(*this) + 200; // 200 is approximate size of speex resample structure
|
||||||
}
|
}
|
||||||
|
|
@ -214,14 +222,15 @@ UniversalResampler::~UniversalResampler()
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
int UniversalResampler::resample(int sourceRate, const void *sourceBuffer, int sourceLength, int& sourceProcessed, int destRate, void *destBuffer, int destCapacity)
|
size_t UniversalResampler::resample(int sourceRate, const void *sourceBuffer, size_t sourceLength,
|
||||||
|
size_t& sourceProcessed, int destRate, void *destBuffer, size_t destCapacity)
|
||||||
{
|
{
|
||||||
assert(destBuffer && sourceBuffer);
|
assert(destBuffer && sourceBuffer);
|
||||||
int result;
|
size_t result;
|
||||||
if (sourceRate == destRate)
|
if (sourceRate == destRate)
|
||||||
{
|
{
|
||||||
assert(destCapacity >= sourceLength);
|
assert(destCapacity >= sourceLength);
|
||||||
memcpy(destBuffer, sourceBuffer, (size_t)sourceLength);
|
memcpy(destBuffer, sourceBuffer, sourceLength);
|
||||||
sourceProcessed = sourceLength;
|
sourceProcessed = sourceLength;
|
||||||
result = sourceLength;
|
result = sourceLength;
|
||||||
}
|
}
|
||||||
|
|
@ -238,7 +247,7 @@ void UniversalResampler::preload()
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
int UniversalResampler::getDestLength(int sourceRate, int destRate, int sourceLength)
|
size_t UniversalResampler::getDestLength(int sourceRate, int destRate, size_t sourceLength)
|
||||||
{
|
{
|
||||||
if (sourceRate == destRate)
|
if (sourceRate == destRate)
|
||||||
return sourceLength;
|
return sourceLength;
|
||||||
|
|
@ -246,7 +255,7 @@ int UniversalResampler::getDestLength(int sourceRate, int destRate, int sourceLe
|
||||||
return findResampler(sourceRate, destRate)->getDestLength(sourceLength);
|
return findResampler(sourceRate, destRate)->getDestLength(sourceLength);
|
||||||
}
|
}
|
||||||
|
|
||||||
int UniversalResampler::getSourceLength(int sourceRate, int destRate, int destLength)
|
size_t UniversalResampler::getSourceLength(int sourceRate, int destRate, size_t destLength)
|
||||||
{
|
{
|
||||||
if (sourceRate == destRate)
|
if (sourceRate == destRate)
|
||||||
return destLength;
|
return destLength;
|
||||||
|
|
|
||||||
|
|
@ -24,14 +24,15 @@ public:
|
||||||
|
|
||||||
void start(int channels, int sourceRate, int destRate);
|
void start(int channels, int sourceRate, int destRate);
|
||||||
void stop();
|
void stop();
|
||||||
int processBuffer(const void* source, int sourceLength, int& sourceProcessed, void* dest, int destCapacity);
|
size_t processBuffer(const void* source, size_t sourceLength, size_t& sourceProcessed,
|
||||||
|
void* dest, size_t destCapacity);
|
||||||
int sourceRate();
|
int sourceRate();
|
||||||
int destRate();
|
int destRate();
|
||||||
int getDestLength(int sourceLen);
|
size_t getDestLength(size_t sourceLen);
|
||||||
int getSourceLength(int destLen);
|
size_t getSourceLength(size_t destLen);
|
||||||
|
|
||||||
// Returns instance + speex encoder size in bytes
|
// Returns instance + speex encoder size in bytes
|
||||||
int getSize() const;
|
size_t getSize() const;
|
||||||
|
|
||||||
protected:
|
protected:
|
||||||
void* mContext;
|
void* mContext;
|
||||||
|
|
@ -59,9 +60,10 @@ public:
|
||||||
UniversalResampler();
|
UniversalResampler();
|
||||||
~UniversalResampler();
|
~UniversalResampler();
|
||||||
|
|
||||||
int resample(int sourceRate, const void* sourceBuffer, int sourceLength, int& sourceProcessed, int destRate, void* destBuffer, int destCapacity);
|
size_t resample(int sourceRate, const void* sourceBuffer, size_t sourceLength, size_t& sourceProcessed,
|
||||||
int getDestLength(int sourceRate, int destRate, int sourceLength);
|
int destRate, void* destBuffer, size_t destCapacity);
|
||||||
int getSourceLength(int sourceRate, int destRate, int destLength);
|
size_t getDestLength(int sourceRate, int destRate, size_t sourceLength);
|
||||||
|
size_t getSourceLength(int sourceRate, int destRate, size_t destLength);
|
||||||
|
|
||||||
protected:
|
protected:
|
||||||
typedef std::pair<int, int> RatePair;
|
typedef std::pair<int, int> RatePair;
|
||||||
|
|
|
||||||
|
|
@ -204,8 +204,9 @@ unsigned WavFileReader::read(short* buffer, unsigned samples)
|
||||||
}
|
}
|
||||||
|
|
||||||
/*int readSamples = */fread(temp, 1, requiredBytes, mHandle);// / mChannels / (mBits / 8);
|
/*int readSamples = */fread(temp, 1, requiredBytes, mHandle);// / mChannels / (mBits / 8);
|
||||||
int processedBytes = 0;
|
size_t processedBytes = 0;
|
||||||
int result = mResampler.processBuffer(temp, requiredBytes, processedBytes, buffer, samples * 2 * AUDIO_CHANNELS);
|
size_t result = mResampler.processBuffer(temp, requiredBytes, processedBytes,
|
||||||
|
buffer, samples * 2 * AUDIO_CHANNELS);
|
||||||
|
|
||||||
return result / 2 / AUDIO_CHANNELS;
|
return result / 2 / AUDIO_CHANNELS;
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -59,7 +59,7 @@ uint64_t ThreadHelper::getCurrentId()
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
#if defined(TARGET_LINUX)||defined(TARGET_OSX)
|
#if defined(TARGET_LINUX)||defined(TARGET_OSX)
|
||||||
return static_cast<uint64_t>(pthread_self());
|
return reinterpret_cast<uint64_t>(pthread_self());
|
||||||
#endif
|
#endif
|
||||||
}
|
}
|
||||||
// ------------------- TimeHelper ---------------
|
// ------------------- TimeHelper ---------------
|
||||||
|
|
@ -122,14 +122,14 @@ void BufferQueue::push(const void* data, int bytes)
|
||||||
{
|
{
|
||||||
std::unique_lock<std::mutex> l(mMutex);
|
std::unique_lock<std::mutex> l(mMutex);
|
||||||
|
|
||||||
Block b = std::make_shared<std::vector<unsigned char>>();
|
PBlock b = std::make_shared<Block>();
|
||||||
b->resize(bytes);
|
b->resize(bytes);
|
||||||
memcpy(b->data(), data, bytes);
|
memcpy(b->data(), data, bytes);
|
||||||
mBlockList.push_back(b);
|
mBlockList.push_back(b);
|
||||||
mSignal.notify_all();
|
mSignal.notify_one();
|
||||||
}
|
}
|
||||||
|
|
||||||
BufferQueue::Block BufferQueue::pull(int milliseconds)
|
BufferQueue::PBlock BufferQueue::pull(int milliseconds)
|
||||||
{
|
{
|
||||||
std::unique_lock<std::mutex> l(mMutex);
|
std::unique_lock<std::mutex> l(mMutex);
|
||||||
std::cv_status status = mBlockList.empty() ? std::cv_status::timeout : std::cv_status::no_timeout;
|
std::cv_status status = mBlockList.empty() ? std::cv_status::timeout : std::cv_status::no_timeout;
|
||||||
|
|
@ -137,11 +137,11 @@ BufferQueue::Block BufferQueue::pull(int milliseconds)
|
||||||
if (mBlockList.empty())
|
if (mBlockList.empty())
|
||||||
status = mSignal.wait_for(l, std::chrono::milliseconds(milliseconds));
|
status = mSignal.wait_for(l, std::chrono::milliseconds(milliseconds));
|
||||||
|
|
||||||
Block r;
|
PBlock r;
|
||||||
if (status == std::cv_status::no_timeout && !mBlockList.empty())
|
if (status == std::cv_status::no_timeout && !mBlockList.empty())
|
||||||
{
|
{
|
||||||
r = mBlockList.front();
|
r = mBlockList.front();
|
||||||
mBlockList.erase(mBlockList.begin());
|
mBlockList.pop_front();
|
||||||
}
|
}
|
||||||
|
|
||||||
return r;
|
return r;
|
||||||
|
|
|
||||||
|
|
@ -75,15 +75,16 @@ public:
|
||||||
BufferQueue();
|
BufferQueue();
|
||||||
~BufferQueue();
|
~BufferQueue();
|
||||||
|
|
||||||
typedef std::shared_ptr<std::vector<unsigned char>> Block;
|
typedef std::vector<uint8_t> Block;
|
||||||
|
typedef std::shared_ptr<Block> PBlock;
|
||||||
|
|
||||||
void push(const void* data, int bytes);
|
void push(const void* data, int bytes);
|
||||||
Block pull(int milliseconds);
|
PBlock pull(int milliseconds);
|
||||||
|
|
||||||
protected:
|
protected:
|
||||||
std::mutex mMutex;
|
std::mutex mMutex;
|
||||||
std::condition_variable mSignal;
|
std::condition_variable mSignal;
|
||||||
std::vector<Block> mBlockList;
|
std::deque<PBlock> mBlockList;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue