نعم يمكنك ذلك ! .. هل حلمت يومًا بأن تقوم ببرمجة موقع يقوم بتحويل النص إلى صوت؟ .. هل فكرت فى برمجته عبر PHP؟!
الآن يمكنك تحويل النص إلى صوت عبر PHP ، سنشاهد فى هذا الدرس كيف تقوم بذلك.
سنقوم بإستخدام كلاس تستطيع من خلاله تحويل النص إلى صوت، وذلك عبر TTS API ، وهو إختصار Text To Speech API ، وهو بمساعدة خدمة ترجمة جوجل، والكلاس كالتالي:
<?php
// FileName: tts.php
/* * A PHP Class that converts Text into Speech using Google's Text to Speech API
*
* Author:
* Ahmed Essam
* http://phpitc.blogspot.com/
*
*/
class TextToSpeech {
public $mp3data;
function __construct($text="") {
$text = trim($text);
if(!empty($text)) {
$text = urlencode($text);
$this->mp3data = file_get_contents("http://www.translate.google.com/translate_tts?tl=en&q={$text}");
}
}
function setText($text) {
$text = trim($text);
if(!empty($text)) {
$text = urlencode($text);
$this->mp3data = file_get_contents("http://www.translate.google.com/translate_tts?tl=en&q={$text}");
return $mp3data;
} else { return false; }
}
function saveToFile($filename) {
$filename = trim($filename);
if(!empty($filename)) {
return file_put_contents($filename,$this->mp3data);
} else { return false; }
}
}
?>
- أمثلة على الإستخدام:// FileName: tts.php
/* * A PHP Class that converts Text into Speech using Google's Text to Speech API
*
* Author:
* Ahmed Essam
* http://phpitc.blogspot.com/
*
*/
class TextToSpeech {
public $mp3data;
function __construct($text="") {
$text = trim($text);
if(!empty($text)) {
$text = urlencode($text);
$this->mp3data = file_get_contents("http://www.translate.google.com/translate_tts?tl=en&q={$text}");
}
}
function setText($text) {
$text = trim($text);
if(!empty($text)) {
$text = urlencode($text);
$this->mp3data = file_get_contents("http://www.translate.google.com/translate_tts?tl=en&q={$text}");
return $mp3data;
} else { return false; }
}
function saveToFile($filename) {
$filename = trim($filename);
if(!empty($filename)) {
return file_put_contents($filename,$this->mp3data);
} else { return false; }
}
}
?>
<?php
require "tts.php";
$tts = new TextToSpeech();
$tts->setText("Hello World!");
$tts->saveToFile("phpvoice.mp3");
?>
require "tts.php";
$tts = new TextToSpeech();
$tts->setText("Hello World!");
$tts->saveToFile("phpvoice.mp3");
?>
<?php
require "tts.php";
$tts = new TextToSpeech("Hello World!");
$tts->saveToFile("phpvoice.mp3");
?>
سيظهر الصوت طبعًا باللغة الإنجليزية، ولكن يمكنك تطوير الكلاس بحيث فى الرابطين الموجودين تستطيع تغيير كلمة en إلى كلمة fr مثلاً لكي يكون الصوت باللغة الفرنسية كهذا السطر:require "tts.php";
$tts = new TextToSpeech("Hello World!");
$tts->saveToFile("phpvoice.mp3");
?>
$this->mp3data = file_get_contents("http://www.translate.google.com/translate_tts?tl=fr&q={$text}");
ملاحظة: هذا الكلام لا ينطبق على اللغة العربية؛ أى أنه لا يمكنكم تحويل الكلام العربي إلى صوت عبر هذا الكلاس.
0 commentaires:
إرسال تعليق
لا تنسى ان تشارك samir soltani بتعليقك
او نشر الموظوع جزاك الله خيرا
اضغط على الابتسامة لظهور الكود الخاص بها
لإدراج تعبيرات الوجه يجب إضافة ما لا يقل عن مسافة واحدة قبل رمز.