Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
86.67% covered (warning)
86.67%
13 / 15
50.00% covered (danger)
50.00%
1 / 2
CRAP
0.00% covered (danger)
0.00%
0 / 1
Sound
86.67% covered (warning)
86.67%
13 / 15
50.00% covered (danger)
50.00%
1 / 2
7.12
0.00% covered (danger)
0.00%
0 / 1
 __construct
100.00% covered (success)
100.00%
2 / 2
100.00% covered (success)
100.00%
1 / 1
1
 toPdf
84.62% covered (warning)
84.62%
11 / 13
0.00% covered (danger)
0.00%
0 / 1
6.13
1<?php
2
3declare(strict_types=1);
4
5namespace Phpdftk\Pdf\Core\Multimedia;
6
7use Phpdftk\Pdf\Core\PdfDictionary;
8use Phpdftk\Pdf\Core\PdfName;
9use Phpdftk\Pdf\Core\PdfNumber;
10use Phpdftk\Pdf\Core\PdfStream;
11use Phpdftk\Pdf\Core\DeprecatedPdfFeature;
12use Phpdftk\Pdf\Core\PdfVersion;
13use Phpdftk\Pdf\Core\RequiresPdfVersion;
14
15/**
16 * Sound object (/Type /Sound) — ISO 32000-2 §13.3.
17 *
18 * A stream that holds sampled audio data. Referenced by the /Sound entry
19 * of a SoundAction and SoundAnnotation.
20 *
21 * Required: R (sample rate). Common: C (channels), B (bits per sample),
22 * E (encoding), CO (compression format), CP (compression params).
23 */
24#[RequiresPdfVersion(PdfVersion::V1_2)]
25#[DeprecatedPdfFeature(since: '2.0', replacement: 'MediaRendition', removedIn: '2.0')]
26class Sound extends PdfStream
27{
28    public const PDF_TYPE = 'Sound';
29
30    public float $r;                       // /R  sample rate (Hz)
31    public ?int $c = null;                 // /C  channels (1, 2)
32    public ?int $b = null;                 // /B  bits per sample
33    public ?PdfName $e = null;             // /E  encoding (Raw, Signed, muLaw, ALaw)
34    public ?PdfName $co = null;            // /CO compression format
35    public ?PdfDictionary $cp = null;      // /CP compression params
36
37    public function __construct(float $sampleRate, string $samples = '')
38    {
39        parent::__construct(new PdfDictionary(), $samples);
40        $this->r = $sampleRate;
41    }
42
43    public function toPdf(): string
44    {
45        $this->dictionary->set('Type', new PdfName(self::PDF_TYPE));
46        $this->dictionary->set('R', new PdfNumber($this->r));
47        if ($this->c !== null) {
48            $this->dictionary->set('C', new PdfNumber($this->c));
49        }
50        if ($this->b !== null) {
51            $this->dictionary->set('B', new PdfNumber($this->b));
52        }
53        if ($this->e !== null) {
54            $this->dictionary->set('E', $this->e);
55        }
56        if ($this->co !== null) {
57            $this->dictionary->set('CO', $this->co);
58        }
59        if ($this->cp !== null) {
60            $this->dictionary->set('CP', $this->cp);
61        }
62        return parent::toPdf();
63    }
64}