Long Audio - Python Sample to C Sharp Sample?
See original GitHub issueI am trying to use the Long Audio API. For some reason, the sample in the documentation is for Python instead of C Sharp. There is a C Sharp sample linked, and I am unable to understand it.
Here is the Python code.
def submit_synthesis():
region = '<region>'
key = '<your_key>'
input_file_path = '<input_file_path>'
locale = '<locale>'
url = 'https://{}.customvoice.api.speech.microsoft.com/api/texttospeech/v3.0/longaudiosynthesis'.format(region)
header = {
'Ocp-Apim-Subscription-Key': key
}
voice_identities = [
{
'voicename': '<voice_name>'
}
]
payload = {
'displayname': 'long audio synthesis sample',
'description': 'sample description',
'locale': locale,
'voices': json.dumps(voice_identities),
'outputformat': 'riff-16khz-16bit-mono-pcm',
'concatenateresult': True,
}
filename = ntpath.basename(input_file_path)
files = {
'script': (filename, open(input_file_path, 'rb'), 'text/plain')
}
response = requests.post(url, payload, headers=header, files=files)
print('response.status_code: %d' % response.status_code)
print(response.headers['Location'])
submit_synthesis()
I dont quiet understand python, but, I recognize the code to some extent. I was able to use this, and refer the linked sample, and came up with the following.
C Sharp Samples : https://github.com/Azure-Samples/Cognitive-Speech-TTS/tree/master/CustomVoice-API-Samples/CSharp
public async Task LongAudioTestOne()
{
var tempStuff = new ExtraStuff();
//path for the input text file
var workingDirectory = Directory.GetCurrentDirectory();
var inputFileName = "samplefile.txt";
var tempFileName = Path.Combine(workingDirectory, inputFileName);
string subscriptionKey = tempStuff.subscriptionKey;
using (FileStream fstream = new FileStream(tempFileName, FileMode.Open))
using (var client = new HttpClient())
using (var content = new MultipartFormDataContent())
{
client.DefaultRequestHeaders.Add("Ocp-Apim-Subscription-Key", subscriptionKey);
content.Add(new StringContent("sample display name"), "displayname");
content.Add(new StringContent("sample description"), "description");
content.Add(new StringContent("en-GB"), "locale");
content.Add(new StringContent("[{\"voiceName\": \"en - GB - LibbyNeural\"}]"), "voices");
content.Add(new StringContent("riff-16khz-16bit-mono-pcm"), "outputformat");
//add file
content.Add(new StreamContent(fstream), inputFileName, inputFileName);
string url = @"https://centralindia.customvoice.api.speech.microsoft.com/api/texttospeech/v3.0/longaudiosynthesis";
var response = client.PostAsync(url, content).Result;
if (response.StatusCode != HttpStatusCode.Accepted)
{
//APIHelper.PrintErrorMessage(response);
//return false;
var something = response.StatusCode;
}
var something2 = response.StatusCode;
}
}
I keep getting BAD REQUEST 400. I wish to know what is the mistake I am making here.
I feel like, this specific section,
files = {
'script': (filename, open(input_file_path, 'rb'), 'text/plain')
}
is where I am losing out.
On a related note, I am curious, why is there isn’t a C # sample code for the same?
The linked sample project is not that easy to use, and it merges two scenarios - training and long audio, into a single function, which is hard to follow.
Or, is this one of those things where, this simply works better on Python?
Issue Analytics
- State:
- Created 2 years ago
- Comments:8 (4 by maintainers)
Top GitHub Comments
@Jay-study-nildana, Thank you for sharing, I believe it would be helpful too, BTW, maybe the add file part can be removed~
Thanks, Ling
@Ling-Cao
It worked like magic! I would not have been successfull without you pointing me in the right direction.
I have put the full code here if someone else comes here looking for this.
Note : This is NOT the most elegant piece of code, but putting here anyway 😃
Placing A Request for Long Audio Service
Checking for status
Getting the Files
Again, not the most elegant code.