Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
100.00% |
27 / 27 |
|
100.00% |
10 / 10 |
CRAP | |
100.00% |
1 / 1 |
| Mp3FileFaker | |
100.00% |
27 / 27 |
|
100.00% |
10 / 10 |
11 | |
100.00% |
1 / 1 |
| createOriginalFilename | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| createMimeType | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| isSupported | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| createResource | |
100.00% |
7 / 7 |
|
100.00% |
1 / 1 |
2 | |||
| createId3Tag | |
100.00% |
12 / 12 |
|
100.00% |
1 / 1 |
1 | |||
| textFrame | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| commentFrame | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| frame | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| utf16 | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| syncSafe | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| 1 | <?php |
| 2 | namespace Apie\Faker\FileFakers; |
| 3 | |
| 4 | use Apie\Faker\Interfaces\ApieAudioFileFaker; |
| 5 | use Apie\Faker\Support\Mp3Notes; |
| 6 | use Faker\Generator; |
| 7 | use Nyholm\Psr7\Stream; |
| 8 | |
| 9 | /** |
| 10 | * Generates an mp3 file with random ID3v2.3 metadata and a random pentatonic tune. |
| 11 | * The tune is made by joining pre-encoded note frames, so no mp3 encoder is required. |
| 12 | */ |
| 13 | final class Mp3FileFaker implements ApieAudioFileFaker |
| 14 | { |
| 15 | private const GENRES = ['Pop', 'Rock', 'Jazz', 'Electronic', 'Ambient', 'Classical', 'Folk', 'Hip-Hop', 'Blues', 'Soundtrack']; |
| 16 | |
| 17 | public function createOriginalFilename(Generator $faker): string |
| 18 | { |
| 19 | return $faker->word() . '.mp3'; |
| 20 | } |
| 21 | |
| 22 | public function createMimeType(): string |
| 23 | { |
| 24 | return 'audio/mpeg'; |
| 25 | } |
| 26 | |
| 27 | public static function isSupported(): bool |
| 28 | { |
| 29 | return true; |
| 30 | } |
| 31 | |
| 32 | /** @return resource */ |
| 33 | public function createResource(Generator $faker, string $originalFilename, string $mimeType): mixed |
| 34 | { |
| 35 | $notes = Mp3Notes::decode(); |
| 36 | $frames = ''; |
| 37 | $current = $faker->numberBetween(0, count($notes) - 1); |
| 38 | for ($i = $faker->numberBetween(12, 48); $i > 0; $i--) { |
| 39 | // mostly stepwise movement with an occasional repeat or leap makes it sound like a melody. |
| 40 | $current = max(0, min(count($notes) - 1, $current + $faker->randomElement([-2, -1, -1, 0, 1, 1, 2]))); |
| 41 | $frames .= $notes[$current]; |
| 42 | } |
| 43 | |
| 44 | return Stream::create($this->createId3Tag($faker) . $frames)->detach(); |
| 45 | } |
| 46 | |
| 47 | private function createId3Tag(Generator $faker): string |
| 48 | { |
| 49 | $trackCount = $faker->numberBetween(1, 16); |
| 50 | $frames = $this->textFrame('TIT2', ucfirst($faker->words(3, true))) |
| 51 | . $this->textFrame('TPE1', $faker->name()) |
| 52 | . $this->textFrame('TALB', ucwords($faker->words(2, true))) |
| 53 | . $this->textFrame('TPE2', $faker->name()) |
| 54 | . $this->textFrame('TYER', (string) $faker->year()) |
| 55 | . $this->textFrame('TRCK', $faker->numberBetween(1, $trackCount) . '/' . $trackCount) |
| 56 | . $this->textFrame('TCON', $faker->randomElement(self::GENRES)) |
| 57 | . $this->textFrame('TBPM', (string) $faker->numberBetween(60, 180)) |
| 58 | . $this->textFrame('TCOP', $faker->year() . ' ' . $faker->company()) |
| 59 | . $this->commentFrame($faker->sentence()); |
| 60 | |
| 61 | return 'ID3' . "\x03\x00\x00" . $this->syncSafe(strlen($frames)) . $frames; |
| 62 | } |
| 63 | |
| 64 | private function textFrame(string $id, string $text): string |
| 65 | { |
| 66 | // encoding 1 is UTF-16 with a byte order mark. |
| 67 | return $this->frame($id, "\x01" . $this->utf16($text)); |
| 68 | } |
| 69 | |
| 70 | private function commentFrame(string $text): string |
| 71 | { |
| 72 | return $this->frame('COMM', "\x01" . 'eng' . "\xFF\xFE\x00\x00" . $this->utf16($text)); |
| 73 | } |
| 74 | |
| 75 | private function frame(string $id, string $body): string |
| 76 | { |
| 77 | return $id . pack('N', strlen($body)) . "\x00\x00" . $body; |
| 78 | } |
| 79 | |
| 80 | private function utf16(string $text): string |
| 81 | { |
| 82 | return "\xFF\xFE" . mb_convert_encoding($text, 'UTF-16LE', 'UTF-8') . "\x00\x00"; |
| 83 | } |
| 84 | |
| 85 | private function syncSafe(int $size): string |
| 86 | { |
| 87 | return chr(($size >> 21) & 0x7F) . chr(($size >> 14) & 0x7F) . chr(($size >> 7) & 0x7F) . chr($size & 0x7F); |
| 88 | } |
| 89 | } |