Could not find TTS server to handle request - google-text-to-speech

Trying to make chinese output.
google.api_core.exceptions.InvalidArgument: 400 Could not find TTS server to handle request for application_id: 'cloud-tts' and trigger_application_id: '' and voice_request: language: "cmn-tw"
working fine with "en-US", but not working with "cmn-tw"
Following the sample code from Google Text-to-Speech API Client Libraries
The following are my code.
#!/usr/bin/python
#coding:utf-8
"""Synthesizes speech from the input string of text or ssml.
Note: ssml must be well-formed according to:
https://www.w3.org/TR/speech-synthesis/
"""
from google.cloud import texttospeech
# Instantiates a client
client = texttospeech.TextToSpeechClient()
mytext = "這是一個測試"
# Set the text input to be synthesized
synthesis_input = texttospeech.types.SynthesisInput(text=mytext)
# Build the voice request, select the language code ("en-US") and the ssml
# voice gender ("neutral")
voice = texttospeech.types.VoiceSelectionParams(
language_code='cmn-tw',
ssml_gender=texttospeech.enums.SsmlVoiceGender.FEMALE)
# Select the type of audio file you want returned
audio_config = texttospeech.types.AudioConfig(
audio_encoding=texttospeech.enums.AudioEncoding.MP3)
# Perform the text-to-speech request on the text input with the selected
# voice parameters and audio file type
response = client.synthesize_speech(synthesis_input, voice, audio_config)
# The response's audio_content is binary.
with open('output.mp3', 'wb') as out:
# Write the response to the output file.
out.write(response.audio_content)
print('Audio content written to file "output.mp3"')

Related

How To set default UTF-8 in Http Test Script Recorder In Jmeter?

I'm using Jmeter and I record http requsets with "Http Test Script Recorder".
How can I set default UTF-8 encoding for the content encoding?
When I record, for the first time, encoding has been a problem, but content encoding for the next time, is ok.
Add a HTTP REQUEST DEFAULT to the test plan and set Content encoding to UTF-8 Then start recording the website
If it didn't help (That's because the website you are recording is sending a request which is not UTF-8 encoded, If you can change the source code of website do it first, else try these steps), You can use USER DEFINED VARIABLES in the test plan:
Click Add
In Name define a variable name i.e: sur_name
Then in value write the name you want i.e: صادقی
In any HttpRequest with parameter named sur_name you should write #sur_name as value
Note that you should do the steps after monitoring.

Azure bing cognitive services speech to text in javascript via REST fails

In JavaScript we use recorder.js to capture microphone input, down sample it to 16kHz, encode it as a WAV file and get a blob.
Next, we obtain the raw blob bytes via a FileReader onload() callback and then use an XMLHttpRequest to send() the raw bytes to Bing.
The XMLHttpRequest includes the headers:
'Ocp-Apim-Subscription-Key' : 'xxxxxx'
'Content-Type' : 'audio/wav; codec=audio/pcm; samplerate=16000'
A sample blob size is 62456 bytes.
FireFox network tracing shows 2 interactions. The first is
Request URL: https://speech.platform.bing.com/speech/recognition/interactive/cognitiveservices/v1?language=en-US&format=simple
Request Method: OPTIONS
and the second
Request URL:https://speech.platform.bing.com/speech/recognition/interactive/cognitiveservices/v1?language=en-US&format=simple
Request Method: POST
content-length: 94476
However, I keep getting the following reply
{"RecognitionStatus":"InitialSilenceTimeout","Offset":29000000,"Duration":0}
FWIW, any idea why the source blob size of 62456 would result in content-length: 94476?
The same raw blob bytes are processed by Amazon Lex properly.
Is there any JavaScript RESTful example?
Many thanks.
/--------------------------------------------------------------
After putting together the test case below I also tried the following without success.
console.log("Send to BING blob");
var self = this;
console.log(blob);
var msUrl = 'https://speech.platform.bing.com/speech/recognition/interactive/cognitiveservices/v1';
msUrl += '?language=en-US';
msUrl += '&format=simple';
console.log(msUrl);
var xhr = new XMLHttpRequest();
xhr.onload = function(evt) { console.log('onload', xhr, evt);};
xhr.open('POST', msUrl, true);
xhr.setRequestHeader('Accept', 'application/json;text/xml');
xhr.setRequestHeader('Ocp-Apim-Subscription-Key', 'xxx');
var bingContentType = 'audio/wav; codec=audio/pcm; samplerate=16000';
xhr.setRequestHeader('Content-Type', bingContentType);
xhr.send(blob);
The shorter code version of sending to Bing was fine. The probelm was that the recorder worker's encodeWAV(samples) function did not
take into account the down sampling to 16000. The function was incorrectly writing the captured sampling rate as the header value. The lines to be tweaked are:
view.setUint32(24, downSampleRate, true);
view.setUint32(28, downSampleRate * 2, true); /*MONO*/
Apparently AWS Lex ignores the header values as it only expects 16kHz mono whereas the Bing service has to look at the header information to determine which of the audio formats supported is being sent.
Today I came across this problem, after spending half an hour, I was able to find the real cause of my issue. Let me go through the steps which are mentioned in this link.
Verified my Bing speech API is in running status.
Verified my key by running the below code in
$FetchTokenHeader = #{
'Content-type'='application/x-www-form-urlencoded';
'Content-Length'= '0';
'Ocp-Apim-Subscription-Key' = ''
}
$OAuthToken = Invoke-RestMethod -Method POST -Uri https://api.cognitive.microsoft.com/sts/v1.0/issueToken -Headers $FetchTokenHeader
show the token received
$OAuthToken
As mentioned in the last point in that link, InitialSilenceTimeout may be the result of the unformatted/invalid wav file. So I downloaded a new wav file from internet and tested with it.
Bingo, that worked. And finally, I was able to get my speech in text format

