-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathasticaVoice_sample.php
More file actions
73 lines (58 loc) · 2.39 KB
/
Copy pathasticaVoice_sample.php
File metadata and controls
73 lines (58 loc) · 2.39 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
<?php
$asticaAPI_key = 'YOUR API KEY'; //visit https://astica.ai
$asticaAPI_timeout = 10; // API request timeout in seconds
$asticaAPI_endpoint = 'https://voice.astica.ai/speak';
$asticaAPI_modelVersion = '1.0_full';
$asticaAPI_voiceid = 0; //See: https://astica.ai/voice/documentation/
$asticaAPI_input = 'hello, how are you doing today?'; //text to be spoken
$asticaAPI_lang = 'en-US'; # language code
$asticaAPI_outputFile = 'output.wav';
// Define payload array
$asticaAPI_payload = [
'tkn' => $asticaAPI_key,
'modelVersion' => $asticaAPI_modelVersion,
'input' => $asticaAPI_input,
'voice' => $asticaAPI_voiceid,
'lang' => $asticaAPI_lang
];
// Call API function and store result
$result = asticaAPI($asticaAPI_endpoint, $asticaAPI_payload, $asticaAPI_timeout);
// decode audio buffer to binary
$wavData = implode(array_map('chr', $result['wavBuffer']['data']));
// Write the contents to a file
file_put_contents($asticaAPI_outputFile, $wavData);
//////////////////////////
////////////////Raw Output
//////////////////////////
echo '<pre><h2>Raw Output:</h2>'; print_r($result); echo '</pre>';
// Define API function
function asticaAPI($endpoint, $payload, $timeout = 15) {
$ch = curl_init();
$payload = json_encode($payload);
// Set cURL options
curl_setopt_array($ch, [
CURLOPT_URL => $endpoint,
CURLOPT_POST => true,
CURLOPT_POSTFIELDS => $payload,
CURLOPT_RETURNTRANSFER => true,
CURLOPT_SSL_VERIFYPEER => 0,
CURLOPT_CONNECTTIMEOUT => $timeout,
CURLOPT_TIMEOUT => $timeout,
CURLOPT_HTTPHEADER => [
'Content-Type: application/json; charset=utf-8',
'Content-Length: ' . strlen($payload),
'Accept: application/json'
]
]);
$response = curl_exec($ch);
if (curl_errno($ch)) {
return curl_error($ch);
}
curl_close($ch);
$result = json_decode($response, true);
//if(!isset($result['status'])) {
// $result = json_decode(json_decode($response), true);
//}
return $result;
}
?>