Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
100.00% |
38 / 38 |
|
100.00% |
6 / 6 |
CRAP | |
100.00% |
1 / 1 |
| WavFileFaker | |
100.00% |
38 / 38 |
|
100.00% |
6 / 6 |
16 | |
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% |
5 / 5 |
|
100.00% |
1 / 1 |
1 | |||
| createSamples | |
100.00% |
15 / 15 |
|
100.00% |
1 / 1 |
4 | |||
| renderNote | |
100.00% |
15 / 15 |
|
100.00% |
1 / 1 |
8 | |||
| 1 | <?php |
| 2 | namespace Apie\Faker\FileFakers; |
| 3 | |
| 4 | use Apie\Faker\Interfaces\ApieAudioFileFaker; |
| 5 | use Faker\Generator; |
| 6 | use Nyholm\Psr7\Stream; |
| 7 | |
| 8 | /** |
| 9 | * Generates a short 16-bit mono PCM wav file: a random pentatonic tune in a random waveform. |
| 10 | */ |
| 11 | final class WavFileFaker implements ApieAudioFileFaker |
| 12 | { |
| 13 | private const SAMPLE_RATE = 22050; |
| 14 | private const PENTATONIC = [0, 2, 4, 7, 9]; |
| 15 | private const WAVEFORMS = ['sine', 'triangle', 'square', 'saw']; |
| 16 | |
| 17 | public function createOriginalFilename(Generator $faker): string |
| 18 | { |
| 19 | return $faker->word() . '.wav'; |
| 20 | } |
| 21 | |
| 22 | public function createMimeType(): string |
| 23 | { |
| 24 | return 'audio/wav'; |
| 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 | $pcm = $this->createSamples($faker); |
| 36 | $header = 'RIFF' . pack('V', 36 + strlen($pcm)) . 'WAVE' |
| 37 | . 'fmt ' . pack('VvvVVvv', 16, 1, 1, self::SAMPLE_RATE, self::SAMPLE_RATE * 2, 2, 16) |
| 38 | . 'data' . pack('V', strlen($pcm)); |
| 39 | |
| 40 | return Stream::create($header . $pcm)->detach(); |
| 41 | } |
| 42 | |
| 43 | private function createSamples(Generator $faker): string |
| 44 | { |
| 45 | $waveform = $faker->randomElement(self::WAVEFORMS); |
| 46 | $root = $faker->numberBetween(48, 60); |
| 47 | $noteLength = $faker->randomElement([0.15, 0.2, 0.3, 0.4]); |
| 48 | $vibrato = $faker->boolean(40); |
| 49 | $degree = $faker->numberBetween(0, 9); |
| 50 | $pcm = ''; |
| 51 | for ($note = $faker->numberBetween(8, 24); $note > 0; $note--) { |
| 52 | $degree = max(0, min(9, $degree + $faker->numberBetween(-2, 2))); |
| 53 | $midi = $root + self::PENTATONIC[$degree % 5] + 12 * intdiv($degree, 5); |
| 54 | $frequency = 440 * 2 ** (($midi - 69) / 12); |
| 55 | $length = (int) (self::SAMPLE_RATE * $noteLength * ($faker->boolean(25) ? 2 : 1)); |
| 56 | $pcm .= $faker->boolean(10) |
| 57 | ? str_repeat("\0\0", $length) |
| 58 | : $this->renderNote($waveform, $frequency, $length, $vibrato); |
| 59 | } |
| 60 | |
| 61 | return $pcm; |
| 62 | } |
| 63 | |
| 64 | private function renderNote(string $waveform, float $frequency, int $length, bool $vibrato): string |
| 65 | { |
| 66 | $samples = []; |
| 67 | $attack = min(300, intdiv($length, 4)); |
| 68 | $phase = 0.0; |
| 69 | for ($i = 0; $i < $length; $i++) { |
| 70 | $freq = $vibrato ? $frequency * (1 + 0.01 * sin(2 * M_PI * 5 * $i / self::SAMPLE_RATE)) : $frequency; |
| 71 | $phase = fmod($phase + $freq / self::SAMPLE_RATE, 1.0); |
| 72 | $wave = match ($waveform) { |
| 73 | 'sine' => sin(2 * M_PI * $phase), |
| 74 | 'triangle' => 4 * abs($phase - 0.5) - 1, |
| 75 | 'square' => $phase < 0.5 ? 0.6 : -0.6, |
| 76 | default => 2 * $phase - 1, |
| 77 | }; |
| 78 | // linear attack and exponential decay avoid clicks between notes |
| 79 | $envelope = min(1.0, $i / max(1, $attack)) * exp(-3 * $i / $length) * min(1.0, ($length - $i) / 200); |
| 80 | $samples[] = (int) round($wave * $envelope * 0.5 * 32767); |
| 81 | } |
| 82 | |
| 83 | return pack('v*', ...array_map(static fn (int $s): int => $s & 0xFFFF, $samples)); |
| 84 | } |
| 85 | } |