Bug 1656438 - Make integer types explicit and unsigned in AudioDriftCorrection and friends. r=padenot

In particular, this patch gets rid of signed integers where negative values
don't make sense anyway.

Differential Revision: https://phabricator.services.mozilla.com/D89777
This commit is contained in:
Andreas Pehrson 2020-09-15 14:42:28 +00:00
Родитель 98fd9bf78d
Коммит d88af83716
8 изменённых файлов: 612 добавлений и 598 удалений

Просмотреть файл

@ -38,8 +38,8 @@ class ClockDrift final {
/**
* Provide the nominal source and the target sample rate.
*/
ClockDrift(int32_t aSourceRate, int32_t aTargetRate,
int32_t aDesiredBuffering)
ClockDrift(uint32_t aSourceRate, uint32_t aTargetRate,
uint32_t aDesiredBuffering)
: mSourceRate(aSourceRate),
mTargetRate(aTargetRate),
mDesiredBuffering(aDesiredBuffering) {}
@ -60,8 +60,8 @@ class ClockDrift final {
* In addition to that, the correction is clamped to 10% to avoid sound
* distortion so the result will be in [0.9, 1.1].
*/
void UpdateClock(int aSourceFrames, int aTargetFrames, int aBufferedFrames,
int aRemainingFrames) {
void UpdateClock(uint32_t aSourceFrames, uint32_t aTargetFrames,
uint32_t aBufferedFrames, uint32_t aRemainingFrames) {
if (mSourceClock >= mSourceRate / 10 || mTargetClock >= mTargetRate / 10) {
// Only update the correction if 100ms has passed since last update.
if (aBufferedFrames < mDesiredBuffering * 4 / 10 /*40%*/ ||
@ -86,19 +86,19 @@ class ClockDrift final {
* 1 - aCalculationWeight. This gives some inertia to the speed at which the
* correction changes, for smoother changes.
*/
void CalculateCorrection(float aCalculationWeight, int aBufferedFrames,
int aRemainingFrames) {
void CalculateCorrection(float aCalculationWeight, uint32_t aBufferedFrames,
uint32_t aRemainingFrames) {
// We want to maintain the desired buffer
int32_t bufferedFramesDiff = aBufferedFrames - mDesiredBuffering;
int32_t resampledSourceClock =
std::max(1, mSourceClock + bufferedFramesDiff);
uint32_t bufferedFramesDiff = aBufferedFrames - mDesiredBuffering;
uint32_t resampledSourceClock =
std::max(1u, mSourceClock + bufferedFramesDiff);
if (mTargetRate != mSourceRate) {
resampledSourceClock *= static_cast<float>(mTargetRate) / mSourceRate;
}
MOZ_LOG(gMediaTrackGraphLog, LogLevel::Verbose,
("ClockDrift %p Calculated correction %.3f (with weight: %.1f -> "
"%.3f) (buffer: %d, desired: %d, remaining: %d)",
"%.3f) (buffer: %u, desired: %u, remaining: %u)",
this, static_cast<float>(mTargetClock) / resampledSourceClock,
aCalculationWeight,
(1 - aCalculationWeight) * mCorrection +
@ -117,16 +117,16 @@ class ClockDrift final {
}
public:
const int32_t mSourceRate;
const int32_t mTargetRate;
const int32_t mAdjustmentIntervalMs = 1000;
const int32_t mDesiredBuffering;
const uint32_t mSourceRate;
const uint32_t mTargetRate;
const uint32_t mAdjustmentIntervalMs = 1000;
const uint32_t mDesiredBuffering;
private:
float mCorrection = 1.0;
int32_t mSourceClock = 0;
int32_t mTargetClock = 0;
uint32_t mSourceClock = 0;
uint32_t mTargetClock = 0;
};
/**
@ -150,7 +150,7 @@ class ClockDrift final {
*/
class AudioDriftCorrection final {
public:
AudioDriftCorrection(int32_t aSourceRate, int32_t aTargetRate)
AudioDriftCorrection(uint32_t aSourceRate, uint32_t aTargetRate)
: mDesiredBuffering(
std::max(5, Preferences::GetInt("media.clockdrift.buffering", 50)) *
aSourceRate / 1000),
@ -168,7 +168,7 @@ class AudioDriftCorrection final {
* AudioSegment will be returned. Not thread-safe.
*/
AudioSegment RequestFrames(const AudioSegment& aInput,
int32_t aOutputFrames) {
uint32_t aOutputFrames) {
// Very important to go first since the Dynamic will get the sample format
// from the chunk.
if (aInput.GetDuration()) {
@ -193,8 +193,8 @@ class AudioDriftCorrection final {
// Only accessible from the same thread that is driving RequestFrames().
uint32_t CurrentBuffering() const { return mResampler.InputDuration(); }
const int32_t mDesiredBuffering;
const int32_t mTargetRate;
const uint32_t mDesiredBuffering;
const uint32_t mTargetRate;
private:
ClockDrift mClockDrift;

Просмотреть файл

@ -35,7 +35,7 @@ class RingBuffer final {
/**
* Write `aSamples` number of zeros in the buffer.
*/
int WriteSilence(int aSamples) {
uint32_t WriteSilence(uint32_t aSamples) {
MOZ_ASSERT(aSamples);
return Write(Span<T>(), aSamples);
}
@ -43,7 +43,7 @@ class RingBuffer final {
/**
* Copy `aBuffer` to the RingBuffer.
*/
int Write(const Span<const T>& aBuffer) {
uint32_t Write(const Span<const T>& aBuffer) {
MOZ_ASSERT(!aBuffer.IsEmpty());
return Write(aBuffer, aBuffer.Length());
}
@ -53,7 +53,7 @@ class RingBuffer final {
* Copy `aSamples` number of elements from `aBuffer` to the RingBuffer. If
* `aBuffer` is empty append `aSamples` of zeros.
*/
int Write(const Span<const T>& aBuffer, int aSamples) {
uint32_t Write(const Span<const T>& aBuffer, uint32_t aSamples) {
MOZ_ASSERT(aSamples > 0 &&
aBuffer.Length() <= static_cast<uint32_t>(aSamples));
@ -61,9 +61,9 @@ class RingBuffer final {
return 0;
}
int toWrite = std::min(AvailableWrite(), aSamples);
int part1 = std::min(Capacity() - mWriteIndex, toWrite);
int part2 = toWrite - part1;
uint32_t toWrite = std::min(AvailableWrite(), aSamples);
uint32_t part1 = std::min(Capacity() - mWriteIndex, toWrite);
uint32_t part2 = toWrite - part1;
Span<T> part1Buffer = mStorage.Subspan(mWriteIndex, part1);
Span<T> part2Buffer = mStorage.To(part2);
@ -90,20 +90,21 @@ class RingBuffer final {
* Copy `aSamples` number of elements from `aBuffer` to the RingBuffer. The
* `aBuffer` does not change.
*/
int Write(const RingBuffer& aBuffer, int aSamples) {
uint32_t Write(const RingBuffer& aBuffer, uint32_t aSamples) {
MOZ_ASSERT(aSamples);
if (IsFull()) {
return 0;
}
int toWriteThis = std::min(AvailableWrite(), aSamples);
int toReadThat = std::min(aBuffer.AvailableRead(), toWriteThis);
int part1 = std::min(aBuffer.Capacity() - aBuffer.mReadIndex, toReadThat);
int part2 = toReadThat - part1;
uint32_t toWriteThis = std::min(AvailableWrite(), aSamples);
uint32_t toReadThat = std::min(aBuffer.AvailableRead(), toWriteThis);
uint32_t part1 =
std::min(aBuffer.Capacity() - aBuffer.mReadIndex, toReadThat);
uint32_t part2 = toReadThat - part1;
Span<T> part1Buffer = aBuffer.mStorage.Subspan(aBuffer.mReadIndex, part1);
DebugOnly<int> ret = Write(part1Buffer);
DebugOnly<uint32_t> ret = Write(part1Buffer);
MOZ_ASSERT(ret == part1);
if (part2) {
Span<T> part2Buffer = aBuffer.mStorage.To(part2);
@ -117,17 +118,17 @@ class RingBuffer final {
/**
* Copy `aBuffer.Length()` number of elements from RingBuffer to `aBuffer`.
*/
int Read(const Span<T>& aBuffer) {
uint32_t Read(const Span<T>& aBuffer) {
MOZ_ASSERT(!aBuffer.IsEmpty());
MOZ_ASSERT(aBuffer.size() <= std::numeric_limits<int>::max());
MOZ_ASSERT(aBuffer.size() <= std::numeric_limits<uint32_t>::max());
if (IsEmpty()) {
return 0;
}
int toRead = std::min(AvailableRead(), static_cast<int>(aBuffer.Length()));
int part1 = std::min(Capacity() - mReadIndex, toRead);
int part2 = toRead - part1;
uint32_t toRead = std::min<uint32_t>(AvailableRead(), aBuffer.Length());
uint32_t part1 = std::min(Capacity() - mReadIndex, toRead);
uint32_t part2 = toRead - part1;
Span<T> part1Buffer = mStorage.Subspan(mReadIndex, part1);
Span<T> part2Buffer = mStorage.To(part2);
@ -164,16 +165,17 @@ class RingBuffer final {
* the `aCallable`. In the body of the `aCallable` those buffers can be used
* directly without any copy or intermediate steps.
*/
int ReadNoCopy(std::function<int(const Span<const T>&)>&& aCallable) {
uint32_t ReadNoCopy(
std::function<uint32_t(const Span<const T>&)>&& aCallable) {
if (IsEmpty()) {
return 0;
}
int part1 = std::min(Capacity() - mReadIndex, AvailableRead());
int part2 = AvailableRead() - part1;
uint32_t part1 = std::min(Capacity() - mReadIndex, AvailableRead());
uint32_t part2 = AvailableRead() - part1;
Span<T> part1Buffer = mStorage.Subspan(mReadIndex, part1);
int toRead = aCallable(part1Buffer);
uint32_t toRead = aCallable(part1Buffer);
MOZ_ASSERT(toRead <= part1);
if (toRead == part1 && part2) {
@ -190,14 +192,14 @@ class RingBuffer final {
/**
* Remove the next `aSamples` number of samples from the ring buffer.
*/
int Discard(int aSamples) {
uint32_t Discard(uint32_t aSamples) {
MOZ_ASSERT(aSamples);
if (IsEmpty()) {
return 0;
}
int toDiscard = std::min(AvailableRead(), aSamples);
uint32_t toDiscard = std::min(AvailableRead(), aSamples);
mReadIndex = NextIndex(mReadIndex, toDiscard);
return toDiscard;
@ -206,12 +208,12 @@ class RingBuffer final {
/**
* Empty the ring buffer.
*/
int Clear() {
uint32_t Clear() {
if (IsEmpty()) {
return 0;
}
int toDiscard = AvailableRead();
uint32_t toDiscard = AvailableRead();
mReadIndex = NextIndex(mReadIndex, toDiscard);
return toDiscard;
@ -232,10 +234,10 @@ class RingBuffer final {
/**
* The number of samples available for writing.
*/
int AvailableWrite() const {
uint32_t AvailableWrite() const {
/* We subtract one element here to always keep at least one sample
* free in the buffer, to distinguish between full and empty array. */
int rv = mReadIndex - mWriteIndex - 1;
uint32_t rv = mReadIndex - mWriteIndex - 1;
if (mWriteIndex >= mReadIndex) {
rv += Capacity();
}
@ -245,7 +247,7 @@ class RingBuffer final {
/**
* The number of samples available for reading.
*/
int AvailableRead() const {
uint32_t AvailableRead() const {
if (mWriteIndex >= mReadIndex) {
return mWriteIndex - mReadIndex;
}
@ -253,14 +255,13 @@ class RingBuffer final {
}
private:
int NextIndex(int aIndex, int aStep) const {
MOZ_ASSERT(aStep >= 0);
uint32_t NextIndex(uint32_t aIndex, uint32_t aStep) const {
MOZ_ASSERT(aStep < Capacity());
MOZ_ASSERT(aIndex < Capacity());
return (aIndex + aStep) % Capacity();
}
int32_t Capacity() const { return mStorage.Length(); }
uint32_t Capacity() const { return mStorage.Length(); }
Span<T> ConvertToSpan(const AlignedByteBuffer& aOther) const {
MOZ_ASSERT(aOther.Length() >= sizeof(T));
@ -274,8 +275,8 @@ class RingBuffer final {
}
private:
int mReadIndex = 0;
int mWriteIndex = 0;
uint32_t mReadIndex = 0;
uint32_t mWriteIndex = 0;
/* Points to the mMemoryBuffer. */
const Span<T> mStorage;
/* The actual allocated memory set from outside. It is set in the ctor and it
@ -296,10 +297,9 @@ class AudioRingBuffer::AudioRingBufferPrivate {
Maybe<AlignedByteBuffer> mBackingBuffer;
};
AudioRingBuffer::AudioRingBuffer(int aSizeInBytes)
AudioRingBuffer::AudioRingBuffer(uint32_t aSizeInBytes)
: mPtr(MakeUnique<AudioRingBufferPrivate>()) {
MOZ_ASSERT(aSizeInBytes > 0);
MOZ_ASSERT(aSizeInBytes < std::numeric_limits<int>::max());
mPtr->mBackingBuffer.emplace(aSizeInBytes);
MOZ_ASSERT(mPtr->mBackingBuffer);
}
@ -323,21 +323,22 @@ void AudioRingBuffer::SetSampleFormat(AudioSampleFormat aFormat) {
MOZ_ASSERT(!mPtr->mBackingBuffer);
}
int AudioRingBuffer::Write(const Span<const float>& aBuffer) {
uint32_t AudioRingBuffer::Write(const Span<const float>& aBuffer) {
MOZ_ASSERT(mPtr->mSampleFormat == AUDIO_FORMAT_FLOAT32);
MOZ_ASSERT(!mPtr->mIntRingBuffer);
MOZ_ASSERT(!mPtr->mBackingBuffer);
return mPtr->mFloatRingBuffer->Write(aBuffer);
}
int AudioRingBuffer::Write(const Span<const int16_t>& aBuffer) {
uint32_t AudioRingBuffer::Write(const Span<const int16_t>& aBuffer) {
MOZ_ASSERT(mPtr->mSampleFormat == AUDIO_FORMAT_S16);
MOZ_ASSERT(!mPtr->mFloatRingBuffer);
MOZ_ASSERT(!mPtr->mBackingBuffer);
return mPtr->mIntRingBuffer->Write(aBuffer);
}
int AudioRingBuffer::Write(const AudioRingBuffer& aBuffer, int aSamples) {
uint32_t AudioRingBuffer::Write(const AudioRingBuffer& aBuffer,
uint32_t aSamples) {
MOZ_ASSERT(mPtr->mSampleFormat == AUDIO_FORMAT_S16 ||
mPtr->mSampleFormat == AUDIO_FORMAT_FLOAT32);
MOZ_ASSERT(!mPtr->mBackingBuffer);
@ -351,7 +352,7 @@ int AudioRingBuffer::Write(const AudioRingBuffer& aBuffer, int aSamples) {
aSamples);
}
int AudioRingBuffer::WriteSilence(int aSamples) {
uint32_t AudioRingBuffer::WriteSilence(uint32_t aSamples) {
MOZ_ASSERT(mPtr->mSampleFormat == AUDIO_FORMAT_S16 ||
mPtr->mSampleFormat == AUDIO_FORMAT_FLOAT32);
MOZ_ASSERT(!mPtr->mBackingBuffer);
@ -363,37 +364,37 @@ int AudioRingBuffer::WriteSilence(int aSamples) {
return mPtr->mFloatRingBuffer->WriteSilence(aSamples);
}
int AudioRingBuffer::Read(const Span<float>& aBuffer) {
uint32_t AudioRingBuffer::Read(const Span<float>& aBuffer) {
MOZ_ASSERT(mPtr->mSampleFormat == AUDIO_FORMAT_FLOAT32);
MOZ_ASSERT(!mPtr->mIntRingBuffer);
MOZ_ASSERT(!mPtr->mBackingBuffer);
return mPtr->mFloatRingBuffer->Read(aBuffer);
}
int AudioRingBuffer::Read(const Span<int16_t>& aBuffer) {
uint32_t AudioRingBuffer::Read(const Span<int16_t>& aBuffer) {
MOZ_ASSERT(mPtr->mSampleFormat == AUDIO_FORMAT_S16);
MOZ_ASSERT(!mPtr->mFloatRingBuffer);
MOZ_ASSERT(!mPtr->mBackingBuffer);
return mPtr->mIntRingBuffer->Read(aBuffer);
}
int AudioRingBuffer::ReadNoCopy(
std::function<int(const Span<const float>&)>&& aCallable) {
uint32_t AudioRingBuffer::ReadNoCopy(
std::function<uint32_t(const Span<const float>&)>&& aCallable) {
MOZ_ASSERT(mPtr->mSampleFormat == AUDIO_FORMAT_FLOAT32);
MOZ_ASSERT(!mPtr->mIntRingBuffer);
MOZ_ASSERT(!mPtr->mBackingBuffer);
return mPtr->mFloatRingBuffer->ReadNoCopy(std::move(aCallable));
}
int AudioRingBuffer::ReadNoCopy(
std::function<int(const Span<const int16_t>&)>&& aCallable) {
uint32_t AudioRingBuffer::ReadNoCopy(
std::function<uint32_t(const Span<const int16_t>&)>&& aCallable) {
MOZ_ASSERT(mPtr->mSampleFormat == AUDIO_FORMAT_S16);
MOZ_ASSERT(!mPtr->mFloatRingBuffer);
MOZ_ASSERT(!mPtr->mBackingBuffer);
return mPtr->mIntRingBuffer->ReadNoCopy(std::move(aCallable));
}
int AudioRingBuffer::Discard(int aSamples) {
uint32_t AudioRingBuffer::Discard(uint32_t aSamples) {
MOZ_ASSERT(mPtr->mSampleFormat == AUDIO_FORMAT_S16 ||
mPtr->mSampleFormat == AUDIO_FORMAT_FLOAT32);
MOZ_ASSERT(!mPtr->mBackingBuffer);
@ -405,7 +406,7 @@ int AudioRingBuffer::Discard(int aSamples) {
return mPtr->mFloatRingBuffer->Discard(aSamples);
}
int AudioRingBuffer::Clear() {
uint32_t AudioRingBuffer::Clear() {
MOZ_ASSERT(mPtr->mSampleFormat == AUDIO_FORMAT_S16 ||
mPtr->mSampleFormat == AUDIO_FORMAT_FLOAT32);
MOZ_ASSERT(!mPtr->mBackingBuffer);
@ -443,7 +444,7 @@ bool AudioRingBuffer::IsEmpty() const {
return mPtr->mFloatRingBuffer->IsEmpty();
}
int AudioRingBuffer::AvailableWrite() const {
uint32_t AudioRingBuffer::AvailableWrite() const {
MOZ_ASSERT(mPtr->mSampleFormat == AUDIO_FORMAT_S16 ||
mPtr->mSampleFormat == AUDIO_FORMAT_FLOAT32);
MOZ_ASSERT(!mPtr->mBackingBuffer);
@ -455,7 +456,7 @@ int AudioRingBuffer::AvailableWrite() const {
return mPtr->mFloatRingBuffer->AvailableWrite();
}
int AudioRingBuffer::AvailableRead() const {
uint32_t AudioRingBuffer::AvailableRead() const {
MOZ_ASSERT(mPtr->mSampleFormat == AUDIO_FORMAT_S16 ||
mPtr->mSampleFormat == AUDIO_FORMAT_FLOAT32);
MOZ_ASSERT(!mPtr->mBackingBuffer);

Просмотреть файл

@ -22,7 +22,7 @@ namespace mozilla {
*/
class AudioRingBuffer final {
public:
explicit AudioRingBuffer(int aSizeInBytes);
explicit AudioRingBuffer(uint32_t aSizeInBytes);
~AudioRingBuffer();
/**
@ -34,55 +34,57 @@ class AudioRingBuffer final {
/**
* Write `aBuffer.Length()` number of samples when the format is float.
*/
int Write(const Span<const float>& aBuffer);
uint32_t Write(const Span<const float>& aBuffer);
/**
* Write `aBuffer.Length()` number of samples when the format is short.
*/
int Write(const Span<const int16_t>& aBuffer);
uint32_t Write(const Span<const int16_t>& aBuffer);
/**
* Write `aSamples` number of samples from `aBuffer`. Note the `aBuffer` does
* not change.
*/
int Write(const AudioRingBuffer& aBuffer, int aSamples);
uint32_t Write(const AudioRingBuffer& aBuffer, uint32_t aSamples);
/**
* Write `aSamples` number of zeros.
*/
int WriteSilence(int aSamples);
uint32_t WriteSilence(uint32_t aSamples);
/**
* Read `aBuffer.Length()` number of samples when the format is float.
*/
int Read(const Span<float>& aBuffer);
uint32_t Read(const Span<float>& aBuffer);
/**
* Read `aBuffer.Length()` number of samples when the format is short.
*/
int Read(const Span<int16_t>& aBuffer);
uint32_t Read(const Span<int16_t>& aBuffer);
/**
* Read the internal buffer without extra copies when sample format is float.
* Check also the RingBuffer::ReadNoCopy() for more details.
*/
int ReadNoCopy(std::function<int(const Span<const float>&)>&& aCallable);
uint32_t ReadNoCopy(
std::function<uint32_t(const Span<const float>&)>&& aCallable);
/**
* Read the internal buffer without extra copies when sample format is short.
* Check also the RingBuffer::ReadNoCopy() for more details.
*/
int ReadNoCopy(std::function<int(const Span<const int16_t>&)>&& aCallable);
uint32_t ReadNoCopy(
std::function<uint32_t(const Span<const int16_t>&)>&& aCallable);
/**
* Remove `aSamples` number of samples.
*/
int Discard(int aSamples);
uint32_t Discard(uint32_t aSamples);
/**
* Remove all available samples.
*/
int Clear();
uint32_t Clear();
/**
* Return true if the buffer is full.
@ -97,12 +99,12 @@ class AudioRingBuffer final {
/**
* Return the number of samples available for writing.
*/
int AvailableWrite() const;
uint32_t AvailableWrite() const;
/**
* Return the number of samples available for reading.
*/
int AvailableRead() const;
uint32_t AvailableRead() const;
private:
class AudioRingBufferPrivate;

Просмотреть файл

@ -7,7 +7,7 @@
namespace mozilla {
DynamicResampler::DynamicResampler(int aInRate, int aOutRate,
DynamicResampler::DynamicResampler(uint32_t aInRate, uint32_t aOutRate,
uint32_t aPreBufferFrames)
: mInRate(aInRate), mPreBufferFrames(aPreBufferFrames), mOutRate(aOutRate) {
MOZ_ASSERT(aInRate);
@ -35,13 +35,13 @@ void DynamicResampler::SetSampleFormat(AudioSampleFormat aFormat) {
}
bool DynamicResampler::Resample(float* aOutBuffer, uint32_t* aOutFrames,
int aChannelIndex) {
uint32_t aChannelIndex) {
MOZ_ASSERT(mSampleFormat == AUDIO_FORMAT_FLOAT32);
return ResampleInternal(aOutBuffer, aOutFrames, aChannelIndex);
}
bool DynamicResampler::Resample(int16_t* aOutBuffer, uint32_t* aOutFrames,
int aChannelIndex) {
uint32_t aChannelIndex) {
MOZ_ASSERT(mSampleFormat == AUDIO_FORMAT_S16);
return ResampleInternal(aOutBuffer, aOutFrames, aChannelIndex);
}
@ -49,7 +49,7 @@ bool DynamicResampler::Resample(int16_t* aOutBuffer, uint32_t* aOutFrames,
void DynamicResampler::ResampleInternal(const float* aInBuffer,
uint32_t* aInFrames, float* aOutBuffer,
uint32_t* aOutFrames,
int aChannelIndex) {
uint32_t aChannelIndex) {
MOZ_ASSERT(mResampler);
MOZ_ASSERT(mChannels);
MOZ_ASSERT(mInRate);
@ -62,7 +62,6 @@ void DynamicResampler::ResampleInternal(const float* aInBuffer,
MOZ_ASSERT(aOutFrames);
MOZ_ASSERT(*aOutFrames > 0);
MOZ_ASSERT(aChannelIndex >= 0);
MOZ_ASSERT(aChannelIndex <= mChannels);
#ifdef DEBUG
@ -77,7 +76,7 @@ void DynamicResampler::ResampleInternal(const int16_t* aInBuffer,
uint32_t* aInFrames,
int16_t* aOutBuffer,
uint32_t* aOutFrames,
int aChannelIndex) {
uint32_t aChannelIndex) {
MOZ_ASSERT(mResampler);
MOZ_ASSERT(mChannels);
MOZ_ASSERT(mInRate);
@ -90,7 +89,6 @@ void DynamicResampler::ResampleInternal(const int16_t* aInBuffer,
MOZ_ASSERT(aOutFrames);
MOZ_ASSERT(*aOutFrames > 0);
MOZ_ASSERT(aChannelIndex >= 0);
MOZ_ASSERT(aChannelIndex <= mChannels);
#ifdef DEBUG
@ -101,7 +99,7 @@ void DynamicResampler::ResampleInternal(const int16_t* aInBuffer,
MOZ_ASSERT(rv == RESAMPLER_ERR_SUCCESS);
}
void DynamicResampler::UpdateResampler(int aOutRate, int aChannels) {
void DynamicResampler::UpdateResampler(uint32_t aOutRate, uint32_t aChannels) {
MOZ_ASSERT(aOutRate);
MOZ_ASSERT(aChannels);
@ -121,7 +119,7 @@ void DynamicResampler::UpdateResampler(int aOutRate, int aChannels) {
mChannels == STEREO) {
// The mono channel is always up to date. When we are going from mono
// to stereo upmix the mono to stereo channel
int bufferedDuration = mInternalInBuffer[0].AvailableRead();
uint32_t bufferedDuration = mInternalInBuffer[0].AvailableRead();
mInternalInBuffer[1].Clear();
if (bufferedDuration) {
mInternalInBuffer[1].Write(mInternalInBuffer[0], bufferedDuration);
@ -135,7 +133,7 @@ void DynamicResampler::UpdateResampler(int aOutRate, int aChannels) {
// upmix or downmix, for now just clear but it has to be updated
// because allocates and this is executed in audio thread.
mInternalInBuffer.Clear();
for (int i = 0; i < mChannels; ++i) {
for (uint32_t i = 0; i < mChannels; ++i) {
// Pre-allocate something big, twice the pre-buffer, or at least 100ms.
AudioRingBuffer* b = mInternalInBuffer.AppendElement(
sizeof(float) * std::max(2 * mPreBufferFrames, mInRate / 10));
@ -165,7 +163,7 @@ void DynamicResampler::UpdateResampler(int aOutRate, int aChannels) {
void DynamicResampler::WarmUpResampler(bool aSkipLatency) {
MOZ_ASSERT(mInputTail.Length());
for (int i = 0; i < mChannels; ++i) {
for (uint32_t i = 0; i < mChannels; ++i) {
if (!mInputTail[i].Length()) {
continue;
}
@ -206,7 +204,7 @@ void DynamicResampler::AppendInput(const nsTArray<const int16_t*>& aInBuffer,
}
bool DynamicResampler::EnoughInFrames(uint32_t aOutFrames,
int aChannelIndex) const {
uint32_t aChannelIndex) const {
if (mInRate == mOutRate) {
return InFramesBuffered(aChannelIndex) >= aOutFrames;
}
@ -220,7 +218,7 @@ bool DynamicResampler::EnoughInFrames(uint32_t aOutFrames,
}
bool DynamicResampler::CanResample(uint32_t aOutFrames) const {
for (int i = 0; i < mChannels; ++i) {
for (uint32_t i = 0; i < mChannels; ++i) {
if (!EnoughInFrames(aOutFrames, i)) {
return false;
}
@ -232,36 +230,34 @@ void DynamicResampler::AppendInputSilence(const uint32_t aInFrames) {
MOZ_ASSERT(aInFrames);
MOZ_ASSERT(mChannels);
MOZ_ASSERT(mInternalInBuffer.Length() >= (uint32_t)mChannels);
for (int i = 0; i < mChannels; ++i) {
for (uint32_t i = 0; i < mChannels; ++i) {
mInternalInBuffer[i].WriteSilence(aInFrames);
}
}
uint32_t DynamicResampler::InFramesBuffered(int aChannelIndex) const {
uint32_t DynamicResampler::InFramesBuffered(uint32_t aChannelIndex) const {
MOZ_ASSERT(mChannels);
MOZ_ASSERT(aChannelIndex >= 0);
MOZ_ASSERT(aChannelIndex <= mChannels);
MOZ_ASSERT((uint32_t)aChannelIndex <= mInternalInBuffer.Length());
MOZ_ASSERT(aChannelIndex <= mInternalInBuffer.Length());
return mInternalInBuffer[aChannelIndex].AvailableRead();
}
uint32_t DynamicResampler::InFramesLeftToBuffer(int aChannelIndex) const {
uint32_t DynamicResampler::InFramesLeftToBuffer(uint32_t aChannelIndex) const {
MOZ_ASSERT(mChannels);
MOZ_ASSERT(aChannelIndex >= 0);
MOZ_ASSERT(aChannelIndex <= mChannels);
MOZ_ASSERT((uint32_t)aChannelIndex <= mInternalInBuffer.Length());
MOZ_ASSERT(aChannelIndex <= mInternalInBuffer.Length());
return mInternalInBuffer[aChannelIndex].AvailableWrite();
}
AudioChunkList::AudioChunkList(int aTotalDuration, int aChannels) {
int numOfChunks = aTotalDuration / mChunkCapacity;
AudioChunkList::AudioChunkList(uint32_t aTotalDuration, uint32_t aChannels) {
uint32_t numOfChunks = aTotalDuration / mChunkCapacity;
if (aTotalDuration % mChunkCapacity) {
++numOfChunks;
}
CreateChunks(numOfChunks, aChannels);
}
void AudioChunkList::CreateChunks(int aNumOfChunks, int aChannels) {
void AudioChunkList::CreateChunks(uint32_t aNumOfChunks, uint32_t aChannels) {
MOZ_ASSERT(!mChunks.Length());
MOZ_ASSERT(aNumOfChunks);
MOZ_ASSERT(aChannels);
@ -274,20 +270,20 @@ void AudioChunkList::CreateChunks(int aNumOfChunks, int aChannels) {
AutoTArray<const float*, STEREO> bufferPtrs;
bufferPtrs.AppendElements(aChannels);
for (int i = 0; i < aChannels; ++i) {
for (uint32_t i = 0; i < aChannels; ++i) {
float* ptr = buffer[i].AppendElements(mChunkCapacity);
bufferPtrs[i] = ptr;
}
chunk.mBuffer = new mozilla::SharedChannelArrayBuffer(std::move(buffer));
chunk.mChannelData.AppendElements(aChannels);
for (int i = 0; i < aChannels; ++i) {
for (uint32_t i = 0; i < aChannels; ++i) {
chunk.mChannelData[i] = bufferPtrs[i];
}
}
}
void AudioChunkList::UpdateToMonoOrStereo(int aChannels) {
void AudioChunkList::UpdateToMonoOrStereo(uint32_t aChannels) {
MOZ_ASSERT(mChunks.Length());
MOZ_ASSERT(mSampleFormat == AUDIO_FORMAT_S16 ||
mSampleFormat == AUDIO_FORMAT_FLOAT32);
@ -343,9 +339,9 @@ AudioChunk& AudioChunkList::GetNext() {
return chunk;
}
void AudioChunkList::Update(int aChannels) {
void AudioChunkList::Update(uint32_t aChannels) {
MOZ_ASSERT(mChunks.Length());
if (mChunks[0].ChannelCount() == (uint32_t)aChannels) {
if (mChunks[0].ChannelCount() == aChannels) {
return;
}
@ -355,12 +351,12 @@ void AudioChunkList::Update(int aChannels) {
return;
}
int numOfChunks = static_cast<int>(mChunks.Length());
uint32_t numOfChunks = mChunks.Length();
mChunks.ClearAndRetainStorage();
CreateChunks(numOfChunks, aChannels);
}
AudioResampler::AudioResampler(int aInRate, int aOutRate,
AudioResampler::AudioResampler(uint32_t aInRate, uint32_t aOutRate,
uint32_t aPreBufferFrames)
: mResampler(aInRate, aOutRate, aPreBufferFrames),
mOutputChunks(aOutRate / 10, STEREO) {}
@ -419,11 +415,11 @@ AudioSegment AudioResampler::Resample(uint32_t aOutFrames) {
return segment;
}
int totalFrames = aOutFrames;
uint32_t totalFrames = aOutFrames;
while (totalFrames) {
MOZ_ASSERT(totalFrames > 0);
AudioChunk& chunk = mOutputChunks.GetNext();
int outFrames = std::min(totalFrames, mOutputChunks.ChunkCapacity());
uint32_t outFrames = std::min(totalFrames, mOutputChunks.ChunkCapacity());
totalFrames -= outFrames;
for (uint32_t i = 0; i < chunk.ChannelCount(); ++i) {
@ -443,7 +439,7 @@ AudioSegment AudioResampler::Resample(uint32_t aOutFrames) {
&outFramesUsed, i);
MOZ_ASSERT(rv);
}
MOZ_ASSERT(outFramesUsed == (uint32_t)outFrames);
MOZ_ASSERT(outFramesUsed == outFrames);
chunk.mDuration = outFrames;
}
@ -456,25 +452,25 @@ AudioSegment AudioResampler::Resample(uint32_t aOutFrames) {
return segment;
}
void AudioResampler::Update(int aOutRate, int aChannels) {
void AudioResampler::Update(uint32_t aOutRate, uint32_t aChannels) {
mResampler.UpdateResampler(aOutRate, aChannels);
mOutputChunks.Update(aChannels);
}
int AudioResampler::InputDuration() const {
uint32_t AudioResampler::InputDuration() const {
if (!mIsSampleFormatSet) {
return (int)mResampler.mPreBufferFrames;
return mResampler.mPreBufferFrames;
}
MOZ_ASSERT((int)mResampler.InFramesBuffered(0) >= 0);
return (int)mResampler.InFramesBuffered(0);
MOZ_ASSERT(mResampler.InFramesBuffered(0) >= 0);
return mResampler.InFramesBuffered(0);
}
int AudioResampler::InputRemainingDuration() const {
uint32_t AudioResampler::InputRemainingDuration() const {
if (!mIsSampleFormatSet) {
return (int)mResampler.mPreBufferFrames;
return mResampler.mPreBufferFrames;
}
MOZ_ASSERT((int)mResampler.InFramesLeftToBuffer(0) >= 0);
return (int)mResampler.InFramesLeftToBuffer(0);
MOZ_ASSERT(mResampler.InFramesLeftToBuffer(0) >= 0);
return mResampler.InFramesLeftToBuffer(0);
}
} // namespace mozilla

Просмотреть файл

@ -13,7 +13,7 @@
namespace mozilla {
const int STEREO = 2;
const uint32_t STEREO = 2;
/**
* DynamicResampler allows updating on the fly the output sample rate and the
@ -43,15 +43,16 @@ class DynamicResampler final {
* The channel count will be set to stereo. Memory allocation will take
* place. The input buffer is non-interleaved.
*/
DynamicResampler(int aInRate, int aOutRate, uint32_t aPreBufferFrames = 0);
DynamicResampler(uint32_t aInRate, uint32_t aOutRate,
uint32_t aPreBufferFrames = 0);
~DynamicResampler();
/**
* Set the sample format type to float or short.
*/
void SetSampleFormat(AudioSampleFormat aFormat);
int GetOutRate() const { return mOutRate; }
int GetChannels() const { return mChannels; }
uint32_t GetOutRate() const { return mOutRate; }
uint32_t GetChannels() const { return mChannels; }
/**
* Append `aInFrames` number of frames from `aInBuffer` to the internal input
@ -68,11 +69,11 @@ class DynamicResampler final {
/**
* Return the number of frames stored in the internal input buffer.
*/
uint32_t InFramesBuffered(int aChannelIndex) const;
uint32_t InFramesBuffered(uint32_t aChannelIndex) const;
/**
* Return the number of frames left to store in the internal input buffer.
*/
uint32_t InFramesLeftToBuffer(int aChannelIndex) const;
uint32_t InFramesLeftToBuffer(uint32_t aChannelIndex) const;
/*
* Resampler as much frame is needed from the internal input buffer to the
@ -80,8 +81,10 @@ class DynamicResampler final {
* not enough input frames to provide the requested output frames no
* resampling is attempted and false is returned.
*/
bool Resample(float* aOutBuffer, uint32_t* aOutFrames, int aChannelIndex);
bool Resample(int16_t* aOutBuffer, uint32_t* aOutFrames, int aChannelIndex);
bool Resample(float* aOutBuffer, uint32_t* aOutFrames,
uint32_t aChannelIndex);
bool Resample(int16_t* aOutBuffer, uint32_t* aOutFrames,
uint32_t aChannelIndex);
/**
* Update the output rate or/and the channel count. If a value is not updated
@ -92,7 +95,7 @@ class DynamicResampler final {
* place. A stereo internal input buffer is always maintained even if the
* sound is mono.
*/
void UpdateResampler(int aOutRate, int aChannels);
void UpdateResampler(uint32_t aOutRate, uint32_t aChannels);
/**
* Returns true if the resampler has enough input data to provide to the
@ -107,27 +110,26 @@ class DynamicResampler final {
void AppendInputInternal(const nsTArray<const T*>& aInBuffer,
uint32_t aInFrames) {
MOZ_ASSERT(aInBuffer.Length() == (uint32_t)mChannels);
for (int i = 0; i < mChannels; ++i) {
for (uint32_t i = 0; i < mChannels; ++i) {
PushInFrames(aInBuffer[i], aInFrames, i);
}
}
void ResampleInternal(const float* aInBuffer, uint32_t* aInFrames,
float* aOutBuffer, uint32_t* aOutFrames,
int aChannelIndex);
uint32_t aChannelIndex);
void ResampleInternal(const int16_t* aInBuffer, uint32_t* aInFrames,
int16_t* aOutBuffer, uint32_t* aOutFrames,
int aChannelIndex);
uint32_t aChannelIndex);
template <typename T>
bool ResampleInternal(T* aOutBuffer, uint32_t* aOutFrames,
int aChannelIndex) {
uint32_t aChannelIndex) {
MOZ_ASSERT(mInRate);
MOZ_ASSERT(mOutRate);
MOZ_ASSERT(mChannels);
MOZ_ASSERT(aChannelIndex >= 0);
MOZ_ASSERT(aChannelIndex <= mChannels);
MOZ_ASSERT((uint32_t)aChannelIndex <= mInternalInBuffer.Length());
MOZ_ASSERT(aChannelIndex <= mInternalInBuffer.Length());
MOZ_ASSERT(aOutFrames);
MOZ_ASSERT(*aOutFrames);
@ -151,7 +153,7 @@ class DynamicResampler final {
mInternalInBuffer[aChannelIndex].ReadNoCopy(
[this, &aOutBuffer, &totalOutFramesNeeded,
aChannelIndex](const Span<const T>& aInBuffer) -> int {
aChannelIndex](const Span<const T>& aInBuffer) -> uint32_t {
if (!totalOutFramesNeeded) {
return 0;
}
@ -169,29 +171,28 @@ class DynamicResampler final {
return true;
}
bool EnoughInFrames(uint32_t aOutFrames, int aChannelIndex) const;
bool EnoughInFrames(uint32_t aOutFrames, uint32_t aChannelIndex) const;
template <typename T>
void PushInFrames(const T* aInBuffer, const uint32_t aInFrames,
int aChannelIndex) {
uint32_t aChannelIndex) {
MOZ_ASSERT(aInBuffer);
MOZ_ASSERT(aInFrames);
MOZ_ASSERT(mChannels);
MOZ_ASSERT(aChannelIndex >= 0);
MOZ_ASSERT(aChannelIndex <= mChannels);
MOZ_ASSERT((uint32_t)aChannelIndex <= mInternalInBuffer.Length());
MOZ_ASSERT(aChannelIndex <= mInternalInBuffer.Length());
mInternalInBuffer[aChannelIndex].Write(Span(aInBuffer, aInFrames));
}
void WarmUpResampler(bool aSkipLatency);
public:
const int mInRate;
const uint32_t mInRate;
const uint32_t mPreBufferFrames;
private:
int mChannels = 0;
int mOutRate;
uint32_t mChannels = 0;
uint32_t mOutRate;
AutoTArray<AudioRingBuffer, STEREO> mInternalInBuffer;
@ -216,15 +217,15 @@ class DynamicResampler final {
mSize = MAXSIZE;
} else {
PodCopy(Buffer<T>(), aInBuffer, aInFrames);
mSize = static_cast<int>(aInFrames);
mSize = aInFrames;
}
}
int Length() { return mSize; }
static const int MAXSIZE = 20;
uint32_t Length() { return mSize; }
static const uint32_t MAXSIZE = 20;
private:
float mBuffer[MAXSIZE] = {};
int mSize = 0;
uint32_t mSize = 0;
};
AutoTArray<TailBuffer, STEREO> mInputTail;
};
@ -272,7 +273,7 @@ class AudioChunkList {
* Constructor, the final total duration might be different from the requested
* `aTotalDuration`. Memory allocation takes place.
*/
AudioChunkList(int aTotalDuration, int aChannels);
AudioChunkList(uint32_t aTotalDuration, uint32_t aChannels);
AudioChunkList(const AudioChunkList&) = delete;
AudioChunkList(AudioChunkList&&) = delete;
~AudioChunkList() = default;
@ -300,7 +301,7 @@ class AudioChunkList {
/**
* Get the capacity of each individual AudioChunk in the list.
*/
int ChunkCapacity() const {
uint32_t ChunkCapacity() const {
MOZ_ASSERT(mSampleFormat == AUDIO_FORMAT_S16 ||
mSampleFormat == AUDIO_FORMAT_FLOAT32);
return mChunkCapacity;
@ -308,30 +309,30 @@ class AudioChunkList {
/**
* Get the total capacity of AudioChunkList.
*/
int TotalCapacity() const {
uint32_t TotalCapacity() const {
MOZ_ASSERT(mSampleFormat == AUDIO_FORMAT_S16 ||
mSampleFormat == AUDIO_FORMAT_FLOAT32);
return CheckedInt<int>(mChunkCapacity * mChunks.Length()).value();
return CheckedInt<uint32_t>(mChunkCapacity * mChunks.Length()).value();
}
/**
* Update the channel count of the AudioChunkList. Memory allocation is
* taking place.
*/
void Update(int aChannels);
void Update(uint32_t aChannels);
private:
void IncrementIndex() {
++mIndex;
mIndex = CheckedInt<int>(mIndex % mChunks.Length()).value();
mIndex = CheckedInt<uint32_t>(mIndex % mChunks.Length()).value();
}
void CreateChunks(int aNumOfChunks, int aChannels);
void UpdateToMonoOrStereo(int aChannels);
void CreateChunks(uint32_t aNumOfChunks, uint32_t aChannels);
void UpdateToMonoOrStereo(uint32_t aChannels);
private:
nsTArray<AudioChunk> mChunks;
int mIndex = 0;
int mChunkCapacity = 128;
uint32_t mIndex = 0;
uint32_t mChunkCapacity = 128;
AudioSampleFormat mSampleFormat = AUDIO_FORMAT_SILENCE;
};
@ -355,7 +356,8 @@ class AudioChunkList {
*/
class AudioResampler final {
public:
AudioResampler(int aInRate, int aOutRate, uint32_t aPreBufferFrames = 0);
AudioResampler(uint32_t aInRate, uint32_t aOutRate,
uint32_t aPreBufferFrames = 0);
/**
* Append input data into the resampler internal buffer. Copy/move of the
@ -366,12 +368,12 @@ class AudioResampler final {
/**
* Get the duration of the internal input buffer in frames.
*/
int InputDuration() const;
uint32_t InputDuration() const;
/**
* Get the duration of the remaining space in the internal input buffer in
* frames.
*/
int InputRemainingDuration() const;
uint32_t InputRemainingDuration() const;
/*
* Reguest `aOutFrames` of audio in the output sample rate. The internal
@ -383,15 +385,15 @@ class AudioResampler final {
/*
* Updates the output rate that will be used by the resampler.
*/
void UpdateOutRate(int aOutRate) {
void UpdateOutRate(uint32_t aOutRate) {
Update(aOutRate, mResampler.GetChannels());
}
private:
void UpdateChannels(int aChannels) {
void UpdateChannels(uint32_t aChannels) {
Update(mResampler.GetOutRate(), aChannels);
}
void Update(int aOutRate, int aChannels);
void Update(uint32_t aOutRate, uint32_t aChannels);
private:
DynamicResampler mResampler;

Просмотреть файл

@ -14,12 +14,13 @@
// Runs UpdateClock() and checks that the reported correction level doesn't
// change for enough time to trigger a correction update on the first
// following UpdateClock(). Returns the first reported correction level.
static float RunUntilCorrectionUpdate(ClockDrift& aC, int aSource, int aTarget,
int aBuffering, int aSaturation,
int aSourceOffset = 0,
int aTargetOffset = 0) {
static float RunUntilCorrectionUpdate(ClockDrift& aC, uint32_t aSource,
uint32_t aTarget, uint32_t aBuffering,
uint32_t aSaturation,
uint32_t aSourceOffset = 0,
uint32_t aTargetOffset = 0) {
Maybe<float> correction;
for (int s = aSourceOffset, t = aTargetOffset;
for (uint32_t s = aSourceOffset, t = aTargetOffset;
s < aC.mSourceRate && t < aC.mTargetRate; s += aSource, t += aTarget) {
aC.UpdateClock(aSource, aTarget, aBuffering, aSaturation);
if (correction) {
@ -35,7 +36,7 @@ static float RunUntilCorrectionUpdate(ClockDrift& aC, int aSource, int aTarget,
TEST(TestClockDrift, Basic)
{
// Keep buffered frames to the wanted level in order to not affect that test.
const int buffered = 5 * 480;
const uint32_t buffered = 5 * 480;
ClockDrift c(48000, 48000, buffered);
EXPECT_EQ(c.GetCorrection(), 1.0);
@ -56,7 +57,7 @@ TEST(TestClockDrift, Basic)
TEST(TestClockDrift, BasicResampler)
{
// Keep buffered frames to the wanted level in order to not affect that test.
const int buffered = 5 * 240;
const uint32_t buffered = 5 * 240;
ClockDrift c(24000, 48000, buffered);
@ -147,7 +148,7 @@ TEST(TestClockDrift, BufferedInputWithResampling)
TEST(TestClockDrift, Clamp)
{
// Keep buffered frames to the wanted level in order to not affect that test.
const int buffered = 5 * 480;
const uint32_t buffered = 5 * 480;
ClockDrift c(48000, 48000, buffered);
@ -166,7 +167,7 @@ TEST(TestClockDrift, Clamp)
TEST(TestClockDrift, SmallDiff)
{
// Keep buffered frames to the wanted level in order to not affect that test.
const int buffered = 5 * 480;
const uint32_t buffered = 5 * 480;
ClockDrift c(48000, 48000, buffered);
@ -187,7 +188,7 @@ TEST(TestClockDrift, SmallBufferedFrames)
ClockDrift c(48000, 48000, 5 * 480);
EXPECT_FLOAT_EQ(c.GetCorrection(), 1.0);
for (int i = 0; i < 10; ++i) {
for (uint32_t i = 0; i < 10; ++i) {
c.UpdateClock(480, 480, 5 * 480, 5 * 480);
}
EXPECT_FLOAT_EQ(c.GetCorrection(), 1.0);
@ -206,7 +207,7 @@ void printAudioSegment(const AudioSegment& segment) {
for (AudioSegment::ConstChunkIterator iter(segment); !iter.IsEnded();
iter.Next()) {
const AudioChunk& c = *iter;
for (int i = 0; i < c.GetDuration(); ++i) {
for (uint32_t i = 0; i < c.GetDuration(); ++i) {
if (c.mBufferFormat == AUDIO_FORMAT_FLOAT32) {
printf("%f\n", c.ChannelData<float>()[0][i]);
} else {
@ -217,7 +218,7 @@ void printAudioSegment(const AudioSegment& segment) {
}
template <class T>
AudioChunk CreateAudioChunk(uint32_t aFrames, int aChannels,
AudioChunk CreateAudioChunk(uint32_t aFrames, uint32_t aChannels,
AudioSampleFormat aSampleFormat);
void testAudioCorrection(int32_t aSourceRate, int32_t aTargetRate) {
@ -237,7 +238,7 @@ void testAudioCorrection(int32_t aSourceRate, int32_t aTargetRate) {
const uint32_t targetFrames = sampleRateReceiver / 100;
// Run for some time: 3 * 1050 = 3150 iterations
for (int j = 0; j < 3; ++j) {
for (uint32_t j = 0; j < 3; ++j) {
// apply some drift
if (j % 2 == 0) {
sourceFrames =
@ -249,7 +250,7 @@ void testAudioCorrection(int32_t aSourceRate, int32_t aTargetRate) {
// 10.5 seconds, allows for at least 10 correction changes, to stabilize
// around the desired buffer.
for (int n = 0; n < 1050; ++n) {
for (uint32_t n = 0; n < 1050; ++n) {
// Create the input (sine tone)
AudioSegment inSegment;
tone.Generate(inSegment, sourceFrames);
@ -291,7 +292,7 @@ TEST(TestAudioDriftCorrection, Basic)
testAudioCorrection(23458, 25113);
}
void testMonoToStereoInput(int aSourceRate, int aTargetRate) {
void testMonoToStereoInput(uint32_t aSourceRate, uint32_t aTargetRate) {
const uint32_t frequency = 100;
const uint32_t sampleRateTransmitter = aSourceRate;
const uint32_t sampleRateReceiver = aTargetRate;
@ -307,7 +308,7 @@ void testMonoToStereoInput(int aSourceRate, int aTargetRate) {
const uint32_t targetFrames = sampleRateReceiver / 100;
// Run for some time: 6 * 250 = 1500 iterations
for (int j = 0; j < 6; ++j) {
for (uint32_t j = 0; j < 6; ++j) {
// apply some drift
if (j % 2 == 0) {
sourceFrames = sampleRateTransmitter / 100 + 10;
@ -315,7 +316,7 @@ void testMonoToStereoInput(int aSourceRate, int aTargetRate) {
sourceFrames = sampleRateTransmitter / 100 - 10;
}
for (int n = 0; n < 250; ++n) {
for (uint32_t n = 0; n < 250; ++n) {
// Create the input (sine tone) of two chunks.
AudioSegment inSegment;
monoTone.Generate(inSegment, sourceFrames / 2);
@ -362,7 +363,7 @@ TEST(TestAudioDriftCorrection, NotEnoughFrames)
AudioDriftCorrection ad(sampleRateTransmitter, sampleRateReceiver);
const uint32_t targetFrames = sampleRateReceiver / 100;
for (int i = 0; i < 7; ++i) {
for (uint32_t i = 0; i < 7; ++i) {
// Input is something small, 10 frames here, in order to dry out fast,
// after 4 iterations
AudioChunk chunk = CreateAudioChunk<float>(10, 1, AUDIO_FORMAT_FLOAT32);
@ -388,7 +389,7 @@ TEST(TestAudioDriftCorrection, CrashInAudioResampler)
AudioDriftCorrection ad(sampleRateTransmitter, sampleRateReceiver);
const uint32_t targetFrames = sampleRateReceiver / 100;
for (int i = 0; i < 100; ++i) {
for (uint32_t i = 0; i < 100; ++i) {
AudioChunk chunk = CreateAudioChunk<float>(sampleRateTransmitter / 1000, 1,
AUDIO_FORMAT_FLOAT32);
AudioSegment inSegment;

Разница между файлами не показана из-за своего большого размера Загрузить разницу

Просмотреть файл

@ -15,9 +15,9 @@ TEST(TestDynamicResampler, SameRates_Float1)
{
const uint32_t in_frames = 100;
const uint32_t out_frames = 100;
int channels = 2;
int in_rate = 44100;
int out_rate = 44100;
uint32_t channels = 2;
uint32_t in_rate = 44100;
uint32_t out_rate = 44100;
DynamicResampler dr(in_rate, out_rate);
dr.SetSampleFormat(AUDIO_FORMAT_FLOAT32);
@ -81,9 +81,9 @@ TEST(TestDynamicResampler, SameRates_Short1)
{
uint32_t in_frames = 2;
uint32_t out_frames = 2;
int channels = 2;
int in_rate = 44100;
int out_rate = 44100;
uint32_t channels = 2;
uint32_t in_rate = 44100;
uint32_t out_rate = 44100;
DynamicResampler dr(in_rate, out_rate);
dr.SetSampleFormat(AUDIO_FORMAT_S16);
@ -126,9 +126,9 @@ TEST(TestDynamicResampler, SameRates_Float2)
{
uint32_t in_frames = 3;
uint32_t out_frames = 2;
int channels = 2;
int in_rate = 44100;
int out_rate = 44100;
uint32_t channels = 2;
uint32_t in_rate = 44100;
uint32_t out_rate = 44100;
DynamicResampler dr(in_rate, out_rate);
dr.SetSampleFormat(AUDIO_FORMAT_FLOAT32);
@ -181,9 +181,9 @@ TEST(TestDynamicResampler, SameRates_Short2)
{
uint32_t in_frames = 3;
uint32_t out_frames = 2;
int channels = 2;
int in_rate = 44100;
int out_rate = 44100;
uint32_t channels = 2;
uint32_t in_rate = 44100;
uint32_t out_rate = 44100;
DynamicResampler dr(in_rate, out_rate);
dr.SetSampleFormat(AUDIO_FORMAT_S16);
@ -236,9 +236,9 @@ TEST(TestDynamicResampler, SameRates_Float3)
{
uint32_t in_frames = 2;
uint32_t out_frames = 3;
int channels = 2;
int in_rate = 44100;
int out_rate = 44100;
uint32_t channels = 2;
uint32_t in_rate = 44100;
uint32_t out_rate = 44100;
DynamicResampler dr(in_rate, out_rate);
dr.SetSampleFormat(AUDIO_FORMAT_FLOAT32);
@ -284,9 +284,9 @@ TEST(TestDynamicResampler, SameRates_Short3)
{
uint32_t in_frames = 2;
uint32_t out_frames = 3;
int channels = 2;
int in_rate = 44100;
int out_rate = 44100;
uint32_t channels = 2;
uint32_t in_rate = 44100;
uint32_t out_rate = 44100;
DynamicResampler dr(in_rate, out_rate);
dr.SetSampleFormat(AUDIO_FORMAT_S16);
@ -332,9 +332,9 @@ TEST(TestDynamicResampler, UpdateOutRate_Float)
{
uint32_t in_frames = 10;
uint32_t out_frames = 40;
int channels = 2;
int in_rate = 24000;
int out_rate = 48000;
uint32_t channels = 2;
uint32_t in_rate = 24000;
uint32_t out_rate = 48000;
uint32_t pre_buffer = 20;
@ -390,9 +390,9 @@ TEST(TestDynamicResampler, UpdateOutRate_Short)
{
uint32_t in_frames = 10;
uint32_t out_frames = 40;
int channels = 2;
int in_rate = 24000;
int out_rate = 48000;
uint32_t channels = 2;
uint32_t in_rate = 24000;
uint32_t out_rate = 48000;
uint32_t pre_buffer = 20;
@ -448,15 +448,15 @@ TEST(TestDynamicResampler, BigRangeOutRates_Float)
{
uint32_t in_frames = 10;
uint32_t out_frames = 10;
int channels = 2;
int in_rate = 44100;
int out_rate = 44100;
uint32_t channels = 2;
uint32_t in_rate = 44100;
uint32_t out_rate = 44100;
uint32_t pre_buffer = 20;
DynamicResampler dr(in_rate, out_rate, pre_buffer);
dr.SetSampleFormat(AUDIO_FORMAT_FLOAT32);
const int in_capacity = 40;
const uint32_t in_capacity = 40;
float in_ch1[in_capacity] = {};
float in_ch2[in_capacity] = {};
for (uint32_t i = 0; i < in_capacity; ++i) {
@ -467,11 +467,11 @@ TEST(TestDynamicResampler, BigRangeOutRates_Float)
in_buffer[0] = in_ch1;
in_buffer[1] = in_ch2;
const int out_capacity = 1000;
const uint32_t out_capacity = 1000;
float out_ch1[out_capacity] = {};
float out_ch2[out_capacity] = {};
for (int rate = 10000; rate < 90000; ++rate) {
for (uint32_t rate = 10000; rate < 90000; ++rate) {
out_rate = rate;
dr.UpdateResampler(out_rate, channels);
EXPECT_EQ(dr.GetOutRate(), out_rate);
@ -479,7 +479,7 @@ TEST(TestDynamicResampler, BigRangeOutRates_Float)
in_frames = 20; // more than we need
out_frames = in_frames * out_rate / in_rate;
uint32_t expected_out_frames = out_frames;
for (int y = 0; y < 2; ++y) {
for (uint32_t y = 0; y < 2; ++y) {
dr.AppendInput(in_buffer, in_frames);
bool rv = dr.Resample(out_ch1, &out_frames, 0);
EXPECT_TRUE(rv);
@ -495,15 +495,15 @@ TEST(TestDynamicResampler, BigRangeOutRates_Short)
{
uint32_t in_frames = 10;
uint32_t out_frames = 10;
int channels = 2;
int in_rate = 44100;
int out_rate = 44100;
uint32_t channels = 2;
uint32_t in_rate = 44100;
uint32_t out_rate = 44100;
uint32_t pre_buffer = 20;
DynamicResampler dr(in_rate, out_rate, pre_buffer);
dr.SetSampleFormat(AUDIO_FORMAT_S16);
const int in_capacity = 40;
const uint32_t in_capacity = 40;
short in_ch1[in_capacity] = {};
short in_ch2[in_capacity] = {};
for (uint32_t i = 0; i < in_capacity; ++i) {
@ -514,17 +514,17 @@ TEST(TestDynamicResampler, BigRangeOutRates_Short)
in_buffer[0] = in_ch1;
in_buffer[1] = in_ch2;
const int out_capacity = 1000;
const uint32_t out_capacity = 1000;
short out_ch1[out_capacity] = {};
short out_ch2[out_capacity] = {};
for (int rate = 10000; rate < 90000; ++rate) {
for (uint32_t rate = 10000; rate < 90000; ++rate) {
out_rate = rate;
dr.UpdateResampler(out_rate, channels);
in_frames = 20; // more than we need
out_frames = in_frames * out_rate / in_rate;
uint32_t expected_out_frames = out_frames;
for (int y = 0; y < 2; ++y) {
for (uint32_t y = 0; y < 2; ++y) {
dr.AppendInput(in_buffer, in_frames);
bool rv = dr.Resample(out_ch1, &out_frames, 0);
EXPECT_TRUE(rv);
@ -540,9 +540,9 @@ TEST(TestDynamicResampler, UpdateChannels_Float)
{
uint32_t in_frames = 10;
uint32_t out_frames = 10;
int channels = 2;
int in_rate = 44100;
int out_rate = 48000;
uint32_t channels = 2;
uint32_t in_rate = 44100;
uint32_t out_rate = 48000;
DynamicResampler dr(in_rate, out_rate);
dr.SetSampleFormat(AUDIO_FORMAT_FLOAT32);
@ -571,7 +571,7 @@ TEST(TestDynamicResampler, UpdateChannels_Float)
// Add 3rd channel
dr.UpdateResampler(out_rate, 3);
EXPECT_EQ(dr.GetOutRate(), out_rate);
EXPECT_EQ(dr.GetChannels(), 3);
EXPECT_EQ(dr.GetChannels(), 3u);
float in_ch3[10] = {};
for (uint32_t i = 0; i < in_frames; ++i) {
@ -603,7 +603,7 @@ TEST(TestDynamicResampler, UpdateChannels_Float)
dr.UpdateResampler(out_rate, 4);
EXPECT_EQ(dr.GetOutRate(), out_rate);
EXPECT_EQ(dr.GetChannels(), 4);
EXPECT_EQ(dr.GetChannels(), 4u);
dr.AppendInput(in_buffer, in_frames);
rv = dr.Resample(out_ch1, &out_frames, 0);
@ -624,9 +624,9 @@ TEST(TestDynamicResampler, UpdateChannels_Short)
{
uint32_t in_frames = 10;
uint32_t out_frames = 10;
int channels = 2;
int in_rate = 44100;
int out_rate = 48000;
uint32_t channels = 2;
uint32_t in_rate = 44100;
uint32_t out_rate = 48000;
DynamicResampler dr(in_rate, out_rate);
dr.SetSampleFormat(AUDIO_FORMAT_S16);
@ -655,7 +655,7 @@ TEST(TestDynamicResampler, UpdateChannels_Short)
// Add 3rd channel
dr.UpdateResampler(out_rate, 3);
EXPECT_EQ(dr.GetOutRate(), out_rate);
EXPECT_EQ(dr.GetChannels(), 3);
EXPECT_EQ(dr.GetChannels(), 3u);
short in_ch3[10] = {};
for (uint32_t i = 0; i < in_frames; ++i) {
@ -688,7 +688,7 @@ TEST(TestDynamicResampler, UpdateChannels_Short)
dr.UpdateResampler(out_rate, 4);
EXPECT_EQ(dr.GetOutRate(), out_rate);
EXPECT_EQ(dr.GetChannels(), 4);
EXPECT_EQ(dr.GetChannels(), 4u);
dr.AppendInput(in_buffer, in_frames);
rv = dr.Resample(out_ch1, &out_frames, 0);
@ -709,14 +709,14 @@ TEST(TestAudioChunkList, Basic1)
{
AudioChunkList list(256, 2);
list.SetSampleFormat(AUDIO_FORMAT_FLOAT32);
EXPECT_EQ(list.ChunkCapacity(), 128);
EXPECT_EQ(list.TotalCapacity(), 256);
EXPECT_EQ(list.ChunkCapacity(), 128u);
EXPECT_EQ(list.TotalCapacity(), 256u);
AudioChunk& c1 = list.GetNext();
float* c1_ch1 = c1.ChannelDataForWrite<float>(0);
float* c1_ch2 = c1.ChannelDataForWrite<float>(1);
EXPECT_EQ(c1.mBufferFormat, AUDIO_FORMAT_FLOAT32);
for (int i = 0; i < list.ChunkCapacity(); ++i) {
for (uint32_t i = 0; i < list.ChunkCapacity(); ++i) {
c1_ch1[i] = c1_ch2[i] = 0.01f * static_cast<float>(i);
}
AudioChunk& c2 = list.GetNext();
@ -728,7 +728,7 @@ TEST(TestAudioChunkList, Basic1)
EXPECT_EQ(c1.mBuffer.get(), c3.mBuffer.get());
float* c3_ch1 = c3.ChannelDataForWrite<float>(0);
float* c3_ch2 = c3.ChannelDataForWrite<float>(1);
for (int i = 0; i < list.ChunkCapacity(); ++i) {
for (uint32_t i = 0; i < list.ChunkCapacity(); ++i) {
EXPECT_FLOAT_EQ(c1_ch1[i], c3_ch1[i]);
EXPECT_FLOAT_EQ(c1_ch2[i], c3_ch2[i]);
}
@ -738,14 +738,14 @@ TEST(TestAudioChunkList, Basic2)
{
AudioChunkList list(256, 2);
list.SetSampleFormat(AUDIO_FORMAT_S16);
EXPECT_EQ(list.ChunkCapacity(), 256);
EXPECT_EQ(list.TotalCapacity(), 512);
EXPECT_EQ(list.ChunkCapacity(), 256u);
EXPECT_EQ(list.TotalCapacity(), 512u);
AudioChunk& c1 = list.GetNext();
EXPECT_EQ(c1.mBufferFormat, AUDIO_FORMAT_S16);
short* c1_ch1 = c1.ChannelDataForWrite<short>(0);
short* c1_ch2 = c1.ChannelDataForWrite<short>(1);
for (int i = 0; i < list.ChunkCapacity(); ++i) {
for (uint32_t i = 0; i < list.ChunkCapacity(); ++i) {
c1_ch1[i] = c1_ch2[i] = static_cast<short>(i);
}
AudioChunk& c2 = list.GetNext();
@ -761,7 +761,7 @@ TEST(TestAudioChunkList, Basic2)
EXPECT_EQ(c1.mBuffer.get(), c5.mBuffer.get());
short* c5_ch1 = c5.ChannelDataForWrite<short>(0);
short* c5_ch2 = c5.ChannelDataForWrite<short>(1);
for (int i = 0; i < list.ChunkCapacity(); ++i) {
for (uint32_t i = 0; i < list.ChunkCapacity(); ++i) {
EXPECT_EQ(c1_ch1[i], c5_ch1[i]);
EXPECT_EQ(c1_ch2[i], c5_ch2[i]);
}
@ -771,8 +771,8 @@ TEST(TestAudioChunkList, Basic3)
{
AudioChunkList list(260, 2);
list.SetSampleFormat(AUDIO_FORMAT_FLOAT32);
EXPECT_EQ(list.ChunkCapacity(), 128);
EXPECT_EQ(list.TotalCapacity(), 256 + 128);
EXPECT_EQ(list.ChunkCapacity(), 128u);
EXPECT_EQ(list.TotalCapacity(), 256u + 128u);
AudioChunk& c1 = list.GetNext();
AudioChunk& c2 = list.GetNext();
@ -787,8 +787,8 @@ TEST(TestAudioChunkList, Basic4)
{
AudioChunkList list(260, 2);
list.SetSampleFormat(AUDIO_FORMAT_S16);
EXPECT_EQ(list.ChunkCapacity(), 256);
EXPECT_EQ(list.TotalCapacity(), 512 + 256);
EXPECT_EQ(list.ChunkCapacity(), 256u);
EXPECT_EQ(list.TotalCapacity(), 512u + 256u);
AudioChunk& c1 = list.GetNext();
AudioChunk& c2 = list.GetNext();
@ -826,7 +826,7 @@ TEST(TestAudioChunkList, UpdateBetweenMonoAndStereo)
AudioChunk& c1 = list.GetNext();
float* c1_ch1 = c1.ChannelDataForWrite<float>(0);
float* c1_ch2 = c1.ChannelDataForWrite<float>(1);
for (int i = 0; i < list.ChunkCapacity(); ++i) {
for (uint32_t i = 0; i < list.ChunkCapacity(); ++i) {
c1_ch1[i] = c1_ch2[i] = 0.01f * static_cast<float>(i);
}
@ -839,7 +839,7 @@ TEST(TestAudioChunkList, UpdateBetweenMonoAndStereo)
AudioChunk& c3 = list.GetNext();
float* c3_ch1 = c3.ChannelDataForWrite<float>(0);
for (int i = 0; i < list.ChunkCapacity(); ++i) {
for (uint32_t i = 0; i < list.ChunkCapacity(); ++i) {
EXPECT_FLOAT_EQ(c3_ch1[i], c1_ch1[i]);
}
@ -849,7 +849,7 @@ TEST(TestAudioChunkList, UpdateBetweenMonoAndStereo)
EXPECT_EQ(static_cast<SharedChannelArrayBuffer<float>*>(c3.mBuffer.get())
->mBuffers[0]
.Length(),
(uint32_t)list.ChunkCapacity());
list.ChunkCapacity());
// Upmix to stereo
list.Update(2);
@ -861,18 +861,18 @@ TEST(TestAudioChunkList, UpdateBetweenMonoAndStereo)
EXPECT_EQ(static_cast<SharedChannelArrayBuffer<float>*>(c5.mBuffer.get())
->mBuffers[0]
.Length(),
(uint32_t)list.ChunkCapacity());
list.ChunkCapacity());
EXPECT_EQ(static_cast<SharedChannelArrayBuffer<float>*>(c5.mBuffer.get())
->mBuffers[1]
.Length(),
(uint32_t)list.ChunkCapacity());
list.ChunkCapacity());
// Downmix to mono
list.Update(1);
AudioChunk& c7 = list.GetNext();
float* c7_ch1 = c7.ChannelDataForWrite<float>(0);
for (int i = 0; i < list.ChunkCapacity(); ++i) {
for (uint32_t i = 0; i < list.ChunkCapacity(); ++i) {
EXPECT_FLOAT_EQ(c7_ch1[i], c1_ch1[i]);
}
@ -882,7 +882,7 @@ TEST(TestAudioChunkList, UpdateBetweenMonoAndStereo)
EXPECT_EQ(static_cast<SharedChannelArrayBuffer<float>*>(c7.mBuffer.get())
->mBuffers[0]
.Length(),
(uint32_t)list.ChunkCapacity());
list.ChunkCapacity());
}
TEST(TestAudioChunkList, ConsumeAndForget)
@ -909,7 +909,7 @@ TEST(TestAudioChunkList, ConsumeAndForget)
}
template <class T>
AudioChunk CreateAudioChunk(uint32_t aFrames, int aChannels,
AudioChunk CreateAudioChunk(uint32_t aFrames, uint32_t aChannels,
AudioSampleFormat aSampleFormat) {
AudioChunk chunk;
nsTArray<nsTArray<T>> buffer;
@ -918,7 +918,7 @@ AudioChunk CreateAudioChunk(uint32_t aFrames, int aChannels,
nsTArray<const T*> bufferPtrs;
bufferPtrs.AppendElements(aChannels);
for (int i = 0; i < aChannels; ++i) {
for (uint32_t i = 0; i < aChannels; ++i) {
T* ptr = buffer[i].AppendElements(aFrames);
bufferPtrs[i] = ptr;
for (uint32_t j = 0; j < aFrames; ++j) {
@ -933,7 +933,7 @@ AudioChunk CreateAudioChunk(uint32_t aFrames, int aChannels,
chunk.mBuffer = new mozilla::SharedChannelArrayBuffer(std::move(buffer));
chunk.mBufferFormat = aSampleFormat;
chunk.mChannelData.AppendElements(aChannels);
for (int i = 0; i < aChannels; ++i) {
for (uint32_t i = 0; i < aChannels; ++i) {
chunk.mChannelData[i] = bufferPtrs[i];
}
chunk.mDuration = aFrames;
@ -941,7 +941,7 @@ AudioChunk CreateAudioChunk(uint32_t aFrames, int aChannels,
}
template <class T>
AudioSegment CreateAudioSegment(uint32_t aFrames, int aChannels,
AudioSegment CreateAudioSegment(uint32_t aFrames, uint32_t aChannels,
AudioSampleFormat aSampleFormat) {
AudioSegment segment;
AudioChunk chunk = CreateAudioChunk<T>(aFrames, aChannels, aSampleFormat);
@ -953,9 +953,9 @@ TEST(TestAudioResampler, OutAudioSegment_Float)
{
uint32_t in_frames = 10;
uint32_t out_frames = 40;
int channels = 2;
int in_rate = 24000;
int out_rate = 48000;
uint32_t channels = 2;
uint32_t in_rate = 24000;
uint32_t out_rate = 48000;
uint32_t pre_buffer = 21;
@ -999,9 +999,9 @@ TEST(TestAudioResampler, OutAudioSegment_Short)
{
uint32_t in_frames = 10;
uint32_t out_frames = 40;
int channels = 2;
int in_rate = 24000;
int out_rate = 48000;
uint32_t channels = 2;
uint32_t in_rate = 24000;
uint32_t out_rate = 48000;
uint32_t pre_buffer = 21;
@ -1045,9 +1045,9 @@ TEST(TestAudioResampler, OutAudioSegmentFail_Float)
{
const uint32_t in_frames = 130;
const uint32_t out_frames = 300;
int channels = 2;
int in_rate = 24000;
int out_rate = 48000;
uint32_t channels = 2;
uint32_t in_rate = 24000;
uint32_t out_rate = 48000;
uint32_t pre_buffer = 5;
@ -1067,9 +1067,9 @@ TEST(TestAudioResampler, InAudioSegment_Float)
{
uint32_t in_frames = 10;
uint32_t out_frames = 40;
int channels = 2;
int in_rate = 24000;
int out_rate = 48000;
uint32_t channels = 2;
uint32_t in_rate = 24000;
uint32_t out_rate = 48000;
uint32_t pre_buffer = 10;
AudioResampler dr(in_rate, out_rate, pre_buffer);
@ -1087,7 +1087,7 @@ TEST(TestAudioResampler, InAudioSegment_Float)
nsTArray<const float*> bufferPtrs;
bufferPtrs.AppendElements(channels);
for (int i = 0; i < channels; ++i) {
for (uint32_t i = 0; i < channels; ++i) {
float* ptr = buffer[i].AppendElements(5);
bufferPtrs[i] = ptr;
for (uint32_t j = 0; j < 5; ++j) {
@ -1098,7 +1098,7 @@ TEST(TestAudioResampler, InAudioSegment_Float)
chunk2.mBuffer = new mozilla::SharedChannelArrayBuffer(std::move(buffer));
chunk2.mBufferFormat = AUDIO_FORMAT_FLOAT32;
chunk2.mChannelData.AppendElements(channels);
for (int i = 0; i < channels; ++i) {
for (uint32_t i = 0; i < channels; ++i) {
chunk2.mChannelData[i] = bufferPtrs[i];
}
chunk2.mDuration = in_frames / 2;
@ -1121,9 +1121,9 @@ TEST(TestAudioResampler, InAudioSegment_Short)
{
uint32_t in_frames = 10;
uint32_t out_frames = 40;
int channels = 2;
int in_rate = 24000;
int out_rate = 48000;
uint32_t channels = 2;
uint32_t in_rate = 24000;
uint32_t out_rate = 48000;
uint32_t pre_buffer = 10;
AudioResampler dr(in_rate, out_rate, pre_buffer);
@ -1142,7 +1142,7 @@ TEST(TestAudioResampler, InAudioSegment_Short)
nsTArray<const short*> bufferPtrs;
bufferPtrs.AppendElements(channels);
for (int i = 0; i < channels; ++i) {
for (uint32_t i = 0; i < channels; ++i) {
short* ptr = buffer[i].AppendElements(5);
bufferPtrs[i] = ptr;
for (uint32_t j = 0; j < 5; ++j) {
@ -1153,7 +1153,7 @@ TEST(TestAudioResampler, InAudioSegment_Short)
chunk2.mBuffer = new mozilla::SharedChannelArrayBuffer(std::move(buffer));
chunk2.mBufferFormat = AUDIO_FORMAT_S16;
chunk2.mChannelData.AppendElements(channels);
for (int i = 0; i < channels; ++i) {
for (uint32_t i = 0; i < channels; ++i) {
chunk2.mChannelData[i] = bufferPtrs[i];
}
chunk2.mDuration = in_frames / 2;
@ -1175,9 +1175,9 @@ TEST(TestAudioResampler, ChannelChange_MonoToStereo)
{
uint32_t in_frames = 10;
uint32_t out_frames = 40;
// int channels = 2;
int in_rate = 24000;
int out_rate = 48000;
// uint32_t channels = 2;
uint32_t in_rate = 24000;
uint32_t out_rate = 48000;
uint32_t pre_buffer = 0;
@ -1205,9 +1205,9 @@ TEST(TestAudioResampler, ChannelChange_StereoToMono)
{
uint32_t in_frames = 10;
uint32_t out_frames = 40;
// int channels = 2;
int in_rate = 24000;
int out_rate = 48000;
// uint32_t channels = 2;
uint32_t in_rate = 24000;
uint32_t out_rate = 48000;
uint32_t pre_buffer = 0;
@ -1235,9 +1235,9 @@ TEST(TestAudioResampler, ChannelChange_StereoToQuad)
{
uint32_t in_frames = 10;
uint32_t out_frames = 40;
// int channels = 2;
int in_rate = 24000;
int out_rate = 48000;
// uint32_t channels = 2;
uint32_t in_rate = 24000;
uint32_t out_rate = 48000;
uint32_t pre_buffer = 0;
@ -1270,9 +1270,9 @@ TEST(TestAudioResampler, ChannelChange_QuadToStereo)
{
uint32_t in_frames = 10;
uint32_t out_frames = 40;
// int channels = 2;
int in_rate = 24000;
int out_rate = 48000;
// uint32_t channels = 2;
uint32_t in_rate = 24000;
uint32_t out_rate = 48000;
AudioResampler dr(in_rate, out_rate);
@ -1303,8 +1303,8 @@ void printAudioSegment(const AudioSegment& segment);
TEST(TestAudioResampler, ChannelChange_Discontinuity)
{
int in_rate = 24000;
int out_rate = 48000;
uint32_t in_rate = 24000;
uint32_t out_rate = 48000;
const float amplitude = 0.5;
const float frequency = 200;
@ -1312,20 +1312,20 @@ TEST(TestAudioResampler, ChannelChange_Discontinuity)
float time = 0.0;
const float deltaTime = 1.0f / static_cast<float>(in_rate);
int in_frames = in_rate / 100;
int out_frames = out_rate / 100;
uint32_t in_frames = in_rate / 100;
uint32_t out_frames = out_rate / 100;
AudioResampler dr(in_rate, out_rate);
AudioChunk monoChunk =
CreateAudioChunk<float>(in_frames, 1, AUDIO_FORMAT_FLOAT32);
for (int i = 0; i < monoChunk.GetDuration(); ++i) {
for (uint32_t i = 0; i < monoChunk.GetDuration(); ++i) {
double value = amplitude * sin(2 * M_PI * frequency * time + phase);
monoChunk.ChannelDataForWrite<float>(0)[i] = static_cast<float>(value);
time += deltaTime;
}
AudioChunk stereoChunk =
CreateAudioChunk<float>(in_frames, 2, AUDIO_FORMAT_FLOAT32);
for (int i = 0; i < stereoChunk.GetDuration(); ++i) {
for (uint32_t i = 0; i < stereoChunk.GetDuration(); ++i) {
double value = amplitude * sin(2 * M_PI * frequency * time + phase);
stereoChunk.ChannelDataForWrite<float>(0)[i] = static_cast<float>(value);
if (stereoChunk.ChannelCount() == 2) {
@ -1359,8 +1359,8 @@ TEST(TestAudioResampler, ChannelChange_Discontinuity)
TEST(TestAudioResampler, ChannelChange_Discontinuity2)
{
int in_rate = 24000;
int out_rate = 48000;
uint32_t in_rate = 24000;
uint32_t out_rate = 48000;
const float amplitude = 0.5;
const float frequency = 200;
@ -1368,20 +1368,20 @@ TEST(TestAudioResampler, ChannelChange_Discontinuity2)
float time = 0.0;
const float deltaTime = 1.0f / static_cast<float>(in_rate);
int in_frames = in_rate / 100;
int out_frames = out_rate / 100;
uint32_t in_frames = in_rate / 100;
uint32_t out_frames = out_rate / 100;
AudioResampler dr(in_rate, out_rate, 10);
AudioChunk monoChunk =
CreateAudioChunk<float>(in_frames / 2, 1, AUDIO_FORMAT_FLOAT32);
for (int i = 0; i < monoChunk.GetDuration(); ++i) {
for (uint32_t i = 0; i < monoChunk.GetDuration(); ++i) {
double value = amplitude * sin(2 * M_PI * frequency * time + phase);
monoChunk.ChannelDataForWrite<float>(0)[i] = static_cast<float>(value);
time += deltaTime;
}
AudioChunk stereoChunk =
CreateAudioChunk<float>(in_frames / 2, 2, AUDIO_FORMAT_FLOAT32);
for (int i = 0; i < stereoChunk.GetDuration(); ++i) {
for (uint32_t i = 0; i < stereoChunk.GetDuration(); ++i) {
double value = amplitude * sin(2 * M_PI * frequency * time + phase);
stereoChunk.ChannelDataForWrite<float>(0)[i] = static_cast<float>(value);
if (stereoChunk.ChannelCount() == 2) {
@ -1420,8 +1420,8 @@ TEST(TestAudioResampler, ChannelChange_Discontinuity2)
TEST(TestAudioResampler, ChannelChange_Discontinuity3)
{
int in_rate = 48000;
int out_rate = 48000;
uint32_t in_rate = 48000;
uint32_t out_rate = 48000;
const float amplitude = 0.5;
const float frequency = 200;
@ -1429,13 +1429,13 @@ TEST(TestAudioResampler, ChannelChange_Discontinuity3)
float time = 0.0;
const float deltaTime = 1.0f / static_cast<float>(in_rate);
int in_frames = in_rate / 100;
int out_frames = out_rate / 100;
uint32_t in_frames = in_rate / 100;
uint32_t out_frames = out_rate / 100;
AudioResampler dr(in_rate, out_rate, 10);
AudioChunk stereoChunk =
CreateAudioChunk<float>(in_frames, 2, AUDIO_FORMAT_FLOAT32);
for (int i = 0; i < stereoChunk.GetDuration(); ++i) {
for (uint32_t i = 0; i < stereoChunk.GetDuration(); ++i) {
double value = amplitude * sin(2 * M_PI * frequency * time + phase);
stereoChunk.ChannelDataForWrite<float>(0)[i] = static_cast<float>(value);
if (stereoChunk.ChannelCount() == 2) {