-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_telephony_simulation.php
More file actions
84 lines (67 loc) · 2.68 KB
/
test_telephony_simulation.php
File metadata and controls
84 lines (67 loc) · 2.68 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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
<?php
/**
* Script de Simulação de Telefonia Real
* Lê um arquivo WAV, processa frame a frame e converte frequências
*/
$wav_file = '/home/lotus/PROJETOS/pcg729/audio_48000_stereo.wav';
if (!file_exists($wav_file)) {
$wav_file = '/home/lotus/PROJETOS/opus/result_48000_stereo.wav';
}
if (!file_exists($wav_file)) {
die("Erro: Arquivo de áudio não encontrado para teste.\n");
}
echo "Iniciando simulação de telefonia...\n";
echo "Lendo arquivo: $wav_file\n";
$data = file_get_contents($wav_file);
$pcm_raw = substr($data, 44); // Pula cabeçalho WAV
$input_rate = 48000;
$input_channels = 2;
$frame_size = 3840; // 20ms @ 48kHz Stereo
$frames = str_split($pcm_raw, $frame_size);
$scenarios = [
['rate' => 8000, 'channels' => 1, 'name' => 'Narrowband Mono (G.711 style)'],
['rate' => 16000, 'channels' => 1, 'name' => 'Wideband Mono (G.722 style)'],
['rate' => 44100, 'channels' => 2, 'name' => 'CD Quality Stereo'],
['rate' => 32000, 'channels' => 2, 'name' => 'Ultra-wideband Stereo']
];
$output_dir = __DIR__ . '/telephony_test_results';
if (!is_dir($output_dir)) {
mkdir($output_dir, 0777, true);
}
function write_wav_header($sample_rate, $channels, $data_len) {
$header = "RIFF";
$header .= pack("V", 36 + $data_len);
$header .= "WAVEfmt ";
$header .= pack("V", 16);
$header .= pack("v", 1); // PCM
$header .= pack("v", $channels);
$header .= pack("V", $sample_rate);
$header .= pack("V", $sample_rate * $channels * 2);
$header .= pack("v", $channels * 2);
$header .= pack("v", 16);
$header .= "data";
$header .= pack("V", $data_len);
return $header;
}
foreach ($scenarios as $scenario) {
echo "\n--- Testando cenário: {$scenario['name']} ({$scenario['rate']}Hz) ---\n";
$processed_pcm = "";
foreach ($frames as $index => $frame) {
if (strlen($frame) < $frame_size) continue;
$converted = resample($frame, $input_rate, $scenario['rate'], [
'input_channels' => $input_channels,
'output_channels' => $scenario['channels'],
'normalize' => ($index === 0)
]);
$processed_pcm .= $converted;
}
$file_name = str_replace([' ', '(', ')', '.', '/'], '_', $scenario['name']) . ".wav";
$file_path = $output_dir . '/' . strtolower($file_name);
$wav_data = write_wav_header($scenario['rate'], $scenario['channels'], strlen($processed_pcm)) . $processed_pcm;
file_put_contents($file_path, $wav_data);
echo "Arquivo salvo: $file_path\n";
echo "Tamanho final: " . strlen($processed_pcm) . " bytes\n";
echo "SUCESSO.\n";
}
echo "\nSimulação finalizada.\n";
var_dump((new opusChannel(48000,1))->getInfo());