{"id":4141,"date":"2020-06-05T10:47:35","date_gmt":"2020-06-05T03:47:35","guid":{"rendered":"https:\/\/briswell-vn.com\/?p=4141"},"modified":"2020-06-16T16:11:17","modified_gmt":"2020-06-16T09:11:17","slug":"gcp-webservice-laravel-2","status":"publish","type":"post","link":"https:\/\/www.briswell-vn.com\/en\/news\/gcp-webservice-laravel-2\/","title":{"rendered":"GCP web services used Laravel framework &#8211; Part 2"},"content":{"rendered":"<p><\/p>\n<h2>VI. Implement Speech To Text API<\/h2>\n<p>You can refer to this main API document\u00a0<a href=\"https:\/\/cloud.google.com\/speech-to-text\/docs\/quickstart-client-libraries\">page<\/a><\/p>\n<p>Install the client library:<\/p>\n<p dir=\"ltr\"><code class=\"language-php\">composer require google\/cloud-speech<\/code><code class=\"language-php\"><\/code><\/p>\n<p dir=\"ltr\">Now you can use Speech-to-Text to transcribe an audio file to text.<\/p>\n<p>We will create a controller to handle 3 things:<\/p>\n<ol>\n<li>Get the uploaded audio file from the request<\/li>\n<li>Convert audio file to\u00a0<strong>FLAC<\/strong>\u00a0with library\u00a0<a href=\"https:\/\/www.ffmpeg.org\/\"><strong>FFMpeg<\/strong><\/a><\/li>\n<li>Use the below code to show the text received from Speech-to-Text API<\/li>\n<\/ol>\n<p>In SpeechToTextController:<\/p>\n<ol>\n<li>Convert audio file received to\u00a0<strong>FLAC<\/strong>\u00a0from requests<img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-3444\" src=\"https:\/\/briswell-vn.com\/wp-content\/uploads\/2020\/05\/Screen-Shot-0032-04-21-at-11.04.26.png\" alt=\"\" width=\"762\" height=\"576\" \/><br \/>\n<strong>Why conversion to FLAC is necessary?<br \/>\n<\/strong>\u2013\u00a0All Speech-to-Text API synchronous recognition requests must include a speech recognition config field (of type RecognitionConfig).<br \/>\n\u2013 encoding\u00a0 (required) specifies the encoding scheme of the supplied audio (of type AudioEncoding). If you have a choice in the codec, prefer a lossless encoding such as FLAC or LINEAR16 for best performance.<\/li>\n<li>Show the text received<br \/>\n<img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-3445\" src=\"https:\/\/briswell-vn.com\/wp-content\/uploads\/2020\/05\/Screen-Shot-0032-04-21-at-11.05.38.png\" alt=\"\" width=\"667\" height=\"775\" \/><\/li>\n<\/ol>\n<p>I create a\u00a0 template with only 1 input file named \u201c<strong>Choose File<\/strong>\u201c, a button submit named \u201c<strong>Transcribe<\/strong>\u201d and\u00a0a selection to select the language with default \u201c<strong>English(US)<\/strong>\u201d<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/briswell-vn.com\/wp-content\/uploads\/2020\/05\/Screen-Shot-0032-04-21-at-11.13.52.png\" \/><\/p>\n<p>We will choose a Japanese audio file.<\/p>\n<audio class=\"wp-audio-shortcode\" id=\"audio-4141-5\" preload=\"none\" style=\"width: 100%;\" controls=\"controls\"><source type=\"audio\/mpeg\" src=\"https:\/\/briswell-vn.com\/wp-content\/uploads\/2020\/05\/japanese_file.mp3?_=5\" \/><a href=\"https:\/\/briswell-vn.com\/wp-content\/uploads\/2020\/05\/japanese_file.mp3\">https:\/\/briswell-vn.com\/wp-content\/uploads\/2020\/05\/japanese_file.mp3<\/a><\/audio>\n<p>And this is the result:<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-3451\" src=\"https:\/\/briswell-vn.com\/wp-content\/uploads\/2020\/05\/Screen-Shot-0032-04-21-at-11.56.52.png\" alt=\"\" width=\"1110\" height=\"364\" \/><\/p>\n<p>Great! It\u2019s works!<\/p>\n<h2>VII. Implement Text To Speech API<\/h2>\n<p>You can refer to this main API document\u00a0<a href=\"https:\/\/cloud.google.com\/text-to-speech\/docs\/quickstart-client-libraries#client-libraries-usage-php\">page<\/a><\/p>\n<p>Install the client library:<\/p>\n<p dir=\"ltr\"><code class=\"language-php\">composer require google\/cloud-text-to-speech<\/code><\/p>\n<p dir=\"ltr\">Now you can use Text-to-Speech to create an audio file of synthetic human speech.<\/p>\n<p>We will create a controller to handle 2 things:<\/p>\n<ol>\n<li>Get the uploaded text file from the request<\/li>\n<li>Use the below code to download an audio file received from Text-To-Speech API<\/li>\n<\/ol>\n<p>In TextToSpeechController:<\/p>\n<ol>\n<li>Prepare everything before conversion<img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-3456\" src=\"https:\/\/briswell-vn.com\/wp-content\/uploads\/2020\/05\/Screen-Shot-0032-04-24-at-13.16.13.png\" alt=\"\" width=\"801\" height=\"770\" \/><br \/>\nIn the image above, I can set some configures for the audio file before converting, such as\u00a0<strong>language,\u00a0voice,\u00a0gender,\u00a0speed<\/strong>\u2026<\/li>\n<li>Download the audio file after conversion<img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-3458\" src=\"https:\/\/briswell-vn.com\/wp-content\/uploads\/2020\/05\/Screen-Shot-0032-04-24-at-13.26.44.png\" alt=\"\" width=\"916\" height=\"643\" \/>The audio file will be received with the MP3 extension.<\/li>\n<\/ol>\n<p>Then I created a template for the Text-to-speech function. But this time, the template will be more detailed.<img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-3460\" src=\"https:\/\/briswell-vn.com\/wp-content\/uploads\/2020\/05\/Screen-Shot-0032-04-24-at-13.26.44.png\" alt=\"\" width=\"1114\" height=\"286\" \/><\/p>\n<p>We will choose a Japanese text <a href=\"https:\/\/briswell-vn.com\/wp-content\/uploads\/2020\/05\/japanese.txt\">file<\/a>. And select the selections on the screen.\u00a0<img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-3462\" src=\"https:\/\/briswell-vn.com\/wp-content\/uploads\/2020\/05\/Screen-Shot-0032-04-24-at-14.06.20.png\" alt=\"\" width=\"1119\" height=\"287\" \/><\/p>\n<p>This is the result:<\/p>\n<div id=\"mep_1\" class=\"mejs-container mejs-container-keyboard-inactive wp-audio-shortcode mejs-audio\" role=\"application\" aria-label=\"Audio Player\">\n<div class=\"mejs-inner\">\n<div class=\"mejs-controls\">\n<audio class=\"wp-audio-shortcode\" id=\"audio-4141-6\" preload=\"none\" style=\"width: 100%;\" controls=\"controls\"><source type=\"audio\/mpeg\" src=\"https:\/\/briswell-vn.com\/wp-content\/uploads\/2020\/05\/text-to-speech-file.mp3?_=6\" \/><a href=\"https:\/\/briswell-vn.com\/wp-content\/uploads\/2020\/05\/text-to-speech-file.mp3\">https:\/\/briswell-vn.com\/wp-content\/uploads\/2020\/05\/text-to-speech-file.mp3<\/a><\/audio>\n<\/div>\n<\/div>\n<\/div>\n<p>Awesome!!!<\/p>\n<p>&nbsp;<\/p>\n<p>References:<\/p>\n<ol>\n<li><a href=\"https:\/\/cloud.google.com\/vision\/docs\/ocr#vision_text_detection-php\">https:\/\/cloud.google.com\/vision\/docs\/ocr#vision_text_detection-php<\/a><\/li>\n<li><a href=\"https:\/\/cloud.google.com\/speech-to-text\/docs\/quickstart-client-libraries\">https:\/\/cloud.google.com\/speech-to-text\/docs\/quickstart-client-libraries<\/a><\/li>\n<li><a href=\"https:\/\/cloud.google.com\/text-to-speech\/docs\/quickstart-client-libraries#client-libraries-usage-php\">https:\/\/cloud.google.com\/text-to-speech\/docs\/quickstart-client-libraries#client-libraries-usage-php<\/a><\/li>\n<li><a href=\"https:\/\/www.ffmpeg.org\/\">https:\/\/www.ffmpeg.org\/<\/a><\/li>\n<\/ol>\n<p><\/p>","protected":false},"excerpt":{"rendered":"<p>VI. Implement Speech To Text API You can refer to this main API document\u00a0page Install the client library: composer require google\/cloud-speech Now you can use Speech-to-Text to transcribe an audio file to text. We will create a controller to handle 3 things: Get the uploaded audio file from the request Convert audio file to\u00a0FLAC\u00a0with library\u00a0FFMpeg Use the below code to show the text received from [&hellip;]<\/p>\n","protected":false},"author":2,"featured_media":4099,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_mi_skip_tracking":false,"footnotes":""},"categories":[4,71,72],"tags":[],"class_list":["post-4141","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-news","category-it-tec","category-gcp"],"_links":{"self":[{"href":"https:\/\/www.briswell-vn.com\/en\/wp-json\/wp\/v2\/posts\/4141","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.briswell-vn.com\/en\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.briswell-vn.com\/en\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.briswell-vn.com\/en\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/www.briswell-vn.com\/en\/wp-json\/wp\/v2\/comments?post=4141"}],"version-history":[{"count":0,"href":"https:\/\/www.briswell-vn.com\/en\/wp-json\/wp\/v2\/posts\/4141\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.briswell-vn.com\/en\/wp-json\/wp\/v2\/media\/4099"}],"wp:attachment":[{"href":"https:\/\/www.briswell-vn.com\/en\/wp-json\/wp\/v2\/media?parent=4141"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.briswell-vn.com\/en\/wp-json\/wp\/v2\/categories?post=4141"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.briswell-vn.com\/en\/wp-json\/wp\/v2\/tags?post=4141"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}