Kodi plugin and URL with basic authentication

I'm writing KODI plugin, where I'm trying to play resources from external service. Access of resources requires basic authentication.
I was following this tutorial of how to write add-on. They call addDirectoryItem method in xbmcplugin.
url = 'http://localhost/some_video.mkv'
li = xbmcgui.ListItem(foldername + ' Video', iconImage='DefaultVideo.png')
xbmcplugin.addDirectoryItem(handle=addon_handle, url=url, listitem=li)
It accepts url as string. I cannot find any option, how to do authentication part.
Kodi player allows to pass custom HTTP headers, including authentication headers, to a remote server like the following:
from urllib import quote
url = 'http://some.server/video.mkv|Header1={0}&Header2={1}'.format(
quote(header1_value),
quote(header2_value)
)
That is, after a pipe | you can pass a set of header=value pairs separated by &. Header values must be URL-quoted.

Need to find the requests equivalent of openurl() from urllib2

I am currently trying to modify a script to use the requests library instead of the urllib2 library. I haven't really used it before and I am looking to do the equivalent of urlopen("http://www.example.org").read(), so I tried the requests.get("http://www.example.org").text function.
This works fine with normal everyday html, however when I fetch from this url (https://gtfsrt.api.translink.com.au/Feed/SEQ) it doesn't seem to work.
So I wrote the below code to print out the responses from the same url using both the requests and urllib2 libraries.
import urllib2
import requests
#urllib2 request
request = urllib2.Request("https://gtfsrt.api.translink.com.au/Feed/SEQ")
result = urllib2.urlopen(request)
#requests request
result2 = requests.get("https://gtfsrt.api.translink.com.au/Feed/SEQ")
print result2.encoding
#urllib2 write to text
open("Output.txt", 'w').close()
text_file = open("Output.txt", "w")
text_file.write(result.read())
text_file.close()
open("Output2.txt", 'w').close()
text_file = open("Output2.txt", "w")
text_file.write(result2.text)
text_file.close()
The openurl().read() works fine but the requests.get().text doesn't work for the given this url. I suspect it has something to do with encoding, but i don't know what. Any thoughts?
Note: The supplied url is a feed in the google protocol buffer format, once I receive the message i give the feed to a google library that interprets it.
Your issue is that you're making the requests module interpret binary content in a response as text.
A response from the requests library has two main way to access the body of the response:
Response.content - will return the response body as a bytestring
Response.text - will decode the response body as text and return unicode
Since protocol buffers are a binary format, you should use result2.content in your code instead of result2.text.
Response.content will return the body of the response as-is, in bytes. For binary content this is exactly what you want. For text content that contains non-ASCII characters this means the content must have been encoded by the server into a bytestring using a particular encoding that is indicated by either a HTTP header or a <meta charset="..." /> tag. In order to make sense of those bytes they therefore need to be decoded after receiving using that charset.
Response.text now is a convenience method that does exactly this for you. It assumes the response body is text, and looks at the response headers to find the encoding, and decodes it for you, returning unicode.
But if your response doesn't contain text, this is the wrong method to use. Binary content doesn't contain characters, because it's not text, so the whole concept of character encoding does not make any sense for binary content - it's only applicable to text composed of characters. (That's also why you're seeing response.encoding == None - it's just bytes, there is no character encoding involved).
See Response Content and Binary Response Content in the requests documentation for more details.

How can I get an iPhone to download a contact card via URL redirect

I am working on a tracking tool and I am wondering if anyone knows how to get the iPhone to download a contact card via a URL redirect. iPhone will not download contact cards from the web. Has anyone figured out a way around this? In other cases with most mobiles, I can redirect them to a vcard or similar file, and using the correct http headers, get the phone to download it. iPhone does not allow vcard downloads so how would one get a vcard onto an iPhone via the web browser?
I have just published an alternative solution on my blog which describes how to attach the contact file as an attachment to a calendar file which is handled by mobile safari
http://mobicontact.info/iphone/download-contact-from-web-page/
Perhaps there is something here that you can work with. Doesn't specifically solve the redirect question but it does allow the user to open contact directly after going through the calendar app.
The blog shows complete solution including source code and images of the whole process and as such is a lot easier to read than what I can put here on Stack Overflow and I was trying to prevent duplication between many such forums. The main point to note is that Apple use :
ATTACH;VALUE=BINARY;ENCODING=BASE64;FMTTYPE=text/directory;
X-APPLE-FILENAME=iPhone Contact.vcf:
QkVHSU46VkNBUkQNClZFUlNJT046M…etc… [base64 encoded VCARD]
for embedded VCARD in VCALENDAR files. Create a VCALENDAR file and then base64 encode your VCARD within it - code snippet below (full details on my blog)
<?php
# Send correct headers
header("Content-type: text/x-vcalendar; charset=utf-8");
# Alternatively: application/octet-stream
# Depending on the desired browser behaviour
# Be sure to test thoroughly cross-browser
header("Content-Disposition: attachment; filename=\"iphonecontact.ics\";");
# Output file contents - simple version
#echo file_get_contents("iphonecontact.ics");
# Generate file contents - advanced version
# BEGIN:VCALENDAR
# VERSION:2.0
# BEGIN:VEVENT
# DTSTART;TZID=Europe/London:20120617T090000
# DTEND;TZID=Europe/London:20120617T100000
# SUMMARY:iPhone Contact
# DTSTAMP:20120617T080516Z
# ATTACH;VALUE=BINARY;ENCODING=BASE64;FMTTYPE=text/directory;
# X-APPLE-FILENAME=iphonecontact.vcf:
# QkVHSU46VkNBUkQNClZFUlNJT046My4wDQpOOkNvbnRhY3Q7aVBob25lOzs7DQpGTjppUGhvbm
# UgQ29udGFjdA0KRU1BSUw7VFlQRT1JTlRFUk5FVDtUWVBFPVdPUks6aXBob25lQHRoZXNpbGlj
# b25nbG9iZS5jb20NClRFTDtUWVBFPUNFTEw7VFlQRT1WT0lDRTtUWVBFPXByZWY6KzQ0MTIzND
# U2Nzg5MA0KRU5EOlZDQVJE
# END:VEVENT
# END:VCALENDAR
echo "BEGIN:VCALENDAR\n";
echo "VERSION:2.0\n";
echo "BEGIN:VEVENT\n";
echo "SUMMARY:Click attached contact below to save to your contacts\n";
$dtstart = date("Ymd")."T".date("Hi")."00";
echo "DTSTART;TZID=Europe/London:".$dtstart."\n";
$dtend = date("Ymd")."T".date("Hi")."01";
echo "DTEND;TZID=Europe/London:".$dtend."\n";
echo "DTSTAMP:".$dtstart."Z\n";
echo "ATTACH;VALUE=BINARY;ENCODING=BASE64;FMTTYPE=text/directory;\n";
echo " X-APPLE-FILENAME=iphonecontact.vcf:\n";
$vcard = file_get_contents("iphonecontact.vcf"); # read the file into memory
$b64vcard = base64_encode($vcard); # base64 encode it so that it can be used as an attachemnt to the "dummy" calendar appointment
$b64mline = chunk_split($b64vcard,74,"\n"); # chunk the single long line of b64 text in accordance with RFC2045 (and the exact line length determined from the original .ics file exported from Apple calendar
$b64final = preg_replace('/(.+)/', ' $1', $b64mline); # need to indent all the lines by 1 space for the iphone (yes really?!!)
echo $b64final; # output the correctly formatted encoded text
echo "END:VEVENT\n";
echo "END:VCALENDAR\n";
?>
You could try redirecting to a .tel site.
For example, my QR code redirects iPhones to http://edent.tel/
Or, if you don't want to buy a .tel, you can create a simple website which contains the phone number that you want with a "click to call" link.
<a href=”tel:123456798″>Call me on 123456789</a>