<?php
/**
* SeekQuarry/Yioop --
* Open Source Pure PHP Search Engine, Crawler, and Indexer
*
* Copyright (C) 2009 - 2026 Chris Pollett chris@pollett.org
*
* LICENSE:
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*
* END LICENSE
*
* @author Chris Pollett chris@pollett.org
* @license https://www.gnu.org/licenses/ GPL3
* @link https://www.seekquarry.com/
* @copyright 2009 - 2026
* @filesource
*/
namespace seekquarry\yioop\tests;
use seekquarry\yioop\library\av_processing\SpeechFilter;
use seekquarry\yioop\library\UnitTest;
/**
* SpeechFilterTest checks that SpeechFilter turns the sixteen
* frequencies describing a stretch of speech into the sixteen terms of
* the filter that plays it back.
*
* The terms were compared against the reference decoder for the Opus
* audio codec, built here and made to print the terms it works out: on
* ten seconds of speech, 198 packets, all sixteen terms of every packet
* agree. The cases below hold that agreement for one stretch and the
* rules the arithmetic must keep for any stretch.
*
* @author Chris Pollett
*/
class SpeechFilterTest extends UnitTest
{
/**
* A_REAL_SHAPE is the sixteen frequencies of the forty-third packet
* of the test speech, each written out of 32768.
* @var array
*/
const A_REAL_SHAPE = [1324, 1675, 3953, 5462, 6723, 8741, 10291,
12230, 13144, 14993, 17664, 20645, 22656, 24687, 27345, 30336];
/**
* ITS_TERMS is the sixteen filter terms the reference decoder works
* out from those frequencies, each written out of 4096.
* @var array
*/
const ITS_TERMS = [12204, -16443, 13071, -6614, 3016, -2091, 326,
2283, -3013, 1807, -652, 334, -526, 715, -705, 259];
/**
* setUp does nothing; every case works from numbers it holds itself.
*/
public function setUp()
{
}
/**
* tearDown does nothing, since no case here writes a file.
*/
public function tearDown()
{
}
/**
* realShapeGivesTheTermsTheReferenceGivesTestCase checks the
* sixteen terms worked out for one stretch of the test speech
* against what the reference decoder works out for it. Every step of
* the arithmetic shows up in these numbers, so one wrong rounding
* anywhere breaks this case.
*/
public function realShapeGivesTheTermsTheReferenceGivesTestCase()
{
$this->assertEqual(self::ITS_TERMS,
SpeechFilter::termsFor(self::A_REAL_SHAPE),
"the sixteen terms are the ones the reference works out");
}
/**
* everyShapeGivesSixteenTermsTestCase checks that a shape gives one
* term for each of its frequencies, and that each term fits in the
* range a term is written in. A term outside that range would be cut
* short when the sound is played back.
*/
public function everyShapeGivesSixteenTermsTestCase()
{
$terms = SpeechFilter::termsFor(self::A_REAL_SHAPE);
$this->assertEqual(SpeechFilter::TERMS_IN_FILTER, count($terms),
"sixteen frequencies give sixteen terms");
foreach ($terms as $one) {
$this->assertTrue($one >= -32768 && $one <= 32767,
"each term fits where it is written, and one was $one");
}
}
/**
* frequenciesSpreadEvenlyGiveASmallFilterTestCase checks that
* frequencies spread evenly across the range give terms that are all
* small. Even spreading is what a flat sound looks like, and a flat
* sound needs almost no filtering.
*/
public function frequenciesSpreadEvenlyGiveASmallFilterTestCase()
{
$even = [];
for ($at = 1; $at <= SpeechFilter::TERMS_IN_FILTER; $at++) {
$even[] = (int)(32768 * $at /
(SpeechFilter::TERMS_IN_FILTER + 1));
}
$terms = SpeechFilter::termsFor($even);
$largest = 0;
foreach ($terms as $one) {
$largest = max($largest, abs($one));
}
$this->assertTrue($largest < 4096,
"evenly spread frequencies give terms under one, and the " .
"largest was $largest");
}
/**
* roundingGoesToTheNearestTestCase checks the rounding the whole
* arithmetic rests on. Rounding toward zero instead moves a term by
* one, which is enough to disagree with every other decoder.
*/
public function roundingGoesToTheNearestTestCase()
{
$this->assertEqual(2, SpeechFilter::roundedShift(3, 1),
"three halved rounds up to two");
$this->assertEqual(1, SpeechFilter::roundedShift(2, 1),
"two halved stays at one");
$this->assertEqual(-1, SpeechFilter::roundedShift(-3, 1),
"minus three halved rounds toward zero to minus one");
$this->assertEqual(5, SpeechFilter::roundedShift(5, 0),
"no halving at all leaves the number alone");
}
}