![]() |
Mixxx
|
00001 #include <gtest/gtest.h> 00002 00003 #include <QtDebug> 00004 00005 #include "defs.h" 00006 #include "configobject.h" 00007 #include "controlobject.h" 00008 #include "sampleutil.h" 00009 #include "soundmanagerutil.h" 00010 #include "engine/enginemicrophone.h" 00011 00012 namespace { 00013 00014 class EngineMicrophoneTest : public testing::Test { 00015 protected: 00016 virtual void SetUp() { 00017 inputLength = MAX_BUFFER_LEN/2; 00018 outputLength = MAX_BUFFER_LEN; 00019 input = new short[inputLength]; 00020 output = SampleUtil::alloc(outputLength); 00021 test = SampleUtil::alloc(outputLength); 00022 00023 m_pMicrophone = new EngineMicrophone("[Microphone]"); 00024 m_pTalkover = ControlObject::getControl(ConfigKey("[Microphone]", "talkover")); 00025 } 00026 00027 virtual void TearDown() { 00028 delete [] input; 00029 SampleUtil::free(output); 00030 SampleUtil::free(test); 00031 delete m_pMicrophone; 00032 } 00033 00034 void ClearBuffer(CSAMPLE* pBuffer, int length) { 00035 memset(pBuffer, 0, sizeof(pBuffer[0])*length); 00036 } 00037 00038 template <typename T> 00039 void FillBuffer(T* pBuffer, T value, int length) { 00040 for (int i = 0; i < length; ++i) { 00041 pBuffer[i] = value; 00042 } 00043 } 00044 00045 template <typename T> 00046 void FillSequentialWithStride(T* pBuffer, T initial, T increment, T max, 00047 unsigned int stride, unsigned int length) { 00048 Q_ASSERT(length % stride == 0); 00049 T value = initial; 00050 for (unsigned int i = 0; i < length/stride; ++i) { 00051 for (unsigned int j = 0; j < stride; ++j) { 00052 pBuffer[i*stride + j] = value; 00053 } 00054 value = value + increment; 00055 if (value > max) 00056 value = initial; 00057 } 00058 } 00059 00060 void AssertWholeBufferEquals(const CSAMPLE* pBuffer, CSAMPLE value, int iBufferLen) { 00061 for (int i = 0; i < iBufferLen; ++i) { 00062 EXPECT_FLOAT_EQ(value, pBuffer[i]); 00063 } 00064 } 00065 00066 template <typename T> 00067 void AssertBuffersEqual(const T* pBuffer, const T* pExpected, int iBufferLen) { 00068 for (int i = 0; i < iBufferLen; ++i) { 00069 EXPECT_FLOAT_EQ(pExpected[i], pBuffer[i]); 00070 } 00071 } 00072 00073 unsigned int inputLength; 00074 unsigned int outputLength; 00075 short* input; 00076 CSAMPLE* output; 00077 CSAMPLE* test; 00078 EngineMicrophone* m_pMicrophone; 00079 ControlObject* m_pTalkover; 00080 }; 00081 00082 TEST_F(EngineMicrophoneTest, TestInputMatchesOutput) { 00083 FillBuffer<short>(input, 1, inputLength); 00084 SampleUtil::applyGain(output, 0.0f, outputLength); 00085 00086 AudioInput micInput = AudioInput(AudioPath::MICROPHONE, 0, 0); // What should channelbase be? 00087 m_pTalkover->set(1.0f); 00088 00089 m_pMicrophone->receiveBuffer(micInput, input, inputLength); 00090 m_pMicrophone->process(output, output, outputLength); 00091 00092 // Check that the output matches the input data. 00093 AssertWholeBufferEquals(output, 1.0f, outputLength); 00094 } 00095 00096 TEST_F(EngineMicrophoneTest, TestTalkoverDisablesOutput) { 00097 SampleUtil::applyGain(output, 0.0f, outputLength); 00098 AudioInput micInput = AudioInput(AudioPath::MICROPHONE, 0, 0); // What should channelbase be? 00099 00100 m_pTalkover->set(0.0f); 00101 FillBuffer<short>(input, 1, inputLength); 00102 m_pMicrophone->receiveBuffer(micInput, input, inputLength); 00103 m_pMicrophone->process(output, output, outputLength); 00104 // Check that the output matches the input data. 00105 AssertWholeBufferEquals(output, 0.0f, outputLength); 00106 00107 // Now fill the buffer with 2's and turn talkover on. Verify that 2's come 00108 // out. 00109 m_pTalkover->set(1.0f); 00110 FillBuffer<short>(input, 2, inputLength); 00111 m_pMicrophone->receiveBuffer(micInput, input, inputLength); 00112 m_pMicrophone->process(output, output, outputLength); 00113 // Check that the output matches the input data. 00114 AssertWholeBufferEquals(output, 2.0f, outputLength); 00115 } 00116 00117 TEST_F(EngineMicrophoneTest, TestRepeatedInputMatchesOutput) { 00118 SampleUtil::applyGain(output, 0.0f, outputLength); 00119 AudioInput micInput = AudioInput(AudioPath::MICROPHONE, 0, 0); // What should channelbase be? 00120 m_pTalkover->set(1.0f); 00121 00122 for (int i = 0; i < 10; i++) { 00123 // We have to limit it since short wraps around at 32768 and float 00124 // doesn't. 00125 FillSequentialWithStride<short>(input, 0, 1, 30000, 1, inputLength); 00126 FillSequentialWithStride<CSAMPLE>(test, 0, 1.0f, 30000.0f, 2, outputLength); 00127 00128 m_pMicrophone->receiveBuffer(micInput, input, inputLength); 00129 m_pMicrophone->process(output, output, outputLength); 00130 00131 // Check that the output matches the expected output 00132 AssertBuffersEqual<CSAMPLE>(output, test, outputLength); 00133 } 00134 } 00135 00136 } // namespace