Home | History | Annotate | Download | only in test
      1 /*
      2  * Copyright (C) 2012 The Android Open Source Project
      3  *
      4  * Licensed under the Apache License, Version 2.0 (the "License"); you may not
      5  * use this file except in compliance with the License. You may obtain a copy of
      6  * the License at
      7  *
      8  * http://www.apache.org/licenses/LICENSE-2.0
      9  *
     10  * Unless required by applicable law or agreed to in writing, software
     11  * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
     12  * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
     13  * License for the specific language governing permissions and limitations under
     14  * the License.
     15  */
     16 #include <stdint.h>
     17 
     18 #include <gtest/gtest.h>
     19 
     20 #include <audio/AudioSignalFactory.h>
     21 
     22 class AudioSignalFactoryTest: public testing::Test {
     23 protected:
     24 
     25     void testSignalBasic(android::sp<Buffer>& buffer, int maxPositive,
     26             AudioHardware::SamplingRate samplingRate, int signalFreq, int samples) {
     27         ASSERT_TRUE(buffer->getSize() == (unsigned int)(AudioHardware::E2BPS * 2 * samples));
     28         int16_t* data = reinterpret_cast<int16_t*>(buffer->getData());
     29         for(int i = 0; i < samples; i++) {
     30             ASSERT_TRUE(*data <= maxPositive);
     31             ASSERT_TRUE(*data >= -maxPositive);
     32             data++;
     33             ASSERT_TRUE(*data <= maxPositive);
     34             ASSERT_TRUE(*data >= -maxPositive);
     35             data++;
     36         }
     37     }
     38 };
     39 
     40 TEST_F(AudioSignalFactoryTest, SineTest) {
     41     const int maxPositive = 1000;
     42     const int signalFreq = AudioHardware::ESampleRate_44100/100;
     43     const int samples = 8192 * 10;
     44     android::sp<Buffer> buffer = AudioSignalFactory::generateSineWave(AudioHardware::E2BPS,
     45             maxPositive, AudioHardware::ESampleRate_44100, signalFreq, samples);
     46     testSignalBasic(buffer, maxPositive, AudioHardware::ESampleRate_44100, signalFreq, samples);
     47 }
     48 
     49 TEST_F(AudioSignalFactoryTest, WhiteNoiseTest) {
     50     const int maxPositive = 1000;
     51     const int signalFreq = AudioHardware::ESampleRate_44100/100;
     52     const int samples = 8192 * 10;
     53     android::sp<Buffer> buffer = AudioSignalFactory::generateWhiteNoise(AudioHardware::E2BPS,
     54             maxPositive, samples);
     55     testSignalBasic(buffer, maxPositive, AudioHardware::ESampleRate_44100, signalFreq, samples);
     56 }
     57 
     58