summaryrefslogtreecommitdiffstats
path: root/services/audioflinger/tests/resampler_tests.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'services/audioflinger/tests/resampler_tests.cpp')
-rw-r--r--services/audioflinger/tests/resampler_tests.cpp213
1 files changed, 11 insertions, 202 deletions
diff --git a/services/audioflinger/tests/resampler_tests.cpp b/services/audioflinger/tests/resampler_tests.cpp
index 8f9c270..4a67d0b 100644
--- a/services/audioflinger/tests/resampler_tests.cpp
+++ b/services/audioflinger/tests/resampler_tests.cpp
@@ -33,200 +33,7 @@
#include <gtest/gtest.h>
#include <media/AudioBufferProvider.h>
#include "AudioResampler.h"
-
-#define ARRAY_SIZE(a) (sizeof(a) / sizeof((a)[0]))
-
-template<typename T, typename U>
-struct is_same
-{
- static const bool value = false;
-};
-
-template<typename T>
-struct is_same<T, T> // partial specialization
-{
- static const bool value = true;
-};
-
-template<typename T>
-static inline T convertValue(double val)
-{
- if (is_same<T, int16_t>::value) {
- return floor(val * 32767.0 + 0.5);
- } else if (is_same<T, int32_t>::value) {
- return floor(val * (1UL<<31) + 0.5);
- }
- return val; // assume float or double
-}
-
-/* Creates a type-independent audio buffer provider from
- * a buffer base address, size, framesize, and input increment array.
- *
- * No allocation or deallocation of the provided buffer is done.
- */
-class TestProvider : public android::AudioBufferProvider {
-public:
- TestProvider(const void* addr, size_t frames, size_t frameSize,
- const std::vector<size_t>& inputIncr)
- : mAddr(addr),
- mNumFrames(frames),
- mFrameSize(frameSize),
- mNextFrame(0), mUnrel(0), mInputIncr(inputIncr), mNextIdx(0)
- {
- }
-
- virtual android::status_t getNextBuffer(Buffer* buffer, int64_t pts __unused = kInvalidPTS )
- {
- size_t requestedFrames = buffer->frameCount;
- if (requestedFrames > mNumFrames - mNextFrame) {
- buffer->frameCount = mNumFrames - mNextFrame;
- }
- if (!mInputIncr.empty()) {
- size_t provided = mInputIncr[mNextIdx++];
- ALOGV("getNextBuffer() mValue[%d]=%u not %u",
- mNextIdx-1, provided, buffer->frameCount);
- if (provided < buffer->frameCount) {
- buffer->frameCount = provided;
- }
- if (mNextIdx >= mInputIncr.size()) {
- mNextIdx = 0;
- }
- }
- ALOGV("getNextBuffer() requested %u frames out of %u frames available"
- " and returned %u frames\n",
- requestedFrames, mNumFrames - mNextFrame, buffer->frameCount);
- mUnrel = buffer->frameCount;
- if (buffer->frameCount > 0) {
- buffer->raw = (char *)mAddr + mFrameSize * mNextFrame;
- return android::NO_ERROR;
- } else {
- buffer->raw = NULL;
- return android::NOT_ENOUGH_DATA;
- }
- }
-
- virtual void releaseBuffer(Buffer* buffer)
- {
- if (buffer->frameCount > mUnrel) {
- ALOGE("releaseBuffer() released %u frames but only %u available "
- "to release\n", buffer->frameCount, mUnrel);
- mNextFrame += mUnrel;
- mUnrel = 0;
- } else {
-
- ALOGV("releaseBuffer() released %u frames out of %u frames available "
- "to release\n", buffer->frameCount, mUnrel);
- mNextFrame += buffer->frameCount;
- mUnrel -= buffer->frameCount;
- }
- buffer->frameCount = 0;
- buffer->raw = NULL;
- }
-
- void reset()
- {
- mNextFrame = 0;
- }
-
- size_t getNumFrames()
- {
- return mNumFrames;
- }
-
- void setIncr(const std::vector<size_t> inputIncr)
- {
- mNextIdx = 0;
- mInputIncr = inputIncr;
- }
-
-protected:
- const void* mAddr; // base address
- size_t mNumFrames; // total frames
- int mFrameSize; // frame size (# channels * bytes per sample)
- size_t mNextFrame; // index of next frame to provide
- size_t mUnrel; // number of frames not yet released
- std::vector<size_t> mInputIncr; // number of frames provided per call
- size_t mNextIdx; // index of next entry in mInputIncr to use
-};
-
-/* Creates a buffer filled with a sine wave.
- *
- * Returns a pair consisting of the sine signal buffer and the number of frames.
- * The caller must delete[] the buffer when no longer needed (no shared_ptr<>).
- */
-template<typename T>
-static std::pair<T*, size_t> createSine(size_t channels,
- double freq, double samplingRate, double time)
-{
- double tscale = 1. / samplingRate;
- size_t frames = static_cast<size_t>(samplingRate * time);
- T* buffer = new T[frames * channels];
- for (size_t i = 0; i < frames; ++i) {
- double t = i * tscale;
- double y = sin(2. * M_PI * freq * t);
- T yt = convertValue<T>(y);
-
- for (size_t j = 0; j < channels; ++j) {
- buffer[i*channels + j] = yt / (j + 1);
- }
- }
- return std::make_pair(buffer, frames);
-}
-
-/* Creates a buffer filled with a chirp signal (a sine wave sweep).
- *
- * Returns a pair consisting of the chirp signal buffer and the number of frames.
- * The caller must delete[] the buffer when no longer needed (no shared_ptr<>).
- *
- * When creating the Chirp, note that the frequency is the true sinusoidal
- * frequency not the sampling rate.
- *
- * http://en.wikipedia.org/wiki/Chirp
- */
-template<typename T>
-static std::pair<T*, size_t> createChirp(size_t channels,
- double minfreq, double maxfreq, double samplingRate, double time)
-{
- double tscale = 1. / samplingRate;
- size_t frames = static_cast<size_t>(samplingRate * time);
- T *buffer = new T[frames * channels];
- // note the chirp constant k has a divide-by-two.
- double k = (maxfreq - minfreq) / (2. * time);
- for (size_t i = 0; i < frames; ++i) {
- double t = i * tscale;
- double y = sin(2. * M_PI * (k * t + minfreq) * t);
- T yt = convertValue<T>(y);
-
- for (size_t j = 0; j < channels; ++j) {
- buffer[i*channels + j] = yt / (j + 1);
- }
- }
- return std::make_pair(buffer, frames);
-}
-
-/* This derived class creates a buffer provider of datatype T,
- * consisting of an input signal, e.g. from createChirp().
- * The number of frames can be obtained from the base class
- * TestProvider::getNumFrames().
- */
-template <typename T>
-class SignalProvider : public TestProvider {
-public:
- SignalProvider(const std::pair<T*, size_t>& bufferInfo, size_t channels,
- const std::vector<size_t>& values)
- : TestProvider(bufferInfo.first, bufferInfo.second, channels * sizeof(T), values),
- mManagedPtr(bufferInfo.first)
- {
- }
-
- virtual ~SignalProvider()
- {
- delete[] mManagedPtr;
- }
-
-protected:
- T* mManagedPtr;
-};
+#include "test_utils.h"
void resample(void *output, size_t outputFrames, const std::vector<size_t> &outputIncr,
android::AudioBufferProvider *provider, android::AudioResampler *resampler)
@@ -261,10 +68,11 @@ void testBufferIncrement(size_t channels, unsigned inputFreq, unsigned outputFre
enum android::AudioResampler::src_quality quality)
{
// create the provider
- std::vector<size_t> inputIncr;
- SignalProvider<int16_t> provider(createChirp<int16_t>(channels,
- 0., outputFreq/2., outputFreq, outputFreq/2000.),
- channels, inputIncr);
+ std::vector<int> inputIncr;
+ SignalProvider provider;
+ provider.setChirp<int16_t>(channels,
+ 0., outputFreq/2., outputFreq, outputFreq/2000.);
+ provider.setIncr(inputIncr);
// calculate the output size
size_t outputFrames = ((int64_t) provider.getNumFrames() * outputFreq) / inputFreq;
@@ -339,10 +147,11 @@ void testStopbandDownconversion(size_t channels,
enum android::AudioResampler::src_quality quality)
{
// create the provider
- std::vector<size_t> inputIncr;
- SignalProvider<int16_t> provider(createChirp<int16_t>(channels,
- 0., inputFreq/2., inputFreq, inputFreq/2000.),
- channels, inputIncr);
+ std::vector<int> inputIncr;
+ SignalProvider provider;
+ provider.setChirp<int16_t>(channels,
+ 0., inputFreq/2., inputFreq, inputFreq/2000.);
+ provider.setIncr(inputIncr);
// calculate the output size
size_t outputFrames = ((int64_t) provider.getNumFrames() * outputFreq) / inputFreq;