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); } }
static void busy_fill(short int *p, int size) { /* variables */ long int sig_600; /* value of the 600 Hz tone signal */ long int sig_120; /* value of the 120 Hz modulating signal */ long int angle; /* angle to find the sine for */ int i; /* general loop index */ /* fill the buffer with data */ for (i = 0; i < size; i++) { /* check if past the end of the tone + silent portion */ if (++ring_busy_timer >= (BUSY_TONE_TIME + BUSY_SILENT_TIME)) /* timer has wrapped, reset it */ ring_busy_timer = 0; /* get the angle for both waveforms (with a factor of 5) */ /* calculation is complicated to keep it from overflowing */ angle = (((600 * ring_busy_timer) / SAMPLE_RATE) * SINE_RESOLUTION) + (((600 * ring_busy_timer) % SAMPLE_RATE) * SINE_RESOLUTION) / SAMPLE_RATE; /* get the 600 Hz sine wave */ sig_600 = sine_wave(angle); /* now get the 120 Hz modulation wave (5 = 600 Hz / 120 Hz) */ sig_120 = sine_wave(angle / 5); /* the modulation signal should always be positive */ if (sig_120 < 0) sig_120 = -sig_120; /* should the tone be output or should it be silent */ if (ring_busy_timer < BUSY_TONE_TIME) /* outputting the tone (make it -13 dBm) */ p[i] = (sig_600 * sig_120) / (4096 * 4); else /* output no tone - store a 0 */ p[i] = 0; } /* all done filling the buffer - return */ return; }
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); } }
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); }
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); } }
playSineWave(unsigned int freq){ sn=3; sine_wave(freq); }