-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgenSilenceAudio.php
More file actions
64 lines (50 loc) · 1.64 KB
/
genSilenceAudio.php
File metadata and controls
64 lines (50 loc) · 1.64 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
<?php
function generateSilenceAudio(float $duration, int $sampleRate = 44100, int $channels = 1, int $bitDepth = 16, string $outputFile = 'silence.wav'): bool
{
if ($duration <= 0) {
throw new InvalidArgumentException("Duração deve ser maior que zero");
}
if (!in_array($channels, [1, 2])) {
throw new InvalidArgumentException("Número de canais deve ser 1 (mono) ou 2 (estéreo)");
}
if (!in_array($bitDepth, [8, 16, 24, 32])) {
throw new InvalidArgumentException("Profundidade de bits deve ser 8, 16, 24 ou 32");
}
$bytesPerSample = $bitDepth / 8;
$blockAlign = $channels * $bytesPerSample;
$byteRate = $sampleRate * $blockAlign;
$numSamples = (int)($duration * $sampleRate);
$dataSize = $numSamples * $blockAlign;
$header = pack(
'a4Va4a4VvvVVvva4V',
'RIFF',
36 + $dataSize,
'WAVE',
'fmt ',
16,
1,
$channels,
$sampleRate,
$byteRate,
$blockAlign,
$bitDepth,
'data',
$dataSize
);
$file = fopen($outputFile, 'wb');
if ($file === false) {
throw new RuntimeException("Não foi possível criar o arquivo: {$outputFile}");
}
fwrite($file, $header);
$bufferSize = 8192;
$silenceBuffer = str_repeat("\0", $bufferSize);
$remainingBytes = $dataSize;
while ($remainingBytes > 0) {
$bytesToWrite = min($bufferSize, $remainingBytes);
fwrite($file, substr($silenceBuffer, 0, $bytesToWrite));
$remainingBytes -= $bytesToWrite;
}
fclose($file);
return true;
}
generateSilenceAudio(5*60, 8000, 1, 16, 'silence_5m.wav');