示例#1
0
TEST (AudioDataTest, SampleMutatorBounds) {
  KeyFinder::AudioData a;
  a.addToSampleCount(5);
  ASSERT_THROW(a.getSample(-1), KeyFinder::Exception);
  ASSERT_THROW(a.getSample(5), KeyFinder::Exception);

  ASSERT_THROW(a.setSample(-1, 1.0), KeyFinder::Exception);
  ASSERT_THROW(a.setSample(5, 1.0), KeyFinder::Exception);

  ASSERT_THROW(a.setSample(0, INFINITY), KeyFinder::Exception);
  ASSERT_THROW(a.setSample(0, NAN), KeyFinder::Exception);
}
TEST (LowPassFilterTest, WorksOnRepetitiveWaves) {
    // make two sine waves, but this time, several seconds long
    unsigned int samples = frameRate * 5;
    KeyFinder::AudioData a;
    a.setChannels(1);
    a.setFrameRate(frameRate);
    a.addToSampleCount(samples);
    for (unsigned int i = 0; i < samples; i++) {
        float sample = 0.0;
        sample += sine_wave(i, highFrequency, frameRate, magnitude); // high freq
        sample += sine_wave(i, lowFrequency, frameRate, magnitude); // low freq
        a.setSample(i, sample);
        // ensure repetition of sine waves is perfect...
        if (i >= frameRate) {
            ASSERT_NEAR(a.getSample(i), a.getSample(i - frameRate), tolerance);
        }
    }

    KeyFinder::LowPassFilter* lpf = new KeyFinder::LowPassFilter(filterOrder, frameRate, cornerFrequency, filterFFT);
    KeyFinder::Workspace w;
    lpf->filter(a, w);
    delete lpf;

    // test for lower wave only
    for (unsigned int i = 0; i < samples; i++) {
        float expected = sine_wave(i, lowFrequency, frameRate, magnitude);
        ASSERT_NEAR(expected, a.getSample(i), tolerance);
    }
}
示例#3
0
TEST (AudioDataTest, DownsamplerResamplesIntegralRelationship) {
  KeyFinder::AudioData a;
  a.setChannels(1);
  a.setFrameRate(100);
  a.addToSampleCount(10);
  for (unsigned int i = 0; i < 5; i++)
    a.setSample(i, 100.0);
  for (unsigned int i = 5; i < 10; i++)
    a.setSample(i, 500.0);

  a.downsample(5);

  ASSERT_EQ(20, a.getFrameRate());
  ASSERT_EQ(2, a.getSampleCount());
  ASSERT_FLOAT_EQ(100.0, a.getSample(0));
  ASSERT_FLOAT_EQ(500.0, a.getSample(1));
}
示例#4
0
TEST (AudioDataTest, DownsamplerResamplesNonintegralRelationship) {
  KeyFinder::AudioData a;
  a.setChannels(1);
  a.setFrameRate(100);
  a.addToSampleCount(12);
  for (unsigned int i = 0; i < 5; i++)
    a.setSample(i, 100.0);
  for (unsigned int i = 5; i < 10; i++)
    a.setSample(i, 500.0);
  for (unsigned int i = 10; i < 12; i++)
    a.setSample(i, 1000.0);

  a.downsample(5);

  ASSERT_EQ(3, a.getSampleCount());
  ASSERT_FLOAT_EQ(100.0, a.getSample(0));
  ASSERT_FLOAT_EQ(500.0, a.getSample(1));
  // this doesn't make total mathematical sense but I'm taking a shortcut for performance
  ASSERT_FLOAT_EQ(1000.0, a.getSample(2));
}
示例#5
0
TEST(AudioDataTest, SamplesBasic){
  KeyFinder::AudioData a;
  a.addToSampleCount(100);
  ASSERT_EQ(100, a.getSampleCount());
  // init values
  for(int i=0; i<100; i++){
    ASSERT_EQ(0.0, a.getSample(i));
  }
  a.setSample(0, 10.0);
  ASSERT_EQ(10.0, a.getSample(0));
}
示例#6
0
TEST (AudioDataTest, Iterators) {
  KeyFinder::AudioData a;
  a.setChannels(1);
  a.setFrameRate(1);
  a.addToSampleCount(10);

  a.setSample(0, 10.0);
  a.setSample(1, 20.0);
  a.setSample(3, 50.0);

  a.resetIterators(); // this is required before each use

  ASSERT_FLOAT_EQ(10.0, a.getSampleAtReadIterator());
  a.setSampleAtWriteIterator(15.0);
  ASSERT_FLOAT_EQ(15.0, a.getSampleAtReadIterator());

  a.advanceReadIterator();
  a.advanceWriteIterator();
  ASSERT_FLOAT_EQ(20.0, a.getSampleAtReadIterator());
  a.setSampleAtWriteIterator(25.0);
  ASSERT_FLOAT_EQ(25.0, a.getSampleAtReadIterator());

  a.advanceReadIterator(2);
  a.advanceWriteIterator(2);
  ASSERT_FLOAT_EQ(50.0, a.getSampleAtReadIterator());
  a.setSampleAtWriteIterator(55.0);
  ASSERT_FLOAT_EQ(55.0, a.getSampleAtReadIterator());

  a.resetIterators();
  ASSERT_FLOAT_EQ(15.0, a.getSampleAtReadIterator());
  a.setSampleAtWriteIterator(150.0);
  ASSERT_FLOAT_EQ(150.0, a.getSampleAtReadIterator());

  ASSERT_TRUE(a.readIteratorWithinUpperBound());
  ASSERT_TRUE(a.writeIteratorWithinUpperBound());
  a.advanceReadIterator(10);
  a.advanceWriteIterator(10);
  ASSERT_FALSE(a.readIteratorWithinUpperBound());
  ASSERT_FALSE(a.writeIteratorWithinUpperBound());
}
示例#7
0
TEST (AudioDataTest, MakeMono) {
  KeyFinder::AudioData a;
  a.setChannels(2);
  a.addToSampleCount(20);
  for (int i = 0; i < 10; i++) {
    a.setSample(i * 2, 100.0);
  }
  a.reduceToMono();
  ASSERT_EQ(10, a.getSampleCount());
  for (int i = 0; i < 10; i++) {
    ASSERT_FLOAT_EQ(50.0, a.getSample(i));
  }
}
示例#8
0
TEST(AudioDataTest, Frames){
  KeyFinder::AudioData a;
  a.addToSampleCount(4);
  ASSERT_THROW(a.getSample(0,0), KeyFinder::Exception);

  a.setChannels(2);
  ASSERT_EQ(0.0, a.getSample(0,0));
  ASSERT_EQ(0.0, a.getSample(0,1));

  ASSERT_THROW(a.getSample(-1, 0), KeyFinder::Exception);
  ASSERT_THROW(a.getSample(-1, 1), KeyFinder::Exception);
  ASSERT_THROW(a.getSample( 2, 0), KeyFinder::Exception);
  ASSERT_THROW(a.getSample( 2, 1), KeyFinder::Exception);
  ASSERT_THROW(a.getSample( 0,-1), KeyFinder::Exception);
  ASSERT_THROW(a.getSample( 0, 2), KeyFinder::Exception);

  a.setSample(0,0, 10.0);
  ASSERT_EQ(10.0, a.getSample(0,0));

  a.setSample(1,0, 20.0);
  ASSERT_EQ(20.0, a.getSample(2));
}
示例#9
0
TEST (AudioDataTest, FrameMutator) {
  KeyFinder::AudioData a;
  a.setChannels(4);
  a.addToFrameCount(5);
  ASSERT_EQ(5, a.getFrameCount());
  ASSERT_EQ(20, a.getSampleCount());

  a.setSample(6, 10.0);
  ASSERT_FLOAT_EQ(10.0, a.getSample(6));
  ASSERT_FLOAT_EQ(10.0, a.getSampleByFrame(1, 2));
  a.setSampleByFrame(1, 2, 20.0);
  ASSERT_FLOAT_EQ(20.0, a.getSample(6));
  ASSERT_FLOAT_EQ(20.0, a.getSampleByFrame(1, 2));
}
TEST (LowPassFilterTest, KillsHigherFreqs) {
    // make a high frequency sine wave, one second long
    KeyFinder::AudioData a;
    a.setChannels(1);
    a.setFrameRate(frameRate);
    a.addToSampleCount(frameRate);
    for (unsigned int i = 0; i < frameRate; i++) {
        a.setSample(i, sine_wave(i, highFrequency, frameRate, magnitude));
    }

    KeyFinder::LowPassFilter* lpf = new KeyFinder::LowPassFilter(filterOrder, frameRate, cornerFrequency, filterFFT);
    KeyFinder::Workspace w;
    lpf->filter(a, w);
    delete lpf;

    // test for near silence
    for (unsigned int i = 0; i < frameRate; i++) {
        ASSERT_NEAR(0.0, a.getSample(i), tolerance);
    }
}
TEST (LowPassFilterTest, MaintainsLowerFreqs) {
    // make a low frequency sine wave, one second long
    KeyFinder::AudioData a;
    a.setChannels(1);
    a.setFrameRate(frameRate);
    a.addToSampleCount(frameRate);
    for (unsigned int i = 0; i < frameRate; i++) {
        a.setSample(i, sine_wave(i, lowFrequency, frameRate, magnitude));
    }

    KeyFinder::LowPassFilter* lpf = new KeyFinder::LowPassFilter(filterOrder, frameRate, cornerFrequency, filterFFT);
    KeyFinder::Workspace w;
    lpf->filter(a, w);
    delete lpf;

    // test for near perfect reproduction
    for (unsigned int i = 0; i < frameRate; i++) {
        float expected = sine_wave(i, lowFrequency, frameRate, magnitude);
        ASSERT_NEAR(expected, a.getSample(i), tolerance);
    }
}
TEST (LowPassFilterTest, DoesBothAtOnce) {
    // make two sine waves, one second long
    KeyFinder::AudioData a;
    a.setChannels(1);
    a.setFrameRate(frameRate);
    a.addToSampleCount(frameRate);
    for (unsigned int i = 0; i < frameRate; i++) {
        float sample = 0.0;
        sample += sine_wave(i, highFrequency, frameRate, magnitude); // high freq
        sample += sine_wave(i, lowFrequency, frameRate, magnitude); // low freq
        a.setSample(i, sample);
    }

    KeyFinder::LowPassFilter* lpf = new KeyFinder::LowPassFilter(filterOrder, frameRate, cornerFrequency, filterFFT);
    KeyFinder::Workspace w;
    lpf->filter(a, w);
    delete lpf;

    // test for lower wave only
    for (unsigned int i = 0; i < frameRate; i++) {
        float expected = sine_wave(i, lowFrequency, frameRate, magnitude);
        ASSERT_NEAR(expected, a.getSample(i), tolerance);
    }
}
示例#13
0
TEST (AudioDataTest, DownsamplerResamplesSineWave) {
  unsigned int frameRate = 10000;
  unsigned int frames = frameRate * 4;
  float freq = 20;
  float magnitude = 32768.0;
  unsigned int factor = 5;

  KeyFinder::AudioData a;
  a.setChannels(1);
  a.setFrameRate(frameRate);
  a.addToSampleCount(frames);
  for (unsigned int i = 0; i < frames; i++)
    a.setSample(i, sine_wave(i, freq, frameRate, magnitude));

  a.downsample(factor);

  unsigned int newFrameRate = frameRate / factor;
  unsigned int newFrames = frames / factor;

  ASSERT_EQ(newFrameRate, a.getFrameRate());
  ASSERT_EQ(newFrames, a.getSampleCount());
  for (unsigned int i = 0; i < newFrames; i++)
    ASSERT_NEAR(sine_wave(i, freq, newFrameRate, magnitude), a.getSample(i), magnitude * 0.05);
}
示例#14
0
TEST (AudioDataTest, SampleMutator) {
  KeyFinder::AudioData a;
  a.addToSampleCount(1);
  a.setSample(0, 10.0);
  ASSERT_FLOAT_EQ(10.0, a.getSample(0));
}
示例#15
0
const char* kfinder_get_key(short signed int   *samples,
                            unsigned int        nb_samples,
                            short unsigned int  frame_rate,
                            short unsigned int  nb_channels)
{
    // Check input parameter.
    if ((samples == NULL) || (nb_samples == 0) || (frame_rate == 0) || (nb_channels == 0))
    {
        return "";
    }

    // Build the main computing object.
    KeyFinder::KeyFinder k;

    // Build an empty audio object
    KeyFinder::AudioData a;

    // Prepare the object for your audio stream
    a.setFrameRate(frame_rate);
    a.setChannels(nb_channels);
    a.addToSampleCount(nb_samples);

    // Copy your audio into the object (as float).
    for (unsigned int i = 0; i < nb_samples; i++)
    {
        a.setSample(i, (float)samples[i]);
    }

    // Run the analysis
    KeyFinder::key_t r;
    try
    {
        r =  k.keyOfAudio(a);
    }
    catch(const std::exception& e)
    {
        cerr << "libKeyFinder: exception: " << e.what() << endl;
        return "";
    }
    catch(...)
    {
        cerr << "libKeyFinder: unknown exception" << endl;
        return "";
    }


    // And do something with the result!
    switch(r)
    {
        case KeyFinder::A_MAJOR:      return "AM";
        case KeyFinder::A_MINOR:      return "Am";
        case KeyFinder::B_FLAT_MAJOR: return "BbM";
        case KeyFinder::B_FLAT_MINOR: return "Bbm";
        case KeyFinder::B_MAJOR:      return "BM";
        case KeyFinder::B_MINOR:      return "Bm";
        case KeyFinder::C_MAJOR:      return "CM";
        case KeyFinder::C_MINOR:      return "Cm";
        case KeyFinder::D_FLAT_MAJOR: return "DbM";
        case KeyFinder::D_FLAT_MINOR: return "Dbm";
        case KeyFinder::D_MAJOR:      return "DM";
        case KeyFinder::D_MINOR:      return "Dm";
        case KeyFinder::E_FLAT_MAJOR: return "EbM";
        case KeyFinder::E_FLAT_MINOR: return "Ebm";
        case KeyFinder::E_MAJOR:      return "EM";
        case KeyFinder::E_MINOR:      return "Em";
        case KeyFinder::F_MAJOR:      return "FM";
        case KeyFinder::F_MINOR:      return "Fm";
        case KeyFinder::G_FLAT_MAJOR: return "GbM";
        case KeyFinder::G_FLAT_MINOR: return "Gbm";
        case KeyFinder::G_MAJOR:      return "GM";
        case KeyFinder::G_MINOR:      return "Gm";
        case KeyFinder::A_FLAT_MAJOR: return "AbM";
        case KeyFinder::A_FLAT_MINOR: return "Abm";
        case KeyFinder::SILENCE:      return "";
        default:                      return "";
    }
}