<?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\Pvq;
/**
* Works out which number stands for a given band shape, which is the
* definition the reader has to agree with.
*
* The Opus specification fixes the order the shapes are numbered in by
* giving this calculation: a short walk from the last slot back to the
* first, adding a count at each step. Going that way is simple. Going
* the other way, from a number back to a shape, is what the reader has
* to do, and is where a mistake could hide. Having both means every
* shape of a small band can be checked, which is what the test beside
* this does.
*
* Only the reading direction is needed to play a recording, so this
* sits here rather than in the library.
*/
class PvqIndexer
{
/**
* Works out which number stands for a shape
*
* @param array $pattern how many pulses fall in each slot,
* negative where they point the other way
* @return int the number standing for that shape
*/
public static function numberFor($pattern)
{
$slots = count($pattern);
if ($slots < 2) {
throw new \Exception("This needs a band of at least two slots");
}
$at = $slots - 1;
$number = ($pattern[$at] < 0) ? 1 : 0;
$running = abs($pattern[$at]);
do {
$at--;
$number += Pvq::helperCount($slots - $at, $running);
$running += abs($pattern[$at]);
if ($pattern[$at] < 0) {
$number += Pvq::helperCount($slots - $at, $running + 1);
}
} while ($at > 0);
return $number;
}
}