52 lines
1.9 KiB
PHP
52 lines
1.9 KiB
PHP
<?php
|
|
|
|
namespace JumPHP\Tests\Audio;
|
|
|
|
use JumPHP\Audio\PcmStream;
|
|
use JumPHP\Audio\SoundManager;
|
|
use JumPHP\Bridge\JsBridge;
|
|
use PHPUnit\Framework\TestCase;
|
|
|
|
final class PcmStreamTest extends TestCase
|
|
{
|
|
public function testWithoutDeviceNothingIsQueued(): void
|
|
{
|
|
$sound = new SoundManager(); // not initialised: no device (headless)
|
|
$pcm = new PcmStream($sound, new JsBridge());
|
|
self::assertFalse($pcm->isAvailable());
|
|
self::assertGreaterThan(60.0, $pcm->buffered());
|
|
$pcm->push(str_repeat("\0\0", 2048), 22050, 2); // does nothing, no error
|
|
$pcm->update();
|
|
$pcm->clear();
|
|
self::assertGreaterThan(60.0, $pcm->buffered());
|
|
}
|
|
|
|
public function testThePageReportsWhatIsQueued(): void
|
|
{
|
|
$bridge = new JsBridge();
|
|
$pcm = new PcmStream(new SoundManager(), $bridge);
|
|
$bridge->emit('pcm', [['buffered' => 0.25]]);
|
|
self::assertFalse($pcm->isAvailable()); // desktop test run: still "nothing to hear"
|
|
}
|
|
|
|
/** Desktop with an audio device: what is pushed is queued on SDL_mixer's output, and clear() drops it. */
|
|
public function testTheStreamIsBufferedAndCleared(): void
|
|
{
|
|
if (!function_exists('JumPHP_StreamOpen')) {
|
|
self::markTestSkipped('the SDL_mixer binding has no continuous music stream');
|
|
}
|
|
$sound = new SoundManager();
|
|
$sound->init();
|
|
if (!$sound->isEnabled()) {
|
|
self::markTestSkipped('no audio device');
|
|
}
|
|
$pcm = new PcmStream($sound, new JsBridge());
|
|
self::assertTrue($pcm->isAvailable());
|
|
$pcm->push(str_repeat("\0\0", 22050), 22050, 1); // one second, mono, 22050 Hz
|
|
self::assertGreaterThan(0.5, $pcm->buffered(), 'the second is queued');
|
|
self::assertLessThanOrEqual(1.05, $pcm->buffered());
|
|
$pcm->clear();
|
|
self::assertLessThan(0.1, $pcm->buffered(), 'clear() drops the queue');
|
|
$sound->close();
|
|
}
|
|
}
